-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodegen.res
More file actions
413 lines (373 loc) · 11 KB
/
Copy pathCodegen.res
File metadata and controls
413 lines (373 loc) · 11 KB
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// SPDX-License-Identifier: MPL-2.0
// Codegen.res - AST to bytecode compiler for Error-Lang
//
// Compiles parsed AST into bytecode while preserving:
// - Positional semantics metadata
// - Computational haptics trace points
// - Paradox injection points
open Types
open Bytecode
// Compiler state
type compiler = {
mutable code: array<opcode>,
mutable constants: array<value>,
mutable locations: array<location>,
mutable localCount: int,
mutable locals: array<string>,
}
let make = (): compiler => {
{
code: [],
constants: [],
locations: [],
localCount: 0,
locals: [],
}
}
// Emit bytecode instruction
let emit = (c: compiler, op: opcode, loc: location): unit => {
c.code->Array.push(op)->ignore
c.locations->Array.push(loc)->ignore
}
// Add constant to constant pool
let addConstant = (c: compiler, value: value): int => {
c.constants->Array.push(value)->ignore
c.constants->Array.length - 1
}
// Resolve local variable
let resolveLocal = (c: compiler, name: string): option<int> => {
c.locals->Array.findIndexOpt(local => local == name)
}
// Add local variable
let addLocal = (c: compiler, name: string): int => {
c.locals->Array.push(name)->ignore
c.localCount = c.localCount + 1
c.localCount - 1
}
// Compile expression to bytecode
let rec compileExpr = (c: compiler, expr: expr): unit => {
switch expr {
| IntLit(n, loc) => {
emit(c, OpPush(VInt(n)), loc)
}
| FloatLit(f, loc) => {
emit(c, OpPush(VFloat(f)), loc)
}
| StringLit(s, loc) => {
emit(c, OpPush(VString(s)), loc)
}
| BoolLit(b, loc) => {
emit(c, OpPush(VBool(b)), loc)
}
| NilLit(loc) => {
emit(c, OpPush(VNil), loc)
}
| Ident(name, loc) => {
// Try local first, then global
switch resolveLocal(c, name) {
| Some(index) => emit(c, OpGetLocal(index), loc)
| None => emit(c, OpGetGlobal(name), loc)
}
}
| Array(elements, loc) => {
// Compile each element
elements->Array.forEach(elem => compileExpr(c, elem))
// Create array with N elements
emit(c, OpArray(elements->Array.length), loc)
}
| Binary(left, op, right, loc) => {
// Compile operands
compileExpr(c, left)
compileExpr(c, right)
// Emit operator with positional metadata
let opcode = switch op {
| Add => {
// Extract position from location
let pos: positionMetadata = {
line: loc.start.line,
column: loc.start.column,
operatorType: PlusOp,
}
OpAdd(pos)
}
| Sub => OpSub
| Mul => {
let pos: positionMetadata = {
line: loc.start.line,
column: loc.start.column,
operatorType: StarOp,
}
OpMul(pos)
}
| Div => OpDiv
| Mod => OpMod
| Eq => OpEq
| Neq => OpNeq
| Lt => OpLt
| Gt => OpGt
| Lte => OpLte
| Gte => OpGte
| LAnd => OpAnd
| LOr => OpOr
| BAnd | BOr | BXor | Shl | Shr => OpPush(VNil) // Not implemented yet
}
emit(c, opcode, loc)
}
| Unary(op, operand, loc) => {
compileExpr(c, operand)
switch op {
| Neg => emit(c, OpNegate, loc)
| LNot => emit(c, OpNot, loc)
| BNot => () // Not implemented
}
}
| Call(callee, args, loc) => {
// Echo builtins compile to dedicated opcodes (the VM has no general call yet).
// Arguments are pushed left-to-right; for `echo(x, y)` the output `y` is on top,
// which is exactly what OpEcho pops first.
switch callee {
| Ident("echo", _) => {
args->Array.forEach(a => compileExpr(c, a))
emit(c, OpEcho, loc)
}
| Ident("echo_to_residue", _) => {
args->Array.forEach(a => compileExpr(c, a))
emit(c, OpEchoToResidue, loc)
}
| Ident("residue_strictly_loses", _) => {
args->Array.forEach(a => compileExpr(c, a))
emit(c, OpResidueStrictlyLoses, loc)
}
| Ident("echo_input", _) => {
args->Array.forEach(a => compileExpr(c, a))
emit(c, OpEchoInput, loc)
}
| Ident("echo_output", _) => {
args->Array.forEach(a => compileExpr(c, a))
emit(c, OpEchoOutput, loc)
}
| _ =>
// Other function calls not fully implemented yet
emit(c, OpPush(VNil), loc)
}
}
| Index(array, index, loc) => {
compileExpr(c, array)
compileExpr(c, index)
emit(c, OpIndex, loc)
}
| Member(_obj, _field, loc) => {
// Member access not implemented
emit(c, OpPush(VNil), loc)
}
| Ternary(cond, then_, else_, loc) => {
// Compile condition
compileExpr(c, cond)
// Jump if false (to else branch)
let elseJump = c.code->Array.length
emit(c, OpJumpIfFalse(0), loc) // Patch later
// Compile then branch
compileExpr(c, then_)
// Jump over else branch
let endJump = c.code->Array.length
emit(c, OpJump(0), loc) // Patch later
// Patch else jump
let elseStart = c.code->Array.length
c.code[elseJump] = OpJumpIfFalse(elseStart - elseJump - 1)
// Compile else branch
compileExpr(c, else_)
// Patch end jump
let end = c.code->Array.length
c.code[endJump] = OpJump(end - endJump - 1)
}
| Lambda(_params, _returnType, _body, loc) => {
// Lambdas not implemented
emit(c, OpPush(VNil), loc)
}
}
}
// Compile statement to bytecode
let rec compileStmt = (c: compiler, stmt: stmt): unit => {
switch stmt {
| LetStmt({mutable_: _, name, type_: _, value, loc}) => {
// Compile initializer
compileExpr(c, value)
// Add local variable
let index = addLocal(c, name)
emit(c, OpSetLocal(index), loc)
}
| AssignStmt({target, value, loc}) => {
// Compile value
compileExpr(c, value)
// Assign to target
switch target {
| Ident(name, _) => {
switch resolveLocal(c, name) {
| Some(index) => emit(c, OpSetLocal(index), loc)
| None => emit(c, OpSetGlobal(name), loc)
}
}
| Index(array, index, _) => {
// Array assignment not fully implemented
compileExpr(c, array)
compileExpr(c, index)
}
| _ => () // Other assignment targets not implemented
}
}
| IfStmt({cond, then_, elseifs, else_, loc}) => {
// Compile condition
compileExpr(c, cond)
// Jump if false to elseif/else
let thenJump = c.code->Array.length
emit(c, OpJumpIfFalse(0), loc)
// Compile then branch
then_->Array.forEach(stmt => compileStmt(c, stmt))
// Jump to end
let endJump = c.code->Array.length
emit(c, OpJump(0), loc)
// Patch then jump
let elseStart = c.code->Array.length
c.code[thenJump] = OpJumpIfFalse(elseStart - thenJump - 1)
// Compile elseifs (simplified - not handling multiple elseifs)
elseifs->Array.forEach(((cond, body)) => {
compileExpr(c, cond)
let elseifJump = c.code->Array.length
emit(c, OpJumpIfFalse(0), loc)
body->Array.forEach(stmt => compileStmt(c, stmt))
let elseifEnd = c.code->Array.length
c.code[elseifJump] = OpJumpIfFalse(elseifEnd - elseifJump - 1)
})
// Compile else branch
switch else_ {
| Some(body) => body->Array.forEach(stmt => compileStmt(c, stmt))
| None => ()
}
// Patch end jump
let end = c.code->Array.length
c.code[endJump] = OpJump(end - endJump - 1)
}
| WhileStmt({cond, body, loc}) => {
let loopStart = c.code->Array.length
// Compile condition
compileExpr(c, cond)
// Jump if false to end
let exitJump = c.code->Array.length
emit(c, OpJumpIfFalse(0), loc)
// Compile body
body->Array.forEach(stmt => compileStmt(c, stmt))
// Jump back to loop start
let loopEnd = c.code->Array.length
emit(c, OpJump(loopStart - loopEnd - 1), loc)
// Patch exit jump
let end = c.code->Array.length
c.code[exitJump] = OpJumpIfFalse(end - exitJump - 1)
}
| ForStmt({var, iter, body, loc}) => {
// For loops over arrays (simplified)
compileExpr(c, iter)
// Add loop variable
let varIndex = addLocal(c, var)
// For now, just a placeholder
emit(c, OpSetLocal(varIndex), loc)
body->Array.forEach(stmt => compileStmt(c, stmt))
}
| ReturnStmt({value, loc}) => {
switch value {
| Some(expr) => {
compileExpr(c, expr)
emit(c, OpReturn, loc)
}
| None => {
emit(c, OpPush(VNil), loc)
emit(c, OpReturn, loc)
}
}
}
| BreakStmt(loc) => {
// Break not fully implemented
emit(c, OpJump(0), loc)
}
| ContinueStmt(loc) => {
// Continue not fully implemented
emit(c, OpJump(0), loc)
}
| PrintStmt({println, args, loc}) => {
// Compile each argument and print
args->Array.forEach(arg => {
compileExpr(c, arg)
emit(c, OpPrint(println), loc)
})
}
| GutterBlock({tokens: _, recovered: _, loc}) => {
// Gutter blocks are error recovery - emit checkpoint
emit(c, OpCheckpoint("gutter"), loc)
}
| ExprStmt(expr) => {
compileExpr(c, expr)
emit(c, OpPop, expr->getLocation)
}
}
}
and getLocation = (expr: expr): location => {
switch expr {
| IntLit(_, loc)
| FloatLit(_, loc)
| StringLit(_, loc)
| BoolLit(_, loc)
| NilLit(loc)
| Ident(_, loc)
| Array(_, loc)
| Binary(_, _, _, loc)
| Unary(_, _, loc)
| Call(_, _, loc)
| Index(_, _, loc)
| Member(_, _, loc)
| Ternary(_, _, _, loc)
| Lambda(_, _, _, loc) => loc
}
}
// Compile declaration
let compileDecl = (c: compiler, decl: decl): unit => {
switch decl {
| FunctionDecl({name: _, params: _, returnType: _, body, loc}) => {
// Functions not fully implemented
emit(c, OpCheckpoint("function"), loc)
body->Array.forEach(stmt => compileStmt(c, stmt))
}
| StructDecl({name: _, fields: _, loc}) => {
// Structs not implemented
emit(c, OpCheckpoint("struct"), loc)
}
| MainBlock({body, loc}) => {
emit(c, OpCheckpoint("main_start"), loc)
body->Array.forEach(stmt => compileStmt(c, stmt))
emit(c, OpCheckpoint("main_end"), loc)
}
| StmtDecl(stmt) => compileStmt(c, stmt)
}
}
// Compile program
let compile = (program: program): result<bytecodeProgram, string> => {
try {
let c = make()
// Compile all declarations
program.declarations->Array.forEach(decl => compileDecl(c, decl))
// Emit halt
emit(c, OpHalt, program.loc)
// Build chunk
let chunk: chunk = {
code: c.code,
constants: c.constants,
locations: c.locations,
}
// Build program
let bytecodeProgram: bytecodeProgram = {
main: chunk,
functions: [], // Functions not implemented yet
}
Ok(bytecodeProgram)
} catch {
| exn => Error(`Compilation error: ${exn->Exn.message->Option.getOr("unknown")}`)
}
}