Dragon 1.0.1来啦!输入R后按Enter运行
以下是源代码:
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <cctype>
#include <cstdlib>
// Dragon语言解释器
class DragonInterpreter {
private:
std::map<std::string, std::string> variables; // 变量存储
std::vector<std::string> lines; // 存储所有输入的代码行
int currentLine; // 当前执行的行号
bool showLineNumbers; // 是否显示行号
// 去除字符串两端的空白字符
std::string trim(const std::string& str) {
std::string result = str;
// 去除前导空格
while (!result.empty() && (result[0] == ' ' || result[0] == '\t')) {
result = result.substr(1);
}
// 去除尾部空格
while (!result.empty() && (result[result.length()-1] == ' ' || result[result.length()-1] == '\t')) {
result = result.substr(0, result.length()-1);
}
return result;
}
// 获取行的缩进级别(空格或制表符的数量)
int getIndentLevel(const std::string& line) {
int level = 0;
while (level < line.length() && (line[level] == ' ' || line[level] == '\t')) {
level++;
}
return level;
}
// 检查字符串是否为数字
bool isNumber(const std::string& str) {
for (int i = 0; i < str.length(); i++) {
if (!std::isdigit(str[i])) {
return false;
}
}
return true;
}
// 计算表达式的值(简化版,仅支持变量和数字)
std::string evaluateExpression(const std::string& expr) {
std::string trimmedExpr = trim(expr);
// 如果是变量引用
if (variables.find(trimmedExpr) != variables.end()) {
return variables[trimmedExpr];
}
// 直接返回表达式作为字符串值
return trimmedExpr;
}
// 计算条件表达式的值(简化版,仅支持相等比较)
bool evaluateCondition(const std::string& condition) {
std::string trimmedCond = trim(condition);
// 查找等号位置
size_t eqPos = trimmedCond.find("==");
if (eqPos != std::string::npos) {
std::string left = evaluateExpression(trimmedCond.substr(0, eqPos));
std::string right = evaluateExpression(trimmedCond.substr(eqPos + 2));
return left == right;
}
// 检查是否为数值条件(简化为是否大于0)
std::string value = evaluateExpression(trimmedCond);
if (isNumber(value)) {
return atoi(value.c_str()) > 0;
}
// 默认返回false
return false;
}
// 执行单行代码
void executeLine(const std::string& line) {
if (showLineNumbers) {
std::cout << "[Line " << (currentLine + 1) << "] ";
}
std::string trimmedLine = trim(line);
// 空行
if (trimmedLine.empty()) {
if (showLineNumbers) std::cout << std::endl;
return;
}
// 变量赋值语句: 变量名=变量值
size_t assignPos = trimmedLine.find("=");
if (assignPos != std::string::npos) {
std::string varName = trim(trimmedLine.substr(0, assignPos));
std::string value = evaluateExpression(trimmedLine.substr(assignPos + 1));
variables[varName] = value;
if (showLineNumbers) std::cout << "Set variable '" << varName << "' to '" << value << "'" << std::endl;
return;
}
// 输出语句: output(输出内容)
if (trimmedLine.find("output(") == 0 && trimmedLine[trimmedLine.length()-1] == ')') {
std::string content = evaluateExpression(trimmedLine.substr(7, trimmedLine.length()-8));
std::cout << content << std::endl;
return;
}
// 输入语句: input()
if (trimmedLine == "input()") {
std::string varName;
std::cout << "Enter variable name: ";
std::cin >> varName;
std::cout << "Enter value: ";
std::string value;
std::cin >> value;
variables[varName] = value;
if (showLineNumbers) std::cout << "Set variable '" << varName << "' to '" << value << "'" << std::endl;
return;
}
// if语句: if (条件表达式):
if (trimmedLine.find("if (") == 0 &&
trimmedLine.rfind("):") == trimmedLine.length()-2) {
std::string condition = trimmedLine.substr(4, trimmedLine.length()-6);
if (showLineNumbers) std::cout << "Evaluating if condition: " << condition << std::endl;
if (evaluateCondition(condition)) {
if (showLineNumbers) std::cout << "Condition is true, executing block" << std::endl;
// 执行if语句块
executeBlock(getIndentLevel(line));
} else {
if (showLineNumbers) std::cout << "Condition is false, skipping block" << std::endl;
// 跳过if语句块
skipBlock(getIndentLevel(line));
}
return;
}
// while语句: while (循环次数):
if (trimmedLine.find("while (") == 0 &&
trimmedLine.rfind("):") == trimmedLine.length()-2) {
std::string countExpr = trimmedLine.substr(7, trimmedLine.length()-9);
std::string countStr = evaluateExpression(countExpr);
int count = 0;
if (isNumber(countStr)) {
count = atoi(countStr.c_str());
}
if (showLineNumbers) std::cout << "Executing while loop with count: " << count << std::endl;
int loopStartLine = currentLine + 1;
int indentLevel = getIndentLevel(line);
for (int i = 0; i < count; i++) {
if (showLineNumbers) std::cout << "Loop iteration " << (i + 1) << " of " << count << std::endl;
currentLine = loopStartLine;
executeBlock(indentLevel);
}
skipBlock(indentLevel);
return;
}
std::cerr << "Syntax error: " << line << std::endl;
}
// 执行代码块(处理缩进)
void executeBlock(int indentLevel) {
currentLine++;
while (currentLine < lines.size() && getIndentLevel(lines[currentLine]) > indentLevel) {
executeLine(lines[currentLine]);
currentLine++;
}
currentLine--;
}
// 跳过代码块(处理缩进)
void skipBlock(int indentLevel) {
currentLine++;
while (currentLine < lines.size() && getIndentLevel(lines[currentLine]) > indentLevel) {
currentLine++;
}
currentLine--;
}
public:
DragonInterpreter(bool showLineNums = true) : currentLine(0), showLineNumbers(showLineNums) {}
// 运行解释器
void run() {
std::cout << "Dragon Language Interpreter" << std::endl;
std::cout << "Press R to execute your code." << std::endl;
// 读取多行代码
std::string line;
while (true) {
std::cout << ">> ";
std::getline(std::cin, line);
// 检查是否输入了R (仅大写)
if (line == "R") {
break;
}
lines.push_back(line);
}
// 执行代码
currentLine = 0;
while (currentLine < lines.size()) {
executeLine(lines[currentLine]);
currentLine++;
}
}
};
int main() {
// 设置为true以显示行号,设置为false则不显示
DragonInterpreter interpreter(true);
interpreter.run();
return 0;
}