-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwaeg.treetop
122 lines (105 loc) · 1.78 KB
/
waeg.treetop
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
grammar Wae
rule waestart
statement:wae ';' {
def eval
statement.eval
end
}
end
rule wae
terminal / expression
end
rule terminal
exit / id / num
end
rule expression
with / sum / negop
{
def eval
expression.eval
end
}
end
rule with
# {with {<id> <WAE>} <WAE>}
'{' space operator_1:set space '{' space statement_1:assign space '}' space operand_3:wae space '}' {
def eval(env={})
operand_3.eval(env={statement_1.name => statement_1.val})
end
}
end
rule set
'with'
end
rule assign
operand_1:id space operand_2:wae {
def eval(env)
env[operand_1.name] = operand_2.eval(env)
end
def val
operand_2.eval(env={})
end
def name
operand_1.name
end
}
end
rule sum
# {+ <WAE> <WAE>} | {- <WAE> <WAE>}
'{' space operator_1:sum_op space operand_1:wae space operand_2:wae space '}' {
def eval(env={})
operator_1.apply(operand_1.eval(env), operand_2.eval(env))
end
}
end
rule sum_op
'+' {
def apply(a, b)
a + b
end
}
/ '-' {
def apply(a,b)
a - b
end
}
end
rule space
' '*
end
rule negop
'-' operand_1:wae {
def eval(env={})
-operand_1.eval(env)
end
}
end
rule id
[a-z] {
def eval(env)
env[name]
end
def name
text_value
end
def bind(value, env)
env.merge(name => value)
end
}
end
rule num
([1-9] [0-9]* / '0') {
def eval(env = {})
text_value.to_i
end
}
end
rule exit
'exit' {
def eval(env = {})
puts "Goodbye"
exit
end
}
end
end