parse_type()
authorBen Pfaff <blp@cs.stanford.edu>
Tue, 14 Dec 2021 06:25:18 +0000 (22:25 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Tue, 14 Dec 2021 06:25:18 +0000 (22:25 -0800)
src/language/expressions/generate.py

index 4d42c112cff78fd5b6228912a3b97555efa1903c..7d375e0eab93a9f1cdebe395aefb4c083857c7f8 100644 (file)
@@ -157,6 +157,18 @@ class Type:
         new.fixed_value = fixed_value
         return new
 
+    def parse():
+        """If the current token is an identifier that names a type, returns
+        the type and skips to the next token.  Otherwise, returns
+        None.
+        """
+        if toktype == 'id':
+            for type_ in types.values():
+                if type_.name == token:
+                    get_token()
+                    return type_
+        return None
+
 def init_type(type_):
     global types
     types[type_.name] = type_
@@ -278,7 +290,7 @@ def parse_input():
             else:
                 break
 
-        return_type = parse_type()
+        return_type = Type.parse()
         if return_type is None:
             return_type = types['number']
         if return_type.name not in ['number', 'string', 'boolean']:
@@ -329,7 +341,7 @@ def parse_input():
 
         aux = []
         while toktype == 'id':
-            type_ = parse_type()
+            type_ = Type.parse()
             if type_ is None:
                 sys.stderr.write('parse error\n')
                 sys.exit(1)
@@ -517,18 +529,6 @@ def get_line():
         if comment_ofs >= 0:
             line = line[:comment_ofs]
 
-def parse_type():
-    """If the current token is an identifier that names a type, returns
-    the type and skips to the next token.  Otherwise, returns
-    undef.
-    """
-    if toktype == 'id':
-        for type_ in types.values():
-            if type_.name == token:
-                get_token()
-                return type_
-    return None
-
 def force(type_):
     """Makes sure that 'toktype' equals 'type', reads the next token, and
     returns the previous 'token'.
@@ -568,7 +568,7 @@ class Arg:
 
 def parse_arg():
     """Parses and returns a function argument."""
-    type_ = parse_type()
+    type_ = Type.parse()
     if type_ is None:
         type_ = types['number']