@@ -9,61 +9,7 @@ import (
9
9
" github.com/ncw/gpython/ast"
10
10
)
11
11
12
- // FIXME what about when the lists (for example) are nested. Eg expr_or_star_exprs can nest.
13
- // - fix this by making exprs point into a thing under construction with a []Expr in it
14
- // - this will naturally recurse
15
- // OR just do a simple stack of them which we push, pop and append to the top item
16
- // will need the stack for stmts and exprs
17
-
18
- // FIXME can put code blocks in not just at the end - help with list initialisation
19
-
20
- // FIXME is the expStack needed at all? Aren't the yylval put into a
21
- // stack anyway by yacc? And in rsc cc he sets yylval to empty every
22
- // lex.
23
-
24
- // A stack of []ast.Expr
25
- type exprsStack [][]ast.Expr
26
-
27
- // Push a new []ast.Expr on the stack
28
- func (es *exprsStack) Push() {
29
- *es = append (*es, []ast.Expr {})
30
- }
31
-
32
- // Pop the last []ast.Expr from the stack
33
- func (es *exprsStack) Pop() []ast.Expr {
34
- i := len (*es)-1
35
- e := (*es)[i]
36
- *es = (*es)[:i]
37
- return e
38
- }
39
-
40
- // Add an ast.Expr to the the last []ast.Expr on the stack
41
- func (es *exprsStack) Add(expr ...ast.Expr) {
42
- i := len (*es)-1
43
- (*es)[i] = append ((*es)[i], expr...)
44
- }
45
-
46
- // A stack of []ast.Stmt
47
- type stmtsStack [][]ast.Stmt
48
-
49
- // Push a new []ast.Stmt on the stack
50
- func (es *stmtsStack) Push() {
51
- *es = append (*es, []ast.Stmt {})
52
- }
53
-
54
- // Pop the last []ast.Stmt from the stack
55
- func (es *stmtsStack) Pop() []ast.Stmt {
56
- i := len (*es)-1
57
- e := (*es)[i]
58
- *es = (*es)[:i]
59
- return e
60
- }
61
-
62
- // Add an ast.Stmt to the the last []ast.Stmt on the stack
63
- func (es *stmtsStack) Add(stmt ...ast.Stmt) {
64
- i := len (*es)-1
65
- (*es)[i] = append ((*es)[i], stmt...)
66
- }
12
+ // NB can put code blocks in not just at the end
67
13
68
14
// Returns a Tuple if > 1 items or a trailing comma, otherwise returns
69
15
// the first item in elts
@@ -78,35 +24,27 @@ func tupleOrExpr(pos ast.Pos, elts []ast.Expr, optional_comma bool) ast.Expr {
78
24
%}
79
25
80
26
%union {
27
+ pos ast.Pos // kept up to date by the lexer
81
28
str string
82
29
obj py.Object
83
- ast ast.Ast
84
30
mod ast.Mod
85
31
stmt ast.Stmt
86
- stmtsStack stmtsStack
87
32
stmts []ast.Stmt
88
- pos ast.Pos // kept up to date by the lexer
89
- op ast.OperatorNumber
90
- cmpop ast.CmpOp
91
33
expr ast.Expr
92
34
exprs []ast.Expr
93
- exprsStack exprsStack
94
- trailers [] ast.Expr // list of trailer expressions
35
+ op ast. OperatorNumber
36
+ cmpop ast.CmpOp
95
37
comma bool
96
- comprehension ast.Comprehension
97
38
comprehensions []ast.Comprehension
98
39
}
99
40
100
41
%type <obj> strings
101
42
%type <mod> inputs file_input single_input eval_input
102
- %type <stmts> simple_stmt stmt
103
- %type <stmtsStack> nl_or_stmt small_stmts stmts
43
+ %type <stmts> simple_stmt stmt nl_or_stmt small_stmts stmts
104
44
%type <stmt> compound_stmt small_stmt expr_stmt del_stmt pass_stmt flow_stmt import_stmt global_stmt nonlocal_stmt assert_stmt break_stmt continue_stmt return_stmt raise_stmt yield_stmt
105
45
%type <op> augassign
106
46
%type <expr> expr_or_star_expr expr star_expr xor_expr and_expr shift_expr arith_expr term factor power trailer atom test_or_star_expr test not_test lambdef test_nocond lambdef_nocond or_test and_test comparison testlist testlist_star_expr yield_expr_or_testlist yield_expr yield_expr_or_testlist_star_expr dictorsetmaker
107
- %type <exprs> exprlist testlistraw comp_if comp_iter
108
- %type <exprsStack> expr_or_star_exprs test_or_star_exprs tests test_colon_tests
109
- %type <trailers> trailers
47
+ %type <exprs> exprlist testlistraw comp_if comp_iter expr_or_star_exprs test_or_star_exprs tests test_colon_tests trailers
110
48
%type <cmpop> comp_op
111
49
%type <comma> optional_comma
112
50
%type <comprehensions> comp_for
@@ -236,20 +174,20 @@ single_input:
236
174
file_input :
237
175
nl_or_stmt ENDMARKER
238
176
{
239
- $$ = &ast.Module{ModBase: ast.ModBase{$<pos>$}, Body: $1 .Pop() }
177
+ $$ = &ast.Module{ModBase: ast.ModBase{$<pos>$}, Body: $1 }
240
178
}
241
179
242
180
// (NEWLINE | stmt )*
243
181
nl_or_stmt :
244
182
{
245
- $$ .Push()
183
+ $$ = nil
246
184
}
247
185
| nl_or_stmt NEWLINE
248
186
{
249
187
}
250
188
| nl_or_stmt stmt
251
189
{
252
- $$ .Add( $2 ...)
190
+ $$ = append( $$ , $2 ...)
253
191
}
254
192
255
193
//eval_input : testlist NEWLINE * ENDMARKER
@@ -358,18 +296,18 @@ optional_semicolon: | ';'
358
296
small_stmts :
359
297
small_stmt
360
298
{
361
- $$ .Push()
362
- $$ .Add( $1 )
299
+ $$ = nil
300
+ $$ = append( $$ , $1 )
363
301
}
364
302
| small_stmts ' ;' small_stmt
365
303
{
366
- $$ .Add( $3 )
304
+ $$ = append( $$ , $3 )
367
305
}
368
306
369
307
simple_stmt :
370
308
small_stmts optional_semicolon NEWLINE
371
309
{
372
- $$ = $1 .Pop()
310
+ $$ = $1
373
311
}
374
312
375
313
small_stmt :
@@ -472,12 +410,12 @@ equals_yield_expr_or_testlist_star_expr:
472
410
test_or_star_exprs :
473
411
test_or_star_expr
474
412
{
475
- $$ .Push()
476
- $$ .Add( $1 )
413
+ $$ = nil
414
+ $$ = append( $$ , $1 )
477
415
}
478
416
| test_or_star_exprs ' ,' test_or_star_expr
479
417
{
480
- $$ .Add( $3 )
418
+ $$ = append( $$ , $3 )
481
419
}
482
420
483
421
test_or_star_expr :
@@ -502,7 +440,7 @@ optional_comma:
502
440
testlist_star_expr :
503
441
test_or_star_exprs optional_comma
504
442
{
505
- $$ = tupleOrExpr($<pos>$, $1 .Pop() , $2 )
443
+ $$ = tupleOrExpr($<pos>$, $1 , $2 )
506
444
}
507
445
508
446
augassign :
@@ -696,12 +634,12 @@ nonlocal_stmt:
696
634
tests :
697
635
test
698
636
{
699
- $$ .Push()
700
- $$ .Add( $1 )
637
+ $$ = nil
638
+ $$ = append( $$ , $1 )
701
639
}
702
640
| tests ' ,' test
703
641
{
704
- $$ .Add( $3 )
642
+ $$ = append( $$ , $3 )
705
643
}
706
644
707
645
assert_stmt :
@@ -779,19 +717,19 @@ except_clause:
779
717
stmts :
780
718
stmt
781
719
{
782
- $$ .Push()
783
- $$ .Add( $1 ...)
720
+ $$ = nil
721
+ $$ = append( $$ , $1 ...)
784
722
}
785
723
| stmts stmt
786
724
{
787
- $$ .Add( $2 ...)
725
+ $$ = append( $$ , $2 ...)
788
726
}
789
727
790
728
suite :
791
729
simple_stmt
792
730
| NEWLINE INDENT stmts DEDENT
793
731
{
794
- // stmts.Pop()
732
+ // stmts
795
733
}
796
734
797
735
test :
@@ -1107,7 +1045,7 @@ atom:
1107
1045
}
1108
1046
| ' (' test_or_star_exprs optional_comma ' )'
1109
1047
{
1110
- $$ = tupleOrExpr($<pos>$, $2 .Pop() , $3 )
1048
+ $$ = tupleOrExpr($<pos>$, $2 , $3 )
1111
1049
}
1112
1050
| ' [' ' ]'
1113
1051
{
@@ -1119,7 +1057,7 @@ atom:
1119
1057
}
1120
1058
| ' [' test_or_star_exprs optional_comma ' ]'
1121
1059
{
1122
- $$ = &ast.List{ExprBase: ast.ExprBase{$<pos>$}, Elts: $2 .Pop() , Ctx: ast.Load}
1060
+ $$ = &ast.List{ExprBase: ast.ExprBase{$<pos>$}, Elts: $2 , Ctx: ast.Load}
1123
1061
}
1124
1062
| ' {' ' }'
1125
1063
{
@@ -1228,25 +1166,25 @@ expr_or_star_expr:
1228
1166
expr_or_star_exprs:
1229
1167
expr_or_star_expr
1230
1168
{
1231
- $$ .Push()
1232
- $$ .Add( $1 )
1169
+ $$ = nil
1170
+ $$ = append( $$ , $1 )
1233
1171
}
1234
1172
| expr_or_star_exprs ' ,' expr_or_star_expr
1235
1173
{
1236
- $$ .Add( $3 )
1174
+ $$ = append( $$ , $3 )
1237
1175
}
1238
1176
1239
1177
exprlist:
1240
1178
expr_or_star_exprs optional_comma
1241
1179
{
1242
- $$ = $1 .Pop()
1180
+ $$ = $1
1243
1181
$<comma>$ = $2
1244
1182
}
1245
1183
1246
1184
testlist:
1247
1185
tests optional_comma
1248
1186
{
1249
- elts := $1 .Pop()
1187
+ elts := $1
1250
1188
if $2 || len(elts) > 1 {
1251
1189
$$ = &ast.Tuple{ExprBase: ast.ExprBase{$<pos>$}, Elts: elts, Ctx: ast.Load}
1252
1190
} else {
@@ -1257,25 +1195,25 @@ testlist:
1257
1195
testlistraw:
1258
1196
tests optional_comma
1259
1197
{
1260
- $$ = $1 .Pop()
1198
+ $$ = $1
1261
1199
}
1262
1200
1263
1201
// (',' test ':' test)*
1264
1202
test_colon_tests:
1265
1203
test ' :' test
1266
1204
{
1267
- $$ .Push()
1268
- $$ .Add( $1 , $3 ) // key, value order
1205
+ $$ = nil
1206
+ $$ = append( $$ , $1 , $3 ) // key, value order
1269
1207
}
1270
1208
| test_colon_tests ' ,' test ' :' test
1271
1209
{
1272
- $$ .Add( $3 , $5 )
1210
+ $$ = append( $$ , $3 , $5 )
1273
1211
}
1274
1212
1275
1213
dictorsetmaker:
1276
1214
test_colon_tests optional_comma
1277
1215
{
1278
- keyValues := $1 .Pop()
1216
+ keyValues := $1
1279
1217
d := &ast.Dict{ExprBase: ast.ExprBase{$<pos>$}, Keys: nil, Values: nil}
1280
1218
for i := 0 ; i < len(keyValues)-1 ; i += 2 {
1281
1219
d.Keys = append(d.Keys, keyValues[i])
@@ -1285,7 +1223,6 @@ dictorsetmaker:
1285
1223
}
1286
1224
| test ' :' test comp_for
1287
1225
{
1288
- // FIXME DictComp
1289
1226
$$ = &ast.DictComp{ExprBase: ast.ExprBase{$<pos>$}, Key: $1 , Value: $3 , Generators: $4 }
1290
1227
}
1291
1228
| testlistraw
@@ -1294,7 +1231,6 @@ dictorsetmaker:
1294
1231
}
1295
1232
| test comp_for
1296
1233
{
1297
- // FIXME SetComp
1298
1234
$$ = &ast.SetComp{ExprBase: ast.ExprBase{$<pos>$}, Elt: $1 , Generators: $2 }
1299
1235
}
1300
1236
0 commit comments