-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.cpp
167 lines (141 loc) · 5.06 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "fpl.h"
int main(int argc, char** argv) {
signal(SIGINT, sigint);
program="main.fpl";
bool verbose=false;
bool objdump=false;
kwds.push_back("func");
kwds.push_back("class");
kwds.push_back("return");
kwds.push_back("if");
kwds.push_back("else");
kwds.push_back("elif");
kwds.push_back("raise");
kwds.push_back("try");
kwds.push_back("except");
kwds.push_back("finally");
kwds.push_back("for");
kwds.push_back("break");
kwds.push_back("while");
kwds.push_back("import");
kwds.push_back("from");
kwds.push_back("del");
kwds.push_back("assert");
kwds.push_back("continue");
kwds.push_back("lambda");
kwds.push_back("yield");
kwds.push_back("with");
glblargc_raw=argc;
glblargv_raw=argv;
if (argc==1){
try{
cout<<"Falcon Programming Language "<<FPL_VERSION<<endl;
cout<<"Eric Buehler 2022"<<endl;
cout<<"Type copyright() for copyright and license information, exit() to exit"<<endl<<endl;
//Prep constants and types
fpl_startup();
struct compiler* compiler = new_compiler();
::vm=new_vm(interpreter.vm_map->size(), NULL, compiler->instructions, NULL); //data is still in scope...
interpreter_add_vm(interpreter.vm_map->size(), vm);
gil_lock(vm->id);
dict_set_noret(::vm->globals, str_new_fromstr("__annotations__"), ::callstack_head(vm->callstack).annotations);
dict_set_noret_opti(::vm->globals, str_new_fromstr("__name__"), str_new_fromstr("__main__"));
while (true){
struct vm* vm_=vm;
vm=NULL;
string data="";
cout<<">>> ";
getline(cin,data);
if(!cin){
cin.clear();
while (!hit_sigint){}
hit_sigint=false;
vm=vm_;
continue;
}
Lexer lexer(data,kwds);
lexer.pos=Position(program);
Position end=lexer.tokenize();
parser=Parser(lexer.tokens, data);
parse_ret ast=parser.parse();
if (ast.errornum>0){
cout<<ast.header<<endl;
cout<<ast.snippet<<endl;
cout<<ast.arrows<<endl;
printf("%s\n",ast.error);
vm=vm_;
continue;
}
glblfildata=new string(data);
vm_->filedata=&data;
object* code=compile(compiler, ast, 0);
if (code==NULL){
cout<<parseretglbl.header<<endl;
cout<<parseretglbl.snippet<<endl;
cout<<parseretglbl.arrows<<endl;
printf("%s\n",parseretglbl.error);
vm=vm_;
continue;
}
vm=vm_;
callstack_head(vm->callstack).code=code;
vm->ip=0;
vm->exec=0;
object* returned=run_vm(code, &vm->ip);
CLEAR_EVALBREAKER();
while (vm->blockstack->size>0){
if (blockstack_head(vm->blockstack).type==WITH_BLOCK){
object_exit_with(blockstack_head(vm->blockstack).obj);
}
pop_blockframe(vm->blockstack);
}
if (returned==TERM_PROGRAM && vm->exception==NULL){
finalize_threads();
socket_cleanup();
return 0;
}
compiler_del(compiler);
FPLDECREF(code);
compiler = new_compiler();
if (returned!=NULL){
FPLDECREF(returned);
}
}
finalize_threads();
socket_cleanup();
return 0;
}
catch (std::bad_alloc){
memory_error();
}
}
if (argc==2){
if ((string)argv[1]==(string)"-h"){
cout<<"FPL V1\n";
cout<<"Eric Buehler 2022\n\n";
cout<<"Standard run: fpl [PROGRAM NAME]\n";
cout<<"Enter REPL: fpl \n";
cout<<"Help: fpl -h\n";
cout<<"Licenses: fpl -l\n";
return 0;
}
if ((string)argv[1]==(string)"-l"){
cout<<"FPL License\n";
cout<<FPL_LICENSE;
return 0;
}
}
if (argc>=2){
program=argv[1];
}
#ifdef DEBUG
verbose=true;
#endif
try{
execute(loadFile(program), objdump, verbose);
return 0;
}
catch (std::bad_alloc){
memory_error();
}
}