1 # Based on GardenSnake - a parser generator demonstration program
2 # GardenSnake was released into the Public Domain by Andrew Dalke.
4 # Portions of this work are derived from Python's Grammar definition
5 # and may be covered under the Python copyright and license
7 # Andrew Dalke / Dalke Scientific Software, LLC
8 # 30 August 2006 / Cape Town, South Africa
10 # Modifications for inclusion in PLY distribution
11 from pprint
import pprint
12 from ply
import lex
, yacc
14 from copy
import deepcopy
16 from openpower
.decoder
.power_decoder
import create_pdecode
17 from openpower
.decoder
.pseudo
.lexer
import IndentLexer
18 from openpower
.decoder
.orderedset
import OrderedSet
20 # I use the Python AST
21 #from compiler import ast
26 regs
= ['RA', 'RS', 'RB', 'RC', 'RT']
27 fregs
= ['FRA', 'FRS', 'FRB', 'FRC', 'FRT', 'FRS']
29 def Assign(autoassign
, assignname
, left
, right
, iea_mode
):
31 print("Assign", assignname
, left
, right
)
32 if isinstance(left
, ast
.Name
):
33 # Single assignment on left
34 # XXX when doing IntClass, which will have an "eq" function,
35 # this is how to access it
36 # eq = ast.Attribute(left, "eq") # get eq fn
37 # return ast.Call(eq, [right], []) # now call left.eq(right)
38 return ast
.Assign([ast
.Name(left
.id, ast
.Store())], right
)
39 elif isinstance(left
, ast
.Tuple
):
40 # List of things - make sure they are Name nodes
42 for child
in left
.getChildren():
43 if not isinstance(child
, ast
.Name
):
44 raise SyntaxError("that assignment not supported")
45 names
.append(child
.name
)
46 ass_list
= [ast
.AssName(name
, 'OP_ASSIGN') for name
in names
]
47 return ast
.Assign([ast
.AssTuple(ass_list
)], right
)
48 elif isinstance(left
, ast
.Subscript
):
50 # XXX changing meaning of "undefined" to a function
51 #if (isinstance(ls, ast.Slice) and isinstance(right, ast.Name) and
52 # right.id == 'undefined'):
53 # # undefined needs to be copied the exact same slice
54 # right = ast.Subscript(right, ls, ast.Load())
55 # return ast.Assign([left], right)
56 res
= ast
.Assign([left
], right
)
57 if autoassign
and isinstance(ls
, ast
.Slice
):
58 # hack to create a variable pre-declared based on a slice.
59 # dividend[0:32] = (RA)[0:32] will create
61 # dividend[0:32] = (RA)[0:32]
62 # the declaration makes the slice-assignment "work"
63 lower
, upper
, step
= ls
.lower
, ls
.upper
, ls
.step
64 print("lower, upper, step", repr(lower
), repr(upper
), step
)
65 if not isinstance(lower
, ast
.Constant
) or \
66 not isinstance(upper
, ast
.Constant
):
68 qty
= ast
.Num(upper
.value
-lower
.value
)
69 keywords
= [ast
.keyword(arg
='repeat', value
=qty
)]
71 right
= ast
.Call(ast
.Name("concat", ast
.Load()), l
, keywords
)
72 declare
= ast
.Assign([ast
.Name(assignname
, ast
.Store())], right
)
75 # XXX HMMM probably not needed...
77 if isinstance(ls
, ast
.Slice
):
78 lower
, upper
, step
= ls
.lower
, ls
.upper
, ls
.step
79 print("slice assign", lower
, upper
, step
)
81 ls
= (lower
, upper
, None)
83 ls
= (lower
, upper
, step
)
85 return ast
.Call(ast
.Name("selectassign", ast
.Load()),
86 [left
.value
, ls
, right
], [])
89 raise SyntaxError("Can't do that yet")
92 # I implemented INDENT / DEDENT generation as a post-processing filter
94 # The original lex token stream contains WS and NEWLINE characters.
95 # WS will only occur before any other tokens on a line.
97 # I have three filters. One tags tokens by adding two attributes.
98 # "must_indent" is True if the token must be indented from the
99 # previous code. The other is "at_line_start" which is True for WS
100 # and the first non-WS/non-NEWLINE on a line. It flags the check so
101 # see if the new line has changed indication level.
104 # No using Python's approach because Ply supports precedence
106 # comparison: expr (comp_op expr)*
107 # arith_expr: term (('+'|'-') term)*
108 # term: factor (('*'|'/'|'%'|'//') factor)*
109 # factor: ('+'|'-'|'~') factor | power
110 # comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
112 def make_le_compare(arg
):
114 return ast
.Call(ast
.Name("le", ast
.Load()), (left
, right
), [])
117 def make_ge_compare(arg
):
119 return ast
.Call(ast
.Name("ge", ast
.Load()), (left
, right
), [])
122 def make_lt_compare(arg
):
124 return ast
.Call(ast
.Name("lt", ast
.Load()), (left
, right
), [])
127 def make_gt_compare(arg
):
129 return ast
.Call(ast
.Name("gt", ast
.Load()), (left
, right
), [])
132 def make_eq_compare(arg
):
134 return ast
.Call(ast
.Name("eq", ast
.Load()), (left
, right
), [])
137 def make_ne_compare(arg
):
139 return ast
.Call(ast
.Name("ne", ast
.Load()), (left
, right
), [])
151 "<=": make_le_compare
,
152 ">=": make_ge_compare
,
153 "<": make_lt_compare
,
154 ">": make_gt_compare
,
155 "=": make_eq_compare
,
156 "!=": make_ne_compare
,
165 def check_concat(node
): # checks if the comparison is already a concat
166 print("check concat", node
)
167 if not isinstance(node
, ast
.Call
):
169 print("func", node
.func
.id)
170 if node
.func
.id != 'concat':
172 if node
.keywords
: # a repeated list-constant, don't optimise
177 # identify SelectableInt pattern [something] * N
178 # must return concat(something, repeat=N)
179 def identify_sint_mul_pattern(p
):
180 if p
[2] != '*': # multiply
182 if not isinstance(p
[3], ast
.Constant
): # rhs = Num
184 if not isinstance(p
[1], ast
.List
): # lhs is a list
187 if len(l
) != 1: # lhs is a list of length 1
189 return True # yippee!
192 def apply_trailer(atom
, trailer
, read_regs
):
193 if trailer
[0] == "TLIST":
194 # assume depth of one
195 atom
= apply_trailer(atom
, trailer
[1], read_regs
)
197 if trailer
[0] == "CALL":
198 #p[0] = ast.Expr(ast.Call(p[1], p[2][1], []))
199 for arg
in trailer
[1]:
200 if isinstance(arg
, ast
.Name
):
202 if name
in regs
+ fregs
:
204 # special-case, function named "SVSTATE_NEXT" must be made "self.xxxx"
205 if atom
.id == 'SVSTATE_NEXT':
206 name
= ast
.Name("self", ast
.Load())
207 atom
= ast
.Attribute(name
, atom
, ast
.Load())
208 return ast
.Call(atom
, trailer
[1], [])
209 # if p[1].id == 'print':
210 # p[0] = ast.Printnl(ast.Tuple(p[2][1]), None, None)
212 # p[0] = ast.CallFunc(p[1], p[2][1], None, None)
214 print("subscript atom", trailer
[1])
215 #raise AssertionError("not implemented %s" % p[2][0])
219 if isinstance(idx
, ast
.Name
) and idx
.id in regs
+ fregs
:
220 read_regs
.add(idx
.id)
221 if isinstance(idx
, ast
.Name
) and idx
.id in regs
:
222 print ("single atom subscript, underscored", idx
.id)
223 idx
= ast
.Name("_%s" % idx
.id, ast
.Load())
225 idx
= ast
.Slice(subs
[0], subs
[1], None)
226 # if isinstance(atom, ast.Name) and atom.id == 'CR':
227 # atom.id = 'CR' # bad hack
228 #print ("apply_trailer Subscript", atom.id, idx)
229 return ast
.Subscript(atom
, idx
, ast
.Load())
231 ########## Parser (tokens -> AST) ######
236 # https://www.mathcs.emory.edu/~valerie/courses/fall10/155/resources/op_precedence.html
237 # python operator precedence
238 # Highest precedence at top, lowest at bottom.
239 # Operators in the same box evaluate left to right.
241 # Operator Description
242 # () Parentheses (grouping)
243 # f(args...) Function call
244 # x[index:index] Slicing
245 # x[index] Subscription
246 # x.attribute Attribute reference
249 # +x, -x Positive, negative
250 # *, /, % mul, div, remainder
251 # +, - Addition, subtraction
252 # <<, >> Bitwise shifts
256 # in, not in, is, is not, <, <=, >, >=, <>, !=, == comp, membership, ident
260 # lambda Lambda expression
266 ("left", "EQ", "NE", "GT", "LT", "LE", "GE", "LTU", "GTU"),
270 ("left", "PLUS", "MINUS"),
271 ("left", "MULT", "DIV", "MOD"),
275 def __init__(self
, form
, include_carry_in_write
=False):
276 self
.include_ca_in_write
= include_carry_in_write
279 form
= self
.sd
.sigforms
[form
]
281 formkeys
= form
._asdict
().keys()
284 self
.declared_vars
= set()
285 self
.fnparm_vars
= set()
286 for rname
in regs
+ fregs
:
287 self
.gprs
[rname
] = None
288 self
.declared_vars
.add(rname
)
289 self
.available_op_fields
= set()
291 if k
not in self
.gprs
:
292 if k
== 'SPR': # sigh, lower-case to not conflict
294 self
.available_op_fields
.add(k
)
295 self
.op_fields
= OrderedSet()
296 self
.read_regs
= OrderedSet()
297 self
.uninit_regs
= OrderedSet()
298 self
.write_regs
= OrderedSet()
299 self
.special_regs
= OrderedSet() # see p_atom_name
301 # The grammar comments come from Python's Grammar/Grammar file
303 # NB: compound_stmt in single_input is followed by extra NEWLINE!
304 # file_input: (NEWLINE | stmt)* ENDMARKER
306 def p_file_input_end(self
, p
):
307 """file_input_end : file_input ENDMARKER"""
311 def p_file_input(self
, p
):
312 """file_input : file_input NEWLINE
316 if isinstance(p
[len(p
)-1], str):
320 p
[0] = [] # p == 2 --> only a blank line
327 # funcdef: [decorators] 'def' NAME parameters ':' suite
328 # ignoring decorators
330 def p_funcdef(self
, p
):
331 "funcdef : DEF NAME parameters COLON suite"
332 p
[0] = ast
.FunctionDef(p
[2], p
[3], p
[5], ())
333 # reset function parameters after suite is identified
334 self
.fnparm_vars
= set()
336 # parameters: '(' [varargslist] ')'
337 def p_parameters(self
, p
):
338 """parameters : LPAR RPAR
339 | LPAR varargslist RPAR"""
344 p
[0] = ast
.arguments(args
=args
, vararg
=None, kwarg
=None, defaults
=[])
345 # during the time between parameters identified and suite is not
346 # there is a window of opportunity to declare the function parameters
347 # in-scope, for use to not overwrite them with auto-assign
348 self
.fnparm_vars
= set()
350 print ("adding fn parm", arg
)
351 self
.fnparm_vars
.add(arg
)
353 # varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] |
357 def p_varargslist(self
, p
):
358 """varargslist : varargslist COMMA NAME
366 # stmt: simple_stmt | compound_stmt
367 def p_stmt_simple(self
, p
):
368 """stmt : simple_stmt"""
369 # simple_stmt is a list
372 def p_stmt_compound(self
, p
):
373 """stmt : compound_stmt"""
376 # simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
377 def p_simple_stmt(self
, p
):
378 """simple_stmt : small_stmts NEWLINE
379 | small_stmts SEMICOLON NEWLINE"""
382 def p_small_stmts(self
, p
):
383 """small_stmts : small_stmts SEMICOLON small_stmt
387 elif isinstance(p
[1], list):
392 # small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt |
393 # import_stmt | global_stmt | exec_stmt | assert_stmt
394 def p_small_stmt(self
, p
):
395 """small_stmt : flow_stmt
398 if isinstance(p
[1], ast
.Call
):
399 p
[0] = ast
.Expr(p
[1])
400 elif isinstance(p
[1], ast
.Name
) and p
[1].id in ['TRAP', 'SVSTATE_NEXT']:
402 # TRAP and SVSTATE_NEXT needs to actually be a function
403 name
= ast
.Name("self", ast
.Load())
404 name
= ast
.Attribute(name
, fname
, ast
.Load())
405 p
[0] = ast
.Call(name
, [], [])
409 # expr_stmt: testlist (augassign (yield_expr|testlist) |
410 # ('=' (yield_expr|testlist))*)
411 # augassign: ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' |
412 # '<<=' | '>>=' | '**=' | '//=')
413 def p_expr_stmt(self
, p
):
414 """expr_stmt : testlist ASSIGNEA testlist
415 | testlist ASSIGN testlist
417 print("expr_stmt", p
)
419 # a list of expressions
420 #p[0] = ast.Discard(p[1])
423 iea_mode
= p
[2] == '<-iea'
426 if isinstance(p
[1], ast
.Name
):
428 elif isinstance(p
[1], ast
.Subscript
):
429 if isinstance(p
[1].value
, ast
.Name
):
431 if name
in self
.gprs
:
432 # add to list of uninitialised
433 self
.uninit_regs
.add(name
)
434 # work out if this is an ininitialised variable
435 # that should be auto-assigned simply by being "sliced"
436 autoassign
= (name
not in self
.declared_vars
and
437 name
not in self
.fnparm_vars
and
438 name
not in self
.special_regs
)
439 elif isinstance(p
[1], ast
.Call
) and p
[1].func
.id in \
440 ['GPR', 'FPR', 'SPR']:
441 print(astor
.dump_tree(p
[1]))
442 # replace GPR(x) with GPR[x]
443 idx
= p
[1].args
[0].id
444 if idx
in regs
+ fregs
:
445 ridx
= ast
.Name("_%s" % idx
, ast
.Load())
447 ridx
= ast
.Name(idx
, ast
.Load())
448 p
[1] = ast
.Subscript(p
[1].func
, ridx
, ast
.Load())
450 self
.read_regs
.add(idx
) # add to list of regs to read
451 elif isinstance(p
[1], ast
.Call
) and p
[1].func
.id == 'MEM':
453 print(astor
.dump_tree(p
[1]))
454 p
[1].func
.id = "memassign" # change function name to set
455 p
[1].args
.append(p
[3])
458 print(astor
.dump_tree(p
[0]))
462 print(astor
.dump_tree(p
[1]))
463 print("expr assign", name
, p
[1], "to", p
[3])
464 if isinstance(p
[3], ast
.Name
):
466 if toname
in self
.gprs
:
467 self
.read_regs
.add(toname
)
468 if name
and name
in self
.gprs
:
469 self
.write_regs
.add(name
) # add to list of regs to write
470 p
[0] = Assign(autoassign
, name
, p
[1], p
[3], iea_mode
)
472 self
.declared_vars
.add(name
)
474 def p_flow_stmt(self
, p
):
475 "flow_stmt : return_stmt"
478 # return_stmt: 'return' [testlist]
479 def p_return_stmt(self
, p
):
480 "return_stmt : RETURN testlist"
481 p
[0] = ast
.Return(p
[2])
483 def p_compound_stmt(self
, p
):
484 """compound_stmt : if_stmt
492 def p_break_stmt(self
, p
):
493 """break_stmt : BREAK
497 def p_for_stmt(self
, p
):
498 """for_stmt : FOR atom EQ test TO test COLON suite
499 | DO atom EQ test TO test COLON suite
503 if start
.value
> end
.value
: # start greater than end, must go -ve
504 # auto-subtract-one (sigh) due to python range
505 end
= ast
.BinOp(p
[6], ast
.Add(), ast
.Constant(-1))
506 arange
= [start
, end
, ast
.Constant(-1)]
508 # auto-add-one (sigh) due to python range
509 end
= ast
.BinOp(p
[6], ast
.Add(), ast
.Constant(1))
510 arange
= [start
, end
]
511 it
= ast
.Call(ast
.Name("range", ast
.Load()), arange
, [])
512 p
[0] = ast
.For(p
[2], it
, p
[8], [])
514 def p_while_stmt(self
, p
):
515 """while_stmt : DO WHILE test COLON suite ELSE COLON suite
516 | DO WHILE test COLON suite
519 p
[0] = ast
.While(p
[3], p
[5], [])
521 p
[0] = ast
.While(p
[3], p
[5], p
[8])
523 def p_switch_smt(self
, p
):
524 """switch_stmt : SWITCH LPAR atom RPAR COLON NEWLINE INDENT switches DEDENT
528 print(astor
.dump_tree(p
[1]))
531 current_cases
= [] # for deferral
532 for (case
, suite
) in p
[8]:
533 print("for", case
, suite
)
536 current_cases
.append(ast
.Num(c
))
538 if case
== 'default': # last
541 current_cases
.append(ast
.Num(c
))
542 print("cases", current_cases
)
543 compare
= ast
.Compare(switchon
, [ast
.In()],
544 [ast
.List(current_cases
, ast
.Load())])
546 cases
.append((compare
, suite
))
548 print("ended", case
, current_cases
)
549 if case
== 'default':
551 compare
= ast
.Compare(switchon
, [ast
.In()],
552 [ast
.List(current_cases
, ast
.Load())])
553 cases
.append((compare
, suite
))
554 cases
.append((None, suite
))
558 for compare
, suite
in cases
:
559 print("after rev", compare
, suite
)
561 assert len(res
) == 0, "last case should be default"
564 if not isinstance(res
, list):
566 res
= ast
.If(compare
, suite
, res
)
569 def p_switches(self
, p
):
570 """switches : switch_list switch_default
578 def p_switch_list(self
, p
):
579 """switch_list : switch_case switch_list
587 def p_switch_case(self
, p
):
588 """switch_case : CASE LPAR atomlist RPAR COLON suite
591 if isinstance(p
[6][0], ast
.Name
) and p
[6][0].id == 'fallthrough':
595 def p_switch_default(self
, p
):
596 """switch_default : DEFAULT COLON suite
598 p
[0] = ('default', p
[3])
600 def p_atomlist(self
, p
):
601 """atomlist : atom COMMA atomlist
604 assert isinstance(p
[1], ast
.Constant
), "case must be numbers"
606 p
[0] = [p
[1].value
] + p
[3]
610 def p_if_stmt(self
, p
):
611 """if_stmt : IF test COLON suite ELSE COLON if_stmt
612 | IF test COLON suite ELSE COLON suite
613 | IF test COLON suite
615 if len(p
) == 8 and isinstance(p
[7], ast
.If
):
616 p
[0] = ast
.If(p
[2], p
[4], [p
[7]])
618 p
[0] = ast
.If(p
[2], p
[4], [])
620 p
[0] = ast
.If(p
[2], p
[4], p
[7])
622 def p_suite(self
, p
):
623 """suite : simple_stmt
624 | NEWLINE INDENT stmts DEDENT"""
630 def p_stmts(self
, p
):
631 """stmts : stmts stmt
638 def p_comparison(self
, p
):
639 """comparison : comparison PLUS comparison
640 | comparison MINUS comparison
641 | comparison MULT comparison
642 | comparison DIV comparison
643 | comparison MOD comparison
644 | comparison EQ comparison
645 | comparison NE comparison
646 | comparison LE comparison
647 | comparison GE comparison
648 | comparison LTU comparison
649 | comparison GTU comparison
650 | comparison LT comparison
651 | comparison GT comparison
652 | comparison BITOR comparison
653 | comparison BITXOR comparison
654 | comparison BITAND comparison
658 | comparison APPEND comparison
663 p
[0] = ast
.Call(ast
.Name("ltu", ast
.Load()), (p
[1], p
[3]), [])
665 p
[0] = ast
.Call(ast
.Name("gtu", ast
.Load()), (p
[1], p
[3]), [])
667 l
= check_concat(p
[1]) + check_concat(p
[3])
668 p
[0] = ast
.Call(ast
.Name("concat", ast
.Load()), l
, [])
669 elif p
[2] in ['/', '%']:
670 # bad hack: if % or / used anywhere other than div/mod ops,
671 # do % or /. however if the argument names are "dividend"
672 # we must call the special trunc_divs and trunc_rems functions
674 # actual call will be "dividend / divisor" - just check
676 # XXX DISABLE BAD HACK (False)
677 if False and isinstance(l
, ast
.Name
) and l
.id == 'dividend':
682 # return "function trunc_xxx(l, r)"
683 p
[0] = ast
.Call(ast
.Name(fn
, ast
.Load()), (l
, r
), [])
685 # return "l {binop} r"
686 p
[0] = ast
.BinOp(p
[1], binary_ops
[p
[2]], p
[3])
687 elif p
[2] in ['<', '>', '=', '<=', '>=', '!=']:
688 p
[0] = binary_ops
[p
[2]]((p
[1], p
[3]))
689 elif identify_sint_mul_pattern(p
):
690 keywords
= [ast
.keyword(arg
='repeat', value
=p
[3])]
692 p
[0] = ast
.Call(ast
.Name("concat", ast
.Load()), l
, keywords
)
694 p
[0] = ast
.BinOp(p
[1], binary_ops
[p
[2]], p
[3])
696 if isinstance(p
[2], str) and p
[2] == '-':
697 p
[0] = ast
.UnaryOp(unary_ops
[p
[2]], p
[1])
699 p
[0] = ast
.UnaryOp(unary_ops
[p
[1]], p
[2])
703 # power: atom trailer* ['**' factor]
704 # trailers enables function calls (and subscripts).
705 # so this is 'trailerlist'
706 def p_power(self
, p
):
708 | atom trailerlist"""
710 print("power dump atom notrailer")
711 print(astor
.dump_tree(p
[1]))
714 print("power dump atom")
715 print(astor
.dump_tree(p
[1]))
716 print("power dump trailerlist")
717 print(astor
.dump_tree(p
[2]))
718 p
[0] = apply_trailer(p
[1], p
[2], self
.read_regs
)
719 if isinstance(p
[1], ast
.Name
):
721 if name
in regs
+ fregs
:
722 self
.read_regs
.add(name
)
724 def p_atom_name(self
, p
):
727 if name
in self
.available_op_fields
:
728 self
.op_fields
.add(name
)
729 if name
== 'overflow':
730 self
.write_regs
.add(name
)
731 if self
.include_ca_in_write
:
732 if name
in ['CA', 'CA32']:
733 self
.write_regs
.add(name
)
734 if name
in ['CR', 'LR', 'CTR', 'TAR', 'FPSCR', 'MSR',
735 'SVSTATE', 'SVREMAP',
736 'SVSHAPE0', 'SVSHAPE1', 'SVSHAPE2', 'SVSHAPE3']:
737 self
.special_regs
.add(name
)
738 self
.write_regs
.add(name
) # and add to list to write
739 p
[0] = ast
.Name(id=name
, ctx
=ast
.Load())
741 def p_atom_number(self
, p
):
746 p
[0] = ast
.Constant(p
[1])
748 # '[' [listmaker] ']' |
750 def p_atom_listmaker(self
, p
):
751 """atom : LBRACK listmaker RBRACK"""
754 def p_listmaker(self
, p
):
755 """listmaker : test COMMA listmaker
759 p
[0] = ast
.List([p
[1]], ast
.Load())
761 p
[0] = ast
.List([p
[1]] + p
[3].nodes
, ast
.Load())
763 def p_atom_tuple(self
, p
):
764 """atom : LPAR testlist RPAR"""
767 print(astor
.dump_tree(p
[2]))
769 if isinstance(p
[2], ast
.Name
):
771 print("tuple name", name
)
772 if name
in self
.gprs
:
773 self
.read_regs
.add(name
) # add to list of regs to read
774 #p[0] = ast.Subscript(ast.Name("GPR", ast.Load()), ast.Str(p[2].id))
777 elif isinstance(p
[2], ast
.BinOp
):
778 if isinstance(p
[2].left
, ast
.Name
) and \
779 isinstance(p
[2].right
, ast
.Constant
) and \
780 p
[2].right
.value
== 0 and \
781 p
[2].left
.id in self
.gprs
:
783 self
.read_regs
.add(rid
) # add to list of regs to read
784 # create special call to GPR.getz or FPR.getz
786 gprz
= ast
.Name("FPR", ast
.Load())
788 gprz
= ast
.Name("GPR", ast
.Load())
789 # get testzero function
790 gprz
= ast
.Attribute(gprz
, "getz", ast
.Load())
791 # *sigh* see class GPR. we need index itself not reg value
792 ridx
= ast
.Name("_%s" % rid
, ast
.Load())
793 p
[0] = ast
.Call(gprz
, [ridx
], [])
794 print("tree", astor
.dump_tree(p
[0]))
800 def p_trailerlist(self
, p
):
801 """trailerlist : trailer trailerlist
807 p
[0] = ("TLIST", p
[1], p
[2])
809 # trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
810 def p_trailer(self
, p
):
811 """trailer : trailer_arglist
816 def p_trailer_arglist(self
, p
):
817 "trailer_arglist : LPAR arglist RPAR"
818 p
[0] = ("CALL", p
[2])
820 def p_trailer_subscript(self
, p
):
821 "trailer_subscript : LBRACK subscript RBRACK"
822 p
[0] = ("SUBS", p
[2])
824 # subscript: '.' '.' '.' | test | [test] ':' [test]
826 def p_subscript(self
, p
):
827 """subscript : test COLON test
832 if isinstance(p
[3], ast
.Constant
):
833 end
= ast
.Constant(p
[3].value
+1)
835 end
= ast
.BinOp(p
[3], ast
.Add(), ast
.Constant(1))
840 # testlist: test (',' test)* [',']
841 # Contains shift/reduce error
843 def p_testlist(self
, p
):
844 """testlist : testlist_multi COMMA
849 # May need to promote singleton to tuple
850 if isinstance(p
[1], list):
854 # Convert into a tuple?
855 if isinstance(p
[0], list):
856 p
[0] = ast
.Tuple(p
[0])
858 def p_testlist_multi(self
, p
):
859 """testlist_multi : testlist_multi COMMA test
865 if isinstance(p
[1], list):
871 # test: or_test ['if' or_test 'else' test] | lambdef
872 # as I don't support 'and', 'or', and 'not' this works down to 'comparison'
878 # arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
880 # XXX INCOMPLETE: this doesn't allow the trailing comma
882 def p_arglist(self
, p
):
883 """arglist : arglist COMMA argument
890 # argument: test [gen_for] | test '=' test # Really [keyword '='] test
891 def p_argument(self
, p
):
895 def p_error(self
, p
):
896 # print "Error!", repr(p)
900 class GardenSnakeParser(PowerParser
):
901 def __init__(self
, lexer
=None, debug
=False, form
=None, incl_carry
=False):
903 self
.sd
= create_pdecode()
904 PowerParser
.__init
__(self
, form
, incl_carry
)
907 lexer
= IndentLexer(debug
=0)
909 self
.tokens
= lexer
.tokens
910 self
.parser
= yacc
.yacc(module
=self
, start
="file_input_end",
911 debug
=debug
, write_tables
=False)
913 def parse(self
, code
):
914 # self.lexer.input(code)
915 result
= self
.parser
.parse(code
, lexer
=self
.lexer
, debug
=self
.debug
)
916 return ast
.Module(result
)
919 ###### Code generation ######
921 #from compiler import misc, syntax, pycodegen
924 _CACHE_PARSERS
= True
927 class GardenSnakeCompiler(object):
928 def __init__(self
, debug
=False, form
=None, incl_carry
=False):
931 parser
= _CACHED_PARSERS
[debug
, form
, incl_carry
]
933 parser
= GardenSnakeParser(debug
=debug
, form
=form
,
934 incl_carry
=incl_carry
)
935 _CACHED_PARSERS
[debug
, form
, incl_carry
] = parser
937 self
.parser
= deepcopy(parser
)
939 self
.parser
= GardenSnakeParser(debug
=debug
, form
=form
,
940 incl_carry
=incl_carry
)
942 def compile(self
, code
, mode
="exec", filename
="<string>"):
943 tree
= self
.parser
.parse(code
)
947 #misc.set_filename(filename, tree)
948 return compile(tree
, mode
="exec", filename
="<string>")
950 gen
= pycodegen
.ModuleCodeGenerator(tree
)