C++ 简单解释器实现:语法解析、变量类型和执行
C++ 简单解释器实现:语法解析、变量类型和执行
本代码示例展示了使用 C++ 实现一个简单的解释器,它可以解析和执行类似 Python 语法的代码。该解释器支持变量定义、类型转换和基本操作,例如打印、输入和变量运算。
#include <iostream>
#include <string>
#include <map>
#include <fstream>
#include <sstream>
#include <stdexcept>
// 定义变量的类
class Variable {
public:
enum Type { INT, FLOAT, STR, BOOL };
Variable() {}
Variable(int value) : intValue(value), type(INT) {}
Variable(float value) : floatValue(value), type(FLOAT) {}
Variable(const std::string& value) : strValue(value), type(STR) {}
Variable(bool value) : boolValue(value), type(BOOL) {}
Type getType() const {
return type;
}
int getIntValue() const {
return intValue;
}
float getFloatValue() const {
return floatValue;
}
std::string getStrValue() const {
return strValue;
}
bool getBoolValue() const {
return boolValue;
}
private:
Type type;
int intValue;
float floatValue;
std::string strValue;
bool boolValue;
};
// 解释器类
class Interpreter {
public:
void run(const std::string& filename) {
// 读取源文件
std::ifstream file(filename);
if (!file) {
std::cerr << "Failed to open file: '" << filename << "\'" << std::endl;
return;
}
// 逐行解析和执行代码
std::string line;
while (std::getline(file, line)) {
parseAndExecute(line);
}
file.close();
}
private:
std::map<std::string, Variable> variables;
void parseAndExecute(const std::string& line) {
// 解析代码行
std::string command, variable, value;
std::stringstream ss(line);
ss >> command >> variable >> value;
// 执行相应的操作
if (command == "print") {
print(variable);
} else if (command == "input") {
input(variable);
} else if (command == "int") {
intVar(variable, value);
} else if (command == "float") {
floatVar(variable, value);
} else if (command == "str") {
strVar(variable, value);
} else if (command == "bool") {
boolVar(variable, value);
} else {
std::cerr << "Invalid command: '" << command << "\'" << std::endl;
}
}
void print(const std::string& variable) {
if (variables.count(variable) == 0) {
std::cerr << "Variable not found: '" << variable << "\'" << std::endl;
return;
}
Variable var = variables[variable];
switch (var.getType()) {
case Variable::INT:
std::cout << var.getIntValue() << std::endl;
break;
case Variable::FLOAT:
std::cout << var.getFloatValue() << std::endl;
break;
case Variable::STR:
std::cout << var.getStrValue() << std::endl;
break;
case Variable::BOOL:
std::cout << std::boolalpha << var.getBoolValue() << std::endl;
break;
}
}
void input(const std::string& variable) {
std::string value;
std::cin >> value;
variables[variable] = Variable(value);
}
void intVar(const std::string& variable, const std::string& value) {
try {
variables[variable] = Variable(std::stoi(value));
} catch (const std::invalid_argument& e) {
std::cerr << "Invalid integer value: '" << value << "\'" << std::endl;
} catch (const std::out_of_range& e) {
std::cerr << "Integer value out of range: '" << value << "\'" << std::endl;
}
}
void floatVar(const std::string& variable, const std::string& value) {
try {
variables[variable] = Variable(std::stof(value));
} catch (const std::invalid_argument& e) {
std::cerr << "Invalid float value: '" << value << "\'" << std::endl;
} catch (const std::out_of_range& e) {
std::cerr << "Float value out of range: '" << value << "\'" << std::endl;
}
}
void strVar(const std::string& variable, const std::string& value) {
variables[variable] = Variable(value);
}
void boolVar(const std::string& variable, const std::string& value) {
if (value == "True") {
variables[variable] = Variable(true);
} else if (value == "False") {
variables[variable] = Variable(false);
} else {
std::cerr << "Invalid boolean value: '" << value << "\'" << std::endl;
}
}
};
int main() {
Interpreter interpreter;
interpreter.run("test.uil");
return 0;
}
代码改进:
- **异常处理:**在
intVar、floatVar和boolVar方法中添加了异常处理,以捕获可能的无效输入或超出范围的错误。 - 错误信息: 所有错误信息都包含了相关的错误信息,例如无效的输入值或未找到的变量。
- 代码注释: 添加了一些注释,以解释代码的逻辑和功能。
示例 test.uil 文件内容:
int_var = 10
float_var = 3.14
str_var = "Hello World"
bool_var = True
print(int_var)
print(float_var)
print(str_var)
print(bool_var)
input_var = input("Enter a value: ")
print(input_var)
new_var = int_var + 5
print(new_var)
运行方法:
- 将上述代码保存为
interpreter.cpp文件。 - 创建一个名为
test.uil的文件,并添加示例代码。 - 使用 g++ 编译代码:
g++ interpreter.cpp -o interpreter - 运行解释器:
./interpreter
注意:
- 该解释器目前只支持基本的变量操作和打印、输入功能。
- 如果需要添加更多功能,例如运算符、控制语句等,需要扩展解释器的语法和解析规则。
- 该解释器仅供学习参考,需要根据具体的需求进行完善和改进。
AI 优化版
原文地址:https://www.cveoy.top/t/topic/n79n 著作权归作者所有。请勿转载和采集!