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

Dependents:   Squirrel

Committer:
jhnwkmn
Date:
Tue Dec 16 11:39:42 2014 +0000
Revision:
3:7268a3ceaffc
Parent:
0:97a4f8cc534c
Accepts \r as line terminator as well.

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 #ifndef _SQFUNCTION_H_
jhnwkmn 0:97a4f8cc534c 3 #define _SQFUNCTION_H_
jhnwkmn 0:97a4f8cc534c 4
jhnwkmn 0:97a4f8cc534c 5 #include "sqopcodes.h"
jhnwkmn 0:97a4f8cc534c 6
jhnwkmn 0:97a4f8cc534c 7 enum SQOuterType {
jhnwkmn 0:97a4f8cc534c 8 otLOCAL = 0,
jhnwkmn 0:97a4f8cc534c 9 otOUTER = 1
jhnwkmn 0:97a4f8cc534c 10 };
jhnwkmn 0:97a4f8cc534c 11
jhnwkmn 0:97a4f8cc534c 12 struct SQOuterVar
jhnwkmn 0:97a4f8cc534c 13 {
jhnwkmn 0:97a4f8cc534c 14
jhnwkmn 0:97a4f8cc534c 15 SQOuterVar(){}
jhnwkmn 0:97a4f8cc534c 16 SQOuterVar(const SQObjectPtr &name,const SQObjectPtr &src,SQOuterType t)
jhnwkmn 0:97a4f8cc534c 17 {
jhnwkmn 0:97a4f8cc534c 18 _name = name;
jhnwkmn 0:97a4f8cc534c 19 _src=src;
jhnwkmn 0:97a4f8cc534c 20 _type=t;
jhnwkmn 0:97a4f8cc534c 21 }
jhnwkmn 0:97a4f8cc534c 22 SQOuterVar(const SQOuterVar &ov)
jhnwkmn 0:97a4f8cc534c 23 {
jhnwkmn 0:97a4f8cc534c 24 _type=ov._type;
jhnwkmn 0:97a4f8cc534c 25 _src=ov._src;
jhnwkmn 0:97a4f8cc534c 26 _name=ov._name;
jhnwkmn 0:97a4f8cc534c 27 }
jhnwkmn 0:97a4f8cc534c 28 SQOuterType _type;
jhnwkmn 0:97a4f8cc534c 29 SQObjectPtr _name;
jhnwkmn 0:97a4f8cc534c 30 SQObjectPtr _src;
jhnwkmn 0:97a4f8cc534c 31 };
jhnwkmn 0:97a4f8cc534c 32
jhnwkmn 0:97a4f8cc534c 33 struct SQLocalVarInfo
jhnwkmn 0:97a4f8cc534c 34 {
jhnwkmn 0:97a4f8cc534c 35 SQLocalVarInfo():_start_op(0),_end_op(0),_pos(0){}
jhnwkmn 0:97a4f8cc534c 36 SQLocalVarInfo(const SQLocalVarInfo &lvi)
jhnwkmn 0:97a4f8cc534c 37 {
jhnwkmn 0:97a4f8cc534c 38 _name=lvi._name;
jhnwkmn 0:97a4f8cc534c 39 _start_op=lvi._start_op;
jhnwkmn 0:97a4f8cc534c 40 _end_op=lvi._end_op;
jhnwkmn 0:97a4f8cc534c 41 _pos=lvi._pos;
jhnwkmn 0:97a4f8cc534c 42 }
jhnwkmn 0:97a4f8cc534c 43 SQObjectPtr _name;
jhnwkmn 0:97a4f8cc534c 44 SQUnsignedInteger _start_op;
jhnwkmn 0:97a4f8cc534c 45 SQUnsignedInteger _end_op;
jhnwkmn 0:97a4f8cc534c 46 SQUnsignedInteger _pos;
jhnwkmn 0:97a4f8cc534c 47 };
jhnwkmn 0:97a4f8cc534c 48
jhnwkmn 0:97a4f8cc534c 49 struct SQLineInfo { SQInteger _line;SQInteger _op; };
jhnwkmn 0:97a4f8cc534c 50
jhnwkmn 0:97a4f8cc534c 51 typedef sqvector<SQOuterVar> SQOuterVarVec;
jhnwkmn 0:97a4f8cc534c 52 typedef sqvector<SQLocalVarInfo> SQLocalVarInfoVec;
jhnwkmn 0:97a4f8cc534c 53 typedef sqvector<SQLineInfo> SQLineInfoVec;
jhnwkmn 0:97a4f8cc534c 54
jhnwkmn 0:97a4f8cc534c 55 #define _FUNC_SIZE(ni,nl,nparams,nfuncs,nouters,nlineinf,localinf,defparams) (sizeof(SQFunctionProto) \
jhnwkmn 0:97a4f8cc534c 56 +((ni-1)*sizeof(SQInstruction))+(nl*sizeof(SQObjectPtr)) \
jhnwkmn 0:97a4f8cc534c 57 +(nparams*sizeof(SQObjectPtr))+(nfuncs*sizeof(SQObjectPtr)) \
jhnwkmn 0:97a4f8cc534c 58 +(nouters*sizeof(SQOuterVar))+(nlineinf*sizeof(SQLineInfo)) \
jhnwkmn 0:97a4f8cc534c 59 +(localinf*sizeof(SQLocalVarInfo))+(defparams*sizeof(SQInteger)))
jhnwkmn 0:97a4f8cc534c 60
jhnwkmn 0:97a4f8cc534c 61
jhnwkmn 0:97a4f8cc534c 62 struct SQFunctionProto : public CHAINABLE_OBJ
jhnwkmn 0:97a4f8cc534c 63 {
jhnwkmn 0:97a4f8cc534c 64 private:
jhnwkmn 0:97a4f8cc534c 65 SQFunctionProto(SQSharedState *ss);
jhnwkmn 0:97a4f8cc534c 66 ~SQFunctionProto();
jhnwkmn 0:97a4f8cc534c 67
jhnwkmn 0:97a4f8cc534c 68 public:
jhnwkmn 0:97a4f8cc534c 69 static SQFunctionProto *Create(SQSharedState *ss,SQInteger ninstructions,
jhnwkmn 0:97a4f8cc534c 70 SQInteger nliterals,SQInteger nparameters,
jhnwkmn 0:97a4f8cc534c 71 SQInteger nfunctions,SQInteger noutervalues,
jhnwkmn 0:97a4f8cc534c 72 SQInteger nlineinfos,SQInteger nlocalvarinfos,SQInteger ndefaultparams)
jhnwkmn 0:97a4f8cc534c 73 {
jhnwkmn 0:97a4f8cc534c 74 SQFunctionProto *f;
jhnwkmn 0:97a4f8cc534c 75 //I compact the whole class and members in a single memory allocation
jhnwkmn 0:97a4f8cc534c 76 f = (SQFunctionProto *)sq_vm_malloc(_FUNC_SIZE(ninstructions,nliterals,nparameters,nfunctions,noutervalues,nlineinfos,nlocalvarinfos,ndefaultparams));
jhnwkmn 0:97a4f8cc534c 77 new (f) SQFunctionProto(ss);
jhnwkmn 0:97a4f8cc534c 78 f->_ninstructions = ninstructions;
jhnwkmn 0:97a4f8cc534c 79 f->_literals = (SQObjectPtr*)&f->_instructions[ninstructions];
jhnwkmn 0:97a4f8cc534c 80 f->_nliterals = nliterals;
jhnwkmn 0:97a4f8cc534c 81 f->_parameters = (SQObjectPtr*)&f->_literals[nliterals];
jhnwkmn 0:97a4f8cc534c 82 f->_nparameters = nparameters;
jhnwkmn 0:97a4f8cc534c 83 f->_functions = (SQObjectPtr*)&f->_parameters[nparameters];
jhnwkmn 0:97a4f8cc534c 84 f->_nfunctions = nfunctions;
jhnwkmn 0:97a4f8cc534c 85 f->_outervalues = (SQOuterVar*)&f->_functions[nfunctions];
jhnwkmn 0:97a4f8cc534c 86 f->_noutervalues = noutervalues;
jhnwkmn 0:97a4f8cc534c 87 f->_lineinfos = (SQLineInfo *)&f->_outervalues[noutervalues];
jhnwkmn 0:97a4f8cc534c 88 f->_nlineinfos = nlineinfos;
jhnwkmn 0:97a4f8cc534c 89 f->_localvarinfos = (SQLocalVarInfo *)&f->_lineinfos[nlineinfos];
jhnwkmn 0:97a4f8cc534c 90 f->_nlocalvarinfos = nlocalvarinfos;
jhnwkmn 0:97a4f8cc534c 91 f->_defaultparams = (SQInteger *)&f->_localvarinfos[nlocalvarinfos];
jhnwkmn 0:97a4f8cc534c 92 f->_ndefaultparams = ndefaultparams;
jhnwkmn 0:97a4f8cc534c 93
jhnwkmn 0:97a4f8cc534c 94 _CONSTRUCT_VECTOR(SQObjectPtr,f->_nliterals,f->_literals);
jhnwkmn 0:97a4f8cc534c 95 _CONSTRUCT_VECTOR(SQObjectPtr,f->_nparameters,f->_parameters);
jhnwkmn 0:97a4f8cc534c 96 _CONSTRUCT_VECTOR(SQObjectPtr,f->_nfunctions,f->_functions);
jhnwkmn 0:97a4f8cc534c 97 _CONSTRUCT_VECTOR(SQOuterVar,f->_noutervalues,f->_outervalues);
jhnwkmn 0:97a4f8cc534c 98 //_CONSTRUCT_VECTOR(SQLineInfo,f->_nlineinfos,f->_lineinfos); //not required are 2 integers
jhnwkmn 0:97a4f8cc534c 99 _CONSTRUCT_VECTOR(SQLocalVarInfo,f->_nlocalvarinfos,f->_localvarinfos);
jhnwkmn 0:97a4f8cc534c 100 return f;
jhnwkmn 0:97a4f8cc534c 101 }
jhnwkmn 0:97a4f8cc534c 102 void Release(){
jhnwkmn 0:97a4f8cc534c 103 _DESTRUCT_VECTOR(SQObjectPtr,_nliterals,_literals);
jhnwkmn 0:97a4f8cc534c 104 _DESTRUCT_VECTOR(SQObjectPtr,_nparameters,_parameters);
jhnwkmn 0:97a4f8cc534c 105 _DESTRUCT_VECTOR(SQObjectPtr,_nfunctions,_functions);
jhnwkmn 0:97a4f8cc534c 106 _DESTRUCT_VECTOR(SQOuterVar,_noutervalues,_outervalues);
jhnwkmn 0:97a4f8cc534c 107 //_DESTRUCT_VECTOR(SQLineInfo,_nlineinfos,_lineinfos); //not required are 2 integers
jhnwkmn 0:97a4f8cc534c 108 _DESTRUCT_VECTOR(SQLocalVarInfo,_nlocalvarinfos,_localvarinfos);
jhnwkmn 0:97a4f8cc534c 109 SQInteger size = _FUNC_SIZE(_ninstructions,_nliterals,_nparameters,_nfunctions,_noutervalues,_nlineinfos,_nlocalvarinfos,_ndefaultparams);
jhnwkmn 0:97a4f8cc534c 110 this->~SQFunctionProto();
jhnwkmn 0:97a4f8cc534c 111 sq_vm_free(this,size);
jhnwkmn 0:97a4f8cc534c 112 }
jhnwkmn 0:97a4f8cc534c 113
jhnwkmn 0:97a4f8cc534c 114 const SQChar* GetLocal(SQVM *v,SQUnsignedInteger stackbase,SQUnsignedInteger nseq,SQUnsignedInteger nop);
jhnwkmn 0:97a4f8cc534c 115 SQInteger GetLine(SQInstruction *curr);
jhnwkmn 0:97a4f8cc534c 116 bool Save(SQVM *v,SQUserPointer up,SQWRITEFUNC write);
jhnwkmn 0:97a4f8cc534c 117 static bool Load(SQVM *v,SQUserPointer up,SQREADFUNC read,SQObjectPtr &ret);
jhnwkmn 0:97a4f8cc534c 118 #ifndef NO_GARBAGE_COLLECTOR
jhnwkmn 0:97a4f8cc534c 119 void Mark(SQCollectable **chain);
jhnwkmn 0:97a4f8cc534c 120 void Finalize(){ _NULL_SQOBJECT_VECTOR(_literals,_nliterals); }
jhnwkmn 0:97a4f8cc534c 121 SQObjectType GetType() {return OT_FUNCPROTO;}
jhnwkmn 0:97a4f8cc534c 122 #endif
jhnwkmn 0:97a4f8cc534c 123 SQObjectPtr _sourcename;
jhnwkmn 0:97a4f8cc534c 124 SQObjectPtr _name;
jhnwkmn 0:97a4f8cc534c 125 SQInteger _stacksize;
jhnwkmn 0:97a4f8cc534c 126 bool _bgenerator;
jhnwkmn 0:97a4f8cc534c 127 SQInteger _varparams;
jhnwkmn 0:97a4f8cc534c 128
jhnwkmn 0:97a4f8cc534c 129 SQInteger _nlocalvarinfos;
jhnwkmn 0:97a4f8cc534c 130 SQLocalVarInfo *_localvarinfos;
jhnwkmn 0:97a4f8cc534c 131
jhnwkmn 0:97a4f8cc534c 132 SQInteger _nlineinfos;
jhnwkmn 0:97a4f8cc534c 133 SQLineInfo *_lineinfos;
jhnwkmn 0:97a4f8cc534c 134
jhnwkmn 0:97a4f8cc534c 135 SQInteger _nliterals;
jhnwkmn 0:97a4f8cc534c 136 SQObjectPtr *_literals;
jhnwkmn 0:97a4f8cc534c 137
jhnwkmn 0:97a4f8cc534c 138 SQInteger _nparameters;
jhnwkmn 0:97a4f8cc534c 139 SQObjectPtr *_parameters;
jhnwkmn 0:97a4f8cc534c 140
jhnwkmn 0:97a4f8cc534c 141 SQInteger _nfunctions;
jhnwkmn 0:97a4f8cc534c 142 SQObjectPtr *_functions;
jhnwkmn 0:97a4f8cc534c 143
jhnwkmn 0:97a4f8cc534c 144 SQInteger _noutervalues;
jhnwkmn 0:97a4f8cc534c 145 SQOuterVar *_outervalues;
jhnwkmn 0:97a4f8cc534c 146
jhnwkmn 0:97a4f8cc534c 147 SQInteger _ndefaultparams;
jhnwkmn 0:97a4f8cc534c 148 SQInteger *_defaultparams;
jhnwkmn 0:97a4f8cc534c 149
jhnwkmn 0:97a4f8cc534c 150 SQInteger _ninstructions;
jhnwkmn 0:97a4f8cc534c 151 SQInstruction _instructions[1];
jhnwkmn 0:97a4f8cc534c 152 };
jhnwkmn 0:97a4f8cc534c 153
jhnwkmn 0:97a4f8cc534c 154 #endif //_SQFUNCTION_H_