git @ Cat's Eye Technologies Decoy / 019bf9f
Add some oomph to the compiler. Chris Pressey 1 year, 8 months ago
2 changed file(s) with 41 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
22 --
33
44 table = require "table"
5 string = require "string"
56 require "decoy_util"
67 require "decoy_model"
78
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
829
930 compile_expr = function(ast, env)
1031 if ast.class == Cons then
1839 compile_cond(ast.tail())
1940 elseif head.text() == "lambda" then
2041 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())
2146 else
2247 compile_application(ast)
2348 end
2449 else
25 compile_application(ast)
50 error("Can't compile application not in A-normal form")
2651 end
2752 elseif ast.class == Symbol then
2853 compile_lookup(ast)
3257 error("Unsupported form for compilation: " .. depict(ast))
3358 end
3459 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
241241
242242 fields.compile_toplevel = function(expr, env)
243243 debug("module", "compiling toplevel: " .. depict(expr))
244 --compile_expr(expr, env)
244 compile_expr(expr, env)
245245 return env
246246 end
247247