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

first stub of handling primitive functions

parent ff83a578
g++ -Wno-write-strings -Wno-pointer-arith main.c desc.c mem.c parse.c -o main
g++ -Wno-write-strings -Wno-pointer-arith main.c desc.c mem.c parse.c prim.c -o main
......@@ -43,6 +43,17 @@ FunEntry* alloc_prim(char* name)
return entry;
}
void gen_slices(void* dest, void* forward_ptr, int arity)
{
for(int i=0; i<arity; i++)
{
SliceEntry* slice = (SliceEntry*) dest + sizeof(SliceEntry) * i;
slice->flags.type = FT_SLICE;
slice->flags.arity = i;
slice->forward_ptr = forward_ptr;
}
}
void init_desc()
{
__INT__ = alloc_prim("INT");
......
......@@ -7,6 +7,7 @@
#define FT_CAF_REDUCED 4
#define FT_FUN 5
#define FT_SLICE 6
#define FT_PRIM 7
struct FunFlags
{
......@@ -58,6 +59,16 @@ struct RecordEntry
char name[];
};
struct PrimEntry
{
struct FunFlags flags;
int strictness;
void (*exec)();
char name[];
};
void gen_slices(void* dest, void* forward_ptr, int arity);
void init_desc();
void add_desc(char* fn, FunFlags* desc);
......
......@@ -4,12 +4,14 @@
#include "mem.h"
#include "desc.h"
#include "prim.h"
#include "parse.h"
int main()
{
init_mem();
init_desc();
init_prim();
char* stream = "40 R10 example._R2 1 9 example.a9 example.b37 F12 example.g_482 3 AF3 add2 VA0 VA1 39 C9 example.fAF12 example.g_482 LI1 LI2 55 F13 example.Start0 0 AF10 example._R2 LI1 VF9 example.f29 F4 main0 0 VF13 example.Start";
......
......@@ -48,17 +48,6 @@ void copyStringAndForward(char* dest, char** source, int length)
*source += length;
}
void genSlices(void* dest, void* forward_ptr, int arity)
{
for(int i=0; i<arity; i++)
{
SliceEntry* slice = (SliceEntry*) dest + sizeof(SliceEntry) * i;
slice->flags.type = FT_SLICE;
slice->flags.arity = i;
slice->forward_ptr = forward_ptr;
}
}
int parseDef1(char** ptr)
{
int defSize;
......@@ -106,7 +95,7 @@ int parseDef1(char** ptr)
entry->body = *ptr;
// generate slices. avoid function call if arity is zero
if(arity>0) genSlices(entry_base, entry, arity);
if(arity>0) gen_slices(entry_base, entry, arity);
add_desc(entry->name, (FunFlags*) entry);
break;
......@@ -171,7 +160,7 @@ int parseDef1(char** ptr)
if(!parseInt(ptr, &entry->strictness)) return 0;
// generate slices. avoid function call if arity is zero
if(arity>0) genSlices(entry_base, entry, arity);
if(arity>0) gen_slices(entry_base, entry, arity);
add_desc(entry->name, (FunFlags*) entry);
}
......@@ -310,7 +299,7 @@ VarEntry* parseVar(char **ptr)
copyStringAndForward(name, ptr, nameLength);
entry->f = (FunFlags*) find_desc(name); // can fail
if(entry->f == 0)
{
printf("%s not found\n", name);
......
#include <string.h>
#include "prim.h"
#include "desc.h"
#include "mem.h"
void __add()
{
}
PrimEntry* add_prim(int arity, int strictness, char* name, void (*exec)())
{
int nameLength = strlen(name);
// before the PrimEntry there are "arity" number of SliceEntries
void* entry_base = alloc_desc(sizeof(SliceEntry) * arity + sizeof(PrimEntry) + nameLength + 1);
PrimEntry* entry = (PrimEntry*) (entry_base + sizeof(SliceEntry) * arity);
entry->flags.type = FT_PRIM;
entry->flags.arity = arity;
entry->strictness = strictness;
entry->exec = exec;
// TODO: should it be copied at all?
memcpy(entry->name, name, nameLength);
entry->name[nameLength] = '\0';
// generate slices. avoid function call if arity is zero
if(arity>0) gen_slices(entry_base, entry, arity);
add_desc(entry->name, (FunFlags*) entry);
}
void init_prim()
{
add_prim(2, 3, "add", &__add);
}
#ifndef __PRIM_H
#define __PRIM_H
void init_prim();
#endif // __PRIM_H
\ No newline at end of file
Markdown is supported
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