[英]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.