]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/code.c
Add Nasal Vs. 1.5
[simgear.git] / simgear / nasal / code.c
1 #include "nasal.h"
2 #include "code.h"
3
4 ////////////////////////////////////////////////////////////////////////
5 // Debugging stuff. ////////////////////////////////////////////////////
6 ////////////////////////////////////////////////////////////////////////
7 #if !defined(DEBUG_NASAL)
8 # define DBG(expr) /* noop */
9 #else
10 # define DBG(expr) expr
11 # include <stdio.h>
12 # include <stdlib.h>
13 #endif
14 char* opStringDEBUG(int op);
15 void printOpDEBUG(int ip, int op);
16 void printRefDEBUG(naRef r);
17 void printStackDEBUG(struct Context* ctx);
18 ////////////////////////////////////////////////////////////////////////
19
20 // FIXME: need to store a list of all contexts
21 struct Context globalContext;
22
23 #define ERR(c, msg) naRuntimeError((c),(msg))
24 void naRuntimeError(struct Context* c, char* msg)
25
26     c->error = msg;
27     longjmp(c->jumpHandle, 1);
28 }
29
30 int boolify(struct Context* ctx, naRef r)
31 {
32     if(IS_NIL(r)) return 0;
33     if(IS_NUM(r)) return r.num != 0;
34     if(IS_STR(r)) return 1;
35     ERR(ctx, "non-scalar used in boolean context");
36     return 0;
37 }
38
39 static double numify(struct Context* ctx, naRef o)
40 {
41     double n;
42     if(IS_NUM(o)) return o.num;
43     else if(IS_NIL(o)) ERR(ctx, "nil used in numeric context");
44     else if(!IS_STR(o)) ERR(ctx, "non-scalar in numeric context");
45     else if(naStr_tonum(o, &n)) return n;
46     else ERR(ctx, "non-numeric string in numeric context");
47     return 0;
48 }
49
50 static naRef stringify(struct Context* ctx, naRef r)
51 {
52     if(IS_STR(r)) return r;
53     if(IS_NUM(r)) return naStr_fromnum(naNewString(ctx), r.num);
54     ERR(ctx, "non-scalar in string context");
55     return naNil();
56 }
57
58 static int checkVec(struct Context* ctx, naRef vec, naRef idx)
59 {
60     int i = (int)numify(ctx, idx);
61     if(i < 0 || i >= vec.ref.ptr.vec->size)
62         ERR(ctx, "vector index out of bounds");
63     return i;
64 }
65
66 static naRef containerGet(struct Context* ctx, naRef box, naRef key)
67 {
68     naRef result = naNil();
69     if(!IS_SCALAR(key)) ERR(ctx, "container index not scalar");
70     if(IS_HASH(box)) {
71         if(!naHash_get(box, key, &result))
72             ERR(ctx, "undefined value in container");
73     } else if(IS_VEC(box)) {
74         result = naVec_get(box, checkVec(ctx, box, key));
75     } else {
76         ERR(ctx, "extract from non-container");
77     }
78     return result;
79 }
80
81 static void containerSet(struct Context* ctx, naRef box, naRef key, naRef val)
82 {
83     if(!IS_SCALAR(key))   ERR(ctx, "container index not scalar");
84     else if(IS_HASH(box)) naHash_set(box, key, val);
85     else if(IS_VEC(box))  naVec_set(box, checkVec(ctx, box, key), val);
86     else                  ERR(ctx, "insert into non-container");
87 }
88
89 static void initContext(struct Context* c)
90 {
91     int i;
92     for(i=0; i<NUM_NASAL_TYPES; i++)
93         naGC_init(&(c->pools[i]), i);
94
95     c->fTop = c->opTop = c->markTop = 0;
96
97     naBZero(c->fStack, MAX_RECURSION * sizeof(struct Frame));
98     naBZero(c->opStack, MAX_STACK_DEPTH * sizeof(naRef));
99
100     // Make sure the args vectors (which are static with the context)
101     // are initialized to nil.
102     for(i=0; i<MAX_RECURSION; i++)
103         c->fStack[i].args = naNil();
104
105     c->argPool = naNewVector(c);
106
107     // Note we can't use naNewVector() for this; it requires that
108     // temps exist first.
109     c->temps = naObj(T_VEC, naGC_get(&(c->pools[T_VEC])));
110
111     c->save = naNil();
112
113     // Cache pre-calculated "me", "arg" and "parents" scalars
114     c->meRef = naStr_fromdata(naNewString(c), "me", 2);
115     c->argRef = naStr_fromdata(naNewString(c), "arg", 3);
116     c->parentsRef = naStr_fromdata(naNewString(c), "parents", 7);
117 }
118
119 struct Context* naNewContext()
120 {
121     // FIXME: need more than one!
122     struct Context* c = &globalContext;
123     initContext(c);
124     return c;
125 }
126
127 void naGarbageCollect()
128 {
129     int i;
130     struct Context* c = &globalContext; // FIXME: more than one!
131
132     for(i=0; i < c->fTop; i++) {
133         naGC_mark(c->fStack[i].func);
134         naGC_mark(c->fStack[i].locals);
135     }
136     for(i=0; i < MAX_RECURSION; i++)
137         naGC_mark(c->fStack[i].args); // collect *all* the argument lists
138     for(i=0; i < c->opTop; i++)
139         naGC_mark(c->opStack[i]);
140
141     naGC_mark(c->argPool);
142     naGC_mark(c->temps);
143     naGC_mark(c->save);
144
145     naGC_mark(c->meRef);
146     naGC_mark(c->argRef);
147     naGC_mark(c->parentsRef);
148
149     // Finally collect all the freed objects
150     for(i=0; i<NUM_NASAL_TYPES; i++)
151         naGC_reap(&(c->pools[i]));
152 }
153
154 void setupFuncall(struct Context* ctx, naRef func, naRef args)
155 {
156     struct Frame* f;
157     f = &(ctx->fStack[ctx->fTop++]);
158     f->func = func;
159     f->ip = 0;
160     f->bp = ctx->opTop;
161     f->line = 0;
162
163     DBG(printf("Entering frame %d\n", ctx->fTop-1);)
164
165     if(!IS_REF(func))
166         ERR(ctx, "function/method call invoked on uncallable object");
167
168     f->args = args;
169     if(IS_CCODE(func.ref.ptr.func->code)) {
170         f->locals = naNil();
171     } else if(IS_CODE(func.ref.ptr.func->code)) {
172         f->locals = naNewHash(ctx);
173         naHash_set(f->locals, ctx->argRef, args);
174     } else {
175         ERR(ctx, "function/method call invoked on uncallable object");
176     }
177 }
178
179 static naRef evalAndOr(struct Context* ctx, int op, naRef ra, naRef rb)
180 {
181     int a = boolify(ctx, ra);
182     int b = boolify(ctx, rb);
183     int result;
184     if(op == OP_AND) result = a && b ? 1 : 0;
185     else             result = a || b ? 1 : 0;
186     return naNum(result);
187 }
188
189 static naRef evalEquality(int op, naRef ra, naRef rb)
190 {
191     int result = naEqual(ra, rb);
192     return naNum((op==OP_EQ) ? result : !result);
193 }
194
195 static naRef evalBinaryNumeric(struct Context* ctx, int op, naRef ra, naRef rb)
196 {
197     double a = numify(ctx, ra), b = numify(ctx, rb);
198     switch(op) {
199     case OP_PLUS:  return naNum(a + b);
200     case OP_MINUS: return naNum(a - b);
201     case OP_MUL:   return naNum(a * b);
202     case OP_DIV:   return naNum(a / b);
203     case OP_LT:    return naNum(a < b ? 1 : 0);
204     case OP_LTE:   return naNum(a <= b ? 1 : 0);
205     case OP_GT:    return naNum(a > b ? 1 : 0);
206     case OP_GTE:   return naNum(a >= b ? 1 : 0);
207     }
208     return naNil();
209 }
210
211 // When a code object comes out of the constant pool and shows up on
212 // the stack, it needs to be bound with the lexical context.
213 static naRef bindFunction(struct Context* ctx, struct Frame* f, naRef code)
214 {
215     naRef next = f->func.ref.ptr.func->closure;
216     naRef closure = naNewClosure(ctx, f->locals, next);
217     naRef result = naNewFunc(ctx, code);
218     result.ref.ptr.func->closure = closure;
219     return result;
220 }
221
222 static int getClosure(struct naClosure* c, naRef sym, naRef* result)
223 {
224     while(c) {
225         if(naHash_get(c->namespace, sym, result)) return 1;
226         c = c->next.ref.ptr.closure;
227     }
228     return 0;
229 }
230
231 // Get a local symbol, or check the closure list if it isn't there
232 static naRef getLocal(struct Context* ctx, struct Frame* f, naRef sym)
233 {
234     naRef result;
235     if(!naHash_get(f->locals, sym, &result)) {
236         naRef c = f->func.ref.ptr.func->closure;
237         if(!getClosure(c.ref.ptr.closure, sym, &result))
238             ERR(ctx, "undefined symbol");
239     }
240     return result;
241 }
242
243 static int setClosure(naRef closure, naRef sym, naRef val)
244 {
245     struct naClosure* c = closure.ref.ptr.closure;
246     if(c == 0) { return 0; }
247     else if(naHash_tryset(c->namespace, sym, val)) { return 1; }
248     else { return setClosure(c->next, sym, val); }
249 }
250
251 static naRef setLocal(struct Frame* f, naRef sym, naRef val)
252 {
253     // Try the locals first, if not already there try the closures in
254     // order.  Finally put it in the locals if nothing matched.
255     if(!naHash_tryset(f->locals, sym, val))
256         if(!setClosure(f->func.ref.ptr.func->closure, sym, val))
257             naHash_set(f->locals, sym, val);
258     return val;
259 }
260
261 // Recursively descend into the parents lists
262 static int getMember(struct Context* ctx, naRef obj, naRef fld, naRef* result)
263 {
264     naRef p;
265     if(!IS_HASH(obj)) ERR(ctx, "non-objects have no members");
266     if(naHash_get(obj, fld, result)) {
267         return 1;
268     } else if(naHash_get(obj, ctx->parentsRef, &p)) {
269         int i;
270         if(!IS_VEC(p)) ERR(ctx, "parents field not vector");
271         for(i=0; i<p.ref.ptr.vec->size; i++)
272             if(getMember(ctx, p.ref.ptr.vec->array[i], fld, result))
273                 return 1;
274     }
275     return 0;
276 }
277
278 static void PUSH(struct Context* ctx, naRef r)
279 {
280     if(ctx->opTop >= MAX_STACK_DEPTH) ERR(ctx, "stack overflow");
281     ctx->opStack[ctx->opTop++] = r;
282 }
283
284 static naRef POP(struct Context* ctx)
285 {
286     if(ctx->opTop == 0) ERR(ctx, "BUG: stack underflow");
287     return ctx->opStack[--ctx->opTop];
288 }
289
290 static naRef TOP(struct Context* ctx)
291 {
292     if(ctx->opTop == 0) ERR(ctx, "BUG: stack underflow");
293     return ctx->opStack[ctx->opTop-1];
294 }
295
296 static int ARG16(unsigned char* byteCode, struct Frame* f)
297 {
298     int arg = byteCode[f->ip]<<8 | byteCode[f->ip+1];
299     f->ip += 2;
300     return arg;
301 }
302
303 // OP_EACH works like a vector get, except that it leaves the vector
304 // and index on the stack, increments the index after use, and pops
305 // the arguments and pushes a nil if the index is beyond the end.
306 static void evalEach(struct Context* ctx)
307 {
308     int idx = (int)(ctx->opStack[ctx->opTop-1].num);
309     naRef vec = ctx->opStack[ctx->opTop-2];
310     if(idx >= vec.ref.ptr.vec->size) {
311         ctx->opTop -= 2; // pop two values
312         PUSH(ctx, naNil());
313         return;
314     }
315     ctx->opStack[ctx->opTop-1].num = idx+1; // modify in place
316     PUSH(ctx, naVec_get(vec, idx));
317 }
318
319 static void run1(struct Context* ctx, struct Frame* f, naRef code)
320 {
321     naRef a, b, c;
322     struct naCode* cd = code.ref.ptr.code;
323     int op, arg;
324
325     if(f->ip >= cd->nBytes) {
326         DBG(printf("Done with frame %d\n", ctx->fTop-1);)
327         ctx->fTop--;
328         if(ctx->fTop <= 0)
329             ctx->done = 1;
330         return;
331     }
332
333     op = cd->byteCode[f->ip++];
334     DBG(printf("Stack Depth: %d\n", ctx->opTop));
335     DBG(printOpDEBUG(f->ip-1, op));
336     switch(op) {
337     case OP_POP:
338         POP(ctx);
339         break;
340     case OP_DUP:
341         PUSH(ctx, ctx->opStack[ctx->opTop-1]);
342         break;
343     case OP_XCHG:
344         a = POP(ctx); b = POP(ctx);
345         PUSH(ctx, a); PUSH(ctx, b);
346         break;
347     case OP_PLUS: case OP_MUL: case OP_DIV: case OP_MINUS:
348     case OP_LT: case OP_LTE: case OP_GT: case OP_GTE:
349         a = POP(ctx); b = POP(ctx);
350         PUSH(ctx, evalBinaryNumeric(ctx, op, b, a));
351         break;
352     case OP_EQ: case OP_NEQ:
353         a = POP(ctx); b = POP(ctx);
354         PUSH(ctx, evalEquality(op, b, a));
355         break;
356     case OP_AND: case OP_OR:
357         a = POP(ctx); b = POP(ctx);
358         PUSH(ctx, evalAndOr(ctx, op, a, b));
359         break;
360     case OP_CAT:
361         // stringify can call the GC, so don't take stuff of the stack!
362         if(ctx->opTop <= 1) ERR(ctx, "BUG: stack underflow");
363         a = stringify(ctx, ctx->opStack[ctx->opTop-1]);
364         b = stringify(ctx, ctx->opStack[ctx->opTop-2]);
365         c = naStr_concat(naNewString(ctx), b, a);
366         ctx->opTop -= 2;
367         PUSH(ctx, c);
368         break;
369     case OP_NEG:
370         a = POP(ctx);
371         PUSH(ctx, naNum(-numify(ctx, a)));
372         break;
373     case OP_NOT:
374         a = POP(ctx);
375         PUSH(ctx, naNum(boolify(ctx, a) ? 0 : 1));
376         break;
377     case OP_PUSHCONST:
378         a = cd->constants[ARG16(cd->byteCode, f)];
379         if(IS_CODE(a)) a = bindFunction(ctx, f, a);
380         PUSH(ctx, a);
381         break;
382     case OP_PUSHONE:
383         PUSH(ctx, naNum(1));
384         break;
385     case OP_PUSHZERO:
386         PUSH(ctx, naNum(0));
387         break;
388     case OP_PUSHNIL:
389         PUSH(ctx, naNil());
390         break;
391     case OP_NEWVEC:
392         PUSH(ctx, naNewVector(ctx));
393         break;
394     case OP_VAPPEND:
395         b = POP(ctx); a = TOP(ctx);
396         naVec_append(a, b);
397         break;
398     case OP_NEWHASH:
399         PUSH(ctx, naNewHash(ctx));
400         break;
401     case OP_HAPPEND:
402         c = POP(ctx); b = POP(ctx); a = TOP(ctx); // a,b,c: hash, key, val
403         naHash_set(a, b, c);
404         break;
405     case OP_LOCAL:
406         a = getLocal(ctx, f, POP(ctx));
407         PUSH(ctx, a);
408         break;
409     case OP_SETLOCAL:
410         a = POP(ctx); b = POP(ctx);
411         PUSH(ctx, setLocal(f, b, a));
412         break;
413     case OP_MEMBER:
414         a = POP(ctx); b = POP(ctx);
415         if(!getMember(ctx, b, a, &c))
416             ERR(ctx, "no such member");
417         PUSH(ctx, c);
418         break;
419     case OP_SETMEMBER:
420         c = POP(ctx); b = POP(ctx); a = POP(ctx); // a,b,c: hash, key, val
421         if(!IS_HASH(a)) ERR(ctx, "non-objects have no members");
422         naHash_set(a, b, c);
423         PUSH(ctx, c);
424         break;
425     case OP_INSERT:
426         c = POP(ctx); b = POP(ctx); a = POP(ctx); // a,b,c: box, key, val
427         containerSet(ctx, a, b, c);
428         PUSH(ctx, c);
429         break;
430     case OP_EXTRACT:
431         b = POP(ctx); a = POP(ctx); // a,b: box, key
432         PUSH(ctx, containerGet(ctx, a, b));
433         break;
434     case OP_JMP:
435         f->ip = ARG16(cd->byteCode, f);
436         DBG(printf("   [Jump to: %d]\n", f->ip);)
437         break;
438     case OP_JIFNIL:
439         arg = ARG16(cd->byteCode, f);
440         a = TOP(ctx);
441         if(IS_NIL(a)) {
442             POP(ctx); // Pops **ONLY** if it's nil!
443             f->ip = arg;
444             DBG(printf("   [Jump to: %d]\n", f->ip);)
445         }
446         break;
447     case OP_JIFNOT:
448         arg = ARG16(cd->byteCode, f);
449         if(!boolify(ctx, POP(ctx))) {
450             f->ip = arg;
451             DBG(printf("   [Jump to: %d]\n", f->ip);)
452         }
453         break;
454     case OP_FCALL:
455         b = POP(ctx); a = POP(ctx); // a,b = func, args
456         setupFuncall(ctx, a, b);
457         break;
458     case OP_MCALL:
459         c = POP(ctx); b = POP(ctx); a = POP(ctx); // a,b,c = obj, func, args
460         setupFuncall(ctx, b, c);
461         naHash_set(ctx->fStack[ctx->fTop-1].locals, ctx->meRef, a);
462         break;
463     case OP_RETURN:
464         a = POP(ctx);
465         ctx->opTop = f->bp; // restore the correct stack frame!
466         ctx->fTop--;
467         ctx->fStack[ctx->fTop].args.ref.ptr.vec->size = 0;
468         naVec_append(ctx->argPool, ctx->fStack[ctx->fTop].args);
469         PUSH(ctx, a);
470         break;
471     case OP_LINE:
472         f->line = ARG16(cd->byteCode, f);
473         break;
474     case OP_EACH:
475         evalEach(ctx);
476         break;
477     case OP_MARK: // save stack state (e.g. "setjmp")
478         ctx->markStack[ctx->markTop++] = ctx->opTop;
479         break;
480     case OP_UNMARK: // pop stack state set by mark
481         ctx->markTop--;
482         break;
483     case OP_BREAK: // restore stack state (FOLLOW WITH JMP!)
484         ctx->opTop = ctx->markStack[--ctx->markTop];
485         break;
486     case OP_NEWARGS: // push a new function arg vector
487         PUSH(ctx, (naVec_size(ctx->argPool) ?
488                    naVec_removelast(ctx->argPool) : naNewVector(ctx)));
489         break;
490     default:
491         ERR(ctx, "BUG: bad opcode");
492     }
493
494     if(ctx->fTop <= 0)
495         ctx->done = 1;
496 }
497
498 static void nativeCall(struct Context* ctx, struct Frame* f, naRef ccode)
499 {
500     naCFunction fptr = ccode.ref.ptr.ccode->fptr;
501     naRef result = (*fptr)(ctx, f->args);
502     ctx->fTop--;
503     ctx->fStack[ctx->fTop].args.ref.ptr.vec->size = 0;
504     PUSH(ctx, result);
505 }
506
507 void naSave(struct Context* ctx, naRef obj)
508 {
509     ctx->save = obj;
510 }
511
512 int naStackDepth(struct Context* ctx)
513 {
514     return ctx->fTop;
515 }
516
517 int naGetLine(struct Context* ctx, int frame)
518 {
519     return ctx->fStack[ctx->fTop-1-frame].line;
520 }
521
522 naRef naGetSourceFile(struct Context* ctx, int frame)
523 {
524     naRef f = ctx->fStack[ctx->fTop-1-frame].func;
525     f = f.ref.ptr.func->code;
526     return f.ref.ptr.code->srcFile;
527 }
528
529 char* naGetError(struct Context* ctx)
530 {
531     return ctx->error;
532 }
533
534 static naRef run(naContext ctx)
535 {
536     // Return early if an error occurred.  It will be visible to the
537     // caller via naGetError().
538     ctx->error = 0;
539     if(setjmp(ctx->jumpHandle))
540         return naNil();
541     
542     ctx->done = 0;
543     while(!ctx->done) {
544         struct Frame* f = &(ctx->fStack[ctx->fTop-1]);
545         naRef code = f->func.ref.ptr.func->code;
546         if(IS_CCODE(code)) nativeCall(ctx, f, code);
547         else               run1(ctx, f, code);
548         
549         ctx->temps.ref.ptr.vec->size = 0; // Reset the temporaries
550         DBG(printStackDEBUG(ctx);)
551     }
552
553     DBG(printStackDEBUG(ctx);)
554     return ctx->opStack[--ctx->opTop];
555 }
556
557 naRef naBindFunction(naContext ctx, naRef code, naRef closure)
558 {
559     naRef func = naNewFunc(ctx, code);
560     func.ref.ptr.func->closure = naNewClosure(ctx, closure, naNil());
561     return func;
562 }
563
564 naRef naCall(naContext ctx, naRef func, naRef args, naRef obj, naRef locals)
565 {
566     // We might have to allocate objects, which can call the GC.  But
567     // the call isn't on the Nasal stack yet, so the GC won't find our
568     // C-space arguments.
569     naVec_append(ctx->temps, func);
570     naVec_append(ctx->temps, args);
571     naVec_append(ctx->temps, obj);
572     naVec_append(ctx->temps, locals);
573
574     if(IS_NIL(args))
575         args = naNewVector(ctx);
576     if(IS_NIL(locals))
577         locals = naNewHash(ctx);
578     if(!IS_FUNC(func)) {
579         // Generate a noop closure for bare code objects
580         naRef code = func;
581         func = naNewFunc(ctx, code);
582         func.ref.ptr.func->closure = naNewClosure(ctx, locals, naNil());
583     }
584     if(!IS_NIL(obj))
585         naHash_set(locals, ctx->meRef, obj);
586
587     ctx->fTop = ctx->opTop = ctx->markTop = 0;
588     setupFuncall(ctx, func, args);
589     ctx->fStack[ctx->fTop-1].locals = locals;
590
591     return run(ctx);
592 }
593