Commit ddf4739e authored by Laszlo Domoszlai's avatar Laszlo Domoszlai
Browse files

use factory function instead of array lookup

parent 397719cb
......@@ -117,6 +117,11 @@ void init_code()
create_thunk_funs[CT_THUNK] = create_thunk_thunk;
}
create_thunk_fun get_create_thunk_fun(CodeType type)
{
return create_thunk_funs[type];
}
void exec(Code* expr, int frame_ptr, int root_frame_ptr)
{
int root_frame_ptr_b = stack_top_b;
......
......@@ -64,8 +64,10 @@ struct IfEntry {
struct Code* fexpr;
};
typedef Thunk* (*create_thunk_fun)(Code*, int);
void init_code();
extern Thunk* (*create_thunk_funs[9]) (Code* expr, int frame_ptr);
create_thunk_fun get_create_thunk_fun(CodeType type);
void exec(Code* expr, int frame_ptr, int root_frame_ptr);
struct Thunk* eval(Thunk* thunk);
......
......@@ -236,7 +236,7 @@ ThunkEntry* parseLit(char **ptr) {
struct ThunkEntry* entry = (ThunkEntry*) alloc_code(sizeof (ThunkEntry) + strlen);
entry->base.type = CT_THUNK;
entry->base.create_thunk = create_thunk_funs[CT_THUNK];
entry->base.create_thunk = get_create_thunk_fun(CT_THUNK);
switch (type) {
case 'I': // Int
......@@ -287,12 +287,12 @@ VarEntry* parseVar(char **ptr, VarEntry* target) {
switch (type) {
case 'L': // Local var
entry->base.type = CT_VAR;
entry->base.create_thunk = create_thunk_funs[CT_VAR];
entry->base.create_thunk = get_create_thunk_fun(CT_VAR);
entry->base.strict = false;
break;
case 'S': // Strict local var
entry->base.type = CT_VAR_STRICT;
entry->base.create_thunk = create_thunk_funs[CT_VAR_STRICT];
entry->base.create_thunk = get_create_thunk_fun(CT_VAR_STRICT);
entry->base.strict = true;
break;
default:
......@@ -328,7 +328,7 @@ Code* parseApp(char **ptr, bool dynamic) {
{
struct ThunkEntry* entry = (ThunkEntry*) alloc_code(sizeof (ThunkEntry));
entry->base.type = CT_THUNK;
entry->base.create_thunk = create_thunk_funs[CT_THUNK];
entry->base.create_thunk = get_create_thunk_fun(CT_THUNK);
entry->thunk.desc = desc;
return (Code*) entry;
}
......@@ -338,7 +338,7 @@ Code* parseApp(char **ptr, bool dynamic) {
// TODO: CAS
entry->base.type = CT_APP_FUN;
entry->base.create_thunk = create_thunk_funs[CT_APP_FUN];
entry->base.create_thunk = get_create_thunk_fun(CT_APP_FUN);
}
else
{
......@@ -353,7 +353,7 @@ Code* parseApp(char **ptr, bool dynamic) {
{
parseVar(ptr, &entry->var);
entry->base.type = CT_APP_DYN;
entry->base.create_thunk = create_thunk_funs[CT_APP_DYN];
entry->base.create_thunk = get_create_thunk_fun(CT_APP_DYN);
}
else
{
......@@ -366,17 +366,17 @@ Code* parseApp(char **ptr, bool dynamic) {
if(desc->type == FT_PRIM)
{
entry->base.type = CT_APP_PRIM;
entry->base.create_thunk = create_thunk_funs[CT_APP_PRIM];
entry->base.create_thunk = get_create_thunk_fun(CT_APP_PRIM);
}
else if(desc->type == FT_FUN)
{
entry->base.type = CT_APP_FUN;
entry->base.create_thunk = create_thunk_funs[CT_APP_FUN];
entry->base.create_thunk = get_create_thunk_fun(CT_APP_FUN);
}
else
{
entry->base.type = CT_APP_THUNK;
entry->base.create_thunk = create_thunk_funs[CT_APP_THUNK];
entry->base.create_thunk = get_create_thunk_fun(CT_APP_THUNK);
}
}
}
......@@ -393,7 +393,7 @@ SelectEntry* parseSelect(char **ptr) {
struct SelectEntry* entry = (SelectEntry*) alloc_code(sizeof (SelectEntry) + sizeof (SelectCaseEntry) * nrCases);
entry->base.type = CT_SELECT;
entry->base.create_thunk = create_thunk_funs[CT_SELECT];
entry->base.create_thunk = get_create_thunk_fun(CT_SELECT);
entry->base.nr_cases = nrCases;
entry->expr = expr;
......@@ -425,7 +425,7 @@ SelectEntry* parseSelect(char **ptr) {
IfEntry* parseIf(char **ptr) {
struct IfEntry* entry = (IfEntry*) alloc_code(sizeof (IfEntry));
entry->base.type = CT_IF;
entry->base.create_thunk = create_thunk_funs[CT_IF];
entry->base.create_thunk = get_create_thunk_fun(CT_IF);
entry->cond = parseTerm(ptr);
entry->texpr = parseTerm(ptr);
entry->fexpr = parseTerm(ptr);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment