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
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);
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;
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)
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));
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;
expect(p, END);
symbol_closescope(p, scope);
- ast_print(p, proc->value);
+// ast_print(p, proc->value);
+ (void)proc->value;
EXIT_RULE();
}
if (accept(p, BEGIN))
{
AstNode* block = statement_seq(p);
- ast_print(p, block);
+// ast_print(p, block);
+ (void)block;
expect(p, END);
}
.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);
}
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)
/* 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_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));
}
}