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);
error(p, "expected end of file");
}
-// symbol_export(p, NULL);
+ symbol_export(p, NULL);
symbol_closescope(p, scope);
EXIT_RULE();
}
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
/* 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',
[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("");
}
}