-
Notifications
You must be signed in to change notification settings - Fork 1
/
grammer.y
165 lines (134 loc) · 4.37 KB
/
grammer.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
class BlangParser
token BIF
token BDEF
token BCLASS
token BNUMBER
token BSTRING
token BTRUE BFALSE BNIL
token BIDENTIFIER
token BEND
token NEWLINE
token BWHILE
prechigh
left '.'
right '!'
left '*' '/'
left '+' '-'
left '>' '>=' '<' '<='
left '==' '!='
left '&&'
left '||'
right '='
left ','
preclow
rule
Program:
/* nothing */ { result = Nodes.new([]) }
| Expressions { result = val[0] }
;
Expressions:
Expression { result = Nodes.new(val) }
| Expressions Terminator Expression { result = val[0] << val[2] }
| Expressions Terminator { result = val[0] }
| Terminator { result = Nodes.new([]) }
;
Expression:
Literal
| Call
| Operator
| GetLocal
| SetLocal
| Def
| Class
| If
| '(' Expression ')' { result = val[1] }
| While
;
Terminator:
";"
| NewLine
;
NewLine:
NEWLINE
Literal:
BNUMBER { result = NumberNode.new(val[0]) }
| BSTRING { result = StringNode.new(val[0]) }
| BTRUE { result = TrueNode.new }
| BFALSE { result = FalseNode.new }
| BNIL { result = NilNode.new }
;
Call:
BIDENTIFIER Arguments { result = CallNode.new(nil, val[0], val[1]) }
| Expression "." BIDENTIFIER { result = CallNode.new(val[0], val[2], val[3]) }
Arguments
| Expression "." BIDENTIFIER { result = CallNode.new(val[0], val[2], []) }
;
Arguments:
"(" ")" { result = [] }
| "(" ArgList ")" { result = val[1] }
;
ArgList:
Expression { result = val }
| ArgList "," Expression { result = val[0] << val[2] }
;
Operator:
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]]) }
| 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], [])}
;
GetLocal:
BIDENTIFIER { result = GetLocalNode.new(val[0]) }
;
SetLocal:
BIDENTIFIER "=" Expression { result = SetLocalNode.new(val[0], val[2]) }
;
Block:
NewLine Expressions BEND { result = val[1] }
;
Def:
BDEF BIDENTIFIER Block { result = DefNode.new(val[1], [], val[2]) }
| BDEF BIDENTIFIER "(" ParamList ")" Block { result = DefNode.new(val[1], val[3], val[5]) }
;
ParamList:
/* nothing */ { result = [] }
| BIDENTIFIER { result = val }
| ParamList "," BIDENTIFIER { result = val[0] << val[2] }
;
Class:
BCLASS BIDENTIFIER Block { result = ClassNode.new(val[1], val[2]) }
;
# Finally, `if` is similar to `class` but receives a *condition*.
If:
BIF Expression Block { result = IfNode.new(val[1], val[2]) }
;
While:
BWHILE Expression Block { result = WhileNode.new(val[1], val[2]) }
;
end
---- header
require_relative "lexer.rex.rb"
require_relative "nodes"
---- inner
def parse(code, show_tokens=false)
@lex = BlangLexer.new
@lex.scan_setup(code)
@show_tokens = show_tokens
do_parse
end
def next_token
token = @lex.next_token
if @show_tokens
print token
end
token
end