10 | 10 |
|
11 | 11 |
local Parser = {}
|
12 | 12 |
|
13 | |
function Parser:parse_exprs()
|
|
13 |
function Parser:parse_sexps()
|
14 | 14 |
local es = {}
|
15 | 15 |
local e
|
16 | |
while not self.scanner:is_eof() and self.scanner:get_token_text() ~= ")" do
|
17 | |
e = self:parse_expr()
|
|
16 |
while (not self.scanner:is_eof()) and self.scanner:get_token_text() ~= ")" do
|
|
17 |
e = self:parse_sexp()
|
18 | 18 |
table.insert(es, e)
|
19 | 19 |
end
|
20 | 20 |
return es
|
21 | 21 |
end
|
22 | 22 |
|
23 | |
function Parser:parse_expr()
|
|
23 |
function Parser:parse_sexp()
|
24 | 24 |
local e
|
25 | 25 |
local scanner = self.scanner
|
26 | 26 |
if scanner:consume("(") then
|
27 | |
if self.parse_special_forms then
|
28 | |
if scanner:consume("define") then
|
29 | |
assert(scanner:get_token_type() == "symbol")
|
30 | |
local name = scanner:get_token_text()
|
31 | |
scanner:consume_type("symbol")
|
32 | |
local defn = self:parse_expr()
|
33 | |
scanner:expect(")")
|
34 | |
e = ast.new_Define(name, defn)
|
35 | |
--elseif scanner:consume("import-from") then
|
36 | |
-- error("You said *import-from")
|
37 | |
-- scanner:expect(")")
|
38 | |
else
|
39 | |
local es = self:parse_exprs()
|
40 | |
scanner:expect(")")
|
41 | |
e = Cons.table_to_list(es)
|
42 | |
end
|
43 | |
else
|
44 | |
local es = self:parse_exprs()
|
45 | |
scanner:expect(")")
|
46 | |
e = Cons.table_to_list(es)
|
47 | |
end
|
|
27 |
local es = self:parse_sexps()
|
|
28 |
scanner:expect(")")
|
|
29 |
e = Cons.table_to_list(es)
|
48 | 30 |
elseif scanner:get_token_type() == "symbol" then
|
49 | 31 |
e = Symbol.new(scanner:get_token_text())
|
50 | 32 |
scanner:scan()
|
|
55 | 37 |
local value = tonumber(scanner:get_token_text())
|
56 | 38 |
e = Number.new(value)
|
57 | 39 |
scanner:scan()
|
|
40 |
else
|
|
41 |
error("what is this I don't even: " .. scanner:get_token_text())
|
|
42 |
end
|
|
43 |
return e
|
|
44 |
end
|
|
45 |
|
|
46 |
function Parser:parse_toplevels()
|
|
47 |
local es = {}
|
|
48 |
local e
|
|
49 |
while not self.scanner:is_eof() do
|
|
50 |
e = self:parse_toplevel()
|
|
51 |
table.insert(es, e)
|
|
52 |
end
|
|
53 |
return es
|
|
54 |
end
|
|
55 |
|
|
56 |
function Parser:parse_toplevel()
|
|
57 |
local e
|
|
58 |
local scanner = self.scanner
|
|
59 |
if scanner:consume("(") then
|
|
60 |
if scanner:consume("define") then
|
|
61 |
assert(scanner:get_token_type() == "symbol")
|
|
62 |
local name = scanner:get_token_text()
|
|
63 |
scanner:consume_type("symbol")
|
|
64 |
local defn = self:parse_sexp()
|
|
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(")")
|
|
70 |
else
|
|
71 |
local es = self:parse_sexps()
|
|
72 |
scanner:expect(")")
|
|
73 |
e = Cons.table_to_list(es)
|
|
74 |
end
|
58 | 75 |
else
|
59 | 76 |
error("what is this I don't even: " .. scanner:get_token_text())
|
60 | 77 |
end
|