"#include \n#include \n#include \n#include \n\n// Token 类型\nenum class TokenType {\n INVALID, \n IDENTIFIER, \n KEYWORD, \n NUMBER, \n STRING, \n ASSIGN, \n ADD, \n SUBTRACT, \n MULTIPLY, \n DIVIDE, \n PRINT, \n INPUT, \n END_OF_FILE\n};\n\n// Token 结构体\nstruct Token {\n TokenType type; \n std::string value; \n};\n\n// 词法分析器\nclass Lexer {\npublic:\n Lexer(const std::string& sourceCode) : sourceCode(sourceCode), currentPosition(0) {}\n\n Token getNextToken() {\n if (currentPosition >= sourceCode.length()) {\n return {TokenType::END_OF_FILE, ""};\n }\n\n char currentChar = sourceCode[currentPosition++];\n if (isalpha(currentChar)) {\n std::string identifier; \n identifier += currentChar; \n\n while (currentPosition < sourceCode.length() && isalnum(sourceCode[currentPosition])) {\n identifier += sourceCode[currentPosition++];\n }\n\n if (identifier == "print" ) {\n return {TokenType::PRINT, ""};\n } else if (identifier == "input" ) {\n return {TokenType::INPUT, ""};\n } else {\n return {TokenType::IDENTIFIER, identifier};\n }\n } else if (isdigit(currentChar)) {\n std::string number; \n number += currentChar; \n\n while (currentPosition < sourceCode.length() && isdigit(sourceCode[currentPosition])) {\n number += sourceCode[currentPosition++];\n }\n\n return {TokenType::NUMBER, number};\n } else if (currentChar == '"') {\n std::string str; \n while (currentPosition < sourceCode.length() && sourceCode[currentPosition] != '"') {\n str += sourceCode[currentPosition++];\n }\n currentPosition++; // 跳过闭引号\n return {TokenType::STRING, str};\n } else if (currentChar == '=') {\n return {TokenType::ASSIGN, ""};\n } else if (currentChar == '+') {\n return {TokenType::ADD, ""};\n } else if (currentChar == '-') {\n return {TokenType::SUBTRACT, ""};\n } else if (currentChar == '*') {\n return {TokenType::MULTIPLY, ""};\n } else if (currentChar == '/') {\n return {TokenType::DIVIDE, ""};\n } else {\n return {TokenType::INVALID, ""};\n }\n }\n\nprivate:\n std::string sourceCode; \n size_t currentPosition; \n};\n\n// 解释器\nclass Interpreter {\npublic:\n Interpreter() {}\n\n void execute(const std::string& sourceCode) {\n Lexer lexer(sourceCode);\n Token token = lexer.getNextToken();\n\n while (token.type != TokenType::END_OF_FILE) {\n if (token.type == TokenType::PRINT) {\n token = lexer.getNextToken();\n std::cout << evaluateExpression(token) << std::endl; \n } else if (token.type == TokenType::INPUT) {\n token = lexer.getNextToken();\n std::string variableName = token.value; \n\n std::cout << "Enter a value for " << variableName << ": ";\n std::cin >> variables[variableName];\n } else if (token.type == TokenType::IDENTIFIER) {\n std::string variableName = token.value; \n\n token = lexer.getNextToken();\n if (token.type == TokenType::ASSIGN) {\n token = lexer.getNextToken();\n variables[variableName] = evaluateExpression(token);\n }\n }\n\n token = lexer.getNextToken();\n }\n }\n\nprivate:\n std::map<std::string, int> variables; \n\n int evaluateExpression(const Token& token) {\n if (token.type == TokenType::NUMBER) {\n return std::stoi(token.value);\n } else if (token.type == TokenType::IDENTIFIER) {\n return variables[token.value];\n } else if (token.type == TokenType::ADD) {\n Token nextToken = lexer.getNextToken();\n return evaluateExpression(token) + evaluateExpression(nextToken);\n } else if (token.type == TokenType::SUBTRACT) {\n Token nextToken = lexer.getNextToken();\n return evaluateExpression(token) - evaluateExpression(nextToken);\n } else if (token.type == TokenType::MULTIPLY) {\n Token nextToken = lexer.getNextToken();\n return evaluateExpression(token) * evaluateExpression(nextToken);\n } else if (token.type == TokenType::DIVIDE) {\n Token nextToken = lexer.getNextToken();\n return evaluateExpression(token) / evaluateExpression(nextToken);\n } else {\n return 0; \n }\n }\n};\n\nint main() {\n std::string sourceCode = R"(\n x = 10\n y = 5\n z = x + y\n print z\n input a\n input b\n c = a + b\n print c\n )";\n\n Interpreter interpreter; \n interpreter.execute(sourceCode); \n\n return 0; \n}\n

C++ 实现简易 Python 解释器:支持输入、输出和变量

原文地址: https://www.cveoy.top/t/topic/obAS 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录