# Helper function
-def Assign(left, right):
+def Assign(left, right, iea_mode):
names = []
print("Assign", left, right)
if isinstance(left, ast.Name):
binary_ops = {
+ "^": ast.BitXor(),
"&": ast.BitAnd(),
"|": ast.BitOr(),
"+": ast.Add(),
precedence = (
("left", "EQ", "GT", "LT", "LE", "GE", "LTU", "GTU"),
("left", "BITOR"),
+ ("left", "BITXOR"),
("left", "BITAND"),
("left", "PLUS", "MINUS"),
("left", "MULT", "DIV", "MOD"),
# augassign: ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' |
# '<<=' | '>>=' | '**=' | '//=')
def p_expr_stmt(self, p):
- """expr_stmt : testlist ASSIGN testlist
+ """expr_stmt : testlist ASSIGNEA testlist
+ | testlist ASSIGN testlist
| testlist """
print("expr_stmt", p)
if len(p) == 2:
#p[0] = ast.Discard(p[1])
p[0] = p[1]
else:
+ iea_mode = p[2] == '<-iea'
name = None
if isinstance(p[1], ast.Name):
name = p[1].id
print("expr assign", name, p[1])
if name and name in self.gprs:
self.write_regs.add(name) # add to list of regs to write
- p[0] = Assign(p[1], p[3])
+ p[0] = Assign(p[1], p[3], iea_mode)
def p_flow_stmt(self, p):
"flow_stmt : return_stmt"
p[0] = ast.Break()
def p_for_stmt(self, p):
- """for_stmt : FOR test EQ test TO test COLON suite
+ """for_stmt : FOR atom EQ test TO test COLON suite
+ | DO atom EQ test TO test COLON suite
"""
- p[0] = ast.While(p[2], p[4], [])
# auto-add-one (sigh) due to python range
start = p[4]
end = ast.BinOp(p[6], ast.Add(), ast.Constant(1))
| comparison LT comparison
| comparison GT comparison
| comparison BITOR comparison
+ | comparison BITXOR comparison
| comparison BITAND comparison
| PLUS comparison
| comparison MINUS
class GardenSnakeParser(PowerParser):
- def __init__(self, lexer=None):
+ def __init__(self, lexer=None, debug=False):
PowerParser.__init__(self)
+ self.debug = debug
if lexer is None:
lexer = IndentLexer(debug=0)
self.lexer = lexer
self.tokens = lexer.tokens
self.parser = yacc.yacc(module=self, start="file_input_end",
- debug=False, write_tables=False)
+ debug=debug, write_tables=False)
self.sd = create_pdecode()
def parse(self, code):
# self.lexer.input(code)
- result = self.parser.parse(code, lexer=self.lexer, debug=False)
+ result = self.parser.parse(code, lexer=self.lexer, debug=self.debug)
return ast.Module(result)
#from compiler import misc, syntax, pycodegen
class GardenSnakeCompiler(object):
- def __init__(self):
- self.parser = GardenSnakeParser()
+ def __init__(self, debug=False):
+ self.parser = GardenSnakeParser(debug=debug)
def compile(self, code, mode="exec", filename="<string>"):
tree = self.parser.parse(code)