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 (
18 IndentLexer
, raise_syntax_error
, SyntaxError2
)
19 from openpower
.decoder
.orderedset
import OrderedSet
20 from openpower
.decoder
.power_enums
import BFP_FLAG_NAMES
22 # I use the Python AST
23 #from compiler import ast
28 regs
= ['RA', 'RS', 'RB', 'RC', 'RT']
29 fregs
= ['FRA', 'FRS', 'FRB', 'FRC', 'FRT', 'FRS']
30 SPECIAL_HELPERS
= {'concat', 'MEM', 'GPR', 'FPR', 'SPR', 'pow'}
31 SUBS_TO_ATTR_EXCEPTIONS
= SPECIAL_HELPERS |
{'CR'}
34 # I implemented INDENT / DEDENT generation as a post-processing filter
36 # The original lex token stream contains WS and NEWLINE characters.
37 # WS will only occur before any other tokens on a line.
39 # I have three filters. One tags tokens by adding two attributes.
40 # "must_indent" is True if the token must be indented from the
41 # previous code. The other is "at_line_start" which is True for WS
42 # and the first non-WS/non-NEWLINE on a line. It flags the check so
43 # see if the new line has changed indication level.
46 # No using Python's approach because Ply supports precedence
48 # comparison: expr (comp_op expr)*
49 # arith_expr: term (('+'|'-') term)*
50 # term: factor (('*'|'/'|'%'|'//') factor)*
51 # factor: ('+'|'-'|'~') factor | power
52 # comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
54 def make_le_compare(arg
):
56 return ast
.Call(ast
.Name("le", ast
.Load()), (left
, right
), [])
59 def make_ge_compare(arg
):
61 return ast
.Call(ast
.Name("ge", ast
.Load()), (left
, right
), [])
64 def make_lt_compare(arg
):
66 return ast
.Call(ast
.Name("lt", ast
.Load()), (left
, right
), [])
69 def make_gt_compare(arg
):
71 return ast
.Call(ast
.Name("gt", ast
.Load()), (left
, right
), [])
74 def make_eq_compare(arg
):
76 return ast
.Call(ast
.Name("eq", ast
.Load()), (left
, right
), [])
79 def make_ne_compare(arg
):
81 return ast
.Call(ast
.Name("ne", ast
.Load()), (left
, right
), [])
93 "<=": make_le_compare
,
94 ">=": make_ge_compare
,
98 "!=": make_ne_compare
,
107 def check_concat(node
): # checks if the comparison is already a concat
108 print("check concat", node
)
109 if not isinstance(node
, ast
.Call
):
111 node_func_id
= getattr(node
.func
, "id", None)
112 print("func", node_func_id
)
113 if node_func_id
!= 'concat':
115 if node
.keywords
: # a repeated list-constant, don't optimise
120 # identify SelectableInt pattern [something] * N
121 # must return concat(something, repeat=N)
122 # here is a TEMPORARY hack to support minimal expressions
123 # such as (XLEN-16), looking for ast.BinOp
124 # have to keep an eye on this
125 def identify_sint_mul_pattern(p
):
126 """here we are looking for patterns of this type:
128 these must specifically be returned as concat(item, repeat=something)
130 #print ("identify_sint_mul_pattern")
132 # print(" ", astor.dump_tree(pat))
133 if p
[2] != '*': # multiply
135 if (not isinstance(p
[3], ast
.Constant
) and # rhs = Num
136 not isinstance(p
[3], ast
.BinOp
) and # rhs = (XLEN-something)
137 (not isinstance(p
[3], ast
.Name
) and # rhs = {a variable}
138 not isinstance(p
[3], ast
.Attribute
))): # rhs = XLEN
140 if not isinstance(p
[1], ast
.List
): # lhs is a list
143 if len(l
) != 1: # lhs is a list of length 1
145 return True # yippee!
148 ########## Parser (tokens -> AST) ######
153 # https://www.mathcs.emory.edu/~valerie/courses/fall10/155/resources/op_precedence.html
154 # python operator precedence
155 # Highest precedence at top, lowest at bottom.
156 # Operators in the same box evaluate left to right.
158 # Operator Description
159 # () Parentheses (grouping)
160 # f(args...) Function call
161 # x[index:index] Slicing
162 # x[index] Subscription
163 # x.attribute Attribute reference
166 # +x, -x Positive, negative
167 # *, /, % mul, div, remainder
168 # +, - Addition, subtraction
169 # <<, >> Bitwise shifts
173 # in, not in, is, is not, <, <=, >, >=, <>, !=, == comp, membership, ident
177 # lambda Lambda expression
183 ("left", "EQ", "NE", "GT", "LT", "LE", "GE", "LTU", "GTU"),
187 ("left", "PLUS", "MINUS"),
188 ("left", "MULT", "DIV", "MOD"),
194 if self
.form
is not None:
195 form
= self
.sd
.sigforms
[self
.form
]
197 formkeys
= form
._asdict
().keys()
200 self
.declared_vars
= set()
201 self
.fnparm_vars
= set()
202 for rname
in regs
+ fregs
:
203 self
.gprs
[rname
] = None
204 self
.declared_vars
.add(rname
)
205 self
.available_op_fields
= set()
207 if k
not in self
.gprs
:
208 if k
== 'SPR': # sigh, lower-case to not conflict
210 self
.available_op_fields
.add(k
)
211 self
.op_fields
= OrderedSet()
212 self
.read_regs
= OrderedSet()
213 self
.uninit_regs
= OrderedSet()
214 self
.write_regs
= OrderedSet()
215 self
.special_regs
= OrderedSet() # see p_atom_name
217 def __init__(self
, form
, include_carry_in_write
=False, helper
=False):
218 self
.include_ca_in_write
= include_carry_in_write
222 self
.input_text
= None
225 # The grammar comments come from Python's Grammar/Grammar file
227 # NB: compound_stmt in single_input is followed by extra NEWLINE!
228 # file_input: (NEWLINE | stmt)* ENDMARKER
230 def p_file_input_end(self
, p
):
231 """file_input_end : file_input ENDMARKER"""
235 def p_file_input(self
, p
):
236 """file_input : file_input NEWLINE
240 if isinstance(p
[len(p
)-1], str):
244 p
[0] = [] # p == 2 --> only a blank line
251 # funcdef: [decorators] 'def' NAME parameters ':' suite
252 # ignoring decorators
254 def p_funcdef(self
, p
):
255 "funcdef : DEF NAME parameters COLON suite"
256 p
[0] = ast
.FunctionDef(p
[2], p
[3], p
[5], (), lineno
=p
.lineno(2))
257 # reset function parameters after suite is identified
258 self
.fnparm_vars
= set()
260 # parameters: '(' [varargslist] ')'
261 def p_parameters(self
, p
):
262 """parameters : LPAR RPAR
263 | LPAR varargslist RPAR"""
269 p
[0] = ast
.arguments(args
=args
, vararg
=None, kwarg
=None, defaults
=[])
270 # during the time between parameters identified and suite is not
271 # there is a window of opportunity to declare the function parameters
272 # in-scope, for use to not overwrite them with auto-assign
273 self
.fnparm_vars
= set()
275 print("adding fn parm", arg
)
276 self
.fnparm_vars
.add(arg
)
278 # varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] |
282 def p_varargslist(self
, p
):
283 """varargslist : varargslist COMMA NAME
291 # stmt: simple_stmt | compound_stmt
292 def p_stmt_simple(self
, p
):
293 """stmt : simple_stmt"""
294 # simple_stmt is a list
297 def p_stmt_compound(self
, p
):
298 """stmt : compound_stmt"""
301 # simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
302 def p_simple_stmt(self
, p
):
303 """simple_stmt : small_stmts NEWLINE
304 | small_stmts SEMICOLON NEWLINE"""
307 def p_small_stmts(self
, p
):
308 """small_stmts : small_stmts SEMICOLON small_stmt
312 elif isinstance(p
[1], list):
317 # small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt |
318 # import_stmt | global_stmt | exec_stmt | assert_stmt
319 def p_small_stmt(self
, p
):
320 """small_stmt : flow_stmt
323 if isinstance(p
[1], ast
.Call
):
324 p
[0] = ast
.Expr(p
[1])
325 elif isinstance(p
[1], ast
.Name
) and p
[1].id not in SPECIAL_HELPERS
:
327 name
= ast
.Name("self", ast
.Load())
328 name
= ast
.Attribute(name
, fname
, ast
.Load())
329 p
[0] = ast
.Call(name
, [], [])
333 # expr_stmt: testlist (augassign (yield_expr|testlist) |
334 # ('=' (yield_expr|testlist))*)
335 # augassign: ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' |
336 # '<<=' | '>>=' | '**=' | '//=')
337 def p_expr_stmt(self
, p
):
338 """expr_stmt : testlist ASSIGNEA testlist
339 | testlist ASSIGN testlist
341 print("expr_stmt", p
)
343 # a list of expressions
344 #p[0] = ast.Discard(p[1])
347 iea_mode
= p
[2] == '<-iea'
352 if isinstance(p
[1], ast
.Name
):
354 elif isinstance(p
[1], ast
.Subscript
):
355 print("assign subscript", p
[1].value
,
359 print(astor
.dump_tree(p
[1]))
360 print(astor
.dump_tree(p
[3]))
361 if isinstance(p
[1].value
, ast
.Name
):
363 print("assign subscript value to name", name
)
364 if name
in self
.gprs
:
365 # add to list of uninitialised
366 self
.uninit_regs
.add(name
)
367 # work out if this is an ininitialised variable
368 # that should be auto-assigned simply by being "sliced"
369 autoassign
= (name
not in self
.declared_vars
and
370 name
not in self
.fnparm_vars
and
371 name
not in self
.special_regs
)
372 elif isinstance(p
[1], (ast
.Attribute
, ast
.Tuple
)):
374 elif isinstance(p
[1], ast
.Call
) and p
[1].func
.id in \
375 ['GPR', 'FPR', 'SPR']:
376 print(astor
.dump_tree(p
[1]))
377 # replace GPR(x) with GPR[x]
378 idx
= p
[1].args
[0].id
379 if idx
in regs
+ fregs
:
380 ridx
= ast
.Name("_%s" % idx
, ast
.Load())
382 ridx
= ast
.Name(idx
, ast
.Load())
383 p
[1] = ast
.Subscript(p
[1].func
, ridx
, ast
.Load())
385 self
.read_regs
.add(idx
) # add to list of regs to read
386 elif isinstance(p
[1], ast
.Call
) and p
[1].func
.id == 'MEM':
388 print(astor
.dump_tree(p
[1]))
389 p
[1].func
.id = "memassign" # change function name to set
390 p
[1].args
.append(p
[3])
393 print(astor
.dump_tree(p
[0]))
396 print(astor
.dump_tree(p
[1]))
397 raise_syntax_error("help, help", self
.filename
,
398 p
.slice[2].lineno
, p
.slice[2].lexpos
,
400 print("expr assign", name
, p
[1], "to", p
[3])
401 print(astor
.dump_tree(p
[3]))
402 if isinstance(p
[3], ast
.Name
):
404 if toname
in self
.gprs
:
405 self
.read_regs
.add(toname
)
406 if name
and name
in self
.gprs
:
407 self
.write_regs
.add(name
) # add to list of regs to write
408 p
[0] = self
.Assign(autoassign
, name
, p
[1], p
[3], iea_mode
,
411 self
.declared_vars
.add(name
)
413 def p_flow_stmt(self
, p
):
414 "flow_stmt : return_stmt"
417 # return_stmt: 'return' [testlist]
418 def p_return_stmt(self
, p
):
419 "return_stmt : RETURN testlist"
420 p
[0] = ast
.Return(p
[2])
422 def p_compound_stmt(self
, p
):
423 """compound_stmt : if_stmt
431 def p_break_stmt(self
, p
):
432 """break_stmt : BREAK
436 def p_for_stmt(self
, p
):
437 """for_stmt : FOR atom EQ comparison TO comparison COLON suite
438 | DO atom EQ comparison TO comparison COLON suite
442 it
= ast
.Call(ast
.Name("RANGE", ast
.Load()), (start
, end
), [])
443 p
[0] = ast
.For(p
[2], it
, p
[8], [])
445 def p_while_stmt(self
, p
):
446 """while_stmt : DO WHILE test COLON suite ELSE COLON suite
447 | DO WHILE test COLON suite
450 p
[0] = ast
.While(p
[3], p
[5], [])
452 p
[0] = ast
.While(p
[3], p
[5], p
[8])
454 def p_switch_smt(self
, p
):
455 """switch_stmt : SWITCH LPAR atom RPAR COLON NEWLINE INDENT switches DEDENT
459 print(astor
.dump_tree(p
[1]))
462 current_cases
= [] # for deferral
463 for (case
, suite
) in p
[8]:
464 print("for", case
, suite
)
467 current_cases
.append(ast
.Num(c
))
469 if case
== 'default': # last
472 current_cases
.append(ast
.Num(c
))
473 print("cases", current_cases
)
474 compare
= ast
.Compare(switchon
, [ast
.In()],
475 [ast
.List(current_cases
, ast
.Load())])
477 cases
.append((compare
, suite
))
479 print("ended", case
, current_cases
)
480 if case
== 'default':
482 compare
= ast
.Compare(switchon
, [ast
.In()],
483 [ast
.List(current_cases
, ast
.Load())])
484 cases
.append((compare
, suite
))
485 cases
.append((None, suite
))
489 for compare
, suite
in cases
:
490 print("after rev", compare
, suite
)
492 assert len(res
) == 0, "last case should be default"
495 if not isinstance(res
, list):
497 res
= ast
.If(compare
, suite
, res
)
500 def p_switches(self
, p
):
501 """switches : switch_list switch_default
509 def p_switch_list(self
, p
):
510 """switch_list : switch_case switch_list
518 def p_switch_case(self
, p
):
519 """switch_case : CASE LPAR atomlist RPAR COLON suite
522 if isinstance(p
[6][0], ast
.Name
) and p
[6][0].id == 'fallthrough':
526 def p_switch_default(self
, p
):
527 """switch_default : DEFAULT COLON suite
529 p
[0] = ('default', p
[3])
531 def p_atomlist(self
, p
):
532 """atomlist : atom COMMA atomlist
535 assert isinstance(p
[1], ast
.Constant
), "case must be numbers"
537 p
[0] = [p
[1].value
] + p
[3]
541 def p_if_stmt(self
, p
):
542 """if_stmt : IF test COLON suite ELSE COLON if_stmt
543 | IF test COLON suite ELSE COLON suite
544 | IF test COLON suite
546 if len(p
) == 8 and isinstance(p
[7], ast
.If
):
547 p
[0] = ast
.If(p
[2], p
[4], [p
[7]])
549 p
[0] = ast
.If(p
[2], p
[4], [])
551 p
[0] = ast
.If(p
[2], p
[4], p
[7])
553 def p_suite(self
, p
):
554 """suite : simple_stmt
555 | NEWLINE INDENT stmts DEDENT"""
561 def p_stmts(self
, p
):
562 """stmts : stmts stmt
569 def p_comparison(self
, p
):
570 """comparison : comparison PLUS comparison
571 | comparison MINUS comparison
572 | comparison MULT comparison
573 | comparison DIV comparison
574 | comparison MOD comparison
575 | comparison EQ comparison
576 | comparison NE comparison
577 | comparison LE comparison
578 | comparison GE comparison
579 | comparison LTU comparison
580 | comparison GTU comparison
581 | comparison LT comparison
582 | comparison GT comparison
583 | comparison BITOR comparison
584 | comparison BITXOR comparison
585 | comparison BITAND comparison
589 | comparison APPEND comparison
594 p
[0] = ast
.Call(ast
.Name("ltu", ast
.Load()), (p
[1], p
[3]), [])
596 p
[0] = ast
.Call(ast
.Name("gtu", ast
.Load()), (p
[1], p
[3]), [])
598 l
= check_concat(p
[1]) + check_concat(p
[3])
599 p
[0] = ast
.Call(ast
.Name("concat", ast
.Load()), l
, [])
600 elif p
[2] in ['/', '%']:
601 # bad hack: if % or / used anywhere other than div/mod ops,
602 # do % or /. however if the argument names are "dividend"
603 # we must call the special trunc_divs and trunc_rems functions
605 # actual call will be "dividend / divisor" - just check
607 # XXX DISABLE BAD HACK (False)
608 if False and isinstance(l
, ast
.Name
) and l
.id == 'dividend':
613 # return "function trunc_xxx(l, r)"
614 p
[0] = ast
.Call(ast
.Name(fn
, ast
.Load()), (l
, r
), [])
616 # return "l {binop} r"
617 p
[0] = ast
.BinOp(p
[1], binary_ops
[p
[2]], p
[3])
618 elif p
[2] in ['<', '>', '=', '<=', '>=', '!=']:
619 p
[0] = binary_ops
[p
[2]]((p
[1], p
[3]))
620 elif identify_sint_mul_pattern(p
):
621 keywords
= [ast
.keyword(arg
='repeat', value
=p
[3])]
623 p
[0] = ast
.Call(ast
.Name("concat", ast
.Load()), l
, keywords
)
625 p
[0] = ast
.BinOp(p
[1], binary_ops
[p
[2]], p
[3])
626 # HORRENDOUS hack, add brackets around the bin-op by
627 # creating a function call with a *blank* function name!
628 # XXX argh doesn't work because of analysis of
629 # identify_sint_pattern
630 #p[0] = ast.Call(ast.Name("", ast.Load()), [p[0]], [])
632 if isinstance(p
[2], str) and p
[2] == '-':
633 p
[0] = ast
.UnaryOp(unary_ops
[p
[2]], p
[1])
635 p
[0] = ast
.UnaryOp(unary_ops
[p
[1]], p
[2])
639 # power: atom trailer* ['**' factor]
640 # trailers enables function calls (and subscripts).
641 # so this is 'trailerlist'
642 def p_power(self
, p
):
644 | atom trailerlist"""
646 print("power dump atom notrailer")
647 print(astor
.dump_tree(p
[1]))
650 print("power dump atom")
651 print(astor
.dump_tree(p
[1]))
652 print("power dump trailerlist")
653 print(astor
.dump_tree(p
[2]))
654 p
[0] = self
.apply_trailer(p
[1], p
[2], self
.read_regs
)
655 if isinstance(p
[1], ast
.Name
):
657 if name
in regs
+ fregs
:
658 self
.read_regs
.add(name
)
660 def p_atom_name(self
, p
):
663 if name
in self
.available_op_fields
:
664 self
.op_fields
.add(name
)
665 if name
in ['overflow', 'CR0']:
666 self
.write_regs
.add(name
)
667 if self
.include_ca_in_write
:
668 if name
in ['CA', 'CA32']:
669 self
.write_regs
.add(name
)
670 if name
in ['CR', 'LR', 'CTR', 'TAR', 'FPSCR', 'MSR',
671 'SVSTATE', 'SVREMAP', 'SRR0', 'SRR1',
672 'SVSHAPE0', 'SVSHAPE1', 'SVSHAPE2', 'SVSHAPE3']:
673 self
.special_regs
.add(name
)
674 self
.write_regs
.add(name
) # and add to list to write
675 if name
in ('XLEN', ) or name
in BFP_FLAG_NAMES
:
676 attr
= ast
.Name("self", ast
.Load())
677 p
[0] = ast
.Attribute(attr
, name
, ast
.Load(), lineno
=p
.lineno(1))
679 p
[0] = ast
.Name(id=name
, ctx
=ast
.Load(), lineno
=p
.lineno(1))
681 def p_atom_number(self
, p
):
686 p
[0] = ast
.Constant(p
[1])
688 # '[' [listmaker] ']' |
690 def p_atom_listmaker(self
, p
):
691 """atom : LBRACK listmaker RBRACK"""
694 def p_listmaker(self
, p
):
695 """listmaker : test COMMA listmaker
699 p
[0] = ast
.List([p
[1]], ast
.Load())
701 p
[0] = ast
.List([p
[1]] + p
[3].nodes
, ast
.Load())
703 def p_atom_tuple(self
, p
):
704 """atom : LPAR testlist RPAR"""
707 print(astor
.dump_tree(p
[2]))
709 if isinstance(p
[2], ast
.Name
):
711 print("tuple name", name
)
712 if name
in self
.gprs
:
713 self
.read_regs
.add(name
) # add to list of regs to read
714 #p[0] = ast.Subscript(ast.Name("GPR", ast.Load()), ast.Str(p[2].id))
717 elif isinstance(p
[2], ast
.BinOp
):
718 if isinstance(p
[2].left
, ast
.Name
) and \
719 isinstance(p
[2].right
, ast
.Constant
) and \
720 p
[2].right
.value
== 0 and \
721 p
[2].left
.id in self
.gprs
:
723 self
.read_regs
.add(rid
) # add to list of regs to read
724 # create special call to GPR.getz or FPR.getz
726 gprz
= ast
.Name("FPR", ast
.Load())
728 gprz
= ast
.Name("GPR", ast
.Load())
729 # get testzero function
730 gprz
= ast
.Attribute(gprz
, "getz", ast
.Load())
731 # *sigh* see class GPR. we need index itself not reg value
732 ridx
= ast
.Name("_%s" % rid
, ast
.Load())
733 p
[0] = ast
.Call(gprz
, [ridx
], [])
734 print("tree", astor
.dump_tree(p
[0]))
740 def p_trailerlist(self
, p
):
741 """trailerlist : trailer trailerlist
747 p
[0] = ("TLIST", p
[1], p
[2])
749 # trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
750 def p_trailer(self
, p
):
751 """trailer : trailer_arglist
757 def p_trailer_arglist(self
, p
):
758 """trailer_arglist : LPAR arglist RPAR
761 args
= [] if len(p
) == 3 else p
[2]
762 p
[0] = ("CALL", args
, p
.slice[1])
764 def p_trailer_subscript(self
, p
):
765 "trailer_subscript : LBRACK subscript RBRACK"
766 p
[0] = ("SUBS", p
[2])
768 def p_trailer_attr(self
, p
):
769 "trailer_attr : PERIOD NAME"
770 p
[0] = ("ATTR", p
[2])
772 # subscript: '.' '.' '.' | test | [test] ':' [test]
774 def p_subscript(self
, p
):
775 """subscript : test COLON test
780 if isinstance(p
[3], ast
.Constant
):
781 end
= ast
.Constant(p
[3].value
+1)
783 end
= ast
.BinOp(p
[3], ast
.Add(), ast
.Constant(1))
788 # testlist: test (',' test)* [',']
789 # Contains shift/reduce error
791 def p_testlist(self
, p
):
792 """testlist : testlist_multi COMMA
797 # May need to promote singleton to tuple
798 if isinstance(p
[1], list):
802 # Convert into a tuple?
803 if isinstance(p
[0], list):
804 p
[0] = ast
.Tuple(p
[0])
806 def p_testlist_multi(self
, p
):
807 """testlist_multi : testlist_multi COMMA test
813 if isinstance(p
[1], list):
819 # test: or_test ['if' or_test 'else' test] | lambdef
820 # as I don't support 'and', 'or', and 'not' this works down to 'comparison'
824 | comparison QMARK test COLON test"""
828 p
[0] = ast
.IfExp(test
=p
[1], body
=p
[3], orelse
=p
[5])
830 # arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
832 # XXX INCOMPLETE: this doesn't allow the trailing comma
834 def p_arglist(self
, p
):
835 """arglist : arglist COMMA argument
842 # argument: test [gen_for] | test '=' test # Really [keyword '='] test
843 def p_argument(self
, p
):
847 def p_error(self
, p
):
848 # print "Error!", repr(p)
849 raise_syntax_error(str(p
), self
.filename
, p
.lineno
, p
.lexpos
,
852 def Assign(self
, autoassign
, assignname
, left
, right
, iea_mode
, eq_tok
):
854 print("Assign", autoassign
, assignname
, left
, right
)
855 if isinstance(left
, ast
.Name
):
856 # Single assignment on left
857 # XXX when doing IntClass, which will have an "eq" function,
858 # this is how to access it
859 # eq = ast.Attribute(left, "eq") # get eq fn
860 # return ast.Call(eq, [right], []) # now call left.eq(right)
861 return ast
.Assign([ast
.Name(left
.id, ast
.Store())], right
)
862 elif isinstance(left
, ast
.Tuple
):
863 # List of things - make sure they are Name nodes
865 for child
in left
.elts
:
866 if not isinstance(child
, ast
.Name
):
868 "that assignment not supported", self
.filename
,
869 eq_tok
.lineno
, eq_tok
.lexpos
, self
.input_text
)
870 names
.append(child
.id)
871 ass_list
= [ast
.Name(name
, ast
.Store()) for name
in names
]
872 return ast
.Assign([ast
.Tuple(ass_list
)], right
)
873 elif isinstance(left
, ast
.Attribute
):
875 ast
.Attribute(left
.value
, left
.attr
, ast
.Store())], right
)
876 elif isinstance(left
, ast
.Subscript
):
878 # XXX changing meaning of "undefined" to a function
879 # if (isinstance(ls, ast.Slice) and isinstance(right, ast.Name) and
880 # right.id == 'undefined'):
881 # # undefined needs to be copied the exact same slice
882 # right = ast.Subscript(right, ls, ast.Load())
883 # return ast.Assign([left], right)
884 res
= ast
.Assign([left
], right
)
885 if autoassign
and isinstance(ls
, ast
.Slice
):
886 # hack to create a variable pre-declared based on a slice.
887 # dividend[0:32] = (RA)[0:32] will create
888 # dividend = [0] * 32
889 # dividend[0:32] = (RA)[0:32]
890 # the declaration makes the slice-assignment "work"
891 lower
, upper
, step
= ls
.lower
, ls
.upper
, ls
.step
892 print("lower, upper, step", repr(lower
), repr(upper
), step
)
893 # XXX relax constraint that indices on auto-assign have
894 # to be constants x[0:32]
895 # if not isinstance(lower, ast.Constant) or \
896 # not isinstance(upper, ast.Constant):
898 qty
= ast
.BinOp(upper
, binary_ops
['-'], lower
)
899 keywords
= [ast
.keyword(arg
='repeat', value
=qty
)]
901 right
= ast
.Call(ast
.Name("concat", ast
.Load()), l
, keywords
)
902 declare
= ast
.Assign(
903 [ast
.Name(assignname
, ast
.Store())], right
)
904 return [declare
, res
]
906 # XXX HMMM probably not needed...
908 if isinstance(ls
, ast
.Slice
):
909 lower
, upper
, step
= ls
.lower
, ls
.upper
, ls
.step
910 print("slice assign", lower
, upper
, step
)
912 ls
= (lower
, upper
, None)
914 ls
= (lower
, upper
, step
)
916 return ast
.Call(ast
.Name("selectassign", ast
.Load()),
917 [left
.value
, ls
, right
], [])
920 raise_syntax_error("Can't do that yet", self
.filename
,
921 eq_tok
.lineno
, eq_tok
.lexpos
, self
.input_text
)
923 def try_extract_name(self
, atom
):
924 if isinstance(atom
, ast
.Attribute
):
925 if isinstance(atom
.value
, ast
.Name
) and atom
.value
.id == "self":
928 if isinstance(atom
, ast
.Name
):
932 def try_extract_uppercase_name(self
, atom
):
933 name
= self
.try_extract_name(atom
)
936 # we want to filter out names like _123 that have no letters with
937 # casing at all, hence the lower() check
938 if name
.upper() != name
or name
.lower() == name
:
942 def try_get_attr(self
, atom
, trailer
):
943 if trailer
[0] == "ATTR":
945 if trailer
[0] != "SUBS":
947 base_name
= self
.try_extract_uppercase_name(atom
)
948 if base_name
is None or base_name
in SUBS_TO_ATTR_EXCEPTIONS
:
950 if len(trailer
[1]) != 1:
952 return self
.try_extract_uppercase_name(trailer
[1][0])
954 def apply_trailer(self
, atom
, trailer
, read_regs
):
955 if trailer
[0] == "TLIST":
956 # assume depth of one
957 atom
= self
.apply_trailer(atom
, trailer
[1], read_regs
)
959 if trailer
[0] == "CALL":
960 #p[0] = ast.Expr(ast.Call(p[1], p[2][1], []))
961 for arg
in trailer
[1]:
962 if isinstance(arg
, ast
.Name
):
964 if name
in regs
+ fregs
:
966 if atom
.id == "SetFX":
967 if len(trailer
[1]) != 1 or \
968 not isinstance(trailer
[1][0], ast
.Attribute
):
970 "SetFX call must have only one argument that is an "
971 "attribute access -- like `SetFX(FPSCR.VXSNAN)`",
972 self
.filename
, trailer
[2].lineno
, trailer
[2].lexpos
,
975 args
= [arg
.value
, ast
.Str(arg
.attr
)]
976 name
= ast
.Name("self", ast
.Load())
977 atom
= ast
.Attribute(name
, atom
, ast
.Load())
978 return ast
.Call(atom
, args
, [])
979 # special-case, these functions must NOT be made "self.xxxx"
980 if atom
.id not in SPECIAL_HELPERS
:
981 name
= ast
.Name("self", ast
.Load())
982 atom
= ast
.Attribute(name
, atom
, ast
.Load())
983 return ast
.Call(atom
, trailer
[1], [])
984 # if p[1].id == 'print':
985 # p[0] = ast.Printnl(ast.Tuple(p[2][1]), None, None)
987 # p[0] = ast.CallFunc(p[1], p[2][1], None, None)
989 attr
= self
.try_get_attr(atom
, trailer
)
991 return ast
.Attribute(atom
, attr
, ast
.Load())
992 assert trailer
[0] == "SUBS"
993 print("subscript atom", trailer
[1])
994 #raise AssertionError("not implemented %s" % p[2][0])
998 if isinstance(idx
, ast
.Name
) and idx
.id in regs
+ fregs
:
999 read_regs
.add(idx
.id)
1000 if isinstance(idx
, ast
.Name
) and idx
.id in regs
:
1001 print("single atom subscript, underscored", idx
.id)
1002 idx
= ast
.Name("_%s" % idx
.id, ast
.Load())
1004 idx
= ast
.Slice(subs
[0], subs
[1], None)
1005 # if isinstance(atom, ast.Name) and atom.id == 'CR':
1006 # atom.id = 'CR' # bad hack
1007 #print ("apply_trailer Subscript", atom.id, idx)
1008 return ast
.Subscript(atom
, idx
, ast
.Load())
1011 _CACHE_DECODER
= True
1012 _CACHED_DECODER
= None
1015 def _create_cached_decoder():
1016 global _CACHED_DECODER
1018 if _CACHED_DECODER
is None:
1019 _CACHED_DECODER
= create_pdecode()
1020 return _CACHED_DECODER
1021 return create_pdecode()
1024 class GardenSnakeParser(PowerParser
):
1025 def __init__(self
, debug
=False, form
=None, incl_carry
=False, helper
=False):
1026 if form
is not None:
1027 self
.sd
= _create_cached_decoder()
1028 PowerParser
.__init
__(self
, form
, incl_carry
, helper
=helper
)
1030 self
.lexer
= IndentLexer(debug
=0)
1031 self
.tokens
= self
.lexer
.tokens
1032 self
.parser
= yacc
.yacc(module
=self
, start
="file_input_end",
1033 debug
=debug
, write_tables
=False)
1035 def parse(self
, code
):
1037 self
.lexer
.filename
= self
.filename
1038 result
= self
.parser
.parse(code
, lexer
=self
.lexer
, debug
=self
.debug
)
1040 result
= [ast
.ClassDef("ISACallerFnHelper", [
1041 "ISACallerHelper"], [], result
, decorator_list
=[])]
1042 return ast
.Module(result
)
1045 ###### Code generation ######
1047 #from compiler import misc, syntax, pycodegen
1049 _CACHED_PARSERS
= {}
1050 _CACHE_PARSERS
= True
1053 class GardenSnakeCompiler(object):
1054 def __init__(self
, debug
=False, form
=None, incl_carry
=False, helper
=False):
1057 self
.parser
= _CACHED_PARSERS
[debug
, form
, incl_carry
, helper
]
1059 self
.parser
= GardenSnakeParser(
1060 debug
=debug
, form
=form
, incl_carry
=incl_carry
,
1062 _CACHED_PARSERS
[debug
, form
, incl_carry
, helper
] = self
.parser
1064 self
.parser
= GardenSnakeParser(debug
=debug
, form
=form
,
1065 incl_carry
=incl_carry
, helper
=helper
)
1067 def compile(self
, code
, mode
="exec", filename
="<string>"):
1068 if filename
in ["string", "<string>"]:
1069 raise ValueError("missing filename")
1070 self
.parser
.filename
= filename
1071 self
.parser
.input_text
= code
1073 tree
= self
.parser
.parse(code
)
1074 except SyntaxError2
as e
:
1075 e
.raise_syntax_error()
1079 #misc.set_filename(filename, tree)
1080 return compile(tree
, mode
="exec", filename
="<string>")
1081 # syntax.check(tree)
1082 gen
= pycodegen
.ModuleCodeGenerator(tree
)
1083 code
= gen
.getCode()