git @ Cat's Eye Technologies Decoy / b0d3659
Emitter subsystem. Output is nearer to syntactically correct now. Chris Pressey 1 year, 8 months ago
3 changed file(s) with 52 addition(s) and 21 deletion(s). Raw diff Collapse all Expand all
137137 ### Compiler
138138
139139 * import statements
140 * emitter subsystem
140 * let_star statements
141141 * more thorough mangling
142142
143143 ### Modules
33
44 table = require "table"
55 string = require "string"
6 io = require "io"
7
68 require "decoy_util"
79 require "decoy_model"
810
1315 end
1416
1517 Compiler = {}
16 Compiler.new = function()
18 Compiler.new = function(outfile)
1719 local fields = {}
1820 fields.class = Compiler
1921
22 local emit = function(s)
23 outfile:write(s)
24 end
25
26 local emit_symbol = function(sym)
27 if sym.class ~= Symbol then error("Not a symbol: " .. depict(sym)) end
28 emit(mangle(depict(sym)))
29 end
30
31 local emit_symbols = function(sexp)
32 while sexp ~= Nil do
33 if sexp.class == Cons then
34 emit_symbol(sexp.head())
35 if sexp.tail() ~= Nil then
36 emit(",")
37 end
38 else
39 error("assertion failed: not a Cons")
40 end
41 sexp = sexp.tail()
42 end
43 end
44
2045 fields.compile_exprs = function(sexp, inter)
21 local exprs = {}
22 while sexp.class ~= Nil do
46 while sexp ~= Nil do
2347 if sexp.class == Cons then
2448 fields.compile_expr(sexp.head())
25 if inter then
49 if sexp.tail() ~= Nil then
2650 inter()
2751 end
2852 else
6488 end
6589
6690 fields.compile_import_from = function(ast)
67 print("/* import " .. mangle(depict(ast.head())) .. " " .. depict(ast.tail()) .. " */")
91 emit("/* import ")
92 emit(depict(ast.head()))
93 emit(" " .. depict(ast.tail()))
94 emit(" */\n")
6895 end
6996
7097 fields.compile_application = function(ast)
71 print(mangle(depict(ast.head())) .. "(")
72 fields.compile_exprs(ast.tail(), function() print "," end)
73 print(")")
98 emit_symbol(ast.head())
99 emit("(")
100 fields.compile_exprs(ast.tail(), function() emit(",") end)
101 emit(")")
74102 end
75103
76104 fields.compile_literal = function(ast)
77 print(depict(ast))
105 emit(depict(ast))
78106 end
79107
80108 fields.compile_define = function(ast, env)
81 print(mangle(depict(ast.head())) .. " = ")
109 emit_symbol(ast.head())
110 emit(" = ")
82111 fields.compile_expr(ast.tail().head(), env)
83 print(";")
112 emit(";\n")
84113 end
85114
86115 fields.compile_lambda = function(ast, env)
87116 local args = ast.head()
88 print("function")
89 print(depict(args)) -- FIXME mangle each of these
90 print("{")
117 emit("function")
118 emit("(")
119 emit_symbols(args)
120 emit(") {")
91121 local body = ast.tail().head()
92 print("return ")
122 emit("return ")
93123 fields.compile_expr(body, env)
94 print(";")
95 print("}")
124 emit(";}")
96125 end
97126
98127 fields.compile_lookup = function(ast)
99 print(mangle(depict(ast)))
128 emit(mangle(depict(ast)))
100129 end
101130
102131 fields.compile_let_star = function(ast)
103 print(depict(ast))
132 emit(depict(ast))
104133 end
105134
106135 return fields
22 --
33
44 table = require "table"
5 io = require "io"
6
57 require "decoy_util"
68 require "decoy_model"
79 require "decoy_parser"
232234 fields.compile_module = function(program_text)
233235 local parser = Parser.new(program_text)
234236 local exprs = parser.parse_exprs()
235 local compiler = Compiler.new() -- ENV? -- OUT?
237 local compiler = Compiler.new(io.stdout) -- ENV?
236238 for j, expr in ipairs(exprs) do
237239 debug("module", "compiling: " .. depict(expr))
238240 compiler.compile_expr(expr)