summaryrefslogtreecommitdiff
path: root/postrun.cpp
blob: 4de856b1443d80d044a31b9b7ab5d2f80ce31685 (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
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <unordered_map>
#include <stack>
#include <functional>
#include <cstdlib>
#include <cstring>

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);}

vector<string> tokenise(istream &stream){
	vector<string> tokens;
	string token;
	char c,stringmode;
	while(true){
		c=stream.get();
		if(!stream)break;
		if(isword(c)||(token.size()>0&&isextword(c))){
			token+=c;
		} else if(isdigit(c)){
			if(token.size()==0)token="#";
			token+=c;
		} else {
			if(token.size())tokens.push_back(move(token));
			else if(isoperator(c))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 'x':{
								int res;
								c=stream.get();
								cerr<<"Parsed "<<c;
								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();
								cerr<<c<<" into ";
								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;
								cerr<<(int)c<<endl;
								break;
							}
						}
					}
					token+=c;
				}
			} else if(isspace(c));
			else throw string("Invalid character found in source: '")+c+'\'';
		}
	}
	return tokens;
}

string readfile(ifstream stream){
	stream.seekg(0,ios::end);
	size_t len=stream.tellg();
	stream.seekg(0,ios::beg);
	string contents;
	contents.resize(len);
	stream.read(&*contents.begin(),len);
	return contents;
}

struct Stackitem{
	bool isstr;
	string strval;
	int intval;
};

const unordered_map<string,function<void(stack<Stackitem>&,unordered_map<string,Stackitem>&)>> builtins={
	{"+",[](stack<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		if(S.size()<2)throw string("Builtin '+' needs 2 stack items");
		Stackitem a,b,c;
		b=S.top(); S.pop();
		a=S.top(); S.pop();
		if(!a.isstr&&!b.isstr){
			S.push({false,"",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.push({true,a.strval+b.strval,0});
	}},
	{"-",[](stack<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		if(S.size()<2)throw string("Builtin '-' needs 2 stack items");
		Stackitem a,b,c;
		b=S.top(); S.pop();
		a=S.top(); S.pop();
		if(a.isstr||b.isstr)throw string("Builtin '-' cannot accept a string argument");
		S.push({false,"",a.intval-b.intval});
	}},
	{"print",[](stack<Stackitem> &S,unordered_map<string,Stackitem> &variables){
		if(S.size()<1)throw string("Builtin 'print' needs 1 stack item");
		Stackitem v;
		v=S.top(); S.pop();
		if(v.isstr)cout<<v.strval<<flush;
		else cout<<v.intval<<flush;
	}}
};

void runpass2(const vector<string> &T,
              stack<Stackitem> &S,
              const unordered_map<string,vector<string>> &functions,
              unordered_map<string,Stackitem> &variables){
	unsigned int cursor;
	for(cursor=0;cursor<T.size();cursor++){
		const string &word=T[cursor];
		//cerr<<"Executing word <"<<word<<'>'<<endl;
		if(word[0]=='#'){
			S.push({false,"",(int)strtol(word.data()+1,NULL,10)});
		} else if(word[0]=='\''){
			S.push({true,word.substr(1),0});
		} else {
			auto it=variables.find(word);
			if(it!=variables.end()){
				S.push(it->second);
			} else {
				auto it=functions.find(word);
				if(it!=functions.end()){
					runpass2(it->second,S,functions,variables);
				} else {
					auto it=builtins.find(word);
					if(it==builtins.end())throw "Unknown variable or function '"+word+"'";
					it->second(S,variables);
				}
			}
		}
	}
}

void run(vector<string> T){
	unordered_map<string,vector<string>> functions;
	unordered_map<string,Stackitem> variables;
	stack<Stackitem> S;
	unsigned int cursor=0;
	//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;
			for(end=start;end<T.size()&&T[end]!="}";end++);
			vector<string> &functoks=functions[name];
			functoks.resize(end-start);
			for(unsigned int i=start;i<end;i++)functoks[i]=move(T[i]);
			T.erase(T.begin()+cursor,T.begin()+end+1);
		} else if(word=="@include"){
			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);
			ifstream file(name);
			if(!file)throw string("Could not open file '")+name+"' specified by @include statement";
			vector<string> included=tokenise(file);
			file.close();
			T.erase(T.begin()+cursor,T.begin()+cursor+2);
			T.insert(T.begin()+cursor,included.begin(),included.end());
		} else {
			cursor++;
		}
	}

	//second pass, evaluate file
	runpass2(T,S,functions,variables);
}

int main(int argc,char **argv){
	if(argc!=2){
		cerr<<"Call this interpreter with the Postrun source file"<<endl;
		return 1;
	}
	ifstream srcf(argv[1]);
	if(!srcf){
		cerr<<"Could not open file '"<<argv[1]<<"'"<<endl;
		return 1;
	}
	//string source=readfile(srcf);
	try {
		const vector<string> tokens=tokenise(srcf);
		for(const string &tok : tokens)cerr<<'['<<tok<<"] "; cerr<<endl;
		run(tokens);
		srcf.close();
	} catch(string e){
		cerr<<"ERROR: "<<e<<endl;
		return 1;
	}
}