-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsloe_compiler.zig
More file actions
407 lines (390 loc) · 16.1 KB
/
sloe_compiler.zig
File metadata and controls
407 lines (390 loc) · 16.1 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
const std = @import("std");
const Writer = std.io.Writer;
const Ally = std.mem.Allocator;
const ArrayList = std.ArrayList;
const StringMap = std.StringHashMap;
const Heap = @import("heap.zig");
const Word = Heap.Word;
const Obj = Heap.Obj;
const Instruction = @import("instruction.zig").Instruction;
const Val = @import("pear_value.zig");
const new_symbol = Heap.new_symbol;
const get_symbol = Heap.get_symbol;
const Vm = @import("vm.zig");
const Str = []const u8;
fn box(ally: Ally, value: anytype) !*@TypeOf(value) {
const ptr = try ally.create(@TypeOf(value));
ptr.* = value;
return ptr;
}
// The abstract syntax tree represents the syntax of the program, omitting stuff
// that's irrelevant for the semantics such as comments and whitespace.
const ProtoAst = union(enum) { name: Str, int: i64, string: Str, group: []const ProtoAst };
const Ast = struct {
defs: []const Def,
pub const Def = struct { name: Str, value: Expr };
pub const Expr = union(enum) {
name: Str,
let: Let,
int: i64,
string: Str,
new: New,
if_: If,
function: Function,
call: Call,
};
pub const Let = struct { name: Str, def: *const Expr, expr: *const Expr };
pub const New = struct { children: []const Expr };
pub const If = struct { condition: *const Expr, then: *const Expr, else_: *const Expr };
pub const Function = struct { params: []Str, body: *const Expr };
pub const Call = struct { callee: *const Expr, args: []const Expr };
pub fn format(ast: Ast, writer: *Writer) !void {
for (ast.defs) |def| {
try writer.print("{s} ", .{def.name});
try format_expr(def.value, writer);
try writer.print("\n", .{});
}
}
pub fn format_expr(expr: Expr, writer: *Writer) !void {
switch (expr) {
.name => |name| try writer.print("{s}", .{name}),
.let => |let| {
try writer.print("(: {s}\n", .{let.name});
try format_expr(let.def.*, writer);
try writer.print(" ", .{});
try format_expr(let.expr.*, writer);
try writer.print(")", .{});
},
.int => |int| try writer.print("{}", .{int}),
.string => |string| try writer.print("\"{s}\"", .{string}),
.new => |new| {
try writer.print("(#", .{});
for (new.children) |child| {
try writer.print(" ", .{});
try format_expr(child, writer);
}
try writer.print(")", .{});
},
.if_ => |if_| {
try writer.print("(if ", .{});
try format_expr(if_.condition.*, writer);
try writer.print(" ", .{});
try format_expr(if_.then.*, writer);
try writer.print(" ", .{});
try format_expr(if_.else_.*, writer);
try writer.print(")", .{});
},
.function => |function| {
try writer.print("(\\ (", .{});
for (0.., function.params) |i, param| {
if (i > 0) try writer.print(" ", .{});
try writer.print("{s}", .{param});
}
try writer.print(") ", .{});
try format_expr(function.body.*, writer);
try writer.print(")", .{});
},
.call => |call| {
try writer.print("(", .{});
try format_expr(call.callee.*, writer);
for (call.args) |arg| {
try writer.print(" ", .{});
try format_expr(arg, writer);
}
try writer.print(")", .{});
},
}
}
};
pub fn str_to_ast(ally: Ally, input: Str) !Ast {
var parser = Parser{ .ally = ally, .input = input, .cursor = 0 };
var defs = std.ArrayList(Ast.Def).empty;
while (true) {
parser.consume_whitespace();
if (parser.is_at_end()) break;
const name = parser.parse_name() orelse {
std.debug.print("Error at {}: Expected name.\n", .{parser.cursor});
@panic("bad input");
};
const raw_value = parser.parse_expr() catch |e| {
std.debug.print("Error at {}: {}\n", .{ parser.cursor, e });
@panic("bad input");
} orelse return error.ExpectedExpr;
const value = add_semantics(ally, raw_value) catch |e| {
std.debug.print("Error at {}: {}\n", .{ parser.cursor, e });
@panic("bad input");
};
try defs.append(ally, .{ .name = name, .value = value });
}
return .{ .defs = defs.items };
}
const Parser = struct {
ally: Ally,
input: Str,
cursor: usize,
fn current(parser: Parser) u8 {
return if (parser.is_at_end()) 0 else parser.input[parser.cursor];
}
fn advance(parser: *Parser) void {
parser.cursor += 1;
}
fn is_at_end(parser: Parser) bool {
return parser.cursor >= parser.input.len;
}
fn consume_whitespace(parser: *Parser) void {
while (true) : (parser.advance()) {
if (parser.current() == ' ') continue;
if (parser.current() == '\n') continue;
break;
}
}
fn consume_char(parser: *Parser) u8 {
const char = parser.current();
parser.advance();
return char;
}
fn consume(parser: *Parser, prefix: Str) bool {
parser.consume_whitespace();
const start = parser.cursor;
for (prefix) |char| {
if (parser.current() != char) {
parser.cursor = start;
return false;
}
parser.advance();
}
return true;
}
fn parse_name(parser: *Parser) ?Str {
parser.consume_whitespace();
const start = parser.cursor;
while (true) {
const char = parser.current();
const is_letter = char != ' ' and char != '\n' and char != '(' and char != ')';
if (is_letter) parser.advance() else break;
}
const end = parser.cursor;
if (start == end) return null;
return parser.input[start..end];
}
fn parse_int(parser: *Parser) ?i64 {
parser.consume_whitespace();
const start = parser.cursor;
var num: i64 = 0;
while (true) {
const char = parser.current();
const digit = std.mem.indexOfScalar(u8, "0123456789", char) orelse break;
num = num * 10 + @as(i64, @intCast(digit));
parser.advance();
}
const end = parser.cursor;
if (start == end) return null;
return num;
}
fn parse_string(parser: *Parser) !?Str {
if (!parser.consume("\"")) return null;
var b = ArrayList(u8).empty;
while (true) {
if (parser.is_at_end()) return error.StringDoesNotEnd;
if (parser.current() == '\"') {
parser.advance();
break;
}
if (try parser.parse_escaped()) |escaped|
try b.appendSlice(parser.ally, escaped)
else
try b.append(parser.ally, parser.consume_char());
}
return b.items;
}
fn parse_escaped(parser: *Parser) !?Str {
if (parser.current() != '\\') return null;
parser.advance();
if (parser.is_at_end()) return error.NoEscapeSequence;
const char = parser.consume_char();
if (char == '\\') return "\\";
if (char == '"') return "\"";
if (char == 'n') return "\n";
return error.UnknownEscapeSequence;
}
fn parse_group(parser: *Parser) !?[]const ProtoAst {
if (!parser.consume("(")) return null;
var parts = ArrayList(ProtoAst).empty;
while (try parser.parse_expr()) |expr| try parts.append(parser.ally, expr);
if (!parser.consume(")")) return error.ExpectedClosingBracket;
return parts.items;
}
fn parse_expr(parser: *Parser) error{
OutOfMemory,
StringDoesNotEnd,
NoEscapeSequence,
UnknownEscapeSequence,
ExpectedExpr,
ExpectedClosingBracket,
}!?ProtoAst {
if (parser.parse_int()) |int| return .{ .int = int };
if (try parser.parse_string()) |string| return .{ .string = string };
if (parser.parse_name()) |name| return .{ .name = name };
if (try parser.parse_group()) |exprs| return .{ .group = exprs };
return null;
}
};
fn add_semantics(ally: Ally, ast: ProtoAst) !Ast.Expr {
switch (ast) {
.name => |n| return .{ .name = n },
.int => |i| return .{ .int = i },
.string => |s| return .{ .string = s },
.group => |group| {
if (group.len == 0) return error.ExpectedExpr;
switch (group[0]) {
.name => |name| {
if (std.mem.eql(u8, name, ":")) {
if (group.len != 4) {
std.debug.print("Bad let with {} children:\n{any}\n", .{ group.len, ast });
return error.BadLet;
}
const let_name = switch (group[1]) {
.name => |n| n,
else => return error.BadLet,
};
const def = try add_semantics(ally, group[2]);
const expr = try add_semantics(ally, group[3]);
return Ast.Expr{ .let = .{
.name = let_name,
.def = try box(ally, def),
.expr = try box(ally, expr),
} };
}
if (std.mem.eql(u8, name, "#")) {
var children = try ally.alloc(Ast.Expr, group.len - 1);
for (group[1..], 0..) |item, i| children[i] = try add_semantics(ally, item);
return .{ .new = .{ .children = children } };
}
if (std.mem.eql(u8, name, "if")) {
if (group.len != 4) {
std.debug.print("Bad if: {any}\n", .{ast});
return error.BadIf;
}
const condition = try add_semantics(ally, group[1]);
const then = try add_semantics(ally, group[2]);
const else_ = try add_semantics(ally, group[3]);
return .{ .if_ = .{
.condition = try box(ally, condition),
.then = try box(ally, then),
.else_ = try box(ally, else_),
} };
}
if (std.mem.eql(u8, name, "\\")) {
if (group.len != 3) return error.BadFunction;
var params = ArrayList(Str).empty;
for (switch (group[1]) {
.group => |g| g,
else => return error.BadFunction,
}) |param| {
switch (param) {
.name => |n| try params.append(ally, n),
else => return error.BadFunction,
}
}
return .{ .function = .{
.params = params.items,
.body = try box(ally, try add_semantics(ally, group[2])),
} };
}
},
else => {},
}
const callee = try add_semantics(ally, group[0]);
var args = ArrayList(Ast.Expr).empty;
for (group[1..]) |arg| try args.append(ally, try add_semantics(ally, arg));
return .{ .call = .{ .callee = try box(ally, callee), .args = args.items } };
},
}
}
pub fn function_to_object(ally: Ally, heap: *Heap, params: []const Str, body: Ast.Expr, defs: *StringMap(Obj)) !Obj {
var vars = ArrayList(Var).empty;
for (params, 0..) |param, i| try vars.append(ally, .{ .name = param, .offset = i });
var instructions = ArrayList(Instruction).empty;
try ast_to_instructions(ally, heap, body, &instructions, defs, &vars, params.len);
if (params.len > 0) try instructions.append(ally, .{ .popover = params.len });
return try Instruction.new_instructions(ally, heap, instructions.items);
}
const Var = struct { name: Str, offset: usize };
fn ast_to_instructions(
ally: Ally,
heap: *Heap,
expr: Ast.Expr,
instructions: *ArrayList(Instruction),
defs: *StringMap(Obj),
vars: *ArrayList(Var),
stack_size: usize,
) error{ OutOfMemory, BadProgram }!void {
switch (expr) {
.name => |name| {
for (0..vars.items.len) |i| {
const entry = &vars.items[vars.items.len - 1 - i];
if (std.mem.eql(u8, entry.name, name)) {
try instructions.append(ally, .{ .stack = stack_size - entry.offset - 1 });
return;
}
}
const object = defs.get(name) orelse {
std.debug.print("Name {s} not in scope.", .{name});
return error.BadProgram;
};
try instructions.append(ally, .{ .address = object });
},
.let => |let| {
try ast_to_instructions(ally, heap, let.def.*, instructions, defs, vars, stack_size);
try vars.append(ally, .{ .name = let.name, .offset = stack_size });
try ast_to_instructions(ally, heap, let.expr.*, instructions, defs, vars, stack_size + 1);
try instructions.append(ally, .{ .popover = 1 });
_ = vars.pop() orelse unreachable;
},
.int => |int| {
try instructions.append(ally, .{ .word = @bitCast(int) });
try instructions.append(ally, .{ .new = .{ .has_pointers = false, .num_words = 1 } });
},
.string => |string| {
const obj = try new_symbol(heap, string);
try instructions.append(ally, .{ .address = obj });
},
.new => |new| {
for (new.children, 0..) |child, i| try ast_to_instructions(ally, heap, child, instructions, defs, vars, stack_size + i);
try instructions.append(ally, .{ .new = .{ .has_pointers = true, .num_words = new.children.len } });
},
.if_ => |if_| {
try ast_to_instructions(ally, heap, if_.condition.*, instructions, defs, vars, stack_size);
try instructions.append(ally, .{ .word = 0 });
try instructions.append(ally, .load);
var then_instructions = ArrayList(Instruction).empty;
try ast_to_instructions(ally, heap, if_.then.*, &then_instructions, defs, vars, stack_size);
var else_instructions = ArrayList(Instruction).empty;
try ast_to_instructions(ally, heap, if_.else_.*, &else_instructions, defs, vars, stack_size);
try instructions.append(ally, .{ .if_ = .{
.then = then_instructions.items,
.else_ = else_instructions.items,
} });
},
.function => |function| {
const obj = try function_to_object(ally, heap, function.params, function.body.*, defs);
try instructions.append(ally, .{ .address = obj });
},
.call => |call| {
for (call.args, 0..) |arg, i| try ast_to_instructions(ally, heap, arg, instructions, defs, vars, stack_size + i);
try ast_to_instructions(ally, heap, call.callee.*, instructions, defs, vars, stack_size + call.args.len);
try instructions.append(ally, .eval);
},
}
}
pub fn eval(ally: Ally, vm: *Vm, code: Str) !StringMap(Obj) {
const ast = try str_to_ast(ally, code);
var defs = StringMap(Obj).init(ally);
for (ast.defs) |def| {
// std.debug.print("Running {s}\n", .{def.name});
const instructions = try function_to_object(ally, vm.get_heap(), &.{}, def.value, &defs);
const result = Obj{ .address = try vm.run(instructions, &.{}) };
try defs.put(def.name, result);
}
return defs;
}