The Squirrel interpreter. See http://www.squirrel-lang.org/

Dependents:   Squirrel

Committer:
jhnwkmn
Date:
Tue Dec 16 10:20:34 2014 +0000
Revision:
0:97a4f8cc534c
Initial import of Squirrel.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jhnwkmn 0:97a4f8cc534c 1 /* see copyright notice in squirrel.h */
jhnwkmn 0:97a4f8cc534c 2 #include <squirrel.h>
jhnwkmn 0:97a4f8cc534c 3 #include <sqstdaux.h>
jhnwkmn 0:97a4f8cc534c 4 #include <assert.h>
jhnwkmn 0:97a4f8cc534c 5
jhnwkmn 0:97a4f8cc534c 6 void sqstd_printcallstack(HSQUIRRELVM v)
jhnwkmn 0:97a4f8cc534c 7 {
jhnwkmn 0:97a4f8cc534c 8 SQPRINTFUNCTION pf = sq_geterrorfunc(v);
jhnwkmn 0:97a4f8cc534c 9 if(pf) {
jhnwkmn 0:97a4f8cc534c 10 SQStackInfos si;
jhnwkmn 0:97a4f8cc534c 11 SQInteger i;
jhnwkmn 0:97a4f8cc534c 12 SQFloat f;
jhnwkmn 0:97a4f8cc534c 13 const SQChar *s;
jhnwkmn 0:97a4f8cc534c 14 SQInteger level=1; //1 is to skip this function that is level 0
jhnwkmn 0:97a4f8cc534c 15 const SQChar *name=0;
jhnwkmn 0:97a4f8cc534c 16 SQInteger seq=0;
jhnwkmn 0:97a4f8cc534c 17 pf(v,_SC("\nCALLSTACK\n"));
jhnwkmn 0:97a4f8cc534c 18 while(SQ_SUCCEEDED(sq_stackinfos(v,level,&si)))
jhnwkmn 0:97a4f8cc534c 19 {
jhnwkmn 0:97a4f8cc534c 20 const SQChar *fn=_SC("unknown");
jhnwkmn 0:97a4f8cc534c 21 const SQChar *src=_SC("unknown");
jhnwkmn 0:97a4f8cc534c 22 if(si.funcname)fn=si.funcname;
jhnwkmn 0:97a4f8cc534c 23 if(si.source)src=si.source;
jhnwkmn 0:97a4f8cc534c 24 pf(v,_SC("*FUNCTION [%s()] %s line [%d]\n"),fn,src,si.line);
jhnwkmn 0:97a4f8cc534c 25 level++;
jhnwkmn 0:97a4f8cc534c 26 }
jhnwkmn 0:97a4f8cc534c 27 level=0;
jhnwkmn 0:97a4f8cc534c 28 pf(v,_SC("\nLOCALS\n"));
jhnwkmn 0:97a4f8cc534c 29
jhnwkmn 0:97a4f8cc534c 30 for(level=0;level<10;level++){
jhnwkmn 0:97a4f8cc534c 31 seq=0;
jhnwkmn 0:97a4f8cc534c 32 while((name = sq_getlocal(v,level,seq)))
jhnwkmn 0:97a4f8cc534c 33 {
jhnwkmn 0:97a4f8cc534c 34 seq++;
jhnwkmn 0:97a4f8cc534c 35 switch(sq_gettype(v,-1))
jhnwkmn 0:97a4f8cc534c 36 {
jhnwkmn 0:97a4f8cc534c 37 case OT_NULL:
jhnwkmn 0:97a4f8cc534c 38 pf(v,_SC("[%s] NULL\n"),name);
jhnwkmn 0:97a4f8cc534c 39 break;
jhnwkmn 0:97a4f8cc534c 40 case OT_INTEGER:
jhnwkmn 0:97a4f8cc534c 41 sq_getinteger(v,-1,&i);
jhnwkmn 0:97a4f8cc534c 42 pf(v,_SC("[%s] %d\n"),name,i);
jhnwkmn 0:97a4f8cc534c 43 break;
jhnwkmn 0:97a4f8cc534c 44 case OT_FLOAT:
jhnwkmn 0:97a4f8cc534c 45 sq_getfloat(v,-1,&f);
jhnwkmn 0:97a4f8cc534c 46 pf(v,_SC("[%s] %.14g\n"),name,f);
jhnwkmn 0:97a4f8cc534c 47 break;
jhnwkmn 0:97a4f8cc534c 48 case OT_USERPOINTER:
jhnwkmn 0:97a4f8cc534c 49 pf(v,_SC("[%s] USERPOINTER\n"),name);
jhnwkmn 0:97a4f8cc534c 50 break;
jhnwkmn 0:97a4f8cc534c 51 case OT_STRING:
jhnwkmn 0:97a4f8cc534c 52 sq_getstring(v,-1,&s);
jhnwkmn 0:97a4f8cc534c 53 pf(v,_SC("[%s] \"%s\"\n"),name,s);
jhnwkmn 0:97a4f8cc534c 54 break;
jhnwkmn 0:97a4f8cc534c 55 case OT_TABLE:
jhnwkmn 0:97a4f8cc534c 56 pf(v,_SC("[%s] TABLE\n"),name);
jhnwkmn 0:97a4f8cc534c 57 break;
jhnwkmn 0:97a4f8cc534c 58 case OT_ARRAY:
jhnwkmn 0:97a4f8cc534c 59 pf(v,_SC("[%s] ARRAY\n"),name);
jhnwkmn 0:97a4f8cc534c 60 break;
jhnwkmn 0:97a4f8cc534c 61 case OT_CLOSURE:
jhnwkmn 0:97a4f8cc534c 62 pf(v,_SC("[%s] CLOSURE\n"),name);
jhnwkmn 0:97a4f8cc534c 63 break;
jhnwkmn 0:97a4f8cc534c 64 case OT_NATIVECLOSURE:
jhnwkmn 0:97a4f8cc534c 65 pf(v,_SC("[%s] NATIVECLOSURE\n"),name);
jhnwkmn 0:97a4f8cc534c 66 break;
jhnwkmn 0:97a4f8cc534c 67 case OT_GENERATOR:
jhnwkmn 0:97a4f8cc534c 68 pf(v,_SC("[%s] GENERATOR\n"),name);
jhnwkmn 0:97a4f8cc534c 69 break;
jhnwkmn 0:97a4f8cc534c 70 case OT_USERDATA:
jhnwkmn 0:97a4f8cc534c 71 pf(v,_SC("[%s] USERDATA\n"),name);
jhnwkmn 0:97a4f8cc534c 72 break;
jhnwkmn 0:97a4f8cc534c 73 case OT_THREAD:
jhnwkmn 0:97a4f8cc534c 74 pf(v,_SC("[%s] THREAD\n"),name);
jhnwkmn 0:97a4f8cc534c 75 break;
jhnwkmn 0:97a4f8cc534c 76 case OT_CLASS:
jhnwkmn 0:97a4f8cc534c 77 pf(v,_SC("[%s] CLASS\n"),name);
jhnwkmn 0:97a4f8cc534c 78 break;
jhnwkmn 0:97a4f8cc534c 79 case OT_INSTANCE:
jhnwkmn 0:97a4f8cc534c 80 pf(v,_SC("[%s] INSTANCE\n"),name);
jhnwkmn 0:97a4f8cc534c 81 break;
jhnwkmn 0:97a4f8cc534c 82 case OT_WEAKREF:
jhnwkmn 0:97a4f8cc534c 83 pf(v,_SC("[%s] WEAKREF\n"),name);
jhnwkmn 0:97a4f8cc534c 84 break;
jhnwkmn 0:97a4f8cc534c 85 case OT_BOOL:{
jhnwkmn 0:97a4f8cc534c 86 sq_getinteger(v,-1,&i);
jhnwkmn 0:97a4f8cc534c 87 pf(v,_SC("[%s] %s\n"),name,i?_SC("true"):_SC("false"));
jhnwkmn 0:97a4f8cc534c 88 }
jhnwkmn 0:97a4f8cc534c 89 break;
jhnwkmn 0:97a4f8cc534c 90 default: assert(0); break;
jhnwkmn 0:97a4f8cc534c 91 }
jhnwkmn 0:97a4f8cc534c 92 sq_pop(v,1);
jhnwkmn 0:97a4f8cc534c 93 }
jhnwkmn 0:97a4f8cc534c 94 }
jhnwkmn 0:97a4f8cc534c 95 }
jhnwkmn 0:97a4f8cc534c 96 }
jhnwkmn 0:97a4f8cc534c 97
jhnwkmn 0:97a4f8cc534c 98 static SQInteger _sqstd_aux_printerror(HSQUIRRELVM v)
jhnwkmn 0:97a4f8cc534c 99 {
jhnwkmn 0:97a4f8cc534c 100 SQPRINTFUNCTION pf = sq_geterrorfunc(v);
jhnwkmn 0:97a4f8cc534c 101 if(pf) {
jhnwkmn 0:97a4f8cc534c 102 const SQChar *sErr = 0;
jhnwkmn 0:97a4f8cc534c 103 if(sq_gettop(v)>=1) {
jhnwkmn 0:97a4f8cc534c 104 if(SQ_SUCCEEDED(sq_getstring(v,2,&sErr))) {
jhnwkmn 0:97a4f8cc534c 105 pf(v,_SC("\nAN ERROR HAS OCCURED [%s]\n"),sErr);
jhnwkmn 0:97a4f8cc534c 106 }
jhnwkmn 0:97a4f8cc534c 107 else{
jhnwkmn 0:97a4f8cc534c 108 pf(v,_SC("\nAN ERROR HAS OCCURED [unknown]\n"));
jhnwkmn 0:97a4f8cc534c 109 }
jhnwkmn 0:97a4f8cc534c 110 sqstd_printcallstack(v);
jhnwkmn 0:97a4f8cc534c 111 }
jhnwkmn 0:97a4f8cc534c 112 }
jhnwkmn 0:97a4f8cc534c 113 return 0;
jhnwkmn 0:97a4f8cc534c 114 }
jhnwkmn 0:97a4f8cc534c 115
jhnwkmn 0:97a4f8cc534c 116 void _sqstd_compiler_error(HSQUIRRELVM v,const SQChar *sErr,const SQChar *sSource,SQInteger line,SQInteger column)
jhnwkmn 0:97a4f8cc534c 117 {
jhnwkmn 0:97a4f8cc534c 118 SQPRINTFUNCTION pf = sq_geterrorfunc(v);
jhnwkmn 0:97a4f8cc534c 119 if(pf) {
jhnwkmn 0:97a4f8cc534c 120 pf(v,_SC("%s line = (%d) column = (%d) : error %s\n"),sSource,line,column,sErr);
jhnwkmn 0:97a4f8cc534c 121 }
jhnwkmn 0:97a4f8cc534c 122 }
jhnwkmn 0:97a4f8cc534c 123
jhnwkmn 0:97a4f8cc534c 124 void sqstd_seterrorhandlers(HSQUIRRELVM v)
jhnwkmn 0:97a4f8cc534c 125 {
jhnwkmn 0:97a4f8cc534c 126 sq_setcompilererrorhandler(v,_sqstd_compiler_error);
jhnwkmn 0:97a4f8cc534c 127 sq_newclosure(v,_sqstd_aux_printerror,0);
jhnwkmn 0:97a4f8cc534c 128 sq_seterrorhandler(v);
jhnwkmn 0:97a4f8cc534c 129 }