@@ -9,61 +9,7 @@ import (
99 " github.com/ncw/gpython/ast"
1010)
1111
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
6713
6814// Returns a Tuple if > 1 items or a trailing comma, otherwise returns
6915// the first item in elts
@@ -78,35 +24,27 @@ func tupleOrExpr(pos ast.Pos, elts []ast.Expr, optional_comma bool) ast.Expr {
7824%}
7925
8026%union {
27+ pos ast.Pos // kept up to date by the lexer
8128 str string
8229 obj py.Object
83- ast ast.Ast
8430 mod ast.Mod
8531 stmt ast.Stmt
86- stmtsStack stmtsStack
8732 stmts []ast.Stmt
88- pos ast.Pos // kept up to date by the lexer
89- op ast.OperatorNumber
90- cmpop ast.CmpOp
9133 expr ast.Expr
9234 exprs []ast.Expr
93- exprsStack exprsStack
94- trailers [] ast.Expr // list of trailer expressions
35+ op ast. OperatorNumber
36+ cmpop ast.CmpOp
9537 comma bool
96- comprehension ast.Comprehension
9738 comprehensions []ast.Comprehension
9839}
9940
10041%type <obj> strings
10142%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
10444%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
10545%type <op> augassign
10646%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
11048%type <cmpop> comp_op
11149%type <comma> optional_comma
11250%type <comprehensions> comp_for
@@ -236,20 +174,20 @@ single_input:
236174file_input :
237175 nl_or_stmt ENDMARKER
238176 {
239- $$ = &ast.Module{ModBase: ast.ModBase{$<pos>$}, Body: $1 .Pop() }
177+ $$ = &ast.Module{ModBase: ast.ModBase{$<pos>$}, Body: $1 }
240178 }
241179
242180// (NEWLINE | stmt )*
243181nl_or_stmt :
244182 {
245- $$ .Push()
183+ $$ = nil
246184 }
247185| nl_or_stmt NEWLINE
248186 {
249187 }
250188| nl_or_stmt stmt
251189 {
252- $$ .Add( $2 ...)
190+ $$ = append( $$ , $2 ...)
253191 }
254192
255193//eval_input : testlist NEWLINE * ENDMARKER
@@ -358,18 +296,18 @@ optional_semicolon: | ';'
358296small_stmts :
359297 small_stmt
360298 {
361- $$ .Push()
362- $$ .Add( $1 )
299+ $$ = nil
300+ $$ = append( $$ , $1 )
363301 }
364302| small_stmts ' ;' small_stmt
365303 {
366- $$ .Add( $3 )
304+ $$ = append( $$ , $3 )
367305 }
368306
369307simple_stmt :
370308 small_stmts optional_semicolon NEWLINE
371309 {
372- $$ = $1 .Pop()
310+ $$ = $1
373311 }
374312
375313small_stmt :
@@ -472,12 +410,12 @@ equals_yield_expr_or_testlist_star_expr:
472410test_or_star_exprs :
473411 test_or_star_expr
474412 {
475- $$ .Push()
476- $$ .Add( $1 )
413+ $$ = nil
414+ $$ = append( $$ , $1 )
477415 }
478416| test_or_star_exprs ' ,' test_or_star_expr
479417 {
480- $$ .Add( $3 )
418+ $$ = append( $$ , $3 )
481419 }
482420
483421test_or_star_expr :
@@ -502,7 +440,7 @@ optional_comma:
502440testlist_star_expr :
503441 test_or_star_exprs optional_comma
504442 {
505- $$ = tupleOrExpr($<pos>$, $1 .Pop() , $2 )
443+ $$ = tupleOrExpr($<pos>$, $1 , $2 )
506444 }
507445
508446augassign :
@@ -696,12 +634,12 @@ nonlocal_stmt:
696634tests :
697635 test
698636 {
699- $$ .Push()
700- $$ .Add( $1 )
637+ $$ = nil
638+ $$ = append( $$ , $1 )
701639 }
702640| tests ' ,' test
703641 {
704- $$ .Add( $3 )
642+ $$ = append( $$ , $3 )
705643 }
706644
707645assert_stmt :
@@ -779,19 +717,19 @@ except_clause:
779717stmts :
780718 stmt
781719 {
782- $$ .Push()
783- $$ .Add( $1 ...)
720+ $$ = nil
721+ $$ = append( $$ , $1 ...)
784722 }
785723| stmts stmt
786724 {
787- $$ .Add( $2 ...)
725+ $$ = append( $$ , $2 ...)
788726 }
789727
790728suite :
791729 simple_stmt
792730| NEWLINE INDENT stmts DEDENT
793731 {
794- // stmts.Pop()
732+ // stmts
795733 }
796734
797735test :
@@ -1107,7 +1045,7 @@ atom:
11071045 }
11081046| ' (' test_or_star_exprs optional_comma ' )'
11091047 {
1110- $$ = tupleOrExpr($<pos>$, $2 .Pop() , $3 )
1048+ $$ = tupleOrExpr($<pos>$, $2 , $3 )
11111049 }
11121050| ' [' ' ]'
11131051 {
@@ -1119,7 +1057,7 @@ atom:
11191057 }
11201058| ' [' test_or_star_exprs optional_comma ' ]'
11211059 {
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}
11231061 }
11241062| ' {' ' }'
11251063 {
@@ -1228,25 +1166,25 @@ expr_or_star_expr:
12281166expr_or_star_exprs:
12291167 expr_or_star_expr
12301168 {
1231- $$ .Push()
1232- $$ .Add( $1 )
1169+ $$ = nil
1170+ $$ = append( $$ , $1 )
12331171 }
12341172| expr_or_star_exprs ' ,' expr_or_star_expr
12351173 {
1236- $$ .Add( $3 )
1174+ $$ = append( $$ , $3 )
12371175 }
12381176
12391177exprlist:
12401178 expr_or_star_exprs optional_comma
12411179 {
1242- $$ = $1 .Pop()
1180+ $$ = $1
12431181 $<comma>$ = $2
12441182 }
12451183
12461184testlist:
12471185 tests optional_comma
12481186 {
1249- elts := $1 .Pop()
1187+ elts := $1
12501188 if $2 || len(elts) > 1 {
12511189 $$ = &ast.Tuple{ExprBase: ast.ExprBase{$<pos>$}, Elts: elts, Ctx: ast.Load}
12521190 } else {
@@ -1257,25 +1195,25 @@ testlist:
12571195testlistraw:
12581196 tests optional_comma
12591197 {
1260- $$ = $1 .Pop()
1198+ $$ = $1
12611199 }
12621200
12631201// (',' test ':' test)*
12641202test_colon_tests:
12651203 test ' :' test
12661204 {
1267- $$ .Push()
1268- $$ .Add( $1 , $3 ) // key, value order
1205+ $$ = nil
1206+ $$ = append( $$ , $1 , $3 ) // key, value order
12691207 }
12701208| test_colon_tests ' ,' test ' :' test
12711209 {
1272- $$ .Add( $3 , $5 )
1210+ $$ = append( $$ , $3 , $5 )
12731211 }
12741212
12751213dictorsetmaker:
12761214 test_colon_tests optional_comma
12771215 {
1278- keyValues := $1 .Pop()
1216+ keyValues := $1
12791217 d := &ast.Dict{ExprBase: ast.ExprBase{$<pos>$}, Keys: nil, Values: nil}
12801218 for i := 0 ; i < len(keyValues)-1 ; i += 2 {
12811219 d.Keys = append(d.Keys, keyValues[i])
@@ -1285,7 +1223,6 @@ dictorsetmaker:
12851223 }
12861224| test ' :' test comp_for
12871225 {
1288- // FIXME DictComp
12891226 $$ = &ast.DictComp{ExprBase: ast.ExprBase{$<pos>$}, Key: $1 , Value: $3 , Generators: $4 }
12901227 }
12911228| testlistraw
@@ -1294,7 +1231,6 @@ dictorsetmaker:
12941231 }
12951232| test comp_for
12961233 {
1297- // FIXME SetComp
12981234 $$ = &ast.SetComp{ExprBase: ast.ExprBase{$<pos>$}, Elt: $1 , Generators: $2 }
12991235 }
13001236
0 commit comments