From 990d2e93d339d9138a7f6a96910e2ed89ed960c8 Mon Sep 17 00:00:00 2001 From: Luke Kenneth Casson Leighton Date: Sun, 29 Mar 2020 12:34:02 +0100 Subject: [PATCH] add while statement to GardenSnake. cute! --- src/soc/decoder/GardenSnake.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) 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)) """ -- 2.30.2