-
Notifications
You must be signed in to change notification settings - Fork 0
/
AST.ts
254 lines (213 loc) · 7.33 KB
/
AST.ts
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
import { CompileData, CompilationContext, FintScope, FintMeta, HangingLabel, locs } from './typesConstansts.ts';
import { ops, ptr, abs, stack, addLabel, resolvePtr, writeToRam, addArgs, absToPtr, resolveRef, stackToPtr } from './macros.ts';
type CompileReturn = {
immediate: CompileData[],
memory: CompileData[]
};
export abstract class FintASTConstruct {
constructor(public scope: FintScope, public meta: FintMeta){}
abstract compile(context: CompilationContext): CompileReturn
}
export abstract class FintValue extends FintASTConstruct {}
export class FintNumberLiteral extends FintValue {
constructor(scope: FintScope, meta: FintMeta, public value: bigint){
super(scope, meta);
}
compile(context: CompilationContext): CompileReturn {
const memLoc = Symbol('intMemoryLocation')
return {
immediate: [
...ops.copy(abs(this.value), stack(0)),
],
memory: [],
}
}
}
export class FintVariableReference extends FintValue {
static instances: FintVariableReference[] = [];
constructor(scope: FintScope, meta: FintMeta, public name: string){
super(scope, meta);
FintVariableReference.instances.push(this);
}
compile(context: CompilationContext): CompileReturn {
return {
immediate: [
...ops.copy(resolvePtr(resolveRef(this.name)), stack(0), context)
],
memory: [],
}
}
}
export class FintTuple extends FintValue {
constructor(scope: FintScope, meta: FintMeta, public values: FintValue[]){
super(scope, meta);
}
compile(context: CompilationContext): CompileReturn {
const compiled = this.values.map(v => v.compile(context));
return {
immediate: [
...ops.copy(stack(-2), stack(0)), // copy stack pointer
...ops.moveStack(2),
...ops.copy(ptr(locs.ramPointer), stack(-1)), // save tuple location
...ops.addTo(abs(this.values.length + 1), ptr(locs.ramPointer)), // allocate space for values
...ops.copy(abs(this.values.length), stackToPtr(stack(-1))), // write length
...compiled.flatMap((c, i) => {
return [
...c.immediate,
...ops.copy(stack(0), addArgs(abs(i + 1), stackToPtr(stack(-1)))), // write into the tuple
];
}),
...ops.moveStack(-2),
...ops.copy(stack(1), stack(0)),
],
memory: compiled.flatMap(c => c.memory),
}
}
}
export class FintCall extends FintValue {
constructor(scope: FintScope, public fn: FintValue, public arg: FintValue){
super(scope, fn.meta);
}
compile(context: CompilationContext): CompileReturn {
const forwardMemory = [];
const resumeLoc = Symbol('funcCall');
return {
immediate: [
...ops.copy(stack(-2), stack(1)),
...ops.moveStack(3),
// fn instance ptr will go in stack-3
// scope is in stack-2
// arg ptr will do in stack-1
// evaluate function (returns func instance pointer)
...(()=>{
const subContext: CompilationContext = {
meta: this.fn.meta,
scope: this.fn.scope,
}
const {immediate, memory} = this.fn.compile(subContext);
forwardMemory.push(...memory);
return immediate;
})(),
...ops.copy(stack(0), stack(-3)),
// evaluate argument (returns func arg pointer)
...(()=>{
const subContext: CompilationContext = {
meta: this.arg.meta,
scope: this.arg.scope,
}
const {immediate, memory} = this.arg.compile(subContext);
forwardMemory.push(...memory);
return immediate;
})(),
...ops.copy(stack(0), stack(-1)),
// create scope in stack+0
...ops.copy(ptr(locs.ramPointer), stack(0)),
...writeToRam(addArgs(stack(-3), ptr(1))), // parent scope
...writeToRam(stack(-1)), // arg pointer
// copy return location to stack+1
...ops.copy(abs(resumeLoc), stack(1)),
...ops.jump(resolvePtr(stackToPtr(stack(-3)))),
new HangingLabel(resumeLoc),
// restore stack location
...ops.moveStack(-3),
// copy return value to stack+0
...ops.copy(stack(3), stack(0)),
],
memory: forwardMemory,
}
}
}
export class FintAssignment extends FintASTConstruct {
constructor(
scope: FintScope,
public ref: FintVariableReference,
public value: FintValue,
public wheres: FintAssignment[],
){
super(scope, ref.meta);
}
compile(context: CompilationContext): CompileReturn {
const continueLoc = Symbol(`fintAssignment-${this.ref.name}`);
// variables from evaluations that need to be passed on
const forwardMemory: CompileData[] = [
addLabel(this.scope.parent!.location!, this.scope.location!),
...this.wheres.map(() => 0),
];
const mainContext: CompilationContext = {
meta: this.value.meta,
scope: this.value.scope,
}
const main = this.value.compile(mainContext);
forwardMemory.push(...main.memory);
return {
immediate: [
// create scope
...ops.copy(abs(this.wheres.length), stack(0), context), // # of things to allocate
...ops.copy(stack(-2), stack(1), context), // parent scope
...ops.copy(abs(continueLoc), stack(2), context), // continue location
...ops.jump(abs(locs.allocateScopeSym)),
new HangingLabel(continueLoc),
...ops.moveStack(2), // constucted scope is now at stack-2
// define wheres
...this.wheres.flatMap(where => {
const {immediate, memory} = where.compile(mainContext);
forwardMemory.push(...memory);
return [
...immediate,
...ops.copy(stack(0), resolveRef(where.ref.name), mainContext), // copy the return value into the scope
]
}),
// execute main
...main.immediate,
// move stack back and set return value
...ops.moveStack(-2),
...ops.copy(stack(2), stack(0)),
],
memory: forwardMemory,
}
}
}
export class FintFunct extends FintValue {
constructor(scope: FintScope, public arg: FintVariableReference, public body: FintValue){
super(scope, arg.meta);
}
compile(context: CompilationContext): CompileReturn {
const defLoc = Symbol('funcDefLoc');
const subContext: CompilationContext = {
meta: this.meta,
scope: this.body.scope,
}
const body = this.body.compile(subContext);
return {
immediate: [
// create inner function instance to return
...ops.copy(ptr(locs.ramPointer), stack(0)),
...writeToRam(abs(defLoc)), // inner function def address
...writeToRam(stack(-2)), // scope
],
memory: [
new HangingLabel(defLoc),
...ops.moveStack(2),
...body.immediate,
// end of function
...ops.moveStack(-2),
...ops.copy(stack(2), stack(0)), // pointer to var
...ops.jump(stack(1)),
// any extra stuff generated
...body.memory,
],
}
}
}
export class FintWrappedValue extends FintValue {
constructor(scope: FintScope, public arg: FintValue, meta?: FintMeta){
super(scope, meta ?? arg.meta);
}
compile(context: CompilationContext): CompileReturn {
return this.arg.compile(context);
}
unwrap(): FintValue {
if(this.arg instanceof FintWrappedValue) return this.arg.unwrap();
return this.arg;
}
}