]> git.mdlowis.com Git - proto/obnc.git/commitdiff
started implementingmodule symbol files
authorMichael D. Lowis <mike@mdlowis.com>
Thu, 17 Nov 2022 12:36:13 +0000 (07:36 -0500)
committerMichael D. Lowis <mike@mdlowis.com>
Thu, 17 Nov 2022 12:36:13 +0000 (07:36 -0500)
cerise/src/grammar.c
cerise/src/sym.c
cerise/tests/Module.m

index 8948a4274cf917c1804eae7d6c9ec927fb8b80bf..f6e05463a5453b17b7608b7dfbdee47262e4d434 100644 (file)
@@ -625,10 +625,6 @@ static void module(Parser* p)
     ENTER_RULE();
     size_t scope = symbol_openscope(p);
 
-    expect(p, MODULE);
-    p->name = expect_text(p, IDENT);
-    /* TODO: Check that it matches filename here */
-
     if (matches(p, IMPORT))
     {
         import_list(p);
@@ -691,7 +687,7 @@ static void module(Parser* p)
         error(p, "expected end of file");
     }
 
-//    symbol_export(p, NULL);
+    symbol_export(p, NULL);
     symbol_closescope(p, scope);
     EXIT_RULE();
 }
@@ -744,118 +740,3 @@ void compile(char* fname)
     codegen_init(p);
     module(p);
 }
-
-/* Grammar Unit Tests
- *****************************************************************************/
-#ifdef CERISE_TESTS
-#include "atf.h"
-
-//Parser Ctx = {0};
-//
-//static void parse_init(char* fname, char* string)
-//{
-//    memset(&Ctx, 0, sizeof(Ctx));
-//    symbol_new(&Ctx, 0, "Bool",   SYM_TYPE, 0)->type = &BoolType;
-//    symbol_new(&Ctx, 0, "Int",    SYM_TYPE, 0)->type = &IntType;
-//    symbol_new(&Ctx, 0, "Real",   SYM_TYPE, 0)->type = &RealType;
-//    symbol_new(&Ctx, 0, "String", SYM_TYPE, 0)->type = &StringType;
-//    LexFile* file = calloc(sizeof(LexFile), 1u);
-//    file->path = strdup(fname);
-//    file->fbeg = file->fpos = strdup(string);
-//    file->next = Ctx.file;
-//    Ctx.file = file;
-//}
-//
-//static void parse_rule(void (*rule)(Parser*, Item*), Item* item, char* string)
-//{
-////    puts("");
-////    printf("%s\n", string);
-//    parse_init("test_input", string);
-//    rule(&Ctx, (item ? item : &(Item){0}));
-//}
-//
-//static void parse_module(char* fname, char* string)
-//{
-//    parse_init(fname, string);
-//    module(&Ctx, &(Item){0});
-//}
-
-TEST_SUITE(Grammar)
-{
-//    TEST(Should parse basic module syntax)
-//    {
-//        parse_module("Empty", "module Empty");
-//        parse_module("ModA",  "module ModA import ModB");
-//        parse_module("ModA",  "module ModA const FOO = 42");
-//        parse_module("ModA",  "module ModA var foo : Int");
-//    }
-//
-//    TEST(Should parse imports)
-//    {
-//        parse_rule(import_list, 0, "import A;");
-//        parse_rule(import_list, 0, "import A = ModA;");
-//        parse_rule(import_list, 0, "import A, B;");
-//        parse_rule(import_list, 0, "import A, B = ModB, C;");
-//    }
-//
-//    TEST(Should parse constant declarations and expressions)
-//    {
-//        parse_rule(const_decl, 0, "FOO = 123");
-//        parse_rule(const_decl, 0, "FOO = 123.123");
-//        parse_rule(const_decl, 0, "FOO = \"\"");
-//        parse_rule(const_decl, 0, "FOO = true");
-//        parse_rule(const_decl, 0, "FOO = false");
-////        parse_rule(const_decl, 0, "FOO = nil");
-//        parse_rule(const_decl, 0, "FOO = not true");
-//        parse_rule(const_decl, 0, "FOO = (not false)");
-//        parse_rule(const_decl, 0, "FOO = +1");
-//        parse_rule(const_decl, 0, "FOO = -1");
-//        parse_rule(const_decl, 0, "FOO = +1.0");
-//        parse_rule(const_decl, 0, "FOO = -1.0");
-//        parse_rule(const_decl, 0, "FOO = 1 + 2");
-//        parse_rule(const_decl, 0, "FOO = 1.0 + 2.0");
-//        parse_rule(const_decl, 0, "FOO = 1 - 2");
-//        parse_rule(const_decl, 0, "FOO = 1.0 - 2.0");
-//        parse_rule(const_decl, 0, "FOO = 3 * 2");
-//        parse_rule(const_decl, 0, "FOO = 3.0 * 2.0");
-//        parse_rule(const_decl, 0, "FOO = 4 / 2");
-//        parse_rule(const_decl, 0, "FOO = 3.0 / 4.0");
-//        parse_rule(const_decl, 0, "FOO = 5 % 3");
-//        parse_rule(const_decl, 0, "FOO = 5 == 3");
-//        parse_rule(const_decl, 0, "FOO = 5 != 3");
-//        parse_rule(const_decl, 0, "FOO = 5 < 3");
-//        parse_rule(const_decl, 0, "FOO = 5 <= 3");
-//        parse_rule(const_decl, 0, "FOO = 5 > 3");
-//        parse_rule(const_decl, 0, "FOO = 5 >= 3");
-//        parse_rule(const_decl, 0, "FOO = 5, BAR = FOO - 3");
-//    }
-//
-//    TEST(Should parse module level type declarations)
-//    {
-//        parse_rule(type_decl, 0, "a = Int");
-//        parse_rule(type_decl, 0, "a = Real");
-//        parse_rule(type_decl, 0, "a = Bool");
-//        parse_rule(type_decl, 0, "a = array 42 of Int");
-//        parse_rule(type_decl, 0, "a = array 42 of array 42 of Int");
-//        parse_rule(type_decl, 0, "a = record end");
-//    }
-//
-//    TEST(Should parse module level variable declarations)
-//    {
-//        parse_rule(var_decl, 0, "i : Int");
-//        parse_rule(var_decl, 0, "i : Real");
-//        parse_rule(var_decl, 0, "i : Bool");
-////        parse_rule(var_decl, 0, "x,y,z : Int");
-//    }
-//
-//    TEST(Should parse module level procedure declarations)
-//    {
-//        parse_rule(proc_decl, 0,
-//            "procedure Foo*() : Int\n"
-//            "begin\n"
-//            "    return 1;\n"
-//            "end"
-//         );
-//    }
-}
-#endif
index ca7764ea2c84438820262352f91f9cd9a040b2a5..6c3704a67330108260d1acdfe40e3a35563e6ecf 100644 (file)
@@ -149,16 +149,16 @@ 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 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',
@@ -168,62 +168,43 @@ static const char SymTypes[5] = {
     [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 char* modname(Parser* p, Symbol* sym)
+//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)
 {
-    char* name = p->name;
-    if (sym->module > 0)
-    {
-        sym = symbol_getbyid(p, sym->module);
-        name = sym->name;
-    }
-    return name;
+    (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->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;
-        if (sym->class == SYM_MODULE && sym->module != 0) continue;
 
-        printf("%c %s %s ",
+        printf("%c %s ",
             SymTypes[sym->class],
-            modname(p, sym),
             sym->name);
-
-        switch (sym->class)
-        {
-            case SYM_MODULE:
-                printf("<signature>\n");
-                break;
-
-            case SYM_CONST:
-            case SYM_VAR:
-            case SYM_TYPE:
-            case SYM_PROC:
-                printf("%ld\n", typeid(p, sym->type));
-                break;
-        }
+        print_type(p, sym->type);
+//        puts("");
     }
 }
 
index 91333c985a0e68901be05cc2b6c5fac2e1de76d1..9e9f11af46b7aa820351b53950f1163935189be0 100644 (file)
@@ -1,7 +1,7 @@
-module Module
-
+const
+  FOO* = 42
 type
-  FooRec = record
+  FooRec* = record
     a : Int
     b : record
       c : Int
@@ -21,11 +21,11 @@ var
     b : array 5 of Int
   end
 
-procedure TestReturnVoid()
+procedure TestReturnVoid*()
 begin
 end
 
-procedure TestReturnIntLiteral() : Int
+procedure TestReturnIntLiteral*() : Int
 begin
     return 0;
 end
@@ -182,7 +182,7 @@ begin
     vRec1.b.c = vRec1.b.c + vRec1.b.c;
 end
 
-procedure Sum(a: Int, b : Int) : Int
+procedure Sum*(a: Int, b : Int) : Int
 begin
     return a + b;
 end