-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.rb
132 lines (104 loc) · 2.29 KB
/
parser.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
require 'set'
require './tokenizer'
class Parser
def definitions
@defs
end
def initialize
@defs = {}
end
def parse_definitions code
@tokenizer = Tokenizer.new code
while @tokenizer.peek
parse_definition
end
@defs.each do |name, ast|
cell = [ast]
resolve_refs(cell, 0, Set.new)
@defs[name] = cell[0]
end
@defs
end
def parse_expression code
@tokenizer = Tokenizer.new code
ast = parse_expr
cell = [ast]
resolve_refs(cell, 0, Set.new)
cell[0]
end
private
def resolve_refs parent_node, index, bound
child_node = parent_node[index]
case child_node[0]
when :fn
bound += [child_node[1]]
resolve_refs(child_node, 2, bound)
when :call
resolve_refs(child_node, 1, bound)
resolve_refs(child_node, 2, bound)
when :deref
name = child_node[1]
if !bound.include?(name) && @defs.keys.include?(name)
parent_node[index] = @defs[name]
end
end
end
def parse_ref
[:deref, consume(:IDENT).to_sym]
end
def parse_definition
name = consume(:IDENT).to_sym
consume(:ASSIGN)
@defs[name] = parse_expr
end
def parse_abstraction
consume(:LAMBDA)
arg_name = consume(:IDENT)
consume(:PERIOD)
[:fn, arg_name.to_sym, parse_expr]
end
def parse_expr
exprs = []
while start_of_expression?
type = peek.first
case type
when :IDENT
exprs << parse_ref
when :LAMBDA
exprs << parse_abstraction
when :LPAREN
consume(:LPAREN)
exprs << parse_expr
consume(:RPAREN)
end
end
exprs.reduce do |acc, n|
[:call, acc, n]
end
end
def start_of_expression?
return false unless peek
type = peek.first
(type == :LPAREN ||
type == :IDENT ||
type == :LAMBDA ) && !start_of_new_definition?
end
def start_of_new_definition?
peek(0) && peek(1) &&
peek(0)[0] == :IDENT &&
peek(1)[0] == :ASSIGN
end
def peek n=0
@tokenizer.peek(n)
end
def next_token
@tokenizer.next_token
end
def consume expected
type, value, line, col = next_token
unless type == expected
raise "parse error at [#{line}, #{col}]; expected #{expected.inspect} token; got #{type.inspect} token"
end
value
end
end