以下是一个简单的解释型语言的示例,使用C++编写:

#include <iostream>
#include <string>
#include <unordered_map>

std::unordered_map<std::string, std::string> variables;

void print(const std::string& value) {
    std::cout << value << std::endl;
}

void set(const std::string& variable, const std::string& value) {
    variables[variable] = value;
}

std::string input() {
    std::string value;
    std::cin >> value;
    return value;
}

int main() {
    std::string line;
    while (std::getline(std::cin, line)) {
        if (line.substr(0, 6) == "print(") {
            std::string value = line.substr(6, line.length() - 7);
            print(value);
        } else if (line.substr(0, 4) == "set(") {
            size_t commaIndex = line.find(",");
            std::string variable = line.substr(4, commaIndex - 4);
            std::string value = line.substr(commaIndex + 2, line.length() - commaIndex - 3);
            set(variable, value);
        } else if (line == "input()") {
            std::string value = input();
            set("yyy", value);
        }
    }
    
    return 0;
}

请将上述代码保存为 xxx.uil 文件,并使用 Dev-C++ 5.11 编译运行

帮我写一个简单的解释型语言输出xxx的语法:printxxx设变量xxx的语法:setxxx输入到yyy变量的语法:yyy=input使用C++编写运行方法:编译后的解释器名 xxxuil要求在dev-c++511中编译

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

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