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 /*
jhnwkmn 0:97a4f8cc534c 2 see copyright notice in squirrel.h
jhnwkmn 0:97a4f8cc534c 3 */
jhnwkmn 0:97a4f8cc534c 4 #include "sqpcheader.h"
jhnwkmn 0:97a4f8cc534c 5 #include <stdarg.h>
jhnwkmn 0:97a4f8cc534c 6 #include "sqvm.h"
jhnwkmn 0:97a4f8cc534c 7 #include "sqfuncproto.h"
jhnwkmn 0:97a4f8cc534c 8 #include "sqclosure.h"
jhnwkmn 0:97a4f8cc534c 9 #include "sqstring.h"
jhnwkmn 0:97a4f8cc534c 10
jhnwkmn 0:97a4f8cc534c 11 SQRESULT sq_getfunctioninfo(HSQUIRRELVM v,SQInteger level,SQFunctionInfo *fi)
jhnwkmn 0:97a4f8cc534c 12 {
jhnwkmn 0:97a4f8cc534c 13 SQInteger cssize = v->_callsstacksize;
jhnwkmn 0:97a4f8cc534c 14 if (cssize > level) {
jhnwkmn 0:97a4f8cc534c 15 SQVM::CallInfo &ci = v->_callsstack[cssize-level-1];
jhnwkmn 0:97a4f8cc534c 16 if(sq_isclosure(ci._closure)) {
jhnwkmn 0:97a4f8cc534c 17 SQClosure *c = _closure(ci._closure);
jhnwkmn 0:97a4f8cc534c 18 SQFunctionProto *proto = c->_function;
jhnwkmn 0:97a4f8cc534c 19 fi->funcid = proto;
jhnwkmn 0:97a4f8cc534c 20 fi->name = type(proto->_name) == OT_STRING?_stringval(proto->_name):_SC("unknown");
jhnwkmn 0:97a4f8cc534c 21 fi->source = type(proto->_name) == OT_STRING?_stringval(proto->_sourcename):_SC("unknown");
jhnwkmn 0:97a4f8cc534c 22 return SQ_OK;
jhnwkmn 0:97a4f8cc534c 23 }
jhnwkmn 0:97a4f8cc534c 24 }
jhnwkmn 0:97a4f8cc534c 25 return sq_throwerror(v,_SC("the object is not a closure"));
jhnwkmn 0:97a4f8cc534c 26 }
jhnwkmn 0:97a4f8cc534c 27
jhnwkmn 0:97a4f8cc534c 28 SQRESULT sq_stackinfos(HSQUIRRELVM v, SQInteger level, SQStackInfos *si)
jhnwkmn 0:97a4f8cc534c 29 {
jhnwkmn 0:97a4f8cc534c 30 SQInteger cssize = v->_callsstacksize;
jhnwkmn 0:97a4f8cc534c 31 if (cssize > level) {
jhnwkmn 0:97a4f8cc534c 32 memset(si, 0, sizeof(SQStackInfos));
jhnwkmn 0:97a4f8cc534c 33 SQVM::CallInfo &ci = v->_callsstack[cssize-level-1];
jhnwkmn 0:97a4f8cc534c 34 switch (type(ci._closure)) {
jhnwkmn 0:97a4f8cc534c 35 case OT_CLOSURE:{
jhnwkmn 0:97a4f8cc534c 36 SQFunctionProto *func = _closure(ci._closure)->_function;
jhnwkmn 0:97a4f8cc534c 37 if (type(func->_name) == OT_STRING)
jhnwkmn 0:97a4f8cc534c 38 si->funcname = _stringval(func->_name);
jhnwkmn 0:97a4f8cc534c 39 if (type(func->_sourcename) == OT_STRING)
jhnwkmn 0:97a4f8cc534c 40 si->source = _stringval(func->_sourcename);
jhnwkmn 0:97a4f8cc534c 41 si->line = func->GetLine(ci._ip);
jhnwkmn 0:97a4f8cc534c 42 }
jhnwkmn 0:97a4f8cc534c 43 break;
jhnwkmn 0:97a4f8cc534c 44 case OT_NATIVECLOSURE:
jhnwkmn 0:97a4f8cc534c 45 si->source = _SC("NATIVE");
jhnwkmn 0:97a4f8cc534c 46 si->funcname = _SC("unknown");
jhnwkmn 0:97a4f8cc534c 47 if(type(_nativeclosure(ci._closure)->_name) == OT_STRING)
jhnwkmn 0:97a4f8cc534c 48 si->funcname = _stringval(_nativeclosure(ci._closure)->_name);
jhnwkmn 0:97a4f8cc534c 49 si->line = -1;
jhnwkmn 0:97a4f8cc534c 50 break;
jhnwkmn 0:97a4f8cc534c 51 default: break; //shutup compiler
jhnwkmn 0:97a4f8cc534c 52 }
jhnwkmn 0:97a4f8cc534c 53 return SQ_OK;
jhnwkmn 0:97a4f8cc534c 54 }
jhnwkmn 0:97a4f8cc534c 55 return SQ_ERROR;
jhnwkmn 0:97a4f8cc534c 56 }
jhnwkmn 0:97a4f8cc534c 57
jhnwkmn 0:97a4f8cc534c 58 void SQVM::Raise_Error(const SQChar *s, ...)
jhnwkmn 0:97a4f8cc534c 59 {
jhnwkmn 0:97a4f8cc534c 60 va_list vl;
jhnwkmn 0:97a4f8cc534c 61 va_start(vl, s);
jhnwkmn 0:97a4f8cc534c 62 scvsprintf(_sp(rsl((SQInteger)scstrlen(s)+(NUMBER_MAX_CHAR*2))), s, vl);
jhnwkmn 0:97a4f8cc534c 63 va_end(vl);
jhnwkmn 0:97a4f8cc534c 64 _lasterror = SQString::Create(_ss(this),_spval,-1);
jhnwkmn 0:97a4f8cc534c 65 }
jhnwkmn 0:97a4f8cc534c 66
jhnwkmn 0:97a4f8cc534c 67 void SQVM::Raise_Error(const SQObjectPtr &desc)
jhnwkmn 0:97a4f8cc534c 68 {
jhnwkmn 0:97a4f8cc534c 69 _lasterror = desc;
jhnwkmn 0:97a4f8cc534c 70 }
jhnwkmn 0:97a4f8cc534c 71
jhnwkmn 0:97a4f8cc534c 72 SQString *SQVM::PrintObjVal(const SQObjectPtr &o)
jhnwkmn 0:97a4f8cc534c 73 {
jhnwkmn 0:97a4f8cc534c 74 switch(type(o)) {
jhnwkmn 0:97a4f8cc534c 75 case OT_STRING: return _string(o);
jhnwkmn 0:97a4f8cc534c 76 case OT_INTEGER:
jhnwkmn 0:97a4f8cc534c 77 scsprintf(_sp(rsl(NUMBER_MAX_CHAR+1)), _PRINT_INT_FMT, _integer(o));
jhnwkmn 0:97a4f8cc534c 78 return SQString::Create(_ss(this), _spval);
jhnwkmn 0:97a4f8cc534c 79 break;
jhnwkmn 0:97a4f8cc534c 80 case OT_FLOAT:
jhnwkmn 0:97a4f8cc534c 81 scsprintf(_sp(rsl(NUMBER_MAX_CHAR+1)), _SC("%.14g"), _float(o));
jhnwkmn 0:97a4f8cc534c 82 return SQString::Create(_ss(this), _spval);
jhnwkmn 0:97a4f8cc534c 83 break;
jhnwkmn 0:97a4f8cc534c 84 default:
jhnwkmn 0:97a4f8cc534c 85 return SQString::Create(_ss(this), GetTypeName(o));
jhnwkmn 0:97a4f8cc534c 86 }
jhnwkmn 0:97a4f8cc534c 87 }
jhnwkmn 0:97a4f8cc534c 88
jhnwkmn 0:97a4f8cc534c 89 void SQVM::Raise_IdxError(const SQObjectPtr &o)
jhnwkmn 0:97a4f8cc534c 90 {
jhnwkmn 0:97a4f8cc534c 91 SQObjectPtr oval = PrintObjVal(o);
jhnwkmn 0:97a4f8cc534c 92 Raise_Error(_SC("the index '%.50s' does not exist"), _stringval(oval));
jhnwkmn 0:97a4f8cc534c 93 }
jhnwkmn 0:97a4f8cc534c 94
jhnwkmn 0:97a4f8cc534c 95 void SQVM::Raise_CompareError(const SQObject &o1, const SQObject &o2)
jhnwkmn 0:97a4f8cc534c 96 {
jhnwkmn 0:97a4f8cc534c 97 SQObjectPtr oval1 = PrintObjVal(o1), oval2 = PrintObjVal(o2);
jhnwkmn 0:97a4f8cc534c 98 Raise_Error(_SC("comparison between '%.50s' and '%.50s'"), _stringval(oval1), _stringval(oval2));
jhnwkmn 0:97a4f8cc534c 99 }
jhnwkmn 0:97a4f8cc534c 100
jhnwkmn 0:97a4f8cc534c 101
jhnwkmn 0:97a4f8cc534c 102 void SQVM::Raise_ParamTypeError(SQInteger nparam,SQInteger typemask,SQInteger type)
jhnwkmn 0:97a4f8cc534c 103 {
jhnwkmn 0:97a4f8cc534c 104 SQObjectPtr exptypes = SQString::Create(_ss(this), _SC(""), -1);
jhnwkmn 0:97a4f8cc534c 105 SQInteger found = 0;
jhnwkmn 0:97a4f8cc534c 106 for(SQInteger i=0; i<16; i++)
jhnwkmn 0:97a4f8cc534c 107 {
jhnwkmn 0:97a4f8cc534c 108 SQInteger mask = 0x00000001 << i;
jhnwkmn 0:97a4f8cc534c 109 if(typemask & (mask)) {
jhnwkmn 0:97a4f8cc534c 110 if(found>0) StringCat(exptypes,SQString::Create(_ss(this), _SC("|"), -1), exptypes);
jhnwkmn 0:97a4f8cc534c 111 found ++;
jhnwkmn 0:97a4f8cc534c 112 StringCat(exptypes,SQString::Create(_ss(this), IdType2Name((SQObjectType)mask), -1), exptypes);
jhnwkmn 0:97a4f8cc534c 113 }
jhnwkmn 0:97a4f8cc534c 114 }
jhnwkmn 0:97a4f8cc534c 115 Raise_Error(_SC("parameter %d has an invalid type '%s' ; expected: '%s'"), nparam, IdType2Name((SQObjectType)type), _stringval(exptypes));
jhnwkmn 0:97a4f8cc534c 116 }