summaryrefslogtreecommitdiff
path: root/evaluate.cpp
blob: 550fc4920b98d9cd71024d76b1de9349c27982a4 (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
#include <stdexcept>
#include <cassert>
#include "evaluate.h"

using namespace std;


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

CompilationError::CompilationError(const string &what_arg)
	:runtime_error(what_arg){}
CompilationError::CompilationError(const char *what_arg)
	:runtime_error(what_arg){}
CompilationError::CompilationError(Site site,const string &what_arg)
	:runtime_error(site.filename+":"+to_string(site.lnum)+":"+to_string(site.linex)+": "+what_arg){}


Value::Value()
	:type(Type::nil){}
Value::Value(double numval)
	:type(Type::number),numval(numval){}
Value::Value(const string &strval)
	:type(Type::string),strval(strval){}
Value::Value(ScopeVal *scopeval)
	:type(Type::scope),scopeval(scopeval){}


ScopeVal::ScopeVal(const ScopeDef &scopeDef)
	:scopeDef(scopeDef){}


namespace A {
	struct CollItem{
		int count;
		void (*deleter)(void*);
	};

	template <typename T>
	static void generic_deleter(void *p){
		delete (T*)p;
	}

	unordered_map<void*,CollItem> collection;

	template <typename T>
	T* ref(T *p){
		auto it=collection.find((void*)p);
		if(it==collection.end()){
			collection.emplace((void*)p,(CollItem){1,generic_deleter<T>});
		} else {
			it->second.count++;
		}
		return p;
	}

	template <typename T>
	void unref(T *p){
		auto it=collection.find(p);
		assert(it!=collection.end());
		if(it->second.count==1){
			if(it->second.deleter!=generic_deleter<T>){
				throw runtime_error("Unref'd pointer type not the same as the ref'd version!");
			}
			it->second.deleter(p);
			collection.erase(it);
		} else {
			it->second.count--;
		}
	}

	void unref_all(){
		for(const pair<void*,CollItem> &p : collection){
			p.second.deleter(p.first);
		}
	}
}


Assembly::Instruction::Instruction(Type type):type(type){
	if(type==Type::pushnum||type==Type::pushstr||type==Type::pushscope||
	   type==Type::create||type==Type::store||type==Type::load||type==Type::call){
		throw runtime_error("Instruction(type) called with invalid type "+to_string((int)type));
	}
}
Assembly::Instruction::Instruction(Type type,double num):type(type),num(num){
	if(type!=Type::pushnum){
		throw runtime_error("Instruction(type,num) called with invalid type "+to_string((int)type));
	}
}
Assembly::Instruction::Instruction(Type type,const string &arg):type(type){
	if(type==Type::pushstr){
		str=arg;
	} else if(type==Type::create||type==Type::store||type==Type::load){
		name=arg;
	} else {
		throw runtime_error("Instruction(type,string) called with invalid type "+to_string((int)type));
	}
}
Assembly::Instruction::Instruction(Type type,ScopeDef *scope):type(type),scope(scope){
	if(type!=Type::pushscope){
		throw runtime_error("Instruction(type,scope) called with invalid type "+to_string((int)type));
	}
}
Assembly::Instruction::Instruction(Type type,int nargs):type(type),nargs(nargs){
	if(type!=Type::call){
		throw runtime_error("Instruction(type,nargs) called with invalid type "+to_string((int)type));
	}
}

void Assembly::codegen(const StatementList &stl){
	for(const Statement &stmt : stl){
		codegen(stmt);
	}
}

void Assembly::codegen(const Statement &stmt){
	switch(stmt.type){
		case Statement::Type::create:
			codegen(stmt.expr);
			listing.emplace_back(Instruction::Type::create,stmt.dstvar);
			break;

		case Statement::Type::assign:
			codegen(stmt.expr);
			listing.emplace_back(Instruction::Type::store,stmt.dstvar);
			break;

		case Statement::Type::expression:
			codegen(stmt.expr);
			listing.emplace_back(Instruction::Type::pop);
			break;
	}
}

void Assembly::codegen(const Expression &expr){
	switch(expr.type){
		case Expression::Type::binop:
			codegen(expr.args[0]);
			codegen(expr.args[1]);
			if(expr.name=="+")listing.emplace_back(Instruction::Type::add);
			else if(expr.name=="-")listing.emplace_back(Instruction::Type::sub);
			else if(expr.name=="*")listing.emplace_back(Instruction::Type::mul);
			else if(expr.name=="/")listing.emplace_back(Instruction::Type::div);
			else if(expr.name=="%")listing.emplace_back(Instruction::Type::mod);
			else throw runtime_error("Unknown binop name '"+expr.name+"'");
			break;

		case Expression::Type::unop:
			throw runtime_error("No unary operators known?");

		case Expression::Type::call:
		case Expression::Type::dive:
			if(expr.name=="nil"){
				if(expr.args.size()!=0||expr.type==Expression::Type::dive){
					if(expr.type==Expression::Type::call)throw CompilationError(expr.site,"Cannot call nil");
					else throw CompilationError(expr.site,"Cannot dive into nil");
				}
				listing.emplace_back(Instruction::Type::pushnil);
				break;
			}
			listing.emplace_back(Instruction::Type::load,expr.name);
			for(const Expression &arg : expr.args){
				codegen(arg);
			}
			if(expr.args.size()!=0){
				listing.emplace_back(Instruction::Type::call,(int)expr.args.size());
			}
			if(expr.type==Expression::Type::dive){
				listing.emplace_back(Instruction::Type::enter);
				assert(expr.scope.type==ScopeDef::Type::direct);
				codegen(expr.scope.body);
				listing.emplace_back(Instruction::Type::leave);
			}
			break;

		case Expression::Type::number:
			listing.emplace_back(Instruction::Type::pushnum,expr.numval);
			break;

		case Expression::Type::string:
			listing.emplace_back(Instruction::Type::pushstr,expr.strval);
			break;

		case Expression::Type::scope:
			listing.emplace_back(Instruction::Type::pushscope,new ScopeDef(expr.scope));
			break;
	}
}

Assembly::Assembly(const StatementList &stl){
	codegen(stl);
}


ostream& operator<<(ostream &os,const Assembly &as){
	for(const Assembly::Instruction &ins : as.listing){
		switch(ins.type){
			case Assembly::Instruction::Type::pushnil: os<<"pushnil\n"; break;
			case Assembly::Instruction::Type::pushnum: os<<"pushnum "<<ins.num<<"\n"; break;
			case Assembly::Instruction::Type::pushstr: os<<"pushstr "<<ins.str<<"\n"; break;
			case Assembly::Instruction::Type::pushscope: os<<"pushscope "<<*ins.scope<<"\n"; break;
			case Assembly::Instruction::Type::pop: os<<"pop\n"; break;
			case Assembly::Instruction::Type::swap: os<<"swap\n"; break;
			case Assembly::Instruction::Type::add: os<<"add\n"; break;
			case Assembly::Instruction::Type::sub: os<<"sub\n"; break;
			case Assembly::Instruction::Type::mul: os<<"mul\n"; break;
			case Assembly::Instruction::Type::div: os<<"div\n"; break;
			case Assembly::Instruction::Type::mod: os<<"mod\n"; break;
			case Assembly::Instruction::Type::create: os<<"create "<<ins.name<<"\n"; break;
			case Assembly::Instruction::Type::store: os<<"store "<<ins.name<<"\n"; break;
			case Assembly::Instruction::Type::load: os<<"load "<<ins.name<<"\n"; break;
			case Assembly::Instruction::Type::enter: os<<"enter\n"; break;
			case Assembly::Instruction::Type::leave: os<<"leave\n"; break;
			case Assembly::Instruction::Type::call: os<<"call "<<ins.nargs<<"\n"; break;
		}
	}
	return os;
}