]> git.mdlowis.com Git - proto/obnc.git/commitdiff
added ability to parse without generating code
authorMichael D. Lowis <mike.lowis@gentex.com>
Thu, 1 Dec 2022 21:13:47 +0000 (16:13 -0500)
committerMichael D. Lowis <mike.lowis@gentex.com>
Thu, 1 Dec 2022 21:13:47 +0000 (16:13 -0500)
cerise/backend/ssa/codegen.c
cerise/inc/cerise.h
cerise/src/grammar.c
cerise/src/sym.c
cerise/tests/Module.m

index a6de7c548deccb74c73f133748758620490a46af..5f414cc94601ecc2b2ed33d512de3cf7eac65d3c 100644 (file)
@@ -105,7 +105,7 @@ void emit_type(Type* type)
 
 void codegen_init(Parser* p)
 {
-    (void)p;
+    p->codegen = true;
     printf("%%Bool = type i1\n");
     printf("%%Int = type i64\n");
     printf("%%Real = type double\n");
@@ -114,7 +114,8 @@ void codegen_init(Parser* p)
 
 void codegen_symbol(Parser* p, Symbol* sym)
 {
-    (void)p;
+    if (!p->codegen) return; // Bail if just importing symbols
+
     if (sym->class == SYM_VAR)
     {
         char* modname = (sym->module == 0
@@ -474,6 +475,8 @@ fflush(stdout);
 
 void codegen_block(Parser* p, SsaBlock* block)
 {
+    if (!p->codegen) return; // Bail if just importing symbols
+
     /* perform a topological sort of the nodes */
     SsaBlock* sorted = NULL;
     Bitset* set = bitset_new(p->blockid);
index 952740fd50ea669dccdd868339f3c07be3cc8537..a6b6accc4a2d63ea2acbbed135e184529218c97f 100644 (file)
@@ -229,6 +229,8 @@ typedef struct {
     size_t mtypes;
     size_t ntypes;
     Type** types;
+
+    bool codegen;
 } Parser;
 
 // src/stdlib.c
@@ -259,8 +261,6 @@ size_t symbol_getid(Parser* p, size_t module, char* name, int class);
 Symbol* symbol_getbyid(Parser* p, size_t id);
 size_t symbol_openscope(Parser* p);
 void symbol_closescope(Parser* p, size_t scope);
-void symbol_import(Parser* p, char* name, char* alias);
-void symbol_export(Parser* p, char* path);
 
 // src/type_checks.c
 void check_int(Parser* p, SsaNode* a);
index f6e05463a5453b17b7608b7dfbdee47262e4d434..96f6bd97deb76c82fe6e8a55bec79f6299398436 100644 (file)
@@ -613,7 +613,8 @@ static void import_list(Parser* p)
             alias = name;
             name = expect_text(p, IDENT);
         }
-        symbol_import(p, name, alias);
+        (void)name, (void)alias;
+//        symbol_import(p, name, alias);
     }
     while (matches(p, IDENT));
 
@@ -658,36 +659,34 @@ static void module(Parser* p)
     sym->type = proctype;
     codegen_symbol(p, sym);
 
+    SsaBlock* block = ssa_block(p);
+    p->curr_join = ssa_block(p);
+    block->links[1] = p->curr_join;
+
     if (accept(p, BEGIN))
     {
-        SsaBlock* block = ssa_block(p);
-        p->curr_join = ssa_block(p);
-        block->links[1] = p->curr_join;
-
         if (!matches(p, END))
         {
-            SsaBlock* seqblock = statement_seq(p);
-            block->links[0] = seqblock;
-            ssa_join(p);
-            codegen_block(p, block);
+            block->links[0] = statement_seq(p);
         }
         else
         {
-            printf("{\n    ret void\n}\n");
+//            block->links[0] = ssa_block(p);
+//            ssa_return(p, NULL);
         }
         expect(p, END);
     }
     else
     {
-        printf("{\n    ret void\n}\n");
     }
+    ssa_join(p);
+    codegen_block(p, block);
 
     if (!matches(p, END_FILE))
     {
         error(p, "expected end of file");
     }
 
-    symbol_export(p, NULL);
     symbol_closescope(p, scope);
     EXIT_RULE();
 }
@@ -712,11 +711,30 @@ static inline char* file_load(char* path)
     return contents;
 }
 
+static char* get_module_name(char* path)
+{
+    char* name = NULL;
+    char* last_slash = strrchr(path, '/');
+    if (last_slash)
+    {
+        char* last_dot = strrchr(last_slash, '.');
+        if (last_dot)
+        {
+            name = strndup(last_slash+1, last_dot-last_slash-1);
+            if (*name == '\0')
+            {
+                name = NULL;
+            }
+        }
+    }
+    return name;
+}
+
 void compile(char* fname)
 {
     char* fcontents = file_load(fname);
-    Parser* p = &(Parser){
-        .name  = NULL,
+    Parser p = {
+        .name  = get_module_name(fname),
         .file  = &(LexFile){
             .path  = fname,
             .fbeg  = fcontents,
@@ -725,18 +743,23 @@ void compile(char* fname)
         .curr_reg = 0
     };
 
-    p->mtypes = 8;
-    p->types = calloc(p->mtypes, sizeof(Type*));
-    symbol_new(p, 0, "$",  SYM_VAR, 0);
-    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;
-
-    codegen_init(p);
-    module(p);
+    if (p.name == NULL)
+    {
+        error(&p, "could not determine module name from path");
+    }
+
+    p.mtypes = 8;
+    p.types = calloc(p.mtypes, sizeof(Type*));
+    symbol_new(&p, 0, "$",  SYM_VAR, 0);
+    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;
+
+    codegen_init(&p);
+    module(&p);
 }
index 6c3704a67330108260d1acdfe40e3a35563e6ecf..5bb73f6e75f6a9c6048afc254a9aaebb33de0be8 100644 (file)
@@ -146,88 +146,6 @@ void symbol_closescope(Parser* p, size_t scope)
     p->nsyms = 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',
-    [SYM_VAR]    = 'V',
-    [SYM_TYPE]   = 'T',
-    [SYM_PROC]   = 'P',
-};
-
-//static size_t typeid(Parser* p, Type* type)
-//{
-//    if (type == NULL) return -1;
-//    size_t i;
-//    for (i = 0; i < p->ntypes; i++)
-//    {
-//        if (p->types[i] == type)
-//            break;
-//    }
-//    assert(i < p->ntypes);
-//    return i;
-//}
-
-static void print_type(Parser* p, Type* type)
-{
-    (void)p;
-    printf("%p\n", type);
-}
-
-void symbol_export(Parser* p, char* path)
-{
-    (void)path;
-//    for (size_t i = 0; i < p->ntypes; i++)
-//    {
-//        printf("t %ld %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 ",
-            SymTypes[sym->class],
-            sym->name);
-        print_type(p, sym->type);
-//        puts("");
-    }
-}
-
-/* Symbol File Parsing
- *****************************************************************************/
-
-void symbol_import(Parser* p, char* name, char* alias)
-{
-    Symbol* mod = symbol_new(p, 0, name, SYM_MODULE, true);
-    size_t modid = symbol_getid(p, 0, mod->name, SYM_MODULE);
-    if (alias)
-    {
-        Symbol* modalias = symbol_new(p, 0, alias, SYM_MODULE, true);
-        modalias->module = modid; // Points to the aliased module
-    }
-
-    /* TODO: read symbols from real symbol file */
-    // All of these should set ->module = modid
-
-    Symbol* sym = symbol_new(p, 0, "testint", SYM_VAR, true);
-    sym->module = modid;
-}
-
 /* Symbol Table Unit Tests
  *****************************************************************************/
 #ifdef CERISE_TESTS
index 9e9f11af46b7aa820351b53950f1163935189be0..c96130015e9ce065535339c0e6e5778d259df896 100644 (file)
@@ -195,4 +195,5 @@ begin
 end
 
 #begin
+#    TestReturnVoid();
 #end