summaryrefslogtreecommitdiff
path: root/runtime.cpp
blob: 9fbb3c5ce849de87c96e7b43882fe5f8697d8495 (plain)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <functional>
#include <cstring>
#include <cassert>
#include <termios.h>

#include "runtime.h"

extern bool input_is_stdin;

using namespace std;

inline bool isword(char c){return isalpha(c)||c=='_'||c=='@'||c=='$';}
inline bool isextword(char c){return isword(c)||isdigit(c);}
inline bool isoperator(char c){return (bool)strchr("+-*/%&|~^!=><{}",c);}


Stackitem::Stackitem(void){}

Stackitem::Stackitem(int _i):type(SIT_INT),intval(_i){}
Stackitem::Stackitem(const string &_s):type(SIT_STR),strval(_s){}
Stackitem::Stackitem(const vector<Stackitem> &_a):type(SIT_ARR),arrval(_a){}

Stackitem::Stackitem(const Stackitem &other)=default;
Stackitem::Stackitem(Stackitem &&other):type(other.type),strval(move(other.strval)),intval(other.intval),arrval(move(other.arrval)){
	other.type=SIT_INT;
}

Stackitem& Stackitem::operator=(const Stackitem &other){
	type=other.type;
	strval=other.strval;
	intval=other.intval;
	arrval=other.arrval;
	return *this;
}
Stackitem& Stackitem::operator=(Stackitem &&other){
	type=other.type;
	strval=move(other.strval);
	intval=other.intval;
	arrval=move(other.arrval);
	other.type=SIT_INT;
	return *this;
}

Stackitem::operator bool(void) const{
	return (type==SIT_INT&&intval)||
	       (type==SIT_STR&&strval.size())||
	       (type==SIT_ARR&&arrval.size());
}

bool Stackitem::operator==(const Stackitem &other) const{
	if(type!=other.type)return false;
	if(type==SIT_INT)return intval==other.intval;
	if(type==SIT_STR)return strval==other.strval;
	assert(type==SIT_ARR);
	auto sz=arrval.size();
	if(sz!=other.arrval.size())return false;
	for(int i=0;i<sz;i++)if(arrval[i]!=other.arrval[i])return false;
	return true;
}
bool Stackitem::operator!=(const Stackitem &other) const{
	return !(*this==other);
}

string to_string(const Stackitem &si){
	if(si.type==SIT_ARR){
		stringstream ss;
		ss<<'[';
		const auto sz=si.arrval.size();
		for(unsigned int i=0;i<sz;i++){
			if(i!=0)ss<<',';
			ss<<to_string(si.arrval[i]);
		}
		ss<<']';
		return ss.str();
	} else if(si.type==SIT_STR)return si.strval;
	else return to_string(si.intval);
}


extern const unordered_map<string,function<void(vector<Stackitem>&,unordered_map<string,Stackitem>&)>> builtins;


vector<string> tokenise(istream &stream){
	vector<string> tokens;
	string token;
	char c,stringmode;
	bool gotminus=false;
	while(true){
		c=stream.get();
		if(!stream)break;
		while(c=='#'){
			while((c=stream.get())!='\n'&&stream);
			c=stream.get();
		}
		if(!stream)break;
		if(isdigit(c)){
			if(token.size()==0){
				token="#";
				if(gotminus)token+="-";
				gotminus=false;
			}
			token+=c;
		} else if(isword(c)||(token.size()>0&&isextword(c))){
			token+=c;
		} else {
			if(token.size())tokens.push_back(move(token));
			if(isoperator(c)){
				if(c=='-'&&isdigit(stream.peek()))gotminus=true;
				else tokens.push_back(string(1,c));
			} else if(c=='"'||c=='\''){
				stringmode=c;
				while(true){
					c=stream.get();
					if(!stream)throw string("Non-terminated string at end of file");
					if(c==stringmode){
						token="'"+token;
						tokens.push_back(move(token));
						break;
					}
					if(c=='\\'){
						c=stream.get();
						if(!stream)throw string("Non-terminated escape sequence in string at end of file");
						switch(c){
							case 'n':c='\n';break;
							case 'r':c='\r';break;
							case 't':c='\t';break;
							case '0':c='\0';break;
							case '"':c='"';break;
							case '\'':c='\'';break;
							case 'x':{
								int res;
								c=stream.get();
								if(!stream)throw string("Non-terminated hexadecimal escape sequence in string at end of file");
								res=c>='0'&&c<='9'?c-'0':(c>='a'&&c<='f')||(c>='A'&&c<='F')?(c&~32)-'A'+10:-1;
								if(res==-1)throw "Invalid character '"+string(1,c)+"'' in hexadecimal escape sequence in string";
								res*=16;
								c=stream.get();
								if(!stream)throw string("Non-terminated hexadecimal escape sequence in string at end of file");
								res+=c>='0'&&c<='9'?c-'0':(c>='a'&&c<='f')||(c>='A'&&c<='F')?(c&~32)-'A'+10:-241;
								if(res<0)throw "Invalid character '"+string(1,c)+"'' in hexadecimal escape sequence in string";
								c=(char)res;
								break;
							}
						}
					}
					token+=c;
				}
			} else if(isspace(c));
			else throw string("Invalid character found in source: '")+c+"' ("+to_string((int)(unsigned char)c)+')';
		}
	}
	if(token.size())tokens.push_back(move(token));
	return tokens;
}

void runpasseval(const vector<string> &T,
                 vector<Stackitem> &S,
                 const unordered_map<string,pair<vector<string>,unordered_map<unsigned int,unsigned int>>> &functions,
                 unordered_map<string,Stackitem> &variables,
                 const unordered_map<unsigned int,unsigned int> &jumpmap){
	unsigned int cursor;
	for(cursor=0;cursor<T.size();cursor++){
		const string &word=T[cursor];
		//cerr<<"Executing word <"<<word<<'>'<<endl;
		if(word[0]=='#'){
			S.emplace_back((int)strtol(word.data()+1,NULL,10));
		} else if(word[0]=='\''){
			S.push_back(word.substr(1));
		} else {
			auto it=variables.find(word);
			if(it!=variables.end()){
				S.push_back(it->second);
			} else {
				auto it=functions.find(word);
				if(it!=functions.end()){
					builtins.find("enterscope")->second(S,variables);
					runpasseval(it->second.first,S,functions,variables,it->second.second);
					builtins.find("leavescope")->second(S,variables);
				} else {
					if(word=="while"){
						if(S.size()<1)throw string("Keyword 'while' needs 1 stack item");
						Stackitem v=S.back(); S.pop_back();
						if(!v){
							auto it=jumpmap.find(cursor);
							if(it==jumpmap.end()){
								cerr<<"NO JUMPMAP ENTRY FOR "<<word<<" AT INDEX "<<cursor<<endl;
							}
							cursor=jumpmap.find(cursor)->second-1; //jump to corresponding end
						}
						continue;
					} else if(word=="if"){
						if(S.size()<1)throw string("Keyword 'if' needs 1 stack item");
						Stackitem v=S.back(); S.pop_back();
						if(!v){
							auto it=jumpmap.find(cursor);
							if(it==jumpmap.end()){
								cerr<<"NO JUMPMAP ENTRY FOR "<<word<<" AT INDEX "<<cursor<<endl;
							}
							cursor=jumpmap.find(cursor)->second-1; //jump to corresponding else/end
						}
						continue;
					} else if(word=="else"||word=="end"){
						auto it=jumpmap.find(cursor);
						if(it==jumpmap.end()){
							cerr<<"NO JUMPMAP ENTRY FOR "<<word<<" AT INDEX "<<cursor<<endl;
						}
						cursor=jumpmap.find(cursor)->second-1;
						continue;
					}

					auto it=builtins.find(word);
					if(it==builtins.end())throw "Unknown variable or function '"+word+"'";
					it->second(S,variables);
				}
			}
		}
	}
}

void populatejumpmap(const vector<string> &T,unordered_map<unsigned int,unsigned int> &jumpmap){
	//first, index flow control keywords
	vector<unsigned int> flowindexes;
	for(unsigned int cursor=0;cursor<T.size();cursor++){
		const string &word=T[cursor];
		if(word=="end"||word=="while"||word=="if"||word=="else")flowindexes.push_back(cursor);
	}

	//then, index jump targets in jumpmap
	for(unsigned int i=0;i<flowindexes.size();i++){
		const string &word=T[flowindexes[i]];
		if(word=="while"){
			if(i==flowindexes.size()-1)throw string("No 'end' after 'while' in source");
			int depth=1;
			unsigned int end;
			for(end=i+1;end<flowindexes.size();end++){
				if(T[flowindexes[end]]=="end")depth--;
				else if(T[flowindexes[end]]=="while"||T[flowindexes[end]]=="if")depth++;
				if(depth==0)break;
			}
			if(end==flowindexes.size())throw string("No 'end' after 'while' in source");
			jumpmap[flowindexes[i]]=flowindexes[end]+1;
			jumpmap[flowindexes[end]]=flowindexes[i];
		} else if(word=="if"){
			if(i==flowindexes.size()-1)throw string("No 'end' after 'if' in source");
			int depth=1;
			unsigned int els,end;
			for(els=i+1;els<flowindexes.size();els++){
				if(depth==1&&T[flowindexes[els]]=="else")break;
				else if(T[flowindexes[els]]=="end")depth--;
				else if(T[flowindexes[els]]=="while"||T[flowindexes[els]]=="if")depth++;
				if(depth==0)break;
			}
			if(els==flowindexes.size())throw string("No 'else' or 'end' after 'if' in source");
			if(depth!=0){ //we found an else
				for(end=els+1;end<flowindexes.size();end++){
					if(T[flowindexes[end]]=="end")depth--;
					else if(T[flowindexes[end]]=="while"||T[flowindexes[end]]=="if")depth++;
					if(depth==0)break;
				}
				if(end==flowindexes.size())throw string("No 'end' after 'if' in source");
				jumpmap[flowindexes[i]]=flowindexes[els]+1;
				jumpmap[flowindexes[els]]=flowindexes[end]+1;
				jumpmap[flowindexes[end]]=flowindexes[end]+1;
			} else { //we found an end in the first place
				//cerr<<"coupling if-end indexes "<<flowindexes[i]<<" and "<<flowindexes[els]<<endl;
				jumpmap[flowindexes[i]]=flowindexes[els]+1;
				jumpmap[flowindexes[els]]=flowindexes[els]+1;
			}
		}
	}
}

void run(vector<string> T){
	unordered_map<string,pair<vector<string>,unordered_map<unsigned int,unsigned int>>> functions;
	//functions is a pair of tokens and jumpmap
	unordered_map<string,Stackitem> variables;
	unordered_map<unsigned int,unsigned int> jumpmap;
	vector<Stackitem> S;
	unordered_set<string> includeonces;
	unsigned int cursor=0;
	bool skipincluding;
	//first pass, handle functions and includes
	while(cursor<T.size()){
		const string &word=T[cursor];
		if(word=="@defun"){
			if(cursor+3>=T.size())throw string("Unterminated @defun statement at end of file");
			string name=T[cursor+1];
			if(name[0]!='\'')throw string("@defun expected a string as name, but got '")+name+"'";
			name.erase(0,1);
			if(T[cursor+2]!="{")throw string("Invalid @defun statement of function ")+name;
			unsigned int start=cursor+3;
			unsigned int end;
			int depth=1;
			for(end=start;end<T.size();end++){
				if(T[end]=="{")depth++;
				else if(T[end]=="}")depth--;
				else if(T[end]=="\""){
					while(++end<T.size()&&T[end]!="\"")if(T[end]=="\\")end++;
				} else if(T[end]=="'"){
					while(++end<T.size()&&T[end]!="'")if(T[end]=="\\")end++;
				}
				if(depth==0)break;
			}
			if(depth!=0)throw string("Non-terminated @defun statement at end of file");
			auto it=functions.find(name);
			if(it!=functions.end()){
				throw string("Re-definition of function '"+name+"'!");
			}
			vector<string> &functoks=functions[name].first;
			functoks.resize(end-start);
			for(unsigned int i=start;i<end;i++)functoks[i-start]=move(T[i]);
			T.erase(T.begin()+cursor,T.begin()+end+1);
		} else if(word=="@include"||word=="@includeonce"){
			if(cursor+1>=T.size())throw string("Unterminated @include statement at end of file");
			string name=T[cursor+1];
			if(name[0]!='\'')throw string("@include expected a string as file, but got '")+name+"'";
			name.erase(0,1);
			skipincluding=false;
			if(word=="@includeonce"){
				if(includeonces.find(name)==includeonces.end())
					includeonces.insert(name);
				else
					skipincluding=true;
			}
			T.erase(T.begin()+cursor,T.begin()+cursor+2);
			if(!skipincluding){
				ifstream file(name);
				if(!file)throw "Could not open file '"+name+"' specified by "+word+" statement";
				vector<string> included=tokenise(file);
				file.close();
				T.insert(T.begin()+cursor,included.begin(),included.end());
			}
		} else {
			cursor++;
		}
	}

	//second pass, populate jumpmaps
	populatejumpmap(T,jumpmap);
	for(auto &p : functions){
		populatejumpmap(p.second.first,p.second.second);
	}

	//third pass, evaluate code
	runpasseval(T,S,functions,variables,jumpmap);

	if(!input_is_stdin){
		//restore canonical mode, since the code might have changed it to raw mode
		struct termios old={0};
		if(tcgetattr(0,&old)<0)perror("tcgetattr");
		old.c_lflag|=ICANON;
		old.c_lflag|=ECHO;
		if(tcsetattr(0,TCSANOW,&old)<0)perror("tcsetattr");
	}
}