add while statement to GardenSnake. cute!
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Sun, 29 Mar 2020 11:34:02 +0000 (12:34 +0100)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Sun, 29 Mar 2020 11:34:02 +0000 (12:34 +0100)
src/soc/decoder/GardenSnake.py

index 7956359f7b705458470fd36619e88494fd859948..a47d8200b4904b62dbe4de7456fa4d855be233ab 100644 (file)
@@ -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))
 """