From 6c2f7812f65b4a5c60f85297d1bc4f918e32936e Mon Sep 17 00:00:00 2001 From: martin dobrev Date: Sat, 3 Nov 2018 14:47:20 +0200 Subject: Introduce interactive interpreter. --- JSImpl/src/main.cpp | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'JSImpl/src/main.cpp') 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 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"); -- cgit v1.3.1-23-gd82d