prim.c 888 Bytes
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

#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
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
18
	SliceEntry* entry_base = (SliceEntry*) alloc_desc(sizeof(SliceEntry) * arity + sizeof(PrimEntry) + nameLength + 1);
19
		
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
20
	PrimEntry* entry = (PrimEntry*) (entry_base + arity);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
21
22
	entry->base.type = FT_PRIM;
	entry->base.arity = arity;
23
24
25
26
27
28
29
30
	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
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
31
	if(arity>0) gen_slices(entry_base, (Desc*) entry, arity);
32
	
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
33
	add_desc(entry->name, (Desc*) entry);
34
35
36
37
38
39
}

void init_prim()
{
	add_prim(2, 3, "add", &__add);
}