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
15 from soc
.decoder
.power_decoder
import create_pdecode
16 from soc
.decoder
.pseudo
.lexer
import IndentLexer
17 from soc
.decoder
.orderedset
import OrderedSet
19 # I use the Python AST
20 #from compiler import ast
26 def Assign(left
, right
, iea_mode
):
28 print("Assign", left
, right
)
29 if isinstance(left
, ast
.Name
):
30 # Single assignment on left
31 # XXX when doing IntClass, which will have an "eq" function,
32 # this is how to access it
33 # eq = ast.Attribute(left, "eq") # get eq fn
34 # return ast.Call(eq, [right], []) # now call left.eq(right)
35 return ast
.Assign([ast
.Name(left
.id, ast
.Store())], right
)
36 elif isinstance(left
, ast
.Tuple
):
37 # List of things - make sure they are Name nodes
39 for child
in left
.getChildren():
40 if not isinstance(child
, ast
.Name
):
41 raise SyntaxError("that assignment not supported")
42 names
.append(child
.name
)
43 ass_list
= [ast
.AssName(name
, 'OP_ASSIGN') for name
in names
]
44 return ast
.Assign([ast
.AssTuple(ass_list
)], right
)
45 elif isinstance(left
, ast
.Subscript
):
46 return ast
.Assign([left
], right
)
47 # XXX HMMM probably not needed...
49 if isinstance(ls
, ast
.Slice
):
50 lower
, upper
, step
= ls
.lower
, ls
.upper
, ls
.step
51 print("slice assign", lower
, upper
, step
)
53 ls
= (lower
, upper
, None)
55 ls
= (lower
, upper
, step
)
57 return ast
.Call(ast
.Name("selectassign", ast
.Load()),
58 [left
.value
, ls
, right
], [])
61 raise SyntaxError("Can't do that yet")
64 # I implemented INDENT / DEDENT generation as a post-processing filter
66 # The original lex token stream contains WS and NEWLINE characters.
67 # WS will only occur before any other tokens on a line.
69 # I have three filters. One tags tokens by adding two attributes.
70 # "must_indent" is True if the token must be indented from the
71 # previous code. The other is "at_line_start" which is True for WS
72 # and the first non-WS/non-NEWLINE on a line. It flags the check so
73 # see if the new line has changed indication level.
76 # No using Python's approach because Ply supports precedence
78 # comparison: expr (comp_op expr)*
79 # arith_expr: term (('+'|'-') term)*
80 # term: factor (('*'|'/'|'%'|'//') factor)*
81 # factor: ('+'|'-'|'~') factor | power
82 # comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
84 def make_le_compare(arg
):
86 return ast
.Call(ast
.Name("le", ast
.Load()), (left
, right
), [])
89 def make_ge_compare(arg
):
91 return ast
.Call(ast
.Name("ge", ast
.Load()), (left
, right
), [])
94 def make_lt_compare(arg
):
96 return ast
.Call(ast
.Name("lt", ast
.Load()), (left
, right
), [])
99 def make_gt_compare(arg
):
101 return ast
.Call(ast
.Name("gt", ast
.Load()), (left
, right
), [])
104 def make_eq_compare(arg
):
106 return ast
.Call(ast
.Name("eq", ast
.Load()), (left
, right
), [])
109 def make_ne_compare(arg
):
111 return ast
.Call(ast
.Name("ne", ast
.Load()), (left
, right
), [])
123 "<=": make_le_compare
,
124 ">=": make_ge_compare
,
125 "<": make_lt_compare
,
126 ">": make_gt_compare
,
127 "=": make_eq_compare
,
128 "!=": make_ne_compare
,
137 def check_concat(node
): # checks if the comparison is already a concat
138 print("check concat", node
)
139 if not isinstance(node
, ast
.Call
):
141 print("func", node
.func
.id)
142 if node
.func
.id != 'concat':
144 if node
.keywords
: # a repeated list-constant, don't optimise
149 # identify SelectableInt pattern [something] * N
150 # must return concat(something, repeat=N)
151 def identify_sint_mul_pattern(p
):
152 if p
[2] != '*': # multiply
154 if not isinstance(p
[3], ast
.Constant
): # rhs = Num
156 if not isinstance(p
[1], ast
.List
): # lhs is a list
159 if len(l
) != 1: # lhs is a list of length 1
161 return True # yippee!
164 def apply_trailer(atom
, trailer
):
165 if trailer
[0] == "TLIST":
166 # assume depth of one
167 atom
= apply_trailer(atom
, trailer
[1])
169 if trailer
[0] == "CALL":
170 #p[0] = ast.Expr(ast.Call(p[1], p[2][1], []))
171 return ast
.Call(atom
, trailer
[1], [])
172 # if p[1].id == 'print':
173 # p[0] = ast.Printnl(ast.Tuple(p[2][1]), None, None)
175 # p[0] = ast.CallFunc(p[1], p[2][1], None, None)
177 print("subscript atom", trailer
[1])
178 #raise AssertionError("not implemented %s" % p[2][0])
183 idx
= ast
.Slice(subs
[0], subs
[1], None)
184 return ast
.Subscript(atom
, idx
, ast
.Load())
186 ########## Parser (tokens -> AST) ######
191 # https://www.mathcs.emory.edu/~valerie/courses/fall10/155/resources/op_precedence.html
192 # python operator precedence
193 # Highest precedence at top, lowest at bottom.
194 # Operators in the same box evaluate left to right.
196 # Operator Description
197 # () Parentheses (grouping)
198 # f(args...) Function call
199 # x[index:index] Slicing
200 # x[index] Subscription
201 # x.attribute Attribute reference
204 # +x, -x Positive, negative
205 # *, /, % mul, div, remainder
206 # +, - Addition, subtraction
207 # <<, >> Bitwise shifts
211 # in, not in, is, is not, <, <=, >, >=, <>, !=, == comp, membership, ident
215 # lambda Lambda expression
221 ("left", "EQ", "NE", "GT", "LT", "LE", "GE", "LTU", "GTU"),
225 ("left", "PLUS", "MINUS"),
226 ("left", "MULT", "DIV", "MOD"),
230 def __init__(self
, form
):
232 form
= self
.sd
.sigforms
[form
]
234 formkeys
= form
._asdict
().keys()
235 for rname
in ['RA', 'RB', 'RC', 'RT', 'RS']:
236 self
.gprs
[rname
] = None
237 self
.available_op_fields
= set()
239 if k
not in self
.gprs
:
240 if k
== 'SPR': # sigh, lower-case to not conflict
242 self
.available_op_fields
.add(k
)
243 self
.op_fields
= OrderedSet()
244 self
.read_regs
= OrderedSet()
245 self
.uninit_regs
= OrderedSet()
246 self
.write_regs
= OrderedSet()
247 self
.special_regs
= OrderedSet() # see p_atom_name
249 # The grammar comments come from Python's Grammar/Grammar file
251 # NB: compound_stmt in single_input is followed by extra NEWLINE!
252 # file_input: (NEWLINE | stmt)* ENDMARKER
254 def p_file_input_end(self
, p
):
255 """file_input_end : file_input ENDMARKER"""
259 def p_file_input(self
, p
):
260 """file_input : file_input NEWLINE
264 if isinstance(p
[len(p
)-1], str):
268 p
[0] = [] # p == 2 --> only a blank line
275 # funcdef: [decorators] 'def' NAME parameters ':' suite
276 # ignoring decorators
278 def p_funcdef(self
, p
):
279 "funcdef : DEF NAME parameters COLON suite"
280 p
[0] = ast
.FunctionDef(p
[2], p
[3], p
[5], ())
282 # parameters: '(' [varargslist] ')'
283 def p_parameters(self
, p
):
284 """parameters : LPAR RPAR
285 | LPAR varargslist RPAR"""
290 p
[0] = ast
.arguments(args
=args
, vararg
=None, kwarg
=None, defaults
=[])
292 # varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] |
296 def p_varargslist(self
, p
):
297 """varargslist : varargslist COMMA NAME
304 # stmt: simple_stmt | compound_stmt
305 def p_stmt_simple(self
, p
):
306 """stmt : simple_stmt"""
307 # simple_stmt is a list
310 def p_stmt_compound(self
, p
):
311 """stmt : compound_stmt"""
314 # simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
315 def p_simple_stmt(self
, p
):
316 """simple_stmt : small_stmts NEWLINE
317 | small_stmts SEMICOLON NEWLINE"""
320 def p_small_stmts(self
, p
):
321 """small_stmts : small_stmts SEMICOLON small_stmt
328 # small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt |
329 # import_stmt | global_stmt | exec_stmt | assert_stmt
330 def p_small_stmt(self
, p
):
331 """small_stmt : flow_stmt
334 if isinstance(p
[1], ast
.Call
):
335 p
[0] = ast
.Expr(p
[1])
336 elif isinstance(p
[1], ast
.Name
) and p
[1].id == 'TRAP':
337 # TRAP needs to actually be a function
338 name
= ast
.Name("self", ast
.Load())
339 name
= ast
.Attribute(name
, "TRAP", ast
.Load())
340 p
[0] = ast
.Call(name
, [], [])
344 # expr_stmt: testlist (augassign (yield_expr|testlist) |
345 # ('=' (yield_expr|testlist))*)
346 # augassign: ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' |
347 # '<<=' | '>>=' | '**=' | '//=')
348 def p_expr_stmt(self
, p
):
349 """expr_stmt : testlist ASSIGNEA testlist
350 | testlist ASSIGN testlist
352 print("expr_stmt", p
)
354 # a list of expressions
355 #p[0] = ast.Discard(p[1])
358 iea_mode
= p
[2] == '<-iea'
360 if isinstance(p
[1], ast
.Name
):
362 elif isinstance(p
[1], ast
.Subscript
):
363 if isinstance(p
[1].value
, ast
.Name
):
365 if name
in self
.gprs
:
366 # add to list of uninitialised
367 self
.uninit_regs
.add(name
)
368 elif isinstance(p
[1], ast
.Call
) and p
[1].func
.id in ['GPR', 'SPR']:
369 print(astor
.dump_tree(p
[1]))
370 # replace GPR(x) with GPR[x]
372 p
[1] = ast
.Subscript(p
[1].func
, idx
, ast
.Load())
373 elif isinstance(p
[1], ast
.Call
) and p
[1].func
.id == 'MEM':
375 print(astor
.dump_tree(p
[1]))
376 p
[1].func
.id = "memassign" # change function name to set
377 p
[1].args
.append(p
[3])
380 print(astor
.dump_tree(p
[0]))
384 print(astor
.dump_tree(p
[1]))
385 print("expr assign", name
, p
[1])
386 if name
and name
in self
.gprs
:
387 self
.write_regs
.add(name
) # add to list of regs to write
388 p
[0] = Assign(p
[1], p
[3], iea_mode
)
390 def p_flow_stmt(self
, p
):
391 "flow_stmt : return_stmt"
394 # return_stmt: 'return' [testlist]
395 def p_return_stmt(self
, p
):
396 "return_stmt : RETURN testlist"
397 p
[0] = ast
.Return(p
[2])
399 def p_compound_stmt(self
, p
):
400 """compound_stmt : if_stmt
408 def p_break_stmt(self
, p
):
409 """break_stmt : BREAK
413 def p_for_stmt(self
, p
):
414 """for_stmt : FOR atom EQ test TO test COLON suite
415 | DO atom EQ test TO test COLON suite
417 # auto-add-one (sigh) due to python range
419 end
= ast
.BinOp(p
[6], ast
.Add(), ast
.Constant(1))
420 it
= ast
.Call(ast
.Name("range", ast
.Load()), [start
, end
], [])
421 p
[0] = ast
.For(p
[2], it
, p
[8], [])
423 def p_while_stmt(self
, p
):
424 """while_stmt : DO WHILE test COLON suite ELSE COLON suite
425 | DO WHILE test COLON suite
428 p
[0] = ast
.While(p
[3], p
[5], [])
430 p
[0] = ast
.While(p
[3], p
[5], p
[8])
432 def p_switch_smt(self
, p
):
433 """switch_stmt : SWITCH LPAR atom RPAR COLON NEWLINE INDENT switches DEDENT
437 print(astor
.dump_tree(p
[1]))
440 current_cases
= [] # for deferral
441 for (case
, suite
) in p
[8]:
442 print("for", case
, suite
)
445 current_cases
.append(ast
.Num(c
))
447 if case
== 'default': # last
450 current_cases
.append(ast
.Num(c
))
451 print("cases", current_cases
)
452 compare
= ast
.Compare(switchon
, [ast
.In()],
453 [ast
.List(current_cases
, ast
.Load())])
455 cases
.append((compare
, suite
))
457 print("ended", case
, current_cases
)
458 if case
== 'default':
460 compare
= ast
.Compare(switchon
, [ast
.In()],
461 [ast
.List(current_cases
, ast
.Load())])
462 cases
.append((compare
, suite
))
463 cases
.append((None, suite
))
467 for compare
, suite
in cases
:
468 print("after rev", compare
, suite
)
470 assert len(res
) == 0, "last case should be default"
473 if not isinstance(res
, list):
475 res
= ast
.If(compare
, suite
, res
)
478 def p_switches(self
, p
):
479 """switches : switch_list switch_default
487 def p_switch_list(self
, p
):
488 """switch_list : switch_case switch_list
496 def p_switch_case(self
, p
):
497 """switch_case : CASE LPAR atomlist RPAR COLON suite
500 if isinstance(p
[6][0], ast
.Name
) and p
[6][0].id == 'fallthrough':
504 def p_switch_default(self
, p
):
505 """switch_default : DEFAULT COLON suite
507 p
[0] = ('default', p
[3])
509 def p_atomlist(self
, p
):
510 """atomlist : atom COMMA atomlist
513 assert isinstance(p
[1], ast
.Constant
), "case must be numbers"
515 p
[0] = [p
[1].value
] + p
[3]
519 def p_if_stmt(self
, p
):
520 """if_stmt : IF test COLON suite ELSE COLON if_stmt
521 | IF test COLON suite ELSE COLON suite
522 | IF test COLON suite
524 if len(p
) == 8 and isinstance(p
[7], ast
.If
):
525 p
[0] = ast
.If(p
[2], p
[4], [p
[7]])
527 p
[0] = ast
.If(p
[2], p
[4], [])
529 p
[0] = ast
.If(p
[2], p
[4], p
[7])
531 def p_suite(self
, p
):
532 """suite : simple_stmt
533 | NEWLINE INDENT stmts DEDENT"""
539 def p_stmts(self
, p
):
540 """stmts : stmts stmt
547 def p_comparison(self
, p
):
548 """comparison : comparison PLUS comparison
549 | comparison MINUS comparison
550 | comparison MULT comparison
551 | comparison DIV comparison
552 | comparison MOD comparison
553 | comparison EQ comparison
554 | comparison NE comparison
555 | comparison LE comparison
556 | comparison GE comparison
557 | comparison LTU comparison
558 | comparison GTU comparison
559 | comparison LT comparison
560 | comparison GT comparison
561 | comparison BITOR comparison
562 | comparison BITXOR comparison
563 | comparison BITAND comparison
567 | comparison APPEND comparison
572 p
[0] = ast
.Call(ast
.Name("ltu", ast
.Load()), (p
[1], p
[3]), [])
574 p
[0] = ast
.Call(ast
.Name("gtu", ast
.Load()), (p
[1], p
[3]), [])
576 l
= check_concat(p
[1]) + check_concat(p
[3])
577 p
[0] = ast
.Call(ast
.Name("concat", ast
.Load()), l
, [])
578 elif p
[2] in ['<', '>', '=', '<=', '>=', '!=']:
579 p
[0] = binary_ops
[p
[2]]((p
[1], p
[3]))
580 elif identify_sint_mul_pattern(p
):
581 keywords
= [ast
.keyword(arg
='repeat', value
=p
[3])]
583 p
[0] = ast
.Call(ast
.Name("concat", ast
.Load()), l
, keywords
)
585 p
[0] = ast
.BinOp(p
[1], binary_ops
[p
[2]], p
[3])
587 if isinstance(p
[2], str) and p
[2] == '-':
588 p
[0] = ast
.UnaryOp(unary_ops
[p
[2]], p
[1])
590 p
[0] = ast
.UnaryOp(unary_ops
[p
[1]], p
[2])
594 # power: atom trailer* ['**' factor]
595 # trailers enables function calls (and subscripts).
596 # so this is 'trailerlist'
597 def p_power(self
, p
):
599 | atom trailerlist"""
603 print("power dump atom")
604 print(astor
.dump_tree(p
[1]))
605 print("power dump trailerlist")
606 print(astor
.dump_tree(p
[2]))
607 p
[0] = apply_trailer(p
[1], p
[2])
609 def p_atom_name(self
, p
):
612 if name
in self
.available_op_fields
:
613 self
.op_fields
.add(name
)
614 if name
in ['CR', 'LR', 'CTR', 'TAR', 'FPSCR', 'MSR']:
615 self
.special_regs
.add(name
)
616 self
.write_regs
.add(name
) # and add to list to write
617 p
[0] = ast
.Name(id=name
, ctx
=ast
.Load())
619 def p_atom_number(self
, p
):
624 p
[0] = ast
.Constant(p
[1])
626 # '[' [listmaker] ']' |
628 def p_atom_listmaker(self
, p
):
629 """atom : LBRACK listmaker RBRACK"""
632 def p_listmaker(self
, p
):
633 """listmaker : test COMMA listmaker
637 p
[0] = ast
.List([p
[1]], ast
.Load())
639 p
[0] = ast
.List([p
[1]] + p
[3].nodes
, ast
.Load())
641 def p_atom_tuple(self
, p
):
642 """atom : LPAR testlist RPAR"""
645 print(astor
.dump_tree(p
[2]))
647 if isinstance(p
[2], ast
.Name
):
649 print("tuple name", name
)
650 if name
in self
.gprs
:
651 self
.read_regs
.add(name
) # add to list of regs to read
652 #p[0] = ast.Subscript(ast.Name("GPR", ast.Load()), ast.Str(p[2].id))
655 elif isinstance(p
[2], ast
.BinOp
):
656 if isinstance(p
[2].left
, ast
.Name
) and \
657 isinstance(p
[2].right
, ast
.Constant
) and \
658 p
[2].right
.value
== 0 and \
659 p
[2].left
.id in self
.gprs
:
661 self
.read_regs
.add(rid
) # add to list of regs to read
662 # create special call to GPR.getz
663 gprz
= ast
.Name("GPR", ast
.Load())
664 # get testzero function
665 gprz
= ast
.Attribute(gprz
, "getz", ast
.Load())
666 # *sigh* see class GPR. we need index itself not reg value
667 ridx
= ast
.Name("_%s" % rid
, ast
.Load())
668 p
[0] = ast
.Call(gprz
, [ridx
], [])
669 print("tree", astor
.dump_tree(p
[0]))
675 def p_trailerlist(self
, p
):
676 """trailerlist : trailer trailerlist
682 p
[0] = ("TLIST", p
[1], p
[2])
684 # trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
685 def p_trailer(self
, p
):
686 """trailer : trailer_arglist
691 def p_trailer_arglist(self
, p
):
692 "trailer_arglist : LPAR arglist RPAR"
693 p
[0] = ("CALL", p
[2])
695 def p_trailer_subscript(self
, p
):
696 "trailer_subscript : LBRACK subscript RBRACK"
697 p
[0] = ("SUBS", p
[2])
699 # subscript: '.' '.' '.' | test | [test] ':' [test]
701 def p_subscript(self
, p
):
702 """subscript : test COLON test
707 if isinstance(p
[3], ast
.Constant
):
708 end
= ast
.Constant(p
[3].value
+1)
710 end
= ast
.BinOp(p
[3], ast
.Add(), ast
.Constant(1))
715 # testlist: test (',' test)* [',']
716 # Contains shift/reduce error
718 def p_testlist(self
, p
):
719 """testlist : testlist_multi COMMA
724 # May need to promote singleton to tuple
725 if isinstance(p
[1], list):
729 # Convert into a tuple?
730 if isinstance(p
[0], list):
731 p
[0] = ast
.Tuple(p
[0])
733 def p_testlist_multi(self
, p
):
734 """testlist_multi : testlist_multi COMMA test
740 if isinstance(p
[1], list):
746 # test: or_test ['if' or_test 'else' test] | lambdef
747 # as I don't support 'and', 'or', and 'not' this works down to 'comparison'
753 # arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
755 # XXX INCOMPLETE: this doesn't allow the trailing comma
757 def p_arglist(self
, p
):
758 """arglist : arglist COMMA argument
765 # argument: test [gen_for] | test '=' test # Really [keyword '='] test
766 def p_argument(self
, p
):
770 def p_error(self
, p
):
771 # print "Error!", repr(p)
775 class GardenSnakeParser(PowerParser
):
776 def __init__(self
, lexer
=None, debug
=False, form
=None):
777 self
.sd
= create_pdecode()
778 PowerParser
.__init
__(self
, form
)
781 lexer
= IndentLexer(debug
=0)
783 self
.tokens
= lexer
.tokens
784 self
.parser
= yacc
.yacc(module
=self
, start
="file_input_end",
785 debug
=debug
, write_tables
=False)
787 def parse(self
, code
):
788 # self.lexer.input(code)
789 result
= self
.parser
.parse(code
, lexer
=self
.lexer
, debug
=self
.debug
)
790 return ast
.Module(result
)
793 ###### Code generation ######
795 #from compiler import misc, syntax, pycodegen
797 class GardenSnakeCompiler(object):
798 def __init__(self
, debug
=False, form
=None):
799 self
.parser
= GardenSnakeParser(debug
=debug
, form
=form
)
801 def compile(self
, code
, mode
="exec", filename
="<string>"):
802 tree
= self
.parser
.parse(code
)
806 #misc.set_filename(filename, tree)
807 return compile(tree
, mode
="exec", filename
="<string>")
809 gen
= pycodegen
.ModuleCodeGenerator(tree
)