-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbttest.c
81 lines (70 loc) · 2.21 KB
/
bttest.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
#include <stdlib.h>
#include "amyplanyyutils.h"
char *str =
"@function $fibonacci($x)\n"
" @if($x<3)\n"
" @return @dyn[\"fibonacci2\"]($x - 1)\n"
" @endif\n"
" @return @dyn[\"fibonacci\"]($x - 1) + @dyn[\"fibonacci\"]($x - 2)\n"
"@endfunction\n";
int main(int argc, char **argv)
{
FILE *f = fmemopen(str, strlen(str), "r");
struct amyplanyy amyplanyy = {};
unsigned char tmpbuf[1024] = {0};
size_t tmpsiz = 0;
size_t i;
amyplanyy_init(&amyplanyy);
if (!f)
{
abort();
}
amyplanyydoparse(f, &amyplanyy);
fclose(f);
abce_add_ins_alt(tmpbuf, &tmpsiz, sizeof(tmpbuf), ABCE_OPCODE_PUSH_DBL);
abce_add_double_alt(tmpbuf, &tmpsiz, sizeof(tmpbuf),
abce_sc_get_rec_str_fun(&amyplanyy.abce.dynscope, "fibonacci", 1));
abce_add_ins_alt(tmpbuf, &tmpsiz, sizeof(tmpbuf), ABCE_OPCODE_FUNIFY);
abce_add_ins_alt(tmpbuf, &tmpsiz, sizeof(tmpbuf), ABCE_OPCODE_PUSH_DBL);
abce_add_double_alt(tmpbuf, &tmpsiz, sizeof(tmpbuf), 30); // arg
abce_add_ins_alt(tmpbuf, &tmpsiz, sizeof(tmpbuf), ABCE_OPCODE_PUSH_DBL);
abce_add_double_alt(tmpbuf, &tmpsiz, sizeof(tmpbuf), 1); // arg cnt
abce_add_ins_alt(tmpbuf, &tmpsiz, sizeof(tmpbuf), ABCE_OPCODE_CALL);
abce_add_ins_alt(tmpbuf, &tmpsiz, sizeof(tmpbuf), ABCE_OPCODE_DUMP);
abce_add_ins_alt(tmpbuf, &tmpsiz, sizeof(tmpbuf), ABCE_OPCODE_EXIT);
amyplanyy.abce.ip = -tmpsiz-ABCE_GUARD;
printf("ret %d\n", abce_engine(&amyplanyy.abce, tmpbuf, tmpsiz));
printf("actual err %d\n", (int)amyplanyy.abce.err.code);
printf("actual typ %d\n", (int)amyplanyy.abce.err.mb.typ);
for (i = 0; i < amyplanyy.abce.btsz; i++)
{
if (amyplanyy.abce.btbase[i].typ == ABCE_T_S)
{
printf("%s\n", amyplanyy.abce.btbase[i].u.area->u.str.buf);
}
else
{
printf("(-)\n");
}
}
// FIXME doesn't support long instructions
#if 0
i = 0;
while (i < amyplanyy.abce.bytecodesz)
{
uint8_t ins = amyplanyy.abce.bytecode[i];
printf("%.5zu: ins %d", i, (int)ins);
i++;
if (ins == ABCE_OPCODE_PUSH_DBL || ins == ABCE_OPCODE_FUN_HEADER)
{
double d;
abce_get_dbl(&d, &amyplanyy.abce.bytecode[i]);
i += 8;
printf(", dbl %g", d);
}
printf("\n");
}
#endif
amyplanyy_free(&amyplanyy);
return 0;
}