summaryrefslogtreecommitdiff
path: root/parser.cpp
blob: 56fb0356bc3f82b29ab0d2862d69611583c0e0ec (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
#include <stdexcept>
#include <stack>
#include <cstring>
#include <cctype>
#include <cassert>
#include "parser.h"

using namespace std;


ParseError::ParseError(const string &what_arg)
	:runtime_error(what_arg){}
ParseError::ParseError(const char *what_arg)
	:runtime_error(what_arg){}


static bool isinitwordchar(char c){
	return isalpha(c)||c=='_';
}

static bool iswordchar(char c){
	return isalpha(c)||isdigit(c)||c=='_';
}

static const vector<string> tok_symbols={
	"==", "!=", ">", "<", ">=", "<=",
	":=", "=",
	"+", "-", "*", "/", "%",
	"(", ")", ",",
	"{", "}", "?{", "??{", "}&",
};

static bool isSymbolPrefix(const string &s){
	for(const string &sym : tok_symbols){
		if(s.size()<=sym.size()&&sym.substr(0,s.size())==s)return true;
	}
	return false;
}

template <typename T>
static bool contains(const vector<T> &v,const T &target){
	for(const T &t : v){
		if(t==target)return true;
	}
	return false;
}


class Token{
public:
	enum class Type{
		word,
		number,
		string,
		symbol,
		terminator,
	};

	Type type;
	string str;
	Site site;

	Token(Type type,const string &str,const Site &site)
		:type(type),str(str),site(site){}
};


class Tokeniser{
	const string &source;
	const string &filename;
	i64 idx,nextidx;
	i64 lnum,linex;
	Token::Type ttype;

	/*struct State{
		i64 idx,nextidx;
		i64 lnum,lineidx;
		Token::Type ttype;
	};

	stack<State> statestack;*/

	bool eof(i64 at){
		return at>=(i64)source.size();
	}

	string get_() const {
		if(eof())throw runtime_error("Tokeniser::get() on eof");
		if(nextidx==-1)throw runtime_error("Tokeniser::get() before advance");
		if(nextidx==-2)throw runtime_error("Tokeniser::get() after eof");
		assert(nextidx>=0);
		return source.substr(idx,nextidx-idx);
	}

public:
	Tokeniser(const string &source,const string &filename)
		:source(source),filename(filename),
		 idx(0),nextidx(-1),
		 lnum(1),linex(1){}

	Tokeniser& operator=(const Tokeniser &other){
		if(&source!=&other.source||&filename!=&other.filename){
			throw runtime_error("Tokeniser::operator= on incompatible Tokeniser");
		}
		idx=other.idx; nextidx=other.nextidx;
		lnum=other.lnum; linex=other.linex;
		ttype=other.ttype;
		return *this;
	}

	/*void save(){
		statestack.push({idx,nextidx,lnum,lineidx,ttype});
	}

	void restore(){
		if(statestack.size()==0)throw runtime_error("Tokeniser::restore() on empty stack");
		const State &st=statestack.top();
		idx=st.idx; nextidx=st.nextidx;
		lnum=st.lnum; linex=st.linex;
		ttype=st.ttype;
		statestack.pop();
	}

	void discardstate(){
		if(statestack.size()==0)throw runtime_error("Tokeniser::discardstate() on empty stack");
		statestack.pop();
	}*/

	bool eof() const {
		return idx>=(i64)source.size();
	}

	Token get(){
		return Token(ttype,get_(),Site(filename,lnum,linex));
	}

	// Returns whether there are more tokens
	bool advance(){
		if(eof())return false;

		// Let nextidx catch up with idx
		while(idx<nextidx){
			if(source[idx]=='\n'){
				lnum++;
				linex=1;
			} else {
				linex++;
			}
			idx++;
		}

		// Skip any whitespace and/or comments, emitting a terminator on a newline
		while(true){
			i64 origidx=idx;
			while(!eof()&&isspace(source[idx])){
				if(source[idx]=='\n'){
					nextidx=idx+1;
					ttype=Token::Type::terminator;
					return true;
				}
				linex++;
				idx++;
			}
			if(eof())return false;

			if(source[idx]=='#'){
				while(!eof()&&source[idx]!='\n')idx++;
				if(eof())return false;
				nextidx=idx+1;
				ttype=Token::Type::terminator;
				return true;
			}

			if(idx==origidx)break;
		}

		nextidx=idx;

		// Terminator semicolon
		if(source[nextidx]==';'){
			ttype=Token::Type::terminator;
			nextidx++;
			return true;
		}

		// Word
		if(isinitwordchar(source[nextidx])){
			ttype=Token::Type::word;
			do nextidx++;
			while(!eof(nextidx)&&iswordchar(source[nextidx]));
			return true;
		}

		// Number literal
		if(isdigit(source[nextidx])||(!eof(nextidx+1)&&source[nextidx]=='-'&&isdigit(source[nextidx+1]))){
			ttype=Token::Type::number;
			if(source[nextidx]=='-')nextidx++;
			while(!eof(nextidx)&&isdigit(source[nextidx]))nextidx++;
			if(eof(nextidx))return true;
			if(source[nextidx]=='.'){
				nextidx++;
				if(eof(nextidx)||!isdigit(source[nextidx])){
					throw ParseError("Incomplete floating point literal at EOF");
				}
				while(!eof(nextidx)&&isdigit(source[nextidx]))nextidx++;
				if(eof(nextidx))return true;
			}
			if(strchr("eE",source[nextidx])!=NULL){
				nextidx++;
				if(eof(nextidx)||strchr("+-0123456789",source[nextidx])==NULL){
					throw ParseError("Incomplete floating point literal at EOF");
				}
				if(strchr("+-",source[nextidx])!=NULL){
					nextidx++;
					if(eof(nextidx))throw ParseError("Incomplete floating point literal at EOF");
				}
				while(!eof(nextidx)&&isdigit(source[nextidx]))nextidx++;
			}
			return true;
		}

		// String literal
		if(source[nextidx]=='"'){
			ttype=Token::Type::string;
			nextidx++;
			while(!eof(nextidx)&&source[nextidx]!='"'){
				if(source[nextidx]=='\\')nextidx++;
			}
			if(eof(nextidx))throw ParseError("Incomplete string literal at EOF");
			nextidx++;
			return true;
		}

		// Symbol
		if(isSymbolPrefix({source[idx]})){
			ttype=Token::Type::symbol;
			nextidx++;
			while(!eof(nextidx)){
				if(!isSymbolPrefix(get_()))return true;
				nextidx++;
			}
			if(contains(tok_symbols,get_()))return true;
			else throw ParseError("Unknown symbol at EOF");
		}

		throw ParseError("Unknown token starting at '"+source.substr(idx,5)+"'");
	}
};


static Expression parseExpression(Tokeniser &tokeniser){
	;
}

static Statement parseStatement(Tokeniser &tokeniser){
	Token tok=tokeniser.get();
	switch(tok.type){
		case Token::Type::word:{
			Tokeniser copyiser(tokeniser);
			copyiser.advance();
			Token tok2=copyiser.get();
			if(tok2.type==Token::Type::symbol&&tok2.str==":="){
				tokeniser=copyiser;
				tokeniser.advance();
				return Statement(Statement::Type::create,tok.str,parseExpression(tokeniser));
			} else if(tok2.type==Token::Type::symbol&&tok2.str=="="){
				tokeniser=copyiser;
				tokeniser.advance();
				return Statement(Statement::Type::assign,tok.str,parseExpression(tokeniser));
			} else {
				return Statement(Statement::Type::expression,parseExpression(tokeniser));
			}
		}

		case Token::Type::number:
		case Token::Type::string:
		case Token::Type::symbol:
			return Statement(Statement::Type::expression,parseExpression(tokeniser));

		case Token::Type::terminator:
			throw runtime_error("Unexpected terminator in parseStatement()");
	}
}

StatementList parse(const string &source,const string &filename){
	Tokeniser tokeniser(source,filename);
	if(!tokeniser.advance())return {};
	StatementList stl;
	while(!tokeniser.eof()){
		if(tokeniser.get().type==Token::Type::terminator)continue;
		stl.push_back(parseStatement(tokeniser));
	}
	return stl;
}