diff options
| Commit | 6c2f7812f65b4a5c60f85297d1bc4f918e32936e (patch) | |
|---|---|---|
| Author | 2018-11-03 14:47:20 +0200 | |
| Committer | 2018-11-03 14:47:20 +0200 | |
| Files | 58a4ea3aba9ed7664908eff465aafd647c274124 /JSImpl/src/main.cpp | |
| IPL-6c2f7812f65b4a5c60f85297d1bc4f918e32936e.tar IPL-6c2f7812f65b4a5c60f85297d1bc4f918e32936e.tar.gz IPL-6c2f7812f65b4a5c60f85297d1bc4f918e32936e.zip | ||
| Parent | 4e7231b52c36fdff699f8d3afd973c184854fdea (diff) | |
Introduce interactive interpreter.
1 files changed, 38 insertions, 1 deletions
| -rw-r--r-- | JSImpl/src/main.cpp | 39 |
diff --git a/JSImpl/src/main.cpp b/JSImpl/src/main.cpp index 0d78a10..f3eb90a 100644 --- a/JSImpl/src/main.cpp +++ b/JSImpl/src/main.cpp @@ -25,9 +25,46 @@ void RunParseCalc() std::cout << "S = " << i.ModifyVariable("a") << std::endl; } +class InterpreterPrinter : public ASTInterpreter::Printer +{ +public: + virtual void PrintVariable(const char* name, double value) override + { + std::cout << name << ": " << value << std::endl; + } +}; + +void InteractiveInterpreter() +{ + char command[1000]; + ASTInterpreter i; + InterpreterPrinter p; + do + { + std::cin.getline(command, 1000); + if (strcmp(command, "exit") == 0) + { + break; + } + if (strcmp(command, "print") == 0) + { + i.Print(p); + continue; + } + IPLVector<Token> tokens; + Tokenize(command, tokens); + auto result = i.Run(Parse(tokens).get()); + while (!result.empty()) { + std::cout << result.back() << std::endl; + result.pop_back(); + } + } while (true); +} + int main() { - RunParseCalc(); + //RunParseCalc(); + InteractiveInterpreter(); #if defined(_WIN32) std::system("pause"); |
