-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.y
179 lines (157 loc) · 5.54 KB
/
grammar.y
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
class Parser
# declare tokens produced by the lexer
token IF ELSE
token WHILE
token DEF
token CLASS
token NEWLINE
token NUMBER
token STRING
token TRUE FALSE NIL
token IDENTIFIER
token CONSTANT
token INDENT DEDENT
# precedence table
# based on http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence
prechigh
left '.'
right '!'
left '*' '/'
left '+' '-'
left '>' '>=' '<' '<='
left '==' '!='
left '&&'
left '||'
right '='
left ','
preclow
rule
# all rules are declared in this format:
#
# Rulename:
# OtherRule TOKEN AnotherRule { code to run when this matches }
# | OtherRule { ... }
# ;
#
# in the code section (inside the {...} on the right):
# - assign to "result" the value returned by the rule
# - use val[index of expresion] to reference the expressions on the left
# all parsing will end in this rule, being the trunk of the AST
Root:
/* nothing */ { result = Nodes.new([]) }
| Expressions { result = val[0] }
;
# any list of expressions, class or method body, separated by line breaks
Expressions:
Expression { result = Nodes.new(val) }
| Expressions Terminator Expression { result = val[0] << val[2] }
# to ignore trailing line breaks
| Expressions Terminator { result = val[0] }
| Terminator { result = Nodes.new([]) }
# all types of expressions in the language
Expression:
Literal
| Call
| Operator
| Constant
| Assign
| Def
| Class
| If
| While
| '(' Expression ')' { result = val[1] }
;
# all tokens that can terminate an expression
Terminator:
NEWLINE
| ";"
;
# all hard-coded values
Literal:
NUMBER { result = NumberNode.new(val[0]) }
| STRING { result = StringNode.new(val[0]) }
| TRUE { result = TrueNode.new }
| FALSE { result = FalseNode.new }
| NIL { result = NilNode.new }
;
# a method call
Call:
# method
IDENTIFIER { result = CallNode.new(nil, val[0], []) }
# method (arguments)
| IDENTIFIER "(" ArgList ")" { result = CallNode.new(nil, val[0], val[2]) }
# receiver.method
| Expression "." IDENTIFIER { result = CallNode.new(val[0], val[2], []) }
# receiver.method (arguments)
| Expression "."
IDENTIFIER "(" ArgList ")" { result = CallNode.new(val[0], val[2], val[4]) }
;
ArgList:
/* nothing */ { result = [] }
| Expression { result = val }
| ArgList "," Expression { result = val[0] << val[2] }
;
Operator:
# binary operators
Expression '||' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
| Expression '&&' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
| Expression '==' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
| '!' Expression { result = CallNode.new(val[1], val[0], []) }
| Expression '!=' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
| Expression '>' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
| Expression '>=' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
| Expression '<' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
| Expression '<=' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
| Expression '+' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
| Expression '-' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
| Expression '*' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
| Expression '/' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
;
Constant:
CONSTANT { result = GetConstantNode.new(val[0]) }
;
# assignment to a variable or constant
Assign:
IDENTIFIER "=" Expression { result = SetLocalNode.new(val[0], val[2]) }
| CONSTANT "=" Expression { result = SetConstantNode.new(val[0], val[2]) }
;
# method definition
Def:
DEF IDENTIFIER Block { result = DefNode.new(val[1], [], val[2]) }
| DEF IDENTIFIER
"(" ParamList ")" Block { result = DefNode.new(val[1], val[3], val[5]) }
;
ParamList:
/* nothing */ { result = [] }
| IDENTIFIER { result = val }
| ParamList "," IDENTIFIER { result = val[0] << val[2] }
;
# class definition
Class:
CLASS CONSTANT Block { result = ClassNode.new(val[1], val[2]) }
;
# if block
If:
IF Expression Block { result = IfNode.new(val[1], val[2]) }
;
# while block
While:
WHILE Expression Block { result = WhileNode.new(val[1], val[2]) }
# block of indented code - hard work is done by the lexer
Block:
INDENT Expressions DEDENT { result = val[1] }
;
end
---- header
require "lexer"
require "nodes"
---- inner
# this code will be put as-is in the Parser class
def parse(code, show_tokens = false)
@tokens = Lexer.new.tokenize(code) # tokenize the code using our lexer
puts @tokens.inspect if show_tokens
do_parse # kickoff the parsing process
end
def next_token
@tokens.shift
end