From: Luke Kenneth Casson Leighton Date: Sat, 4 Apr 2020 20:28:55 +0000 (+0100) Subject: add support for syntax "do i = N to N" equivalent to for X-Git-Tag: div_pipeline~1517 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=df74adc175e8d257852ca078e854a9bac8eff618;p=soc.git add support for syntax "do i = N to N" equivalent to for also fix bug where "for i = N" was getting early-reduced to "for {comparison}" because "=" is also used as a comparator --- diff --git a/src/soc/decoder/power_pseudo.py b/src/soc/decoder/power_pseudo.py index 37a5d72b..3ac60c14 100644 --- a/src/soc/decoder/power_pseudo.py +++ b/src/soc/decoder/power_pseudo.py @@ -135,12 +135,12 @@ MEM(EA, 4) <- GPR(r)[32:63] """ testdo = r""" -for i = 0 to 7 +do i = 0 to 7 print(i) """ code = testdo -code = _bpermd +#code = _bpermd #code = testmul #code = testgetzero #code = testcat @@ -180,7 +180,7 @@ def convert_to_python(pcode): def test(): - gsc = GardenSnakeCompiler() + gsc = GardenSnakeCompiler(debug=True) gsc.regfile = {} for i in range(32): diff --git a/src/soc/decoder/pseudo/lexer.py b/src/soc/decoder/pseudo/lexer.py index 3638a7fe..3d398817 100644 --- a/src/soc/decoder/pseudo/lexer.py +++ b/src/soc/decoder/pseudo/lexer.py @@ -47,7 +47,7 @@ def python_colonify(lexer, tokens): token = copy(token) token.type = "COLON" yield token - elif token.type in ['WHILE', 'FOR']: + elif token.type in ['DO', 'WHILE', 'FOR']: forwhile_seen = True yield token elif token.type == 'NEWLINE': diff --git a/src/soc/decoder/pseudo/parser.py b/src/soc/decoder/pseudo/parser.py index c7db0cbd..8613d2a4 100644 --- a/src/soc/decoder/pseudo/parser.py +++ b/src/soc/decoder/pseudo/parser.py @@ -381,7 +381,8 @@ class PowerParser: 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 """ # auto-add-one (sigh) due to python range start = p[4] @@ -645,20 +646,21 @@ class PowerParser: 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) @@ -667,8 +669,8 @@ class GardenSnakeParser(PowerParser): #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=""): tree = self.parser.parse(code)