Commit 4619092e authored by Laszlo Domoszlai's avatar Laszlo Domoszlai
Browse files

create thunk from simple function pointers (not an application)

parent 5dd354ca
......@@ -19,6 +19,16 @@ struct Thunk* exec(Code* expr)
exit(-1);
}
break;
case CT_VAR:
switch(expr->local_type)
{
case VAR_FN:
return createF(((VarEntry*) expr)->f, 0);
break;
default:
printf("Exec: Unhandled VAR type");
exit(-1);
}
default:
printf("Exec: Unhandled CODE type");
exit(-1);
......
......@@ -50,7 +50,7 @@ void gen_slices(void* dest, void* forward_ptr, int arity)
SliceEntry* slice = (SliceEntry*) dest + sizeof(SliceEntry) * i;
slice->base.type = FT_SLICE;
slice->base.arity = i;
slice->forward_ptr = forward_ptr;
slice->forward_ptr = (Desc*) forward_ptr;
}
}
......
......@@ -36,7 +36,7 @@ struct FunEntry
struct SliceEntry
{
struct Desc base;
void* forward_ptr; // FunEntry or ADTEntry
Desc* forward_ptr; // FunEntry or ADTEntry
};
struct ADTEntry
{
......
......@@ -23,8 +23,7 @@ int main()
// TODO: put it into a special "expression" space, instead of "code"
//char *exprstream = "VF13 example.Start";
char *exprstream = "LI1 ";
char *exprstream = "VF13 example.Start";
Code* expr = parseTerm(&exprstream);
Thunk* res = exec(expr);
......
No preview for this file type
......@@ -13,6 +13,36 @@ struct Thunk* createI(int i)
return thunk;
}
struct Thunk* createF(Desc* f, int nrargs)
{
// Even if no arguments, still allocate enough space for a forward pointer
Thunk* thunk = (Thunk*) alloc_heap(sizeof(Thunk) + sizeof(Thunk*) * (nrargs > 0 ? nrargs - 1 : 0));
Desc* slice = f;
if(f->arity > 0)
{
slice = &(((SliceEntry*) f)[-f->arity].base);
}
thunk->desc = slice;
return thunk;
}
int printDesc(Desc* f)
{
switch(f->type)
{
case FT_SLICE:
return printDesc(((SliceEntry*) f)->forward_ptr);
case FT_FUN:
printf("%s", ((FunEntry*) f)->name);
return f->arity;
default:
printf("printDesc: unhandled DESC\n");
exit(-1);
}
}
void print(Thunk* thunk)
{
if((FunEntry*) thunk->desc == __INT__)
......@@ -21,7 +51,12 @@ void print(Thunk* thunk)
}
else
{
printf("print: unhandled DESC\n");
exit(-1);
int arity = printDesc(thunk->desc);
if(arity > 0)
{
printf("print: unhandled arity\n");
exit(-1);
}
}
}
\ No newline at end of file
}
......@@ -19,6 +19,7 @@ struct Thunk
};
struct Thunk* createI(int i);
struct Thunk* createF(Desc* f, int nrargs);
void print(Thunk* thunk);
......
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