-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathloose_eel.c
200 lines (200 loc) · 4.86 KB
/
loose_eel.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include "s_str.h"
#include "eelCommon.h"
#ifdef _WIN32
#include <vld.h>
#endif
void NSEEL_HOSTSTUB_EnterMutex() { }
void NSEEL_HOSTSTUB_LeaveMutex() { }
int32_t runcode(NSEEL_VMCTX m_vm, const char *codeptr, int32_t showerr, const char *showerrfn, int32_t canfree, int32_t ignoreEndOfInputChk, int32_t doExec)
{
NSEEL_CODEHANDLE code = NSEEL_code_compile_ex(m_vm, codeptr, 0, canfree ? 0 : NSEEL_CODE_COMPILE_FLAG_COMMONFUNCS);
char *err;
if (!code && (err = NSEEL_code_getcodeerror(m_vm)))
{
if (!ignoreEndOfInputChk && (NSEEL_code_geterror_flag(m_vm) & 1)) return 1;
if (showerr)
{
if (showerr == 2)
printf("Warning: %s:%s\n", showerrfn, err);
else
printf("%s:%s\n", showerrfn, err);
}
return -1;
}
else
{
if (code)
{
if (doExec)
NSEEL_code_execute(code);
if (canfree)
NSEEL_code_free(code);
}
return 0;
}
return -1;
}
NSEEL_CODEHANDLE loadfileAndCompile(NSEEL_VMCTX m_vm, const char *fn)
{
FILE *fp = fopen(fn, "r");
if (!fp)
{
printf("Error opening script file %s", fn);
return 0;
}
s_str code = s_str_create();
s_str_clear(&code);
char line[4096];
for (;;)
{
line[0] = 0;
fgets(line, sizeof(line), fp);
if (!line[0]) break;
s_str_append_c_str(&code, line);
}
fclose(fp);
const char *codetext = s_str_c_str(&code);
NSEEL_CODEHANDLE compiledCode = NSEEL_code_compile_ex(m_vm, codetext, 0, 1);
char *err;
if (!compiledCode && (err = NSEEL_code_getcodeerror(m_vm)))
{
printf("%s\n", err);
return 0;
}
s_str_destroy(&code);
return compiledCode;
}
int32_t loadfile(NSEEL_VMCTX m_vm, const char *fn, const char *callerfn, int32_t allowstdin)
{
FILE *fp = fopen(fn, "r");
if (!fp)
{
if (callerfn)
printf("Warning: @import could not open '%s'", fn);
else
printf("Error opening script file %s", fn);
return -1;
}
s_str code = s_str_create();
s_str_clear(&code);
char line[4096];
for (;;)
{
line[0] = 0;
fgets(line, sizeof(line), fp);
if (!line[0]) break;
if (!strnicmp(line, "@import", 7) && isspace(line[7]))
{
char *p = line + 7;
while (isspace(*p)) p++;
char *ep = p;
while (*ep) ep++;
while (ep > p && isspace(ep[-1])) ep--;
*ep = 0;
if (*p)
loadfile(m_vm, p, fn, 0);
}
else
s_str_append_c_str(&code, line);
}
fclose(fp);
const char *codetext = s_str_c_str(&code);
int32_t ret = runcode(m_vm, codetext, callerfn ? 2 : 1, fn, 1, 1, !callerfn);
s_str_destroy(&code);
return ret;
}
void* thread1Spawns(void *args)
{
NSEEL_code_execute(args);
return 0;
}
extern void printFunctions();
int32_t main(int32_t argc, char **argv)
{
int32_t argpos = 1;
char *scriptfn = argv[0];
int32_t g_interactive = 0;
while (argpos < argc && argv[argpos][0] == '-' && argv[argpos][1])
{
if (!strcmp(argv[argpos], "-i")) g_interactive++;
else
{
fprintf(stderr, "Usage: %s [scriptfile | -]\n", argv[0]);
return -1;
}
argpos++;
}
if (argpos < argc && !g_interactive)
scriptfn = argv[argpos++];
else
g_interactive = 1;
// EEL global data
NSEEL_start();
s_str code, t;
code = s_str_create();
t = s_str_create();
NSEEL_VMCTX m_vm = NSEEL_VM_alloc();
if (g_interactive)
{
printf("EEL interactive mode, type @quit() to quit, @abort() to abort multiline entry, @help() for printing built-in function\n");
float *resultVar = NSEEL_VM_regvar(m_vm, "__result");
s_str_clear(&code);
s_str_clear(&t);
char line[4096];
for (;;)
{
const char *fi = s_str_c_str(&code);
if (!fi[0])
printf("EEL> ");
else
printf("> ");
fflush(stdout);
line[0] = 0;
fgets(line, sizeof(line), stdin);
if (!line[0])
break;
while (line[0] && (line[strlen(line) - 1] == '\r' || line[strlen(line) - 1] == '\n' || line[strlen(line) - 1] == '\t' || line[strlen(line) - 1] == ' '))
line[strlen(line) - 1] = 0;
s_str_append_c_str(&code, line);
if (!strcmp(line, "@quit()"))
break;
if (!strcmp(line, "@abort()"))
s_str_assign_c_str(&code, "");
if (!strcmp(line, "@help()"))
{
printFunctions();
s_str_assign_c_str(&code, "");
}
s_str_assign_c_str(&t, "__result = (");
const char *codetext = s_str_c_str(&code);
s_str_append_c_str(&t, codetext);
s_str_append_c_str(&t, ");");
const char *tget = s_str_c_str(&t);
int32_t res = runcode(m_vm, tget, 0, "", 1, 1, 1); // allow free, since functions can't be defined locally
if (!res)
{
if (resultVar) printf("=%.8g\n", *resultVar);
s_str_assign_c_str(&code, "");
}
else // try compiling again allowing function definitions (and not allowing free), but show errors if not continuation
{
res = runcode(m_vm, codetext, 1, "(stdin)", 0, 0, 1);
if (res <= 0)
s_str_assign_c_str(&code, "");
// res>0 means need more lines
}
}
}
else
loadfile(m_vm, scriptfn, NULL, 1);
s_str_destroy(&code);
s_str_destroy(&t);
NSEEL_VM_free(m_vm);
NSEEL_quit();
return 0;
}