eel2: fix loose_eel compile -- from 9d533d46
[wdl.git] / WDL / eel2 / loose_eel.cpp
blobb41211c48320626a02a84f94a6b2e7ca6741ac6d
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include <stdarg.h>
6 #include "../mutex.h"
8 int g_verbose, g_interactive;
10 static void writeToStandardError(const char *fmt, ...)
12 va_list arglist;
13 va_start(arglist, fmt);
14 vfprintf(stderr,fmt,arglist);
15 fprintf(stderr,"\n");
16 fflush(stderr);
17 va_end(arglist);
19 #define EEL_STRING_DEBUGOUT writeToStandardError // no parameters, since it takes varargs
21 #ifndef EEL_LICE_WANT_STANDALONE
22 #define EELSCRIPT_NO_LICE
23 #endif
25 #include "eelscript.h"
28 void NSEEL_HOSTSTUB_EnterMutex() { }
29 void NSEEL_HOSTSTUB_LeaveMutex() { }
32 int main(int argc, char **argv)
34 bool want_args = true;
35 int argpos = 1;
36 const char *scriptfn = argv[0];
37 while (argpos < argc && argv[argpos][0] == '-' && argv[argpos][1])
39 if (!strcmp(argv[argpos],"-v")) g_verbose++;
40 else if (!strcmp(argv[argpos],"-i")) g_interactive++;
41 else if (!strcmp(argv[argpos],"--no-args")) want_args=false;
42 else
44 fprintf(stderr,"Usage: %s [-v] [--no-args] [-i | scriptfile | -]\n",argv[0]);
45 return -1;
47 argpos++;
49 if (argpos < argc && !g_interactive)
51 scriptfn = argv[argpos++];
53 else
55 #ifndef _WIN32
56 if (!g_interactive && isatty(0))
57 #else
58 if (1)
59 #endif
60 g_interactive=1;
63 if (eelScriptInst::init())
65 fprintf(stderr,"NSEEL_init(): error initializing\n");
66 return -1;
70 #ifndef EELSCRIPT_NO_LICE
71 #ifdef __APPLE__
72 SWELL_InitAutoRelease();
73 #endif
74 #endif
76 WDL_FastString code,t;
78 eelScriptInst inst;
79 if (want_args)
81 #ifndef EELSCRIPT_DO_DISASSEMBLE
82 const int argv_offs = 1<<22;
83 code.SetFormatted(64,"argc=0; argv=%d;\n",argv_offs);
84 int x;
85 for (x=argpos-1;x<argc;x++)
87 code.AppendFormatted(64,"argv[argc]=%d; argc+=1;\n",
88 inst.m_string_context->AddString(new WDL_FastString(x<argpos ? scriptfn : argv[x])));
90 inst.runcode(code.Get(),2,"__cmdline__",true,true,true);
91 #endif
94 if (g_interactive)
96 #ifndef EELSCRIPT_NO_LICE
97 if (inst.m_gfx_state && inst.m_gfx_state->m_gfx_clear) inst.m_gfx_state->m_gfx_clear[0] = -1;
98 #endif
100 printf("EEL interactive mode, type quit to quit, abort to abort multiline entry\n");
101 EEL_F *resultVar = NSEEL_VM_regvar(inst.m_vm,"__result");
102 code.Set("");
103 char line[4096];
104 for (;;)
106 #ifndef EELSCRIPT_NO_LICE
107 _gfx_update(&inst,NULL);
108 #endif
109 if (!code.Get()[0]) printf("EEL> ");
110 else printf("> ");
111 fflush(stdout);
112 line[0]=0;
113 fgets(line,sizeof(line),stdin);
114 if (!line[0]) break;
115 code.Append(line);
116 while (line[0] && (
117 line[strlen(line)-1] == '\r' ||
118 line[strlen(line)-1] == '\n' ||
119 line[strlen(line)-1] == '\t' ||
120 line[strlen(line)-1] == ' '
121 )) line[strlen(line)-1]=0;
123 if (!strcmp(line,"quit")) break;
124 if (!strcmp(line,"abort")) code.Set("");
126 #ifndef EELSCRIPT_DO_DISASSEMBLE
127 t.Set("__result = (");
128 #else
129 t.Set("");
130 #endif
131 t.Append(code.Get());
132 #ifndef EELSCRIPT_DO_DISASSEMBLE
133 t.Append(");");
134 #endif
135 int res=inst.runcode(t.Get(),false,"",true,true,true); // allow free, since functions can't be defined locally
136 if (!res)
138 if (resultVar) printf("=%g ",*resultVar);
139 code.Set("");
141 else // try compiling again allowing function definitions (and not allowing free)
142 // but show errors if not continuation
144 res=inst.runcode(code.Get(),true,"(stdin)", false,false,true);
145 if (res<=0) code.Set("");
146 // res>0 means need more lines
148 while (inst.run_deferred());
151 else
153 inst.loadfile(scriptfn,NULL,true);
154 while (inst.run_deferred());
157 return 0;
160 #ifndef _WIN32
161 INT_PTR SWELLAppMain(int msg, INT_PTR parm1, INT_PTR parm2)
163 return 0;
165 #endif