众所周知
C
+
+
C++
C++和
P
a
s
c
a
l
Pascal
Pascal是
O
I
e
r
OIer
OIer常用的两种语言。但由于语法不同,很多P党都不愿意转
C
+
+
C++
C++。因此本人在此启动一个项目:
P
2
C
P2C
P2C1项目。
项目名称:
P
2
C
P2C
P2C
项目编号:
001
001
001
项目等级:
t
h
e
m
o
s
t
i
m
p
o
r
t
a
n
t
the\ most\ important
the most important
项目内容:
P
a
s
c
a
l
T
o
C
p
p
Pascal\ To\ Cpp
Pascal To Cpp翻译器制作。
项目性质:
开源项目2(是不是很惊喜?)
但是由于
p
a
s
c
a
l
pascal
pascal的一些特性
C
+
+
C++
C++不支持本人也没办法,所以目前就把源代码和程序放上来,看看各位大神有没有什么办法(逃)。
目前代码已经写到了循环部分,包括变量,定量,for,while,repert,判断语句(case还没有完成),赋值语句。但子程序还没有写。
欢迎各位大神和我一起把这个项目完成。3
同时,该程序具有自动编译 p a s c a l pascal pascal代码,利用 A s t y l e Astyle Astyle整理 C + + C++ C++代码的功能.其中 B i n Bin Bin目录下的文件是编译 p a s c a l pascal pascal代码的库。
如何和我一起完成这个项目?
- 在源代码的开头写下你的大名(我会依据贡献留名)
- 用“/**/”注释掉你写的内容,方便我查看(或者你想怎样就怎样,只要让我知道就行了)
- 在发给我之前先自测一下有没有bug。
- 请将所有文件打成一个压缩包,用“
P
2
C
P2C
P2C”命名(所有文件如下)。
- 私信发分享链接给我
- 你可以完成任何一个结构(比如case和子程序部分),但是要求能成功运行。
- 请勿更改如下内容(包括
使用方法(必读).txt
)
freopen("pascal.pas","r",stdin);
freopen("c++.cpp","w",stdout);
if(compile_check() == -1)
{
system("cls");
cerr << "Pascal文件编译错误,我们终止了程序。\nPascal File compilation error, we terminated the program.\n";
cerr << "请按任意键继续. . .\nPlease press any key to continue. . .\n";
getch();
return 1;
}
system("AStyle\\bin\\AStyle --style=allman -N -H -p -n -k3 --quiet c++.cpp");
cerr << "翻译成功。\nTranslate successfully.\n";
cerr << "请按任意键继续. . .\nPlease press any key to continue. . .\n";
一起加油吧!
目前的源代码:
#include<bits/stdc++.h>
#include<io.h>
#include<conio.h>
using namespace std;
deque <string> lines;
stringstream ss,ss1;
string type[2][100010];
map <string,string> to_cpp;
const int MaxN = 15;
void init()
{
type[0][1] = "longint";
type[1][1] = "int";
to_cpp["longint"] = "int";
type[0][2] = "integer";
type[1][2] = "short";
to_cpp["integer"] = "short";
type[0][3] = "word";
type[1][3] = "unsigned short";
to_cpp["word"] = "unsigned short";
type[0][4] = "byte";
type[1][4] = "unsigned char";
to_cpp["byte"] = "unsigned char";
type[0][5] = "char";
type[1][5] = "char";
to_cpp["char"] = "char";
type[0][6] = "string";
type[1][6] = "string";
to_cpp["string"] = "string";
type[0][7] = "ansistring";
type[1][7] = "string";
to_cpp["ansistring"] = "string";
type[0][8] = "boolean";
type[1][8] = "bool";
to_cpp["boolean"] = "bool";
type[0][9] = "real";
type[1][9] = "float";
to_cpp["real"] = "float";
type[0][10] = "double";
type[1][10] = "double";
to_cpp["double"] = "double";
type[0][11] = "extended";
type[1][11] = "long double";
to_cpp["extended"] = "long double";
type[0][12] = "int64";
type[1][12] = "long long";
to_cpp["int64"] = "long long";
type[0][13] = "qword";
type[1][13] = "unsigned long long";
to_cpp["qword"] = "unsigned long long";
type[0][14] = "dword";
type[1][14] = "unsigned int";
to_cpp["dword"] = "unsigned int";
type[0][15] = "shortint";
type[1][15] = "char";
to_cpp["shortint"] = "char";
}
/*int password_check()
{
string password = "root";
string input;
cout << "请输入密码:\nPlease enter your password:\n";
cin >> input;
if(input == password)
{
cerr << "正确!\nCorrect!\n";
return 1;
}
else
{
cerr << "错误!\nError!\n";
cerr << "我们终止了翻译!\nWe terminated the translation!\n";
system("pause");
return -1;
}
}*/
void delete_all_mark(string &s, const string &mark)
{
size_t nSize = mark.size();
while(1)
{
size_t pos = s.find(mark);
if(pos == string::npos)
{
return;
}
s.erase(pos, nSize);
}
}
bool file_exists(const char *path)
{
return (access(path,0) == 0);
}
int compile_check()
{
system("bin\\fpc pascal.pas");
if(!file_exists("pascal.exe"))
return -1;
else
{
remove("pascal.exe");
remove("pascal.o");
return 0;
}
}
void replace_all(string &src,const char *mark,const char *to)
{
int size = strlen(mark);
while(src.find(mark) != string::npos)
src.replace(src.find(mark),size,to);
}
string t;
string str1;
int main()
{
/*if(password_check() == -1)
exit(0);*/
init();
freopen("pascal.pas","r",stdin);
freopen("c++.cpp","w",stdout);
if(compile_check() == -1)
{
system("cls");
cerr << "Pascal文件编译错误,我们终止了程序。\nPascal File compilation error, we terminated the program.\n";
cerr << "请按任意键继续. . .\nPlease press any key to continue. . .\n";
getch();
return 1;
}
puts("#include <bits/stdc++.h>\nusing namespace std;");
puts("template <typename _T> ostream &operator,(ostream &o,_T val) {o << val; return o;}");
while(getline(cin,t))
lines.push_back(t);
bool flag_var = 0;
bool flag_const = 0;
bool flag_main = 0;
bool flag_case = 0;
bool flag_case_sub = 0;
for(int i = 0;i < lines.size();i++)
{
string &cur = lines[i];
const size_t &npos = string::npos;
if(cur.find("var") != npos)
flag_var = 1,flag_const = 0;
if(cur.find("const") != npos)
flag_const = 1,flag_var = 0;
if(cur.find("begin") != npos)
{
flag_main = 1,flag_const = 0,flag_var = 0;
puts("{");
}
if(flag_main && cur.find("begin") == npos)
{
if(cur.find("end") != npos)
{
puts("}");
flag_main = 0;
}
else
{
delete_all_mark(cur," ");
delete_all_mark(cur,"\t");
replace_all(cur,"'","\"");
replace_all(cur,"<>","!=");
replace_all(cur,"repeat","do\n\t{");
if(cur.find("write(") != npos)
{
delete_all_mark(cur,"write(");
cur.erase(cur.end() - 2);
cout << "cout<<";
}
if(cur.find("writeln(") != npos)
{
delete_all_mark(cur,"writeln(");
cur.erase(cur.end() - 2);
cur.insert(cur.length() - 1,",\"\\n\"");
cout << "cout<<";
}
if(cur.find("if") != npos)
{
int pos = cur.find("if");
cur.insert(pos + 2,"(");
cur.insert(cur.length(),")");
}
if(cur.find("while") != npos)
{
delete_all_mark(cur,"do");
int pos = cur.find("while");
cur.insert(pos + 5,"(");
cur.insert(cur.length(),")");
}
if(cur.find("until") != npos)
{
replace_all(cur,"until","}while");
int pos = cur.find("while");
cur.insert(pos + 5,"(!(");
cur.insert(cur.length() - 1,"))");
}
if(cur.find("for") != npos)
{
string var_name(cur,3,cur.find(":=") - 3);
if(cur.find("downto") != npos)
{
int l,r;
ss << string(cur,cur.find(":=") + 2,cur.find("downto") - cur.find(":=") - 2);
ss >> r;
ss.clear();
ss << string(cur,cur.find("downto") + 6,cur.find("do") - cur.find("downto") - 6);
ss >> l;
ss.clear();
ss1 << "for(" << var_name << ":=" << r << ";" << var_name << ">=" << l << ";--" << var_name << ")" << string(cur,cur.rfind("do") + 2,npos) << endl;
ss1 >> cur;
ss1.clear();
}
else
{
int l,r;
ss1 << string(cur,cur.find(":=") + 2,cur.find("to") - cur.find(":=") - 2);
ss1 >> l;
ss1.clear();
ss1 << string(cur,cur.find("to") + 2,cur.find("do") - cur.find("to") - 2);
ss1 >> r;
ss1.clear();
ss1 << "for(" << var_name << ":=" << l << ";" << var_name << "<=" << r << ";++" << var_name << ")" << string(cur,cur.rfind("do") + 2,npos) << endl;
ss1 >> cur;
ss1.clear();
}
}
/*if(flag_case)
{
if(cur.find("end;") == npos)
{
cur.insert(0,"case ");
cur.replace("case else","default:");
if(flag_case_sub)
cur.insert(0,"break;\n");
else
flag_case_sub = 1;
}
else
flag_case = 0;
}
else if(cur.find("case") != npos)
{
cur.insert(cur.find("case") + 4,"(");
cur.insert(cur.find("of"),")");
replace_all(cur,"case","switch");
flag_case = 1;
flag_case_sub = 0;
}这部分还没有完成*/
delete_all_mark(cur,"then");
delete_all_mark(cur,"of");
for(register int i = 0;i < cur.length();++i)
if(cur[i] == '=' && cur[i - 1] != ':' && cur[i - 1] != '>' && cur[i - 1] != '<' && cur[i - 1] != '!')
cout << "==";
else if(cur[i] == ':' && cur[i + 1] == '=')
{
cout << "=";
++i;
}
else
cout << cur[i];
puts("");
}
}
if(flag_var && cur.find("var") == npos)
{
if(lines[i].find("array") != npos)
{
delete_all_mark(lines[i],"\t");
delete_all_mark(lines[i]," ");
bool flag = 0;
string type_t(lines[i],lines[i].find("of") + 2,lines[i].length() - (lines[i].find("of") + 2) - 1);
cout << to_cpp[type_t] << " " << string(lines[i],0,lines[i].find(":"));
string t(lines[i],lines[i].find("[") + 1,lines[i].find("]") - 1 - lines[i].find("["));
while(t.find(",") != npos)
{
int l,r;
ss << string(t,0,t.find(".."));
ss >> l;
ss.clear();
ss << string(t,t.find("..") + 2,t.find(",") - 1 - t.find("..") - 2 + 1);
ss >> r;
cout << "[" << r + 10 << "]";
ss.clear();
t.assign(t,t.find(",") + 1,npos);
}
int l,r;
ss << string(t,0,t.find(".."));
ss >> l;
ss.clear();
ss << string(t,t.find("..") + 2,t.find("]") - 1 - t.find("..") - 2 + 1);
ss >> r;
ss.clear();
cout << "[" << r + 10 << "];\n";
}
for(int j = 1;j <= MaxN;++j)
if(lines[i].find(":" + type[0][j]) != npos)
{
cout << type[1][j] << " ";
delete_all_mark(lines[i],"\t");
delete_all_mark(lines[i]," ");
int pos = lines[i].find(":");
lines[i].erase(pos,lines[i].size() - pos);
cout << lines[i] << ";\n";
}
}
if(flag_const && cur.find("const") == npos)
{
delete_all_mark(lines[i],"\t");
delete_all_mark(lines[i]," ");
int pos = lines[i].find("=");
lines[i][pos] = ' ';
lines[i].erase(lines[i].length() - 1,1);
cout << "#define " << lines[i] << "\n";
}
}
fclose(stdout);
freopen("","w",stdout);
system("AStyle\\bin\\AStyle --style=allman -N -H -p -n -k3 --quiet c++.cpp");
cerr << "翻译成功。\nTranslate successfully.\n";
cerr << "请按任意键继续. . .\nPlease press any key to continue. . .\n";
getch();
return 0;
}