]> git.mdlowis.com Git - proto/obnc.git/commitdiff
initial record pass using symbols. Added a field type, should convert to using that...
authorMichael D. Lowis <mike.lowis@gentex.com>
Fri, 14 May 2021 20:23:03 +0000 (16:23 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Fri, 14 May 2021 20:23:03 +0000 (16:23 -0400)
cerise/backend/c99/codegen.c
cerise/inc/cerise.h
cerise/src/grammar.c
cerise/src/sym.c

index 9a1f73812382059a2322790893961042c199f64c..1db17e90b75e5c3d8df640a0b1df245380a1d2b4 100644 (file)
@@ -23,7 +23,7 @@ Type StringType = {
 };
 
 static char* TypeNames[FORM_COUNT] = {
-    [FORM_BOOL]   = "bool",
+    [FORM_BOOL]   = "_Bool",
     [FORM_INT]    = "long",
     [FORM_REAL]   = "double",
     [FORM_STRING] = "char*"
index 56891e7478a3d502aafa3b756e4aa750947ba1c7..f550ecb53a1bb6fbdb7c4febeaf91bf0c11ea91a 100644 (file)
@@ -79,6 +79,13 @@ typedef struct LexFile {
 
 struct Symbol;
 
+typedef struct Field {
+    struct Field* next;
+    char* name;
+    Type* type;
+    long offset;
+} Field;
+
 typedef struct Type {
     enum {
         FORM_BOOL, FORM_INT, FORM_REAL, FORM_ARRAY, FORM_STRING,
@@ -99,7 +106,7 @@ typedef union {
 typedef struct Symbol {
     struct Symbol* next;
     enum{
-        SYM_SCOPE, SYM_CONST, SYM_VAR, SYM_TYPE, SYM_PROC
+        SYM_SCOPE, SYM_CONST, SYM_VAR, SYM_TYPE, SYM_PROC, SYM_FIELD
     } class;
     char* name;
     Type* type;
index 3372ae4df80c2bfbb066f61c9fab0a1fd5abf277..811b8d29020c12dd28f281fbbecd1eadcc02e512 100644 (file)
@@ -347,35 +347,33 @@ RULE(type)
     {
         item->type = calloc(1, sizeof(Type));
         item->type->form = FORM_RECORD;
-//        Symbol **fields = &(item->type->fields);
-//        while (peek(p)->type != END)
-//        {
-//            do
-//            {
-//                char* name = expect_text(p, IDENT);
-//                bool export = accept(p, '*');
-//                *prev = symbol_new(p, name, SYM_VAR, export);
-//                nsyms++;
-//            }
-//            while (accept(p, ','));
-//
-//
-////            int nsyms = 0;
-////            Symbol head = {0};
-////            Symbol **prev = &(head.next);
-////
-////            Item base_type = {0};
-////            expect(p, ':');
-////            type(p, &base_type);
-//
-////            /* apply the type to the newly created symbols */
-////            for (int i = 0; i < nsyms; i++)
-////            {
-////                first->type = base_type.type;
-////                codegen_var(p, first);
-////                sym = first->next;
-////            }
-//        }
+        Symbol head = {0};
+        int nfields = 0;
+
+        while (peek(p)->type != END)
+        {
+            do
+            {
+                char* name = expect_text(p, IDENT);
+                bool export = accept(p, '*');
+                (void)symbol_addfield(p, &head, name, SYM_FIELD, export);
+                nfields++;
+            }
+            while (accept(p, ','));
+
+            Item field_type = {0};
+            expect(p, ':');
+            type(p, &field_type);
+
+            /* apply the type to the newly created symbols */
+            Symbol* field = head.desc;
+            for (int i = 0; i < nfields; i++)
+            {
+                field->type = field_type.type;
+                field = field->next;
+            }
+            item->type->fields = head.desc;
+        }
 
         expect(p, END);
     }
@@ -555,6 +553,7 @@ RULE(proc_decl)
         var_decl(p, item);
     }
 
+//    /* TODO: Support nested procedures */
 //    while (matches(p, PROCEDURE))
 //    {
 //        proc_decl(p, item);
index 6f1e15cf6a4d69429aa61032c5183577bc8ed1e0..97a021aa58132e0f66a15afc9bebdd9b3242ec33 100644 (file)
@@ -36,6 +36,29 @@ static int sym_matches(Parser* p, int class, char* name, Symbol* sym)
     return 0;
 }
 
+static Symbol* lookup(Parser* p, int class, char* name)
+{
+    for (Symbol* scope = p->scope; scope; scope = scope->next)
+    {
+//       printf("SCOPE(n: %p):\n", scope->next);
+        if (sym_matches(p, class, name, scope))
+        {
+            return scope;
+        }
+
+        for (Symbol* sym = scope->desc; sym; sym = sym->next)
+        {
+//            printf("    %s == %s %d\n", name, sym->name, sym_matches(p, class, name, scope));
+            if (sym_matches(p, class, name, sym))
+            {
+                return sym;
+            }
+        }
+    }
+    return NULL;
+}
+
+
 Symbol* symbol_new(Parser* p, char* name, int class, bool export)
 {
     Symbol* prev = NULL;
@@ -53,6 +76,13 @@ Symbol* symbol_new(Parser* p, char* name, int class, bool export)
         curr = curr->next;
     }
 
+//    Symbol* existing = lookup(p, SYM_VAR, name);
+//    printf("%s : %p\n", name, existing);
+//    if ((class == SYM_VAR) && existing && !existing->global)
+//    {
+//        error(p, "local definition '%s' makes procedure argument inaccessible", name);
+//    }
+
     Symbol* sym = calloc(1, sizeof(Symbol));
     sym->name = name;
     sym->class = class;
@@ -102,25 +132,12 @@ Symbol* symbol_addfield(Parser* p, Symbol* parent, char* name, int class, bool e
 
 Symbol* symbol_get(Parser* p, int class, char* name)
 {
-    for (Symbol* scope = p->scope; scope; scope = scope->next)
+    Symbol* sym = lookup(p, class, name);
+    if (!sym)
     {
-//       printf("SCOPE(n: %p):\n", scope->next);
-        if (sym_matches(p, class, name, scope))
-        {
-            return scope;
-        }
-
-        for (Symbol* sym = scope->desc; sym; sym = sym->next)
-        {
-//            printf("    %s == %s %d\n", name, sym->name, sym_matches(p, class, name, scope));
-            if (sym_matches(p, class, name, sym))
-            {
-                return sym;
-            }
-        }
+        error(p, "unknown identifier '%s'", name);
     }
-    error(p, "unknown identifier '%s'", name);
-    return NULL;
+    return sym;
 }
 
 void symbol_openscope(Parser* p)