-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiny.y
219 lines (201 loc) · 9.94 KB
/
tiny.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
%{
package main
import (
"strconv"
"github.com/bantl23/tiny/syntree"
)
var root *syntree.Node
var savedName string
%}
%union {
node *syntree.Node
str string
}
%token <str> IF THEN ELSE END REPEAT UNTIL READ WRITE ASSIGN EQ LT PLUS MINUS TIMES OVER LPAREN RPAREN SEMI ID NUM ERROR
%type <node> program stmt_seq stmt if_stmt repeat_stmt assign_stmt read_stmt write_stmt error exp simple_exp term factor
%%
program : stmt_seq {
root = $1
}
stmt_seq : stmt_seq SEMI stmt {
t := $1
if (t != nil) {
for (t.Sibling != nil) {
t = t.Sibling
}
t.Sibling = $3
$$ = $1
} else {
$$ = $3
}
}
| stmt {
$$ = $1
}
;
stmt : if_stmt {
$$ = $1
}
| repeat_stmt {
$$ = $1
}
| assign_stmt {
$$ = $1
}
| read_stmt {
$$ = $1
}
| write_stmt {
$$ = $1
}
| error {
$$ = nil
}
;
if_stmt : IF exp THEN stmt_seq END {
$$ = syntree.NewNode()
$$.NodeKind = syntree.STATEMENT_KIND
$$.StmtKind = syntree.IF_KIND
$$.Children = append($$.Children, $2)
$$.Children = append($$.Children, $4)
}
| IF exp THEN stmt_seq ELSE stmt_seq END
{
$$ = syntree.NewNode()
$$.NodeKind = syntree.STATEMENT_KIND
$$.StmtKind = syntree.IF_KIND
$$.Children = append($$.Children, $2)
$$.Children = append($$.Children, $4)
$$.Children = append($$.Children, $6)
}
;
repeat_stmt : REPEAT stmt_seq UNTIL exp {
$$ = syntree.NewNode()
$$.NodeKind = syntree.STATEMENT_KIND
$$.StmtKind = syntree.REPEAT_KIND
$$.Children = append($$.Children, $2)
$$.Children = append($$.Children, $4)
}
;
assign_stmt : ID {
savedName = currText(yylex)
}
ASSIGN exp
{
$$ = syntree.NewNode()
$$.NodeKind = syntree.STATEMENT_KIND
$$.StmtKind = syntree.ASSIGN_KIND
$$.Children = append($$.Children, $<node>4)
$$.Name = savedName
$$.LineNumber = currLine(yylex)
}
;
read_stmt : READ ID {
$$ = syntree.NewNode()
$$.NodeKind = syntree.STATEMENT_KIND
$$.StmtKind = syntree.READ_KIND
$$.Name = currText(yylex)
}
write_stmt : WRITE exp {
$$ = syntree.NewNode()
$$.NodeKind = syntree.STATEMENT_KIND
$$.StmtKind = syntree.WRITE_KIND
$$.Children = append($$.Children, $2)
}
exp : simple_exp LT simple_exp {
$$ = syntree.NewNode()
$$.NodeKind = syntree.EXPRESSION_KIND
$$.ExpKind = syntree.OP_KIND
$$.TokenType = syntree.LT
$$.Children = append($$.Children, $1)
$$.Children = append($$.Children, $3)
}
| simple_exp EQ simple_exp {
$$ = syntree.NewNode()
$$.NodeKind = syntree.EXPRESSION_KIND
$$.ExpKind = syntree.OP_KIND
$$.TokenType = syntree.EQ
$$.Children = append($$.Children, $1)
$$.Children = append($$.Children, $3)
}
| simple_exp {
$$ = $1
}
simple_exp : simple_exp PLUS term {
$$ = syntree.NewNode()
$$.NodeKind = syntree.EXPRESSION_KIND
$$.ExpKind = syntree.OP_KIND
$$.TokenType = syntree.PLUS
$$.Children = append($$.Children, $1)
$$.Children = append($$.Children, $3)
}
| simple_exp MINUS term {
$$ = syntree.NewNode()
$$.NodeKind = syntree.EXPRESSION_KIND
$$.ExpKind = syntree.OP_KIND
$$.TokenType = syntree.MINUS
$$.Children = append($$.Children, $1)
$$.Children = append($$.Children, $3)
}
| term {
$$ = $1
}
;
term : term TIMES factor {
$$ = syntree.NewNode()
$$.NodeKind = syntree.EXPRESSION_KIND
$$.ExpKind = syntree.OP_KIND
$$.TokenType = syntree.TIMES
$$.Children = append($$.Children, $1)
$$.Children = append($$.Children, $3)
}
| term OVER factor {
$$ = syntree.NewNode()
$$.NodeKind = syntree.EXPRESSION_KIND
$$.ExpKind = syntree.OP_KIND
$$.TokenType = syntree.OVER
$$.Children = append($$.Children, $1)
$$.Children = append($$.Children, $3)
}
| factor {
$$ = $1
}
;
factor : LPAREN exp RPAREN {
$$ = $2
}
| NUM {
$$ = syntree.NewNode()
$$.NodeKind = syntree.EXPRESSION_KIND
$$.ExpKind = syntree.CONST_KIND
$$.Value, _ = strconv.Atoi(currText(yylex))
}
| ID {
$$ = syntree.NewNode()
$$.NodeKind = syntree.EXPRESSION_KIND
$$.ExpKind = syntree.ID_KIND
$$.Name = currText(yylex)
}
| error {
$$ = nil
}
;
%%
func currText(y yyLexer) string {
if len(y.(*Lexer).stack) > 0 {
return y.(*Lexer).stack[0].s
}
return ""
}
func currLine(y yyLexer) int {
if len(y.(*Lexer).stack) > 0 {
return y.(*Lexer).stack[0].line
}
return 0
}
func currCol(y yyLexer) int {
if len(y.(*Lexer).stack) > 0 {
return y.(*Lexer).stack[0].column
}
return 0
}