c++ - 编译我的脚本语言

标签 c++ compiler-construction scripting interpreter

我已经为脚本语言开发了一个框架,我正在尝试为它制作一个编译器,以将自定义的十六进制代码输出到某个文件上,供游戏和电影等应用程序的解释器读取。解释器将读取十六进制并通过循环执行操作,并为我完成大部分艰苦的工作。但我需要一种方法将文本字符串编译成我自定义的十六进制。这是我的编译器的示例。

#include <iostream>
#include <string>
#include <Windows.h>
#include <wincon.h>
#include <fstream>;
using namespace std;

int main(){
    char egScriptpath[999];
    char path[999];
    char title[999];
    ifstream readfile;
    ofstream myfile;
    int optn;

    cout << "Enter the path where your *.egSCRIPT file is: " << endl;
    cin.getline(egScriptpath,999);

    cout << "Enter your path where you want to compile your *.egSCRIPT file: " << endl;
    cin.getline(path,999);

    cout << "Enter your title for your program: ";
    cin.getline(title,999);

    cout << endl << "Enter for your option of file extention:" <<  endl;
    cout << "   Enter 1 for .egGame :" << endl;
    cout << "   Enter 2 for .egMovie: ";
    cin >> optn;

    if(optn==1){
        readfile.open((string)egScriptpath);
        ofstream egMovieFile((string)path+"\\"+(string)title+".egGame");
        myfile.open((string)path+"\\"+(string)title+".egGame");
        while(!readfile.eof()){
            if(readfile.getline("validated()",1)){
              myfile << "              \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n"
                        "               \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n"
                        "                \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n"
                        "                ////////////////\n"
                        "               ////////////////\n"
                        "              ||||||||||||||||\n"
                        "              ||||||||||||||||\n"
                        "              ||||||||||||||||\n"
                        "              \1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\n"
                        "              \2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\n";
            }
        }
        myfile.close();
        readfile.close();

    }else if(optn==2){
        readfile.open((string)egScriptpath);
        ofstream egGameFile((string)path+"\\"+(string)title+".egMovie");
        myfile.open((string)path+"\\"+(string)title+".egMovie");

        while(!readfile.eof()){
            if(readfile.getline("validated()",1)){
            myfile << "              \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n"
                      "               \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n"
                      "                \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n"
                      "                ////////////////\n"
                      "               ////////////////\n"
                      "              ||||||||||||||||\n"
                      "              ||||||||||||||||\n"
                      "              ||||||||||||||||\n"
                      "              \1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\n"
                      "              \2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\n";
            }
        }
        myfile.close();
        readfile.close();
    }

    cout << "Compile complete" << endl;
    system("pause");

    return 0;
}

所以我想做的是在 if 语句中它说 if(readfile.getline("validated()",1)){}可以看到只是一个例子。我希望我清楚我在说什么。如果编译器在我的脚本文件上读取“validated()”,那么它会将“十六进制”代码写入带有变量“myfile”的新文件,而该变量将由解释器解释为游戏和电影等应用程序.谢谢,如果你知道哪里出了问题以及为什么它没有将十六进制代码写入它生成的新文件中。

最佳答案

看起来您的问题出在这里:

if(readfile.getline("validated()",1)){

真诚地,我什至不知道为什么编译器允许您这样做,但我想向您建议以下更改:

代替:

while(!readfile.eof()){
    if(readfile.getline("validated()",1)){

使用这个:

std::string buffer;
while (std::getline(readfile, buffer, '\n')){
    if (buffer == "validated()")

此外,由于您使用的是 C++,而不是使用 char 数组,您应该更多地依赖 std::string 类。

关于c++ - 编译我的脚本语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15891626/

相关文章:

c++ - g++静态库依赖动态库

c++ - Buck 是否支持与文件夹同名的标题?

c++ - 如何在Wxwidgets中调试?

linux - for 循环和 while 循环遍历目录在 bash 中的工作方式不同

c++ - 克服 "fixing it later"的坏习惯

linux - nvcc: 没有那个文件或目录

programming-languages - 词法范围是如何实现的?

c++ - 如何获取LLVM中指令引用的所有全局变量?

excel - VBA Sap Scripting 打开或连接/保留在已打开的 session 窗口中

bash - 我如何在 Bash 中解析命令行参数?