-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathamyplan.c
143 lines (132 loc) · 4.05 KB
/
amyplan.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
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
#include <stdlib.h>
#include "amyplanyyutils.h"
#include "abcetrees.h"
void usage(const char *argv0)
{
fprintf(stderr, "Usage: %s file.amy\n", argv0);
exit(1);
}
int main(int argc, char **argv, char **envp)
{
FILE *f;
struct amyplanyy amyplanyy = {};
unsigned char tmpbuf[1024] = {0};
size_t tmpsiz = 0;
struct abce_mb *mbar = NULL;
struct abce_mb *mbt = NULL;
const struct abce_mb *mbf = NULL;
int64_t idx;
int i;
int ret;
char **envit;
if (argc < 1)
{
usage(argv[0]);
}
if (argc == 1)
{
usage(argv[0]); // REPL not implemented yet
}
f = fopen(argv[1], "r");
amyplanyy_init(&amyplanyy);
if (!f)
{
fprintf(stderr, "Can't open file %s\n", argv[1]);
exit(1);
}
amyplanyydoparse(f, &amyplanyy);
fclose(f);
tmpsiz = 0;
amyplanyy.abce.sp = 0;
abce_add_ins_alt(tmpbuf, &tmpsiz, sizeof(tmpbuf), ABCE_OPCODE_PUSH_DBL);
mbf = abce_sc_get_rec_str(&amyplanyy.abce.dynscope, "main", 1);
if (mbf == NULL || mbf->typ != ABCE_T_F)
{
fprintf(stderr, "Can't find main function\n");
exit(1);
}
abce_add_double_alt(tmpbuf, &tmpsiz, sizeof(tmpbuf), mbf->u.d);
abce_add_ins_alt(tmpbuf, &tmpsiz, sizeof(tmpbuf), ABCE_OPCODE_FUNIFY);
mbar = abce_mb_cpush_create_array(&amyplanyy.abce);
for (i = 1; i < argc; i++)
{
struct abce_mb *mb;
mb = abce_mb_cpush_create_string(&amyplanyy.abce, argv[i], strlen(argv[i]));
if (abce_mb_array_append(&amyplanyy.abce, mbar, mb) != 0)
{
fprintf(stderr, "Out of memory\n");
exit(1);
}
abce_cpop(&amyplanyy.abce);
}
idx = abce_cache_add(&amyplanyy.abce, mbar);
abce_cpop(&amyplanyy.abce);
abce_add_ins_alt(tmpbuf, &tmpsiz, sizeof(tmpbuf), ABCE_OPCODE_PUSH_DBL);
abce_add_double_alt(tmpbuf, &tmpsiz, sizeof(tmpbuf), idx);
abce_add_ins_alt(tmpbuf, &tmpsiz, sizeof(tmpbuf), ABCE_OPCODE_PUSH_FROM_CACHE);
mbt = abce_mb_cpush_create_tree(&amyplanyy.abce);
for (envit = envp; *envit != NULL; envit++)
{
struct abce_mb *mbkey, *mbval;
char *env = *envit;
char *res;
res = strchr(env, '=');
if (res != NULL) {
mbkey = abce_mb_cpush_create_string(&amyplanyy.abce, env, res - env);
mbval = abce_mb_cpush_create_string(&amyplanyy.abce, res+1, strlen(res+1));
if (abce_tree_set_str(&amyplanyy.abce, mbt, mbkey, mbval) != 0)
{
fprintf(stderr, "Out of memory\n");
exit(1);
}
abce_cpop(&amyplanyy.abce);
abce_cpop(&amyplanyy.abce);
}
}
idx = abce_cache_add(&amyplanyy.abce, mbt);
abce_cpop(&amyplanyy.abce);
abce_add_ins_alt(tmpbuf, &tmpsiz, sizeof(tmpbuf), ABCE_OPCODE_PUSH_DBL);
abce_add_double_alt(tmpbuf, &tmpsiz, sizeof(tmpbuf), idx);
abce_add_ins_alt(tmpbuf, &tmpsiz, sizeof(tmpbuf), ABCE_OPCODE_PUSH_FROM_CACHE);
abce_add_ins_alt(tmpbuf, &tmpsiz, sizeof(tmpbuf), ABCE_OPCODE_PUSH_DBL);
abce_add_double_alt(tmpbuf, &tmpsiz, sizeof(tmpbuf), 2); // fun arg cnt
abce_add_ins_alt(tmpbuf, &tmpsiz, sizeof(tmpbuf), ABCE_OPCODE_CALL);
abce_add_ins_alt(tmpbuf, &tmpsiz, sizeof(tmpbuf), ABCE_OPCODE_EXIT);
if (abce_engine(&amyplanyy.abce, tmpbuf, tmpsiz) != 0)
{
fprintf(stderr, "error %s\n", abce_err_to_str(amyplanyy.abce.err.code));
fprintf(stderr, "Backtrace:\n");
for (i = 0; i < amyplanyy.abce.btsz; i++)
{
if (amyplanyy.abce.btbase[i].typ == ABCE_T_S)
{
fprintf(stderr, "%s\n", amyplanyy.abce.btbase[i].u.area->u.str.buf);
}
else
{
fprintf(stderr, "(-)\n");
}
}
fprintf(stderr, "Additional information:\n");
abce_mb_dump(&amyplanyy.abce.err.mb);
return 255;
}
if (amyplanyy.abce.sp < 1)
{
fprintf(stderr, "No return value from engine\n");
return 254;
}
if (amyplanyy.abce.stackbase[amyplanyy.abce.sp-1].typ != ABCE_T_D)
{
fprintf(stderr, "Return value from engine not a number\n");
return 253;
}
ret = (int)amyplanyy.abce.stackbase[amyplanyy.abce.sp-1].u.d;
if (amyplanyy.abce.stackbase[amyplanyy.abce.sp-1].u.d != (double)ret)
{
fprintf(stderr, "Return value from engine not an integer\n");
return 252;
}
amyplanyy_free(&amyplanyy);
return ret;
}