]> git.mdlowis.com Git - proto/obnc.git/commitdiff
re-enabled variable and type declarations
authorMichael D. Lowis <mike.lowis@gentex.com>
Mon, 12 Jul 2021 17:54:51 +0000 (13:54 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Mon, 12 Jul 2021 17:54:51 +0000 (13:54 -0400)
cerise/src/grammar.c
cerise/tests/Module.m

index 2070d3b2ac563dc18d3961f646d4efd0a5e2bd5d..afcb070d4499bc46b68ffb202e6ced9df8451d01 100644 (file)
@@ -326,8 +326,7 @@ static Type* type(Parser* p)
     if (matches(p, IDENT))
     {
         char* text = expect_text(p, IDENT);
-        Symbol* sym = symbol_get(p, text, SYM_TYPE);
-        ret = sym->type;
+        ret = symbol_get(p, text, SYM_TYPE)->type;
     }
     else if (accept(p, ARRAY))
     {
@@ -344,49 +343,48 @@ static Type* type(Parser* p)
         ret->size = length;
         ret->base = base;
     }
-//    else if (accept(p, RECORD))
-//    {
-//        long offset = 0;
-//        item->type = calloc(1, sizeof(Type));
-//        item->type->form = FORM_RECORD;
-//
-//        while (peek(p)->type != END)
-//        {
-//            /* parse the field name list */
-//            Field* first = NULL;
-//            int nfields = 0;
-//            do
-//            {
-//                char* name = expect_text(p, IDENT);
-//                bool export = accept(p, '*');
-//                Field* f = add_field(p, item->type, name, export);
-//                nfields++;
-//                if (!first)
-//                {
-//                    first = f;
-//                }
-//            }
-//            while (accept(p, ','));
-//
-//            /* now parse the type designation */
-//            Item field_type = {0};
-//            expect(p, ':');
-//            type(p, &field_type);
-//
-//            /* apply the type to the newly created symbols */
-//            for (int i = 0; i < nfields; i++)
-//            {
-//                offset = align_item(offset, field_type.type->size);
-//                first->type = field_type.type;
-//                first->offset = offset;
-//                offset += size_of(first->type);
-//                first = first->next;
-//            }
-//        }
-//        item->type->size = offset;
-//
-//        expect(p, END);
-//    }
+    else if (accept(p, RECORD))
+    {
+        long offset = 0;
+        ret = calloc(1, sizeof(Type));
+        ret->form = FORM_RECORD;
+
+        while (peek(p)->type != END)
+        {
+            /* parse the field name list */
+            Field* first = NULL;
+            int nfields = 0;
+            do
+            {
+                char* name = expect_text(p, IDENT);
+                bool export = accept(p, '*');
+                Field* f = add_field(p, ret, name, export);
+                nfields++;
+                if (!first)
+                {
+                    first = f;
+                }
+            }
+            while (accept(p, ','));
+
+            /* now parse the type designation */
+            expect(p, ':');
+            Type* field_type = type(p);
+
+            /* apply the type to the newly created symbols */
+            for (int i = 0; i < nfields; i++)
+            {
+                offset = align_item(offset, field_type->size);
+                first->type = field_type;
+                first->offset = offset;
+                offset += size_of(first->type);
+                first = first->next;
+            }
+        }
+        ret->size = offset;
+
+        expect(p, END);
+    }
     else
     {
         error(p, "expected a type");
@@ -444,23 +442,20 @@ static Type* type(Parser* p)
 //    EXIT_RULE();
 //}
 
-//RULE(var_decl, Item* item)
-//{
-//    ENTER_RULE();
-//    (void)item;
-//    do
-//    {
-//        char* name = expect_text(p, IDENT);
-//        bool export = accept(p, '*');
-//        Symbol* sym = symbol_new(p, 0, name, SYM_VAR, export);
-//        Item base_type = {0};
-//        expect(p, ':');
-//        type(p, &base_type);
-//        sym->type = base_type.type;
-//    }
-//    while (matches(p, IDENT));
-//    EXIT_RULE();
-//}
+static void var_decl(Parser* p)
+{
+    ENTER_RULE();
+    do
+    {
+        char* name = expect_text(p, IDENT);
+        bool export = accept(p, '*');
+        Symbol* sym = symbol_new(p, 0, name, SYM_VAR, export);
+        expect(p, ':');
+        sym->type = type(p);
+    }
+    while (matches(p, IDENT));
+    EXIT_RULE();
+}
 
 static void type_decl(Parser* p)
 {
@@ -633,10 +628,10 @@ static void module(Parser* p)
         type_decl(p);
     }
 
-//    if (accept(p, VAR))
-//    {
-//        var_decl(p);
-//    }
+    if (accept(p, VAR))
+    {
+        var_decl(p);
+    }
 
 //    while (matches(p, PROCEDURE))
 //    {
index c934edde6258eb5a5604789c510bcb1d492b219e..7904aee9136c6cab975c4db5d930c130a9e522a0 100644 (file)
@@ -18,28 +18,28 @@ type
   TypeA = Int
   TypeB = array 5*B of Int
   TypeC = array 5 of array 10 of Int
-#  TypeD = record
-#    x,y : Int
-#    label : array 10 of Int
-#    dim : record
-#      w,h : Int
-#    end
-#  end
-#  TypeE = record
-#    i : Int
-#    a : array 5 of Int
-#  end
-#  TypeF = array 5 of TypeE
+  TypeD = record
+    x,y : Int
+    label : array 10 of Int
+    dim : record
+      w,h : Int
+    end
+  end
+  TypeE = record
+    i : Int
+    a : array 5 of Int
+  end
+  TypeF = array 5 of TypeE
 
-#var
-#  a : Bool
-#  b : Int
-#  c : Int
-#  d : Real
-#  e : array 5 of array 10 of Int
-#  f : TypeD
-#  g : array 5 of Int
-#  h : TypeF
+var
+  a : Bool
+  b : Int
+  c : Int
+  d : Real
+  e : array 5 of array 10 of Int
+  f : TypeD
+  g : array 5 of Int
+  h : TypeF
 
 #procedure Foo*(e : Int, z : Int, q1 : TypeD, q2 : array 5 of Int) : Int
 #  const FOO = 2