aboutsummaryrefslogtreecommitdiffstats
path: root/builtins.c
blob: 0ab6f3b993d3e6ea657fe7607a98e1fb09a3f843 (plain)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "builtins.h"
#include "stack.h"
#include "shelpers.h"

#include <memory.h>
#include <string.h>

int ifunc_set(interpreter *it);
int ifunc_label(interpreter *it);
int ifunc_end(interpreter *it);
int ifunc_if(interpreter *it);
int ifunc_do(interpreter *it);
int ifunc_while(interpreter *it);
int ifunc_ret(interpreter *it);
int ifunc_else(interpreter *it);
int ifunc_solve(interpreter *it);

const func_t indent_up[IUP_COUNT] = {
	ifunc_if, ifunc_do, ifunc_label
};

const func_t indent_down[IDOWN_COUNT] = {
	ifunc_else, ifunc_end, ifunc_while, 
};

void iload_core(interpreter *interp)
{
	inew_cfunc(interp, "set", ifunc_set);
	inew_cfunc(interp, "func", ifunc_label);
	inew_cfunc(interp, "end", ifunc_end);
	inew_cfunc(interp, "if", ifunc_if);
	inew_cfunc(interp, "do", ifunc_do);
	inew_cfunc(interp, "while", ifunc_while);
	inew_cfunc(interp, "ret", ifunc_ret);
	inew_cfunc(interp, "else", ifunc_else);
	inew_cfunc(interp, "solve", ifunc_solve);
}

int ifunc_solve(interpreter *it)
{
	const char *expr = igetarg_string(it, 0);
	int len = strlen(expr);
	char *buf = (char *)malloc(len + 2);
	strcpy(buf, expr);
	buf[len] = ')';
	buf[len + 1] = '\0';
	variable *r = idoexpr(it, buf);
	free(buf);
	if (r == 0)
		r = make_varn(0, 0.0f);
	iret(it, r);
	free(r);
	return 0;
}

int ifunc_set(interpreter *it)
{
	variable *n = igetarg(it, 0);
	variable *v = igetarg(it, 1);

	if (n == 0)
		return -1;

	if (n->valtype == STRING)
		free((void *)n->value.p);
	n->valtype = v->valtype;
	n->value.p = v->value.p;
	return 0;
}

int ifunc_label(interpreter *it)
{
	variable *n = igetarg(it, 0);
	
	if (n == 0)
		return -1;

	n->valtype = FUNC;
	n->value.p = it->lnidx;
	iskip(it);
	return 0;
}

int ifunc_if(interpreter *it)
{
	int v = igetarg(it, 0)->value.p;
	if (v == 0)
		iskip(it);
	void *arg = ipop(it);
	ipush(it, (void *)v);
	ipush(it, (void *)-1);
	ipush(it, arg);
	return 0;
}

int ifunc_end(interpreter *it)
{
	if (it->stidx == 0)
		return 0;

	uint32_t lnidx = (uint32_t)ipop(it) + 1;
	if (lnidx == 0) { // from an if, have conditional
		ipop(it); // whatever
	} else {
		if (lnidx == (uint32_t)-1) {
			// script-func call
			lnidx = (uint32_t)ipop(it);
			it->indent = (uint32_t)ipop(it);
		}
		it->lnidx = lnidx;
	}
	return 0;
}

int ifunc_else(interpreter *it)
{
	if (it->stidx == 0)
		return 0;

	ipop(it); // the -1
	int cond = (int)ipop(it);
	it->indent++;
	if (cond != 0)
		iskip(it);
	// otherwise it's whatever?
	ipush(it, 0);
	ipush(it, (void *)-1);

	return 0;
}

int ifunc_do(interpreter *it)
{
	ipush(it, (void *)it->lnidx);
	return 0;
}

int ifunc_while(interpreter *it)
{
	int c = igetarg(it, 0)->value.p;
	ipop(it);
	int nidx = (int)ipop(it);
	if (c != 0) {
		//ipush(it, (void *)nidx);
		it->lnidx = nidx - 1;
	}
	ipush(it, 0);
	return 0;
}

void iret(interpreter *it, variable *v)
{
	switch (v->valtype) {
	case NUMBER:
		inew_number(it, "RET", v->value.f);
		break;
	case STRING:
		inew_string(it, "RET", (char *)v->value.p);
		break;
	default:
		return;
		break;
	}
	if (it->ret != 0) {
		if (it->ret->valtype == STRING && it->ret->value.p != 0)
			free((void *)it->ret->value.p);
		it->ret->valtype = v->valtype;
		it->ret->value.p = v->value.p;
		it->ret = 0;
	}
}

int ifunc_ret(interpreter *it)
{
	variable *v = igetarg(it, 0);
	iret(it, v);
	return 0;
}