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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
#include "builtins.h"
#include <stdlib.h>
#define IF_SIG (uint32_t)-1
#define WHILE_SIG (uint32_t)-2
#define ELSE_SIG (uint32_t)-3
#define FUNC_SIG (uint32_t)-4
int bn_set(instance *it);
int bn_if(instance *it);
int bn_else(instance *it);
int bn_end(instance *it);
int bn_while(instance *it);
int bn_func(instance *it);
int bn_solve(instance *it);
void iload_builtins(instance *it)
{
inew_cfunc(it, "set", bn_set);
inew_cfunc(it, "if", bn_if);
inew_cfunc(it, "else", bn_else);
inew_cfunc(it, "while", bn_while);
inew_cfunc(it, "func", bn_func);
inew_cfunc(it, "solve", bn_solve);
}
int bn_set(instance *it)
{
variable *var = igetarg(it, 0);
variable *value = igetarg(it, 1);
var->type = value->type;
var->value.p = value->value.p;
ipush(it, (uint32_t)var);
return 0;
}
int bn_if(instance *it)
{
variable *cond = (variable *)ipop(it);
uint32_t result = cond->value.p;
ipush(it, result);
ipush(it, IF_SIG);
if (result == 0)
it->sindent = SKIP | it->indent;
ipush(it, 0);
ipush(it, 0); // need to return because stack modify
return 0;
}
static uint32_t if_cond = 0;
int bn_else(instance *it)
{
uint32_t cond = if_cond;
if (cond != 0)
it->sindent = SKIP | it->indent;
ipush(it, ELSE_SIG);
ipush(it, 0); // for ret
return 0;
}
int bn_end(instance *it)
{
uint32_t sig = ipop(it);
if (sig == IF_SIG) {
if_cond = ipop(it);
} else if (sig == WHILE_SIG) {
uint32_t lnidx = ipop(it);
if (lnidx != (int32_t)-1)
it->lnidx = lnidx - 1;
} else if (sig == CALL_SIG) {
it->lnidx = ipop(it);
it->indent++;
}
return 0;
}
int bn_while(instance *it)
{
variable *cond = (variable *)ipop(it);
uint32_t result = cond->value.p;
if (result == 0) {
it->sindent = SKIP | it->indent;
ipush(it, (uint32_t)-1);
} else {
ipush(it, it->lnidx);
}
ipush(it, WHILE_SIG);
ipush(it, 0);
ipush(it, 0); // need to ret
return 0;
}
int bn_func(instance *it)
{
variable *f = igetarg(it, 0);
if (f == 0)
return -1;
f->type = FUNC;
f->value.p = it->lnidx;
it->sindent = SKIP | it->indent;
ipush(it, FUNC_SIG);
ipush(it, 0); // for ret
return 0;
}
int bn_solve(instance *it)
{
variable *s = igetarg(it, 0);
variable **ops = iparse(it, (const char *)s->value.p);
if (ops == 0)
return -1;
variable *a = isolve(it, ops, 0);
free(ops);
ipush(it, (uint32_t)a);
return 0;
}
|