lox.cpp (886B)
1 void Lox::execStr(std::string input) { 2 run(input); 3 if(hadError) { 4 exit(65); 5 } 6 } 7 void Lox::repl() { 8 while(true) { 9 printf("lox> "); 10 std::string inp; 11 if (!std::getline(std::cin, inp)) { 12 std::cout << std::endl; 13 break; 14 } 15 run(inp + "\n"); 16 hadError = false; 17 } 18 } 19 void Lox::run(std::string inputStr) { 20 Scanner scanner = Scanner(inputStr, this); 21 std::vector<Token> tokens = scanner.scanTokens(); 22 if(!hadError) { 23 for(auto& token: tokens) { 24 std::cout << token.toString() << std::endl; 25 } 26 } 27 } 28 void Lox::error(int line, std::string message) { 29 report(line, "", message); 30 } 31 void Lox::report(int line, std::string where, std::string message) { 32 std::cout << "[line " << line << "] Error" << where << ": " << message << std::endl; 33 hadError = true; 34 }