prim.c 887 Bytes
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

#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);
}