-
Notifications
You must be signed in to change notification settings - Fork 0
/
func.c
88 lines (77 loc) · 2.1 KB
/
func.c
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "h/sporth.h"
#define LENGTH(x) ((int)(sizeof(x) / sizeof *(x)))
int sporth_register_func(sporth_data *sporth, sporth_func *flist)
{
sporth->flist = flist;
uint32_t i = 0;
while(sporth->flist[i].name != NULL) {
#ifdef DEBUG_MODE
fprintf(stderr,"Registering function \"%s\" at position %d\n",
sporth->flist[i].name, i + SPORTH_FOFFSET);
#endif
sporth_htable_add(&sporth->dict, sporth->flist[i].name, i);
i++;
}
sporth->nfunc = i;
return SPORTH_OK;
}
int sporth_exec(sporth_data *sporth, const char *keyword)
{
uint32_t id;
if(sporth_search(&sporth->dict, keyword, &id) != SPORTH_OK) {
sporth_print(sporth,
"Could not find function called '%s'.\n",
keyword);
return SPORTH_NOTOK;
}
#ifdef DEBUG_MODE
fprintf(stderr,"Executing function \"%s\" (id %d)\n", keyword, id);
#endif
return sporth->flist[id].func(&sporth->stack, sporth->flist[id].ud);
}
int sporth_check_args(sporth_stack *stack, const char *args)
{
if(stack->error > 0) return SPORTH_NOTOK;
int len = (int) strlen(args);
int i;
if(len > stack->pos) {
stack->error++;
return SPORTH_NOTOK;
}
int pos = stack->pos - len;
for(i = 0; i < len; i++) {
switch(args[i]) {
case 'f':
if(stack->stack[pos].type != SPORTH_FLOAT) {
stack->error++;
return SPORTH_NOTOK;
}
break;
case 's':
if(stack->stack[pos].type != SPORTH_STRING) {
stack->error++;
return SPORTH_NOTOK;
}
break;
case 'n':
break;
}
pos++;
}
return SPORTH_OK;
}
int sporth_init(sporth_data *sporth)
{
sporth_stack_init(&sporth->stack);
sporth_htable_init(&sporth->dict);
return SPORTH_OK;
}
int sporth_destroy(sporth_data *sporth)
{
sporth_htable_destroy(&sporth->dict);
return SPORTH_OK;
}