C++ 实现简易类 Python 解释器 - 支持变量定义、输入输出
#include
using namespace std;
// 定义变量类型 enum VarType { INTEGER, FLOAT, STRING, BOOLEAN };
// 变量类 class Variable { private: VarType type; string value; public: Variable(VarType t, string v) : type(t), value(v) {}
// 获取变量类型
VarType getType() const {
return type;
}
// 获取变量值
string getValue() const {
return value;
}
};
// 解释器类 class Interpreter { private: map<string, Variable> variables;
// 将字符串转换为变量类型
VarType getType(const string& typeStr) {
if (typeStr == "int")
return INTEGER;
else if (typeStr == "float")
return FLOAT;
else if (typeStr == "str")
return STRING;
else if (typeStr == "bool")
return BOOLEAN;
else
return STRING; // 默认为字符串类型
}
public: // 执行源代码 void execute(const string& code) { istringstream iss(code); string line;
while (getline(iss, line)) {
processLine(line);
}
}
private: // 处理每行代码 void processLine(const string& line) { istringstream iss(line); string token; iss >> token;
if (token == "print") {
// 输出变量值
string varName;
iss >> varName;
printVariable(varName);
} else if (token == "input") {
// 输入变量值
string varName;
iss >> varName;
inputVariable(varName);
} else if (token == "var") {
// 定义变量
string varName;
string varType;
string varValue;
iss >> varName >> varType >> varValue;
defineVariable(varName, varType, varValue);
} else {
cout << "Invalid statement: " << line << endl;
}
}
// 输出变量值
void printVariable(const string& varName) {
if (variables.count(varName) == 0) {
cout << "Variable not found: " << varName << endl;
return;
}
Variable var = variables[varName];
cout << var.getValue() << endl;
}
// 输入变量值
void inputVariable(const string& varName) {
if (variables.count(varName) == 0) {
cout << "Variable not found: " << varName << endl;
return;
}
Variable var = variables[varName];
string input;
cin >> input;
// 检查输入类型和变量类型是否匹配
if (var.getType() == INTEGER && !isInteger(input)) {
cout << "Invalid input type, expected integer" << endl;
return;
} else if (var.getType() == FLOAT && !isFloat(input)) {
cout << "Invalid input type, expected float" << endl;
return;
} else if (var.getType() == BOOLEAN && !isBoolean(input)) {
cout << "Invalid input type, expected boolean" << endl;
return;
}
variables[varName] = Variable(var.getType(), input);
}
// 定义变量
void defineVariable(const string& varName, const string& varType, const string& varValue) {
VarType type = getType(varType);
variables[varName] = Variable(type, varValue);
}
// 检查是否为整数
bool isInteger(const string& str) {
if (str.empty())
return false;
for (char c : str) {
if (!isdigit(c))
return false;
}
return true;
}
// 检查是否为浮点数
bool isFloat(const string& str) {
if (str.empty())
return false;
bool decimalFound = false;
for (char c : str) {
if (c == '.') {
if (decimalFound)
return false;
decimalFound = true;
} else if (!isdigit(c)) {
return false;
}
}
return true;
}
// 检查是否为布尔值
bool isBoolean(const string& str) {
return (str == "True" || str == "False");
}
};
int main() { // 创建解释器实例 Interpreter interpreter;
// 执行源文件
interpreter.execute(
"var name str \"John\"
" "var age int 20 " "var height float 1.75 " "var is_student bool True " "print name " "print age " "print height " "print is_student " "input name " "input age " "input height " "input is_student " "print name " "print age " "print height " "print is_student " );
return 0;
}
原文地址: https://www.cveoy.top/t/topic/nWcU 著作权归作者所有。请勿转载和采集!