roza/lib/prepass.c

86 lines
1.9 KiB
C

#include "prepass.h"
#include "lib/sym.h"
#include "lib/tysy.h"
#include "node.h"
void prepass_init(prepass_t* prepass,
int* id,
sym_t* sym,
tysy_t* tysy,
tysolver_t* tysolver,
err_t* err)
{
assert(prepass);
assert(sym);
assert(tysy);
assert(tysolver);
assert(err);
prepass->sym = sym;
prepass->tysy = tysy;
prepass->tysolver = tysolver;
prepass->err = err;
prepass->scope = 0;
prepass->id = id;
}
void prepass_free(prepass_t* prepass)
{
assert(prepass);
}
void prepass_run(prepass_t* prepass, node_t* node)
{
assert(prepass);
assert(node);
switch (node->type)
{
case NODE_BLOCK: {
prepass->scope++;
for (size_t i=0; i<node->children.size; i++)
{
node_t* child = (node_t*) node->children.data[i];
prepass_run(prepass, child);
}
prepass->scope--;
} break;
case NODE_VARDECL: {
char* name = ((node_t*) node->children.data[0])->value.data;
prepass_run(prepass, (node_t*) node->children.data[1]);
type_t* type = tysolver_try_solve_node(prepass->tysolver,
(node_t*) node->children.data[1]);
sym_declare(prepass->sym, (*prepass->id)++, name, type,
prepass->scope, SYM_PRE,
node);
} break;
case NODE_FUNDECL: {
char* name = ((node_t*) node->children.data[0])->value.data;
sym_declare(prepass->sym,
(*prepass->id)++,
name,
tysy_try_find_type(prepass->tysy, "fun"),
prepass->scope,
SYM_PRE,
node);
} break;
default: {
for (size_t i=0; i<node->children.size; i++)
{
prepass_run(prepass, (node_t*) node->children.data[i]);
}
} break;
}
}