From: Luke Kenneth Casson Leighton Date: Sun, 29 Mar 2020 11:34:02 +0000 (+0100) Subject: add while statement to GardenSnake. cute! X-Git-Tag: div_pipeline~1615 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=990d2e93d339d9138a7f6a96910e2ed89ed960c8;p=soc.git add while statement to GardenSnake. cute! --- diff --git a/src/soc/decoder/GardenSnake.py b/src/soc/decoder/GardenSnake.py index 7956359f..a47d8200 100644 --- a/src/soc/decoder/GardenSnake.py +++ b/src/soc/decoder/GardenSnake.py @@ -49,6 +49,7 @@ tokens = ( 'DEF', 'IF', 'ELSE', + 'WHILE', 'NAME', 'NUMBER', # Python decimals 'STRING', # single quoted strings only; syntax of raw strings @@ -103,6 +104,7 @@ RESERVED = { "def": "DEF", "if": "IF", "else": "ELSE", + "while": "WHILE", "return": "RETURN", } @@ -479,9 +481,20 @@ def p_return_stmt(p): def p_compound_stmt(p): """compound_stmt : if_stmt - | funcdef""" + | while_stmt + | funcdef + """ p[0] = p[1] +def p_while_stmt(p): + """while_stmt : WHILE test COLON suite ELSE COLON suite + | WHILE test COLON suite + """ + if len(p) == 5: + p[0] = ast.While(p[2], p[4], None) + else: + p[0] = ast.While(p[2], p[4], p[7]) + def p_if_stmt(p): """if_stmt : IF test COLON suite ELSE COLON suite | IF test COLON suite @@ -752,6 +765,13 @@ def x(a): return a +count = 0 +while count < 5: + count = count + 1 + print (count) +else: + print ('done', count) + print (x(1)) print (x(2)) """