summaryrefslogtreecommitdiff
path: root/functions.cpp
blob: 7a6ce3658112fe9210c4e64e5ee4ce743cf13382 (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
362
363
364
365
366
367
368
369
370
371
372
373
#include <iostream>
#include <fstream>
#include <unordered_map>
// #include <unordered_set>
#include <vector>
#include <stack>
#include <string>
#include <functional>
#include <cmath>
#include <unistd.h>
#include <fcntl.h>

#include "runtime.h"

using namespace std;

extern int g_argc;
extern char **g_argv;

#define BUILTIN_GUARD_STACKSIZE(nm,sz) {if(S.size()<(sz))throw string("Builtin '" nm "' needs " #sz " stack item")+(sz==1?"":"s");}

stack<unordered_map<string,pair<Stackitem,bool>>> scopestack;
//Each stack frame contains the variables local to the stack frame. The bool is
//whether it was already present in the previous frame, and the Stackitem
//is the value held by the previous frame, if any.

//please extern this shit with const!
unordered_map<string,function<void(vector<Stackitem>&,unordered_map<string,Stackitem>&)>> builtins={
	{"+",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("+",2)
		Stackitem b=move(S.back()); S.pop_back();
		Stackitem a=move(S.back()); S.pop_back();
		if(!a.isstr&&!b.isstr){
			S.emplace_back(a.intval+b.intval);
			return;
		}
		if(!a.isstr){
			a.strval=to_string(a.intval);
			a.isstr=true;
		}
		if(!b.isstr){
			b.strval=to_string(b.intval);
			b.isstr=true;
		}
		S.emplace_back(a.strval+b.strval);
	}},
	{"-",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("-",2)
		Stackitem b=move(S.back()); S.pop_back();
		Stackitem a=move(S.back()); S.pop_back();
		if(a.isstr||b.isstr)throw string("Builtin '-' cannot accept a string argument");
		S.emplace_back(a.intval-b.intval);
	}},
	{"*",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("*",2)
		Stackitem b=move(S.back()); S.pop_back();
		Stackitem a=move(S.back()); S.pop_back();
		if(a.isstr||b.isstr)throw string("Builtin '*' cannot accept a string argument");
		S.emplace_back(a.intval*b.intval);
	}},
	{"/",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("/",2)
		Stackitem b=move(S.back()); S.pop_back();
		Stackitem a=move(S.back()); S.pop_back();
		if(a.isstr||b.isstr)throw string("Builtin '/' cannot accept a string argument");
		S.emplace_back(a.intval/b.intval);
	}},
	{"%",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("%",2)
		Stackitem b=move(S.back()); S.pop_back();
		Stackitem a=move(S.back()); S.pop_back();
		if(a.isstr||b.isstr)throw string("Builtin '%' cannot accept a string argument");
		S.emplace_back(a.intval%b.intval);
	}},
	{"&",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("&",2)
		Stackitem b=move(S.back()); S.pop_back();
		Stackitem a=move(S.back()); S.pop_back();
		if(a.isstr||b.isstr)throw string("Builtin '&' cannot accept a string argument");
		S.emplace_back(a.intval&b.intval);
	}},
	{"|",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("|",2)
		Stackitem b=move(S.back()); S.pop_back();
		Stackitem a=move(S.back()); S.pop_back();
		if(a.isstr||b.isstr)throw string("Builtin '|' cannot accept a string argument");
		S.emplace_back(a.intval|b.intval);
	}},
	{"~",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("~",1)
		Stackitem &back=S.back();
		if(back.isstr)throw string("Builtin '~' cannot accept a string argument");
		else back.intval=~back.intval;
	}},
	{"^",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("^",2)
		Stackitem b=move(S.back()); S.pop_back();
		Stackitem a=move(S.back()); S.pop_back();
		if(a.isstr||b.isstr)throw string("Builtin '^' cannot accept a string argument");
		S.emplace_back((int)pow(a.intval,b.intval));
	}},
	{"!",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("!",1)
		Stackitem &back=S.back();
		if(back.isstr){
			back.isstr=false;
			back.intval=!back.strval.size();
		} else back.intval=!back.intval;
	}},
	{"=",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("=",2)
		Stackitem b=move(S.back()); S.pop_back();
		Stackitem a=move(S.back()); S.pop_back();
		S.emplace_back(a==b);
	}},
	{"<",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("<",2)
		Stackitem b=move(S.back()); S.pop_back();
		Stackitem a=move(S.back()); S.pop_back();
		if(a.isstr&&b.isstr)S.emplace_back(a.strval<b.strval);
		else if(!a.isstr&&!b.isstr)S.emplace_back(a.intval<b.intval);
		else if(a.isstr)S.emplace_back(a.strval<to_string(b.intval));
		else S.emplace_back(to_string(a.intval)<b.strval);
	}},
	{">",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE(">",2)
		Stackitem b=move(S.back()); S.pop_back();
		Stackitem a=move(S.back()); S.pop_back();
		if(a.isstr&&b.isstr)S.emplace_back(a.strval>b.strval);
		else if(!a.isstr&&!b.isstr)S.emplace_back(a.intval>b.intval);
		else if(a.isstr)S.emplace_back(a.strval>to_string(b.intval));
		else S.emplace_back(to_string(a.intval)>b.strval);
	}},
	{"print",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("print",1)
		Stackitem v=move(S.back()); S.pop_back();
		if(v.isstr)cout<<v.strval<<flush;
		else cout<<v.intval<<flush;
	}},
	{"lf",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("lf",0)
		cout<<endl;
	}},
	{"getline",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("getline",0)
		string line;
		getline(cin,line);
		if(!cin)S.emplace_back(-1);
		else S.emplace_back(line);
	}},
	{"getc",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("getc",0)
		char c=cin.get();
		if(!cin)S.emplace_back(-1);
		else S.emplace_back(string(1,c));
	}},
	{"fprint",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("fprint",2)
		Stackitem file=move(S.back()); S.pop_back();
		Stackitem v=move(S.back()); S.pop_back();
		if(file.isstr)throw string("Second argument to 'fprint' not a number");
		if(v.isstr)write(file.intval,v.strval.data(),v.strval.size());
		else {
			string s=to_string(v.intval);
			write(file.intval,s.data(),s.size());
		}
	}},
	{"flf",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("flf",1)
		Stackitem file=move(S.back()); S.pop_back();
		if(file.isstr)throw string("Argument to 'flf' not a number");
		write(file.intval,"\n",1);
	}},
	{"fgetc",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("fgetc",1)
		Stackitem file=move(S.back()); S.pop_back();
		if(file.isstr)throw string("Argument to 'fgetc' not a number");
		char c;
		int ret=read(file.intval,&c,1);
		if(ret==-1)perror("read");
		if(ret<=0)S.emplace_back(-1);
		else S.emplace_back(string(1,c));
	}},
	{"fopen",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("fopen",2)
		Stackitem mode=move(S.back()); S.pop_back();
		Stackitem fname=move(S.back()); S.pop_back();
		if(!fname.isstr)throw string("First argument to 'fopen' not a string");
		if(!mode.isstr)throw string("Second argument to 'fopen' not a string");
		int oflag=0;
		if(mode.strval.find('r')!=string::npos)oflag|=O_RDONLY;
		if(mode.strval.find('w')!=string::npos){
			if(oflag&O_RDONLY)oflag=(oflag&~O_RDONLY)|O_RDWR;
			else oflag|=O_WRONLY;
		}
		if(mode.strval.find('a')!=string::npos)oflag|=O_APPEND;
		if(mode.strval.find('x')!=string::npos)oflag|=O_EXCL|O_CREAT;
		if(oflag==0)oflag=O_RDONLY;
		int ret=open(fname.strval.c_str(),oflag);
		if(ret<0)perror("open");
		S.emplace_back(ret);
	}},
	{"argc",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("argc",0)
		S.emplace_back(g_argc);
	}},
	{"argvget",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("argvget",1)
		Stackitem idx=move(S.back()); S.pop_back();
		if(idx.isstr)throw string("Argument to 'argvget' not a number");
		if(idx.intval<0||idx.intval>=g_argc)throw string("Argument to 'argvget' out of bounds");
		S.emplace_back(g_argv[idx.intval]);
	}},
	{"dup",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("dup",1)
		S.push_back(S.back());
	}},
	{"pop",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("pop",1)
		S.pop_back();
	}},
	{"swap",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("swap",2)
		Stackitem b=move(S.back()); S.pop_back();
		Stackitem a=move(S.back()); S.pop_back();
		S.push_back(b);
		S.push_back(a);
	}},
	{"store",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("store",2)
		Stackitem name=move(S.back()); S.pop_back();
		if(!name.isstr)throw string("Second argument to 'store' not a string");
		auto it=variables.find(name.strval);
		bool exists=it!=variables.end();
		if(scopestack.size()&&scopestack.top().find(name.strval)==scopestack.top().end()){
			if(exists)
				scopestack.top()[name.strval]={move(variables[name.strval]),true};
			else
				scopestack.top()[name.strval]={Stackitem(),false};
		}
		//insert_or_assign is C++17 sadly
		/*if(exists)variables.insert_or_assign(it,make_pair(name.strval,move(S.back())));
		else variables[name.strval]=move(S.back());*/
		variables[name.strval]=move(S.back());
		S.pop_back();
	}},
	{"gstore",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("gstore",2)
		Stackitem name=move(S.back()); S.pop_back();
		if(!name.isstr)throw string("Second argument to 'store' not a string");
		variables[name.strval]=move(S.back());
		S.pop_back();
	}},
	{"get",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("get",1)
		Stackitem name=move(S.back()); S.pop_back();
		if(!name.isstr)throw string("Argument to 'get' not a string");
		S.push_back(variables[name.strval]);
	}},
	//leaves variable with value 0
	{"swapoutvar",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("swapoutvar",1)
		Stackitem name=move(S.back()); S.pop_back();
		if(!name.isstr)throw string("Second argument to 'swapoutvar' not a string");
		S.push_back(move(variables[name.strval]));
		variables[name.strval].intval=0;
	}},
	{"enterscope",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("enterscope",0)
		scopestack.emplace();
	}},
	{"leavescope",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("leavescope",0)
		if(scopestack.size()<1)throw string("Scope stack empty while requesting scope leave");
		for(const pair<string,pair<Stackitem,bool>> &var : scopestack.top()){
			if(var.second.second){
				variables[var.first]=move(var.second.first);
			} else {
				variables.erase(var.first);
			}
		}
		scopestack.pop();
	}},
	//positive n: roll OFF top to bottom; negative n: roll off bottom ONTO top
	{"roll",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("roll",1)
		Stackitem ns=move(S.back()); S.pop_back();
		if(ns.isstr)throw string("Argument to 'roll' not a number");
		int n=ns.intval;
		if(S.size()<2)return;
		bool negative=n<0;
		if(negative)n=-n;
		n%=S.size();
		if(n==0)return;
		if(negative){
			vector<Stackitem> tempstore;
			tempstore.reserve(n);
			int ssz=S.size();
			for(int i=ssz-n;i<ssz;i++)tempstore.push_back(move(S[i]));
			S.erase(S.end()-n,S.end());
			S.insert(S.begin(),n,Stackitem());
			for(int i=0;i<n;i++)S[i]=move(tempstore[i]);
		} else {
			//cerr<<"rolling back n="<<n<<endl;
			vector<Stackitem> tempstore;
			tempstore.reserve(n);
			for(int i=0;i<n;i++)tempstore.push_back(move(S[i]));
			S.erase(S.begin(),S.begin()+n);
			for(int i=0;i<n;i++)S.push_back(move(tempstore[i]));
		}
	}},
	//leaves the string on the stack
	{"stridx",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("stridx",2)
		Stackitem idx=move(S.back()); S.pop_back();
		const Stackitem &str=S.back();
		if(!str.isstr)throw string("First argument to 'stridx' not a string");
		if(idx.isstr)throw string("Second argument to 'stridx' not a number");
		if(idx.intval<0||idx.intval>=(int)str.strval.size())throw string("Index argument to 'stridx' out of range");
		S.emplace_back(string(1,str.strval[idx.intval]));
	}},
	//leaves the string on the stack
	{"strlen",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("strlen",1)
		const Stackitem &str=S.back();
		if(!str.isstr)throw string("Argument to 'strlen' not a string");
		S.emplace_back(str.strval.size());
	}},
	//leaves the string on the stack
	{"substr",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("substr",3)
		const Stackitem to=move(S.back()); S.pop_back();
		const Stackitem from=move(S.back()); S.pop_back();
		const Stackitem &str=S.back();
		if(!str.isstr)throw string("First argument to 'substr' not a string");
		if(from.isstr)throw string("Second argument to 'substr' not a number");
		if(to.isstr)throw string("Third argument to 'substr' not a number");
		S.emplace_back(str.strval.substr(from.intval,to.intval-from.intval));
	}},
	{"chr",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("chr",1)
		Stackitem ascii=move(S.back()); S.pop_back();
		if(ascii.isstr)throw string("Argument to 'chr' not a number");
		S.emplace_back(string(1,ascii.intval));
	}},
	{"ord",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("ord",1)
		if(!S.back().isstr)throw string("Argument to 'ord' not a string");
		S.back().isstr=false;
		S.back().intval=(int)S.back().strval[0];
		S.back().strval="";
	}},
	{"sleep",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("sleep",1)
		if(S.back().isstr)throw string("Argument to 'sleep' not a number");
		usleep(S.back().intval);
		S.pop_back();
	}},
	{"stackdump",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("stackdump",0)
		cerr<<"STACKDUMP: ";
		for(const Stackitem &si : S){
			if(si.isstr)cerr<<'"'<<si.strval<<"\" ";
			else cerr<<si.intval<<' ';
		}
		cerr<<endl;
	}},
	{"exit",[](vector<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		BUILTIN_GUARD_STACKSIZE("exit",0)
		exit(0);
	}},
};
#undef BUILTIN_GUARD_STACKSIZE