Make fields be a list of ref rather than a list of type, name pairs
authorNick Downing <nick@ndcode.org>
Sat, 26 Jan 2019 23:08:23 +0000 (10:08 +1100)
committerNick Downing <nick@ndcode.org>
Sat, 26 Jan 2019 23:08:23 +0000 (10:08 +1100)
ast.py

diff --git a/ast.py b/ast.py
index 1bdf4e5..953162a 100644 (file)
--- a/ast.py
+++ b/ast.py
@@ -859,10 +859,10 @@ class AST(element.Element):
               [
                 ',\n{0:s}  {1:s} = {2:s}'.format(
                   context.indent,
-                  name,
-                  default_value[type]
+                  i[1].get_text(),
+                  default_value[element.to_text(i[0])]
                 )
-                for type, name in context.fields
+                for i in context.fields
               ]
             ),
             context.indent,
@@ -877,15 +877,17 @@ class AST(element.Element):
               [
                 ',\n{0:s}    {1:s}'.format(
                   context.indent,
-                  name
+                  i[1].get_text()
                 )
-                for type, name in context.fields[:n_base_fields]
+                for i in context.fields[:n_base_fields]
               ]
             ),
             context.indent
           )
         )
-        for type, name in context.fields[n_base_fields:]:
+        for i in context.fields[n_base_fields:]:
+          type = element.to_text(i[0])
+          name = i[1].get_text()
           if type == 'ref' or type == 'list(ref)' or type == 'set(ref)' or type == 'str':
             context.fout.write(
               '''{0:s}  self.{1:s} = {2:s}
@@ -959,7 +961,9 @@ class AST(element.Element):
 {1:s}  {2:s}.serialize(self, ref_list)
 '''.format(context.indent, context.indent, full_base_class)
           )
-          for type, name in context.fields[n_base_fields:]:
+          for i in context.fields[n_base_fields:]:
+            type = element.to_text(i[0])
+            name = i[1].get_text()
             if type[:5] == 'list(' and type[-1:] == ')':
               subtype = type[5:-1]
               context.fout.write(
@@ -1012,7 +1016,9 @@ class AST(element.Element):
 {1:s}  {2:s}.deserialize(self, ref_list)
 '''.format(context.indent, context.indent, full_base_class)
           )
-          for type, name in context.fields[n_base_fields:]:
+          for i in context.fields[n_base_fields:]:
+            type = element.to_text(i[0])
+            name = i[1].get_text()
             if type[:5] == 'list(' and type[-1:] == ')':
               subtype = type[5:-1]
               context.fout.write(
@@ -1084,10 +1090,10 @@ class AST(element.Element):
               [
                 '\n{0:s}  result.{1:s} = self.{2:s}'.format(
                   context.indent,
-                  name,
-                  name
+                  i[1].get_text(),
+                  i[1].get_text()
                 )
-                for _, name in context.fields[n_base_fields:]
+                for i in context.fields[n_base_fields:]
               ]
             ),
             context.indent
@@ -1103,7 +1109,9 @@ class AST(element.Element):
               full_base_class
             )
           )
-          for type, name in context.fields[n_base_fields:]:
+          for i in context.fields[n_base_fields:]:
+            type = element.to_text(i[0])
+            name = i[1].get_text()
             if type[:5] == 'list(' and type[-1:] == ')':
               subtype = type[5:-1]
               context.fout.write(
@@ -1214,7 +1222,7 @@ class AST(element.Element):
         return 'ast.AST.Section2.FieldDef({0:s})'.format(', '.join(params))
       # GENERATE END
       def generate_class_or_field_def(self, context):
-        context.fields.append((element.to_text(self[0]), self[1].get_text()))
+        context.fields.append(self)
     # GENERATE ELEMENT() BEGIN
     def __init__(
       self,