]> git.mdlowis.com Git - proto/obnc.git/commitdiff
added type cache output to symbol file
authorMichael D. Lowis <mike@mdlowis.com>
Fri, 16 Jul 2021 02:46:13 +0000 (22:46 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Fri, 16 Jul 2021 02:46:13 +0000 (22:46 -0400)
cerise/inc/cerise.h
cerise/src/grammar.c
cerise/src/sym.c

index 0b656567ae5c56ab7f8d258577df9e981f952140..08f147471916a9529152b66196aee8e3c0069539 100644 (file)
@@ -131,10 +131,15 @@ typedef struct {
     Tok tok;
     char* name;
     long curr_reg;
+    size_t scope;
+
     size_t msyms;
     size_t nsyms;
     Symbol* syms;
-    size_t scope;
+
+    size_t mtypes;
+    size_t ntypes;
+    Type** types;
 } Parser;
 
 // src/stdlib.c
@@ -158,6 +163,7 @@ char* expect_text(Parser* p, TokType type);
 int consume(Parser* p);
 
 // src/sym.c
+Type* symbol_newtype(Parser* p);
 Symbol* symbol_new(Parser* p, size_t scope, char* name, int class, bool export);
 Symbol* symbol_get(Parser* p, size_t module, char* name, int class);
 size_t symbol_getid(Parser* p, size_t module, char* name, int class);
index 071fee523daf93060f905bd2547a6f9a0b7ce817..b5dbffab5a3cbbd677def33bcc978588b5d5766e 100644 (file)
@@ -338,7 +338,8 @@ static Type* type(Parser* p)
         long long length = ast_asint(lnode);
         expect(p, OF);
         Type* base = type(p);
-        ret = calloc(1, sizeof(Type));
+//        ret = calloc(1, sizeof(Type));
+        ret = symbol_newtype(p);
         ret->form = FORM_ARRAY;
         ret->size = length;
         ret->base = base;
@@ -346,7 +347,8 @@ static Type* type(Parser* p)
     else if (accept(p, RECORD))
     {
         long offset = 0;
-        ret = calloc(1, sizeof(Type));
+//        ret = calloc(1, sizeof(Type));
+        ret = symbol_newtype(p);
         ret->form = FORM_RECORD;
 
         while (peek(p)->type != END)
@@ -483,6 +485,7 @@ static void const_decl(Parser* p)
         sym = symbol_new(p, 0, name, SYM_CONST, export);
         expect(p, '=');
         sym->value = expression(p);
+        sym->type = sym->value->hdr.type;
     }
     while (matches(p, IDENT));
 
@@ -497,7 +500,8 @@ void proc_decl(Parser* p)
     char* name = expect_text(p, IDENT);
     bool export = accept(p, '*');
     Symbol* proc = symbol_new(p, 0, name, SYM_PROC, export);
-    Type* proctype = calloc(1, sizeof(Type));
+//    Type* proctype = calloc(1, sizeof(Type));
+    Type* proctype = symbol_newtype(p);
     proctype->form = FORM_PROC;
     proc->type = proctype;
 
@@ -554,7 +558,8 @@ void proc_decl(Parser* p)
     expect(p, END);
     symbol_closescope(p, scope);
 
-    ast_print(p, proc->value);
+//    ast_print(p, proc->value);
+    (void)proc->value;
 
     EXIT_RULE();
 }
@@ -617,7 +622,8 @@ static void module(Parser* p)
     if (accept(p, BEGIN))
     {
         AstNode* block = statement_seq(p);
-        ast_print(p, block);
+//        ast_print(p, block);
+        (void)block;
         expect(p, END);
     }
 
@@ -664,10 +670,16 @@ void compile(char* fname)
         .curr_reg = 0
     };
 
+    p->mtypes = 8;
+    p->types = calloc(p->mtypes, sizeof(Type*));
     symbol_new(p, 0, "Bool",   SYM_TYPE, 0)->type = &BoolType;
+    p->types[p->ntypes++] = &BoolType;
     symbol_new(p, 0, "Int",    SYM_TYPE, 0)->type = &IntType;
+    p->types[p->ntypes++] = &IntType;
     symbol_new(p, 0, "Real",   SYM_TYPE, 0)->type = &RealType;
+    p->types[p->ntypes++] = &RealType;
     symbol_new(p, 0, "String", SYM_TYPE, 0)->type = &StringType;
+    p->types[p->ntypes++] = &StringType;
     module(p);
 }
 
index 77b6c59c1d410095c8f6d346778af2ed152d2b0b..687f4067802e4c56d6763c7665fa1330abd2c995 100644 (file)
@@ -59,6 +59,22 @@ static size_t lookup(Parser* p, size_t scope, size_t module, char* name, int cla
     return ret;
 }
 
+Type* symbol_newtype(Parser* p)
+{
+    Type* type = calloc(1, sizeof(Type));
+
+    if (p->ntypes == p->mtypes)
+    {
+        p->mtypes = (p->mtypes ? (p->mtypes << 1) : 512u);
+        p->types  = realloc(p->types, p->mtypes * sizeof(Type*));
+    }
+
+    p->types[p->ntypes] = type;
+    p->ntypes++;
+
+    return type;
+}
+
 Symbol* symbol_new(Parser* p, size_t scope, char* name, int class, bool export)
 {
     if (p->nsyms == p->msyms)
@@ -129,6 +145,17 @@ void symbol_closescope(Parser* p, size_t scope)
 /* Symbol File Generation
  *****************************************************************************/
 
+static const char TypeIdents[FORM_COUNT] = {
+    [FORM_BOOL]   = 'b',
+    [FORM_INT]    = 'i',
+    [FORM_REAL]   = 'r',
+    [FORM_ARRAY]  = 'a',
+    [FORM_STRING] = 's',
+    [FORM_RECORD] = 'r',
+    [FORM_PROC]   = 'p',
+    [FORM_VOID]   = 'v',
+};
+
 static const char SymTypes[5] = {
     [SYM_MODULE] = 'M',
     [SYM_CONST]  = 'C',
@@ -137,14 +164,33 @@ static const char SymTypes[5] = {
     [SYM_PROC]   = 'P',
 };
 
+static size_t typeid(Parser* p, Type* type)
+{
+    size_t i;
+    for (i = 0; i < p->ntypes; i++)
+    {
+        if (p->types[i] == type)
+            break;
+    }
+    assert(i < p->ntypes);
+    return i;
+}
+
 void symbol_export(Parser* p, char* path)
 {
     (void)path;
+    for (size_t i = 0; i < p->ntypes; i++)
+    {
+        printf("t %d %c\n", i, TypeIdents[p->types[i]->form]);
+    }
+
     for (size_t i = 0; i < p->nsyms; i++)
     {
         Symbol* sym = &(p->syms[i]);
         if (!sym->export) continue;
-        printf("%c %s\n", SymTypes[sym->class], sym->name);
+        printf("%c %s ", SymTypes[sym->class], sym->name);
+        fflush(stdout);
+        printf("%d\n", typeid(p, sym->type));
     }
 }