繁体   English   中英

编译我的脚本语言

[英]Compiling my scripting language

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

#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)){}我希望它写入流变量的“myfile”你可以看到的文本只是举个例子。 我希望我清楚我说的是什么。 如果编译器在我的脚本文件中读取“validated()”,那么它会将“十六进制”代码写入具有“myfile”变量的新文件中,该变量将由解释器解释为游戏和电影等应用程序。 谢谢,如果你知道什么是错的,为什么它不是将十六进制代码写入它所创建的新文件。

看起来您的问题位于此处:

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

真诚地,我甚至不知道为什么compiller允许你这样做,但我想建议你以下的改变:

代替:

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

使用这个:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM