Checkpoint moving towards non-sexpr representation of toplevels.
Chris Pressey
1 year, 4 months ago
3 | 3 | |
4 | 4 | require "decoy_model" |
5 | 5 | |
6 | ||
6 | 7 | local Define = {} |
8 | ||
9 | function Define:depict() | |
10 | return "(define " .. tostring(name) .. ": " .. tostring(defn) .. ")" | |
11 | end | |
12 | ||
13 | Define.is_class_of = function(obj) | |
14 | local mt = getmetatable(obj) | |
15 | return mt and mt.__index == Define | |
16 | end | |
7 | 17 | |
8 | 18 | Define.new = function(name, defn) |
9 | 19 | local self = { |
14 | 24 | return self |
15 | 25 | end |
16 | 26 | |
27 | ||
17 | 28 | local ImportFrom = {} |
29 | ||
30 | function ImportFrom:depict() | |
31 | return "(import-from " .. tostring(name) .. ": " .. tostring(import_list) .. ")" | |
32 | end | |
33 | ||
34 | ImportFrom.is_class_of = function(obj) | |
35 | local mt = getmetatable(obj) | |
36 | return mt and mt.__index == ImportFrom | |
37 | end | |
18 | 38 | |
19 | 39 | ImportFrom.new = function(name, import_list) |
20 | 40 | local self = { |
81 | 81 | end |
82 | 82 | end |
83 | 83 | |
84 | function Compiler:compile_print_expr(sexp, env) | |
85 | self:emit("console.log(") | |
86 | self:compile_expr(sexp, env) | |
87 | self:emit(");\n") | |
88 | end | |
89 | ||
84 | 90 | --[[ toplevels ]]-- |
85 | 91 | |
86 | 92 | function Compiler:compile_import_from(ast, env) |
101 | 101 | return tostring(sexp:value()) |
102 | 102 | elseif Boolean.is_class_of(sexp) then |
103 | 103 | if sexp:value() then return "#t" else return "#f" end |
104 | elseif sexp.depict then | |
105 | return sexp:depict() | |
104 | 106 | else |
105 | 107 | error("Invalid lua representation of s-expression: " .. render(sexp) .. ": " .. type(sexp)) |
106 | 108 | end |
63 | 63 | scanner:consume_type("symbol") |
64 | 64 | local defn = self:parse_sexp() |
65 | 65 | scanner:expect(")") |
66 | e = ast.new_Define(name, defn) | |
67 | --elseif scanner:consume("import-from") then | |
68 | -- error("You said *import-from") | |
69 | -- scanner:expect(")") | |
66 | e = ast.Define.new(name, defn) | |
67 | elseif scanner:consume("import-from") then | |
68 | assert(scanner:get_token_type() == "strlit") | |
69 | local name = scanner:get_token_text() | |
70 | scanner:consume_type("strlit") | |
71 | local import_list = self:parse_sexp() | |
72 | scanner:expect(")") | |
73 | e = ast.ImportFrom.new(name, import_list) | |
70 | 74 | else |
71 | 75 | local es = self:parse_sexps() |
72 | 76 | scanner:expect(")") |
6 | 6 | |
7 | 7 | |
8 | 8 | require "decoy_model" |
9 | local ast = require "decoy_ast" | |
9 | 10 | local envi = require "decoy_env" |
10 | 11 | local Parser = require "decoy_parser" |
11 | 12 | local Desugarer = require "decoy_desugar" |
41 | 42 | -- |
42 | 43 | local process_toplevel = function(registry, expr, env, module, expr_processor) |
43 | 44 | debug("module", "_process_toplevel: " .. depict(expr)) |
44 | if Cons.is_class_of(expr) then | |
45 | if ast.Define.is_class_of(expr) then | |
46 | local dvalue = registry.evaluator:eval_expr(expr.defn, env) | |
47 | env = envi.bind(expr.name, dvalue, env) | |
48 | module[name:text()] = dvalue | |
49 | elseif ast.ImportFrom.is_class_of(expr) then | |
50 | do_debug("module", function() | |
51 | print("import_from", env, registry, expr.name, depict(expr.import_list)) | |
52 | end) | |
53 | env = registry:import_from(env, expr.name, expr.import_list) | |
54 | elseif Cons.is_class_of(expr) then | |
55 | -- DEPRECATED | |
45 | 56 | if Symbol.is_class_of(expr:head()) then |
46 | 57 | if expr:head():text() == "define" then |
47 | 58 | local name = expr:tail():head() |
86 | 97 | local env = self.preload_env |
87 | 98 | |
88 | 99 | local parser = Parser.new(program_text) |
89 | -- local exprs = parser:parse_toplevels() | |
100 | --local exprs = parser:parse_toplevels() | |
90 | 101 | local exprs = parser:parse_sexps() |
91 | 102 | for j, expr in ipairs(exprs) do |
92 | 103 | env = process_toplevel(self, expr, env, module, expr_processor) |
187 | 198 | end |
188 | 199 | |
189 | 200 | function Registry:compile_module(program_text) |
201 | -- accumulate the exprs | |
190 | 202 | local exprs, env = self:process_module(program_text, function(expr, env) end) |
191 | 203 | |
192 | -- now compile it | |
204 | -- now compile them | |
193 | 205 | local f = assert(io.open(self.output_filename, "w")) |
194 | 206 | local compiler = Compiler.new(f) |
195 | 207 | for j, expr in ipairs(exprs) do |
196 | 208 | debug("module", "compiling: " .. depict(expr)) |
197 | 209 | expr = self.desugarer:desugar_expr(expr) |
210 | --compiler:compile_toplevel(expr, env) | |
198 | 211 | compiler:compile_expr(expr, env) |
199 | 212 | end |
200 | 213 | f:close() |