Add some oomph to the compiler.
Chris Pressey
1 year, 8 months ago
2 | 2 | -- |
3 | 3 | |
4 | 4 | table = require "table" |
5 | string = require "string" | |
5 | 6 | require "decoy_util" |
6 | 7 | require "decoy_model" |
7 | 8 | |
9 | ||
10 | mangle = function(s) | |
11 | s = string.gsub(s, "-", "_") | |
12 | return s | |
13 | end | |
14 | ||
15 | compile_exprs = function(sexp, inter) | |
16 | local exprs = {} | |
17 | while sexp.class ~= Nil do | |
18 | if sexp.class == Cons then | |
19 | compile_expr(sexp.head(), env) | |
20 | if inter then | |
21 | inter() | |
22 | end | |
23 | else | |
24 | error("assertion failed: not a Cons") | |
25 | end | |
26 | sexp = sexp.tail() | |
27 | end | |
28 | end | |
8 | 29 | |
9 | 30 | compile_expr = function(ast, env) |
10 | 31 | if ast.class == Cons then |
18 | 39 | compile_cond(ast.tail()) |
19 | 40 | elseif head.text() == "lambda" then |
20 | 41 | compile_lambda(ast.tail()) |
42 | elseif head.text() == "define" then | |
43 | compile_define(ast.tail()) | |
44 | elseif head.text() == "import-from" then | |
45 | compile_import_from(ast.tail()) | |
21 | 46 | else |
22 | 47 | compile_application(ast) |
23 | 48 | end |
24 | 49 | else |
25 | compile_application(ast) | |
50 | error("Can't compile application not in A-normal form") | |
26 | 51 | end |
27 | 52 | elseif ast.class == Symbol then |
28 | 53 | compile_lookup(ast) |
32 | 57 | error("Unsupported form for compilation: " .. depict(ast)) |
33 | 58 | end |
34 | 59 | end |
60 | ||
61 | compile_import_from = function(ast) | |
62 | print("/* import " .. mangle(depict(ast.head())) .. " " .. depict(ast.tail()) .. " */") | |
63 | end | |
64 | ||
65 | compile_application = function(ast) | |
66 | print(mangle(depict(ast.head())) .. "(") | |
67 | compile_exprs(ast.tail(), function() print "," end) | |
68 | print(")") | |
69 | end | |
70 | ||
71 | compile_literal = function(ast) | |
72 | print(depict(ast)) | |
73 | end |