sqcompiler.cpp 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725
  1. /*
  2. see copyright notice in squirrel.h
  3. */
  4. #include "sqpcheader.h"
  5. #ifndef NO_COMPILER
  6. #include <stdarg.h>
  7. #include <setjmp.h>
  8. #include "sqopcodes.h"
  9. #include "sqstring.h"
  10. #include "sqfuncproto.h"
  11. #include "sqcompiler.h"
  12. #include "sqfuncstate.h"
  13. #include "sqlexer.h"
  14. #include "sqvm.h"
  15. #include "sqtable.h"
  16. enum
  17. {
  18. eFunctionType_global = 1,
  19. eFunctionType_local,
  20. eFunctionType_lambda,
  21. eFunctionType_member,
  22. eFunctionType_anonymous
  23. };
  24. #define EXPR 1
  25. #define OBJECT 2
  26. #define BASE 3
  27. #define LOCAL 4
  28. #define OUTER 5
  29. #define EXPR_STATEMENT 6 //we start from statement state
  30. struct SQExpState {
  31. SQInteger etype; /* expr. type; one of EXPR, OBJECT, BASE, OUTER, LOCAL or EXPR_STATEMENT */
  32. SQInteger epos; /* expr. location on stack; -1 for OBJECT and BASE */
  33. bool donot_get; /* signal not to deref the next value */
  34. };
  35. #define MAX_COMPILER_ERROR_LEN 256
  36. struct SQScope {
  37. SQInt32 stacksize;
  38. SQInt16 outers;
  39. SQInt16 nested;
  40. };
  41. #define BEGIN_SCOPE() SQScope __oldscope__ = _scope; \
  42. ++_scope.nested; \
  43. _scope.outers = _fs->_outers; \
  44. _scope.stacksize = _fs->GetStackSize();\
  45. _scope_consts.push_back(SQTable::Create(_ss(_vm),0));
  46. #define RESOLVE_OUTERS() if(_fs->GetStackSize() != _scope.stacksize) { \
  47. if(_fs->CountOuters(_scope.stacksize)) { \
  48. _fs->AddInstruction(_OP_CLOSE,0,_scope.stacksize); \
  49. } \
  50. }
  51. #define END_SCOPE_NO_CLOSE() { if(_fs->GetStackSize() != _scope.stacksize) { \
  52. _fs->SetStackSize(_scope.stacksize); \
  53. } \
  54. _scope = __oldscope__; \
  55. _scope_consts.pop_back();\
  56. }
  57. #define END_SCOPE() { SQInteger oldouters = _fs->_outers;\
  58. if(_fs->GetStackSize() != _scope.stacksize) { \
  59. _fs->SetStackSize(_scope.stacksize); \
  60. if(oldouters != _fs->_outers) { \
  61. _fs->AddInstruction(_OP_CLOSE,0,_scope.stacksize); \
  62. } \
  63. } \
  64. _scope = __oldscope__; \
  65. }
  66. #define BEGIN_BREAKBLE_BLOCK() SQInteger __nbreaks__=_fs->_unresolvedbreaks.size(); \
  67. SQInteger __ncontinues__=_fs->_unresolvedcontinues.size(); \
  68. _fs->_breaktargets.push_back(0);_fs->_continuetargets.push_back(0);
  69. #define END_BREAKBLE_BLOCK(continue_target) {__nbreaks__=_fs->_unresolvedbreaks.size()-__nbreaks__; \
  70. __ncontinues__=_fs->_unresolvedcontinues.size()-__ncontinues__; \
  71. if(__ncontinues__>0)ResolveContinues(_fs,__ncontinues__,continue_target); \
  72. if(__nbreaks__>0)ResolveBreaks(_fs,__nbreaks__); \
  73. _fs->_breaktargets.pop_back();_fs->_continuetargets.pop_back();}
  74. #define CASE_TK_LOCAL_CHAR_TYPES \
  75. case TK_LOCAL_STRING_T: \
  76. case TK_LOCAL_CHAR_T: \
  77. case TK_LOCAL_WCHAR_T
  78. #define CASE_TK_LOCAL_INT_TYPES \
  79. case TK_LOCAL_INT8_T: \
  80. case TK_LOCAL_INT16_T: \
  81. case TK_LOCAL_INT32_T: \
  82. case TK_LOCAL_INT64_T: \
  83. case TK_LOCAL_INT_T: \
  84. case TK_LOCAL_UINT8_T: \
  85. case TK_LOCAL_UINT16_T: \
  86. case TK_LOCAL_UINT32_T: \
  87. case TK_LOCAL_UINT64_T: \
  88. case TK_LOCAL_UINT_T: \
  89. case TK_LOCAL_SIZE_T: \
  90. case TK_LOCAL_SSIZE_T
  91. #define CASE_TK_LOCAL_FLOAT_TYPES \
  92. case TK_LOCAL_FLOAT_T: \
  93. case TK_LOCAL_DOUBLE_T: \
  94. case TK_LOCAL_LONG_DOUBLE_T
  95. #define CASE_TK_NUMBER_TYPES \
  96. CASE_TK_LOCAL_FLOAT_TYPES: \
  97. CASE_TK_LOCAL_INT_TYPES: \
  98. case TK_LOCAL_NUMBER_T
  99. #define CASE_TK_LOCAL_TYPES \
  100. CASE_TK_LOCAL_CHAR_TYPES: \
  101. CASE_TK_NUMBER_TYPES: \
  102. case TK_LOCAL_BOOL_T: \
  103. case TK_LOCAL_TABLE_T: \
  104. case TK_LOCAL_ANY_T: \
  105. case TK_LOCAL_ARRAY_T: \
  106. case TK_VOID: \
  107. case TK_LOCAL_VOIDPTR_T: \
  108. case TK_LOCAL_WEAKREF_T
  109. static SQInteger compilerReadFunc(SQUserPointer fp)
  110. {
  111. SQInteger c = fgetc((FILE *)fp);
  112. if(c == EOF)
  113. {
  114. return 0;
  115. }
  116. return c;
  117. }
  118. class SQCompiler
  119. {
  120. public:
  121. SQCompiler(SQVM *v, SQLEXREADFUNC rg, SQUserPointer up, const SQChar* sourcename,
  122. bool raiseerror, bool lineinfo, bool show_warnings, SQInteger max_nested_includes)
  123. {
  124. _vm=v;
  125. _lex.Init(_ss(v), rg, up,ThrowError,this);
  126. _sourcename = SQString::Create(_ss(v), sourcename);
  127. _lineinfo = lineinfo;_raiseerror = raiseerror;
  128. _show_warnings = show_warnings;
  129. _scope.outers = 0;
  130. _scope.stacksize = 0;
  131. _scope.nested = 0;
  132. _compilererror = NULL;
  133. _globals = SQTable::Create(_ss(_vm),0);
  134. _type_names = SQTable::Create(_ss(_vm),0);
  135. _extern_names = SQTable::Create(_ss(_vm),0);
  136. _max_nested_includes = max_nested_includes;
  137. _nested_includes_count = 0;
  138. _is_parsing_extern = false;
  139. }
  140. ~SQCompiler(){
  141. _table(_globals)->Finalize();
  142. _globals.Null();
  143. }
  144. static void ThrowError(void *ud, const SQChar *s) {
  145. SQCompiler *c = (SQCompiler *)ud;
  146. c->Error(s);
  147. }
  148. void Error(const SQChar *s, ...) //__attribute__((format(printf, 2, 3)))
  149. {
  150. va_list vl;
  151. va_start(vl, s);
  152. scvsprintf(error_buf, sizeof(error_buf), s, vl);
  153. va_end(vl);
  154. _compilererror = error_buf;
  155. longjmp(_errorjmp,1);
  156. }
  157. void Warning(const SQChar *s, ...) //__attribute__((format(printf, 2, 3)))
  158. {
  159. if(!_show_warnings) return;
  160. va_list vl;
  161. va_start(vl, s);
  162. scvfprintf(stderr, s, vl);
  163. va_end(vl);
  164. }
  165. bool CheckNameIsType(const SQObject &name){
  166. return _table(_type_names)->Exists(name);
  167. }
  168. bool CheckTypeName(const SQObject &name, bool addIfNotExists=false){
  169. bool found = _table(_type_names)->Exists(name);
  170. if(addIfNotExists && !found) {
  171. SQObjectPtr oname = name, otrue = true;
  172. _table(_type_names)->NewSlot(oname, otrue);
  173. }
  174. return found;
  175. }
  176. bool TypesGet(const SQObjectPtr &key,SQObjectPtr &val){
  177. return _table(_type_names)->Get(key,val);
  178. }
  179. bool TypesNewSlot(const SQObjectPtr &key, const SQObjectPtr &val){
  180. return _table(_type_names)->NewSlot(key,val);
  181. }
  182. int CheckExternName(const SQObject &name, bool addIfNotExists=false){
  183. SQObjectPtr orefcount;
  184. int found = _table(_extern_names)->Get(name, orefcount);
  185. if(addIfNotExists && !found) {
  186. SQObjectPtr oname = name;
  187. _integer(orefcount) = 1; //1 means only declared, 2 declared and assigned
  188. _table(_extern_names)->NewSlot(oname, orefcount);
  189. _table(_globals)->NewSlot(oname, orefcount);
  190. }
  191. return found ? _integer(orefcount) : 0;
  192. }
  193. int ExternNameSetRefCount(const SQObject &name)
  194. {
  195. int ref_count = CheckExternName(name);
  196. if(ref_count == 1)
  197. {
  198. SQObjectPtr orefcount;
  199. _integer(orefcount) = ref_count = 2;
  200. _table(_extern_names)->Set(name, orefcount);
  201. }
  202. return ref_count;
  203. }
  204. void AddGlobalName(const SQObject &name)
  205. {
  206. SQObjectPtr oname = name, otrue = true;
  207. _table(_globals)->NewSlot(oname, otrue);
  208. }
  209. void CheckGlobalName(const SQObject &name, bool addIfNotExists=false, bool checkLocals=true){
  210. const SQChar *found = NULL;
  211. if(CheckExternName(name)){
  212. found = _SC("extern");
  213. }
  214. else if(_table(_globals)->Exists(name)){
  215. found = _SC("global");
  216. }
  217. if(found) {
  218. if(checkLocals) Error(_SC("%s '%s' already declared"), found, _stringval(name));
  219. else Warning(_SC("%s:%d:%d warning %s '%s' already declared will be shadowed\n"),
  220. _stringval(_sourcename), _lex.data->currentline, _lex.data->currentcolumn,
  221. found, _stringval(name));
  222. }
  223. else if(checkLocals) CheckLocalNameScope(name, -1, false);
  224. if(addIfNotExists) {
  225. AddGlobalName(name);
  226. }
  227. }
  228. void CheckLocalNameScope(const SQObject &name, SQInteger scope, bool checkGlobals=true){
  229. SQInteger found = _fs->GetLocalVariable(name);
  230. if(found >= 0){
  231. SQLocalVarInfo &lvi = _fs->_vlocals[found];
  232. if(lvi._scope == scope)
  233. Error(_SC("local '%s' already declared"), _stringval(name));
  234. else
  235. Warning(_SC("%s:%d:%d warning local '%s' already declared will be shadowed\n"),
  236. _stringval(_sourcename), _lex.data->currentline, _lex.data->currentcolumn, _stringval(name));
  237. }
  238. else
  239. {
  240. found = _fs->FindOuterVariable(name);
  241. if(found >= 0) Warning(_SC("%s:%d:%d warning outer variable '%s' already declared will be shadowed\n"),
  242. _stringval(_sourcename), _lex.data->currentline, _lex.data->currentcolumn, _stringval(name));
  243. }
  244. if(checkGlobals) CheckGlobalName(name, false, false);
  245. SQObjectPtr strongid = name;
  246. CheckConstsExists(strongid);
  247. }
  248. bool IsConstant(const SQObject &name,SQObject &e){
  249. SQObjectPtr val;
  250. for(int i=_scope.nested-1; i >= 0; --i){
  251. if(_table(_scope_consts[i])->Get(name,val)) {
  252. e = val;
  253. return true;
  254. }
  255. }
  256. if(_table(_ss(_vm)->_consts)->Get(name,val)) {
  257. e = val;
  258. return true;
  259. }
  260. return false;
  261. }
  262. SQInteger ExpressionConstant(SQObject id) {
  263. SQObject constant;
  264. SQInteger epos;
  265. if(IsConstant(id, constant)) {
  266. /* Handle named constant */
  267. SQObjectPtr constval;
  268. SQObject constid;
  269. if(type(constant) == OT_TABLE) {
  270. Expect('.');
  271. constid = Expect(TK_IDENTIFIER);
  272. if(!_table(constant)->Get(constid, constval)) {
  273. constval.Null();
  274. Error(_SC("invalid constant [%s.%s]"), _stringval(id), _stringval(constid));
  275. }
  276. }
  277. else {
  278. constval = constant;
  279. }
  280. epos = _fs->PushTarget();
  281. /* generate direct or literal function depending on size */
  282. SQObjectType ctype = type(constval);
  283. switch(ctype) {
  284. case OT_INTEGER: EmitLoadConstInt(_integer(constval),epos); break;
  285. case OT_FLOAT: EmitLoadConstFloat(_float(constval),epos); break;
  286. default: _fs->AddInstruction(_OP_LOAD,epos,_fs->GetConstant(constval)); break;
  287. }
  288. } else {
  289. Error(_SC("invalid constant [%s]"), _stringval(id));
  290. }
  291. return epos;
  292. }
  293. void CheckConstsExists(const SQObjectPtr &key){
  294. int found = -1;
  295. for(int i=_scope.nested-1; i >= 0; --i){
  296. if(_table(_scope_consts[i])->Exists(key)) {
  297. found = i+1;
  298. break;
  299. }
  300. }
  301. if(found < 0 && _table(_ss(_vm)->_consts)->Exists(key)) found = 0;
  302. if(found == _scope.nested) {
  303. Error(_SC("constant '%s' already exists\n"), _stringval(key));
  304. }
  305. if(found >= 0) Warning(_SC("%s:%d:%d warning an already defined constant '%s' will be shadowed\n"),
  306. _stringval(_sourcename), _lex.data->currentline, _lex.data->currentcolumn, _stringval(key));
  307. }
  308. bool ConstsGet(const SQObjectPtr &key,SQObjectPtr &val){
  309. for(int i=_scope.nested-1; i >= 0; --i){
  310. if(_table(_scope_consts[i])->Get(key,val)) return true;
  311. }
  312. return _table(_ss(_vm)->_consts)->Get(key,val);
  313. }
  314. bool ConstsNewSlot(const SQObjectPtr &key, const SQObjectPtr &val){
  315. if(_scope.nested) return _table(_scope_consts[_scope.nested-1])->NewSlot(key,val);
  316. return _table(_ss(_vm)->_consts)->NewSlot(key,val);
  317. }
  318. void Lex()
  319. {
  320. _token = _lex.Lex();
  321. }
  322. SQObjectPtr GetTokenObject(SQInteger tok, bool doLex=true)
  323. {
  324. SQObjectPtr ret;
  325. switch(tok)
  326. {
  327. case TK_IDENTIFIER:
  328. ret = _fs->CreateString(_lex.data->svalue);
  329. break;
  330. case TK_STRING_LITERAL:
  331. ret = _fs->CreateString(_lex.data->svalue,_lex.data->longstr.size()-1);
  332. break;
  333. case TK_INTEGER:
  334. ret = SQObjectPtr(_lex.data->nvalue);
  335. break;
  336. case TK_FLOAT:
  337. ret = SQObjectPtr(_lex.data->fvalue);
  338. break;
  339. default:
  340. ret = _fs->CreateString(_lex.GetTokenName(_token));
  341. }
  342. if(doLex) Lex();
  343. return ret;
  344. }
  345. void ErrorIfNotToken(SQInteger tok){
  346. if(_token != tok) {
  347. if(((_token == TK_CONSTRUCTOR) || (_token == TK_DESTRUCTOR)) && tok == TK_IDENTIFIER) {
  348. //do nothing
  349. }
  350. else {
  351. const SQChar *etypename;
  352. if(tok > 255) {
  353. switch(tok)
  354. {
  355. case TK_IDENTIFIER:
  356. etypename = _SC("IDENTIFIER");
  357. break;
  358. case TK_STRING_LITERAL:
  359. etypename = _SC("STRING_LITERAL");
  360. break;
  361. case TK_INTEGER:
  362. etypename = _SC("INTEGER");
  363. break;
  364. case TK_FLOAT:
  365. etypename = _SC("FLOAT");
  366. break;
  367. default:
  368. etypename = _lex.Tok2Str(tok);
  369. }
  370. Error(_SC("expected '%s'"), etypename);
  371. }
  372. Error(_SC("expected '%c'"), tok);
  373. }
  374. }
  375. }
  376. SQObject Expect(SQInteger tok, bool doLex=true)
  377. {
  378. ErrorIfNotToken(tok);
  379. return GetTokenObject(tok, doLex);
  380. }
  381. SQObject ExpectTypeToken()
  382. {
  383. switch(_token)
  384. {
  385. CASE_TK_LOCAL_TYPES:
  386. return GetTokenObject(_token);
  387. break;
  388. case TK_IDENTIFIER:
  389. break;
  390. default:
  391. ErrorIfNotToken(TK_IDENTIFIER);
  392. }
  393. return GetTokenObject(TK_IDENTIFIER);
  394. }
  395. bool IsEndOfStatement() { return ((_lex.data->prevtoken == _SC('\n')) || (_token == SQUIRREL_EOB) || (_token == _SC('}')) || (_token == _SC(';'))); }
  396. void OptionalSemicolon()
  397. {
  398. if(_token == _SC(';')) { Lex(); return; }
  399. if(!IsEndOfStatement()) {
  400. Error(_SC("end of statement expected (; or lf) found (%d)"), _token);
  401. }
  402. }
  403. void MoveIfCurrentTargetIsLocal() {
  404. SQInteger trg = _fs->TopTarget();
  405. if(_fs->IsLocal(trg)) {
  406. trg = _fs->PopTarget(); //pops the target and moves it
  407. _fs->AddInstruction(_OP_MOVE, _fs->PushTarget(), trg);
  408. }
  409. }
  410. void Pragma()
  411. {
  412. int line = _lex.data->currentline;
  413. //int column = _lex.data->currentcolumn;
  414. Lex();
  415. SQObject id = Expect(TK_IDENTIFIER);
  416. if(scstrcmp(_stringval(id), _SC("include")) == 0)
  417. {
  418. #if SQ_ENABLE_INCLUDES
  419. SQInteger nested_count = _nested_includes_count + 1;
  420. if((_max_nested_includes <= 0) || (nested_count > _max_nested_includes))
  421. {
  422. Error(_SC("Error: too many nested includes %d %s\n"), nested_count, _stringval(id));
  423. }
  424. //do not call Lex() since next token can try search for id defined in the include
  425. //and then go global instead of local, we'll call it after including
  426. id = Expect(TK_STRING_LITERAL, false);
  427. //Warning(_SC("%s:%d:%d warning pragma include %s\n"),
  428. // _stringval(_sourcename), line, column, _stringval(id));
  429. FILE *fp = scfopen(_stringval(id), "r");
  430. if(fp != NULL)
  431. {
  432. //increment nested count
  433. ++_nested_includes_count;
  434. //save current source file and lex state
  435. SQUserPointer saved_up = _lex._up; //current userpointer
  436. SQLEXREADFUNC saved_readf = _lex._readf; //current readfunction
  437. SQInteger saved_line = _lex.data->currentline;
  438. SQInteger saved_column = _lex.data->currentcolumn;
  439. SQInteger saved_curdata = _lex.data->currdata;
  440. SQInteger saved_prevtoken = _lex.data->prevtoken;
  441. SQInteger saved_token = _token;
  442. SQObjectPtr saved_source_name = _sourcename;
  443. //set new source file
  444. _fs->_sourcename = id;
  445. _sourcename = id;
  446. _lex.ResetReader(compilerReadFunc, fp, 1);
  447. //compile the include file
  448. Lex();
  449. while(_token > 0){
  450. Statement();
  451. if(_lex.data->prevtoken != _SC('}') && _lex.data->prevtoken != _SC(';')) OptionalSemicolon();
  452. }
  453. //close file
  454. fclose(fp);
  455. //restore saved source file and lex state
  456. _fs->_sourcename = saved_source_name;
  457. _sourcename = saved_source_name;
  458. _token = saved_token;
  459. _lex.data->currdata = saved_curdata;
  460. _lex.data->prevtoken = saved_prevtoken;
  461. _lex.data->currentcolumn = saved_column;
  462. _lex.data->currentline = saved_line;
  463. _lex._readf = saved_readf;
  464. _lex._up = saved_up;
  465. --_nested_includes_count;
  466. //done let's continue working
  467. //Now we do the Lex() call skiped before compile the include file
  468. Lex();
  469. }
  470. else
  471. {
  472. Error(_SC("Error: opening include file %s\n"), _stringval(id));
  473. }
  474. #else
  475. Error(_SC("Error: includes are not enabled\n"));
  476. #endif
  477. }
  478. else
  479. {
  480. _lex.data->currentline = line;
  481. Error(_SC("Error: unknown pragma %s\n"), _stringval(id));
  482. }
  483. }
  484. bool Compile(SQObjectPtr &o)
  485. {
  486. _debugline = 1;
  487. _debugop = 0;
  488. SQFuncState funcstate(_ss(_vm), NULL,ThrowError,this);
  489. funcstate._name = SQString::Create(_ss(_vm), _SC("main"));
  490. _fs = &funcstate;
  491. _fs->AddParameter(_fs->CreateString(_SC("this")), _scope.nested+1);
  492. _fs->AddParameter(_fs->CreateString(_SC("vargv")), _scope.nested+1);
  493. _fs->_varparams = true;
  494. _fs->_sourcename = _sourcename;
  495. SQInteger stacksize = _fs->GetStackSize();
  496. if(setjmp(_errorjmp) == 0) {
  497. Lex();
  498. while(_token > 0){
  499. Statement();
  500. if(_lex.data->prevtoken != _SC('}') && _lex.data->prevtoken != _SC(';')) OptionalSemicolon();
  501. }
  502. _fs->SetStackSize(stacksize);
  503. _fs->AddLineInfos(_lex.data->currentline, _lineinfo, true);
  504. _fs->AddInstruction(_OP_RETURN, 0xFF);
  505. _fs->SetStackSize(0);
  506. o =_fs->BuildProto();
  507. #ifdef _DEBUG_DUMP
  508. _fs->Dump(_funcproto(o));
  509. #endif
  510. }
  511. else {
  512. if(_raiseerror && _ss(_vm)->_compilererrorhandler) {
  513. _ss(_vm)->_compilererrorhandler(_vm, _compilererror, type(_sourcename) == OT_STRING?_stringval(_sourcename):_SC("unknown"),
  514. _lex.data->currentline, _lex.data->currentcolumn);
  515. }
  516. _vm->_lasterror = SQString::Create(_ss(_vm), _compilererror, -1);
  517. _vm->_lasterror_line = _lex.data->currentline;
  518. _vm->_lasterror_column = _lex.data->currentcolumn;
  519. return false;
  520. }
  521. return true;
  522. }
  523. void Statements()
  524. {
  525. while(_token != _SC('}') && _token != TK_DEFAULT && _token != TK_CASE) {
  526. Statement();
  527. if(_lex.data->prevtoken != _SC('}') && _lex.data->prevtoken != _SC(';')) OptionalSemicolon();
  528. }
  529. }
  530. void Statement(bool closeframe = true)
  531. {
  532. _es.etype = EXPR_STATEMENT;
  533. SQObject id;
  534. _fs->AddLineInfos(_lex.data->currentline, _lineinfo);
  535. start_again:
  536. switch(_token){
  537. case _SC(';'): Lex(); break;
  538. case TK_IF: IfStatement(); break;
  539. case TK_WHILE: WhileStatement(); break;
  540. case TK_DO: DoWhileStatement(); break;
  541. case TK_FOR: ForStatement(); break;
  542. case TK_FOREACH: ForEachStatement(); break;
  543. case TK_SWITCH: SwitchStatement(); break;
  544. case TK_LOCAL:
  545. //case TK_CONST:
  546. CASE_TK_LOCAL_TYPES: LocalDeclStatement(); break;
  547. case TK_RETURN:
  548. case TK_YIELD: {
  549. SQOpcode op;
  550. if(_token == TK_RETURN) {
  551. op = _OP_RETURN;
  552. }
  553. else {
  554. op = _OP_YIELD;
  555. _fs->_bgenerator = true;
  556. }
  557. Lex();
  558. if(!IsEndOfStatement()) {
  559. SQInteger retexp = _fs->GetCurrentPos()+1;
  560. CommaExpr(true);
  561. if(op == _OP_RETURN && _fs->_traps > 0)
  562. _fs->AddInstruction(_OP_POPTRAP, _fs->_traps, 0);
  563. _fs->_returnexp = retexp;
  564. _fs->AddInstruction(op, 1, _fs->PopTarget(),_fs->GetStackSize());
  565. }
  566. else{
  567. if(op == _OP_RETURN && _fs->_traps > 0)
  568. _fs->AddInstruction(_OP_POPTRAP, _fs->_traps ,0);
  569. _fs->_returnexp = -1;
  570. _fs->AddInstruction(op, 0xFF,0,_fs->GetStackSize());
  571. }
  572. break;}
  573. case TK_GOTO: {
  574. //error if outside any function
  575. if(!_fs->_parent) Error(_SC("'goto' has to be in a function block"));
  576. Lex(); //ignore for now
  577. id = Expect(TK_IDENTIFIER);
  578. SQGotoLabelsInfo info;
  579. info.name = id;
  580. info.line = _lex.data->currentline; //need to get line number here
  581. info.traps = _fs->_traps;
  582. info.nested = _scope.nested;
  583. //Expect(_SC(';'));
  584. if(info.traps) _fs->AddInstruction(_OP_NOP, 0, 0); //for _OP_POPTRAP
  585. RESOLVE_OUTERS();
  586. _fs->AddInstruction(_OP_JMP, 0, -1234);
  587. //instruction pos
  588. info.pos = _fs->GetCurrentPos();
  589. _fs->_unresolvedgotos.push_back(info);
  590. }
  591. break;
  592. case TK_BREAK:
  593. if(_fs->_breaktargets.size() <= 0)Error(_SC("'break' has to be in a loop block"));
  594. if(_fs->_breaktargets.top() > 0){
  595. _fs->AddInstruction(_OP_POPTRAP, _fs->_breaktargets.top(), 0);
  596. }
  597. RESOLVE_OUTERS();
  598. _fs->AddInstruction(_OP_JMP, 0, -1234);
  599. _fs->_unresolvedbreaks.push_back(_fs->GetCurrentPos());
  600. Lex();
  601. break;
  602. case TK_CONTINUE:
  603. if(_fs->_continuetargets.size() <= 0)Error(_SC("'continue' has to be in a loop block"));
  604. if(_fs->_continuetargets.top() > 0) {
  605. _fs->AddInstruction(_OP_POPTRAP, _fs->_continuetargets.top(), 0);
  606. }
  607. RESOLVE_OUTERS();
  608. _fs->AddInstruction(_OP_JMP, 0, -1234);
  609. _fs->_unresolvedcontinues.push_back(_fs->GetCurrentPos());
  610. Lex();
  611. break;
  612. case TK_FUNCTION:
  613. FunctionStatement();
  614. break;
  615. case TK_CLASS:
  616. case TK_STRUCT:
  617. ClassStatement();
  618. break;
  619. case TK_ENUM:
  620. EnumStatement();
  621. break;
  622. case _SC('{'):{
  623. BEGIN_SCOPE();
  624. Lex();
  625. Statements();
  626. Expect(_SC('}'));
  627. if(closeframe) {
  628. END_SCOPE();
  629. }
  630. else {
  631. END_SCOPE_NO_CLOSE();
  632. }
  633. }
  634. break;
  635. case TK_TRY:
  636. TryCatchStatement();
  637. break;
  638. case TK_THROW:
  639. Lex();
  640. CommaExpr();
  641. _fs->AddInstruction(_OP_THROW, _fs->PopTarget());
  642. break;
  643. case TK_CONST:
  644. {
  645. Lex();
  646. if(_token == TK_IDENTIFIER)
  647. {
  648. id = _fs->CreateString(_lex.data->svalue);
  649. if(CheckTypeName(id)) //C/C++ type declaration;
  650. {
  651. goto start_again;
  652. }
  653. Lex();
  654. }
  655. else id = Expect(TK_IDENTIFIER);
  656. if(_token == _SC(':')) {
  657. //type specifier like typescript
  658. Lex();
  659. ExpectTypeToken(); //ignore for now
  660. }
  661. Expect('=');
  662. SQObjectPtr strongid = id;
  663. CheckLocalNameScope(id, _scope.nested);
  664. //CheckConstsExists(strongid);
  665. SQObject val = ExpectScalar();
  666. OptionalSemicolon();
  667. ConstsNewSlot(strongid,SQObjectPtr(val));
  668. }
  669. break;
  670. case TK_INLINE: //accept and ignore
  671. case TK_CONSTEXPR: //accept and ignore
  672. case TK_FRIEND:
  673. case TK_VOLATILE:
  674. Lex();
  675. goto start_again;
  676. case TK_STATIC:
  677. if(_scope.nested)
  678. {
  679. Warning(_SC("%s:%d:%d warning static cualifier is ignored\n"),
  680. _stringval(_sourcename), _lex.data->currentline, _lex.data->currentcolumn);
  681. }
  682. Lex(); //ignore it only to allow run some C/C++ code
  683. goto start_again;
  684. case TK_DECLARE: //parse as extern
  685. case TK_EXTERN: ExternDeclStatement(); break;
  686. case TK_TYPEDEF:
  687. {
  688. Lex();
  689. SQObject type_val = ExpectTypeToken();
  690. id = ExpectTypeToken();
  691. SQObjectPtr strongid = id;
  692. CheckLocalNameScope(id, _scope.nested);
  693. //Expect(_SC(';'));
  694. TypesNewSlot(strongid,SQObjectPtr(type_val));
  695. }
  696. break;
  697. case TK_USING:
  698. {
  699. Lex();
  700. id = Expect(TK_IDENTIFIER);
  701. Expect('=');
  702. SQObjectPtr strongid = id;
  703. CheckLocalNameScope(id, _scope.nested);
  704. SQObject type_val = ExpectTypeToken();
  705. //Expect(_SC(';'));
  706. TypesNewSlot(strongid,SQObjectPtr(type_val));
  707. }
  708. break;
  709. case TK_PRAGMA:
  710. Pragma();
  711. break;
  712. case TK_UNSAFE:
  713. Lex(); //ignore for now
  714. goto start_again;
  715. break;
  716. case TK_TEMPLATE:
  717. TemplateStatement();
  718. break;
  719. case TK_IDENTIFIER:{
  720. id = _fs->CreateString(_lex.data->svalue);
  721. SQInteger lhtk = _lex.LookaheadLex();
  722. if(lhtk == _SC(':'))
  723. {
  724. if(!_fs->_parent) Error(_SC("'label' has to be inside a function block"));
  725. if(!_fs->AddGotoTarget(id, _lex.data->currentline, _fs->_traps, _scope.nested))
  726. {
  727. Error(_SC("Label already declared"));
  728. }
  729. Lex(); //eat ':'
  730. Lex();
  731. break;
  732. }
  733. if(CheckTypeName(id)) //C/C++ type declaration;
  734. {
  735. if(lhtk != _SC('.'))
  736. {
  737. LocalDeclStatement();
  738. break;
  739. }
  740. }
  741. }
  742. default:
  743. CommaExpr();
  744. _fs->DiscardTarget();
  745. /*
  746. //Fagiano says that this is not a bug
  747. //and with this modification stack grow by one lement all the time
  748. if(_token == TK_IDENTIFIER){
  749. CommaExpr();
  750. if(_token == TK_IDENTIFIER){
  751. Error(_SC(" '=' expected near '%s'"), _lex.data->svalue);
  752. }
  753. }
  754. */
  755. //_fs->PopTarget();
  756. break;
  757. }
  758. _fs->SnoozeOpt();
  759. }
  760. void EmitDerefOp(SQOpcode op)
  761. {
  762. SQInteger val = _fs->PopTarget();
  763. SQInteger key = _fs->PopTarget();
  764. SQInteger src = _fs->PopTarget();
  765. _fs->AddInstruction(op,_fs->PushTarget(),src,key,val);
  766. }
  767. void Emit2ArgsOP(SQOpcode op, SQInteger p3 = 0)
  768. {
  769. SQInteger p2 = _fs->PopTarget(); //src in OP_GET
  770. SQInteger p1 = _fs->PopTarget(); //key in OP_GET
  771. _fs->AddInstruction(op,_fs->PushTarget(), p1, p2, p3);
  772. }
  773. void EmitCompoundArith(SQInteger tok, SQInteger etype, SQInteger pos)
  774. {
  775. /* Generate code depending on the expression type */
  776. switch(etype) {
  777. case LOCAL:{
  778. SQInteger p2 = _fs->PopTarget(); //src in OP_GET
  779. SQInteger p1 = _fs->PopTarget(); //key in OP_GET
  780. _fs->PushTarget(p1);
  781. //EmitCompArithLocal(tok, p1, p1, p2);
  782. _fs->AddInstruction(ChooseArithOpByToken(tok),p1, p2, p1, 0);
  783. _fs->SnoozeOpt(); //FIX: stop optimizer in retargeting opcode
  784. }
  785. break;
  786. case OBJECT:
  787. case BASE:
  788. {
  789. SQInteger val = _fs->PopTarget();
  790. SQInteger key = _fs->PopTarget();
  791. SQInteger src = _fs->PopTarget();
  792. /* _OP_COMPARITH mixes dest obj and source val in the arg1 */
  793. _fs->AddInstruction(_OP_COMPARITH, _fs->PushTarget(), (src<<16)|val, key, ChooseCompArithCharByToken(tok));
  794. }
  795. break;
  796. case OUTER:
  797. {
  798. SQInteger val = _fs->TopTarget();
  799. SQInteger tmp = _fs->PushTarget();
  800. _fs->AddInstruction(_OP_GETOUTER, tmp, pos);
  801. _fs->AddInstruction(ChooseArithOpByToken(tok), tmp, val, tmp, 0);
  802. _fs->PopTarget();
  803. _fs->PopTarget();
  804. _fs->AddInstruction(_OP_SETOUTER, _fs->PushTarget(), pos, tmp);
  805. }
  806. break;
  807. }
  808. }
  809. void CommaExpr(bool warningAssign=false)
  810. {
  811. for(Expression(warningAssign);_token == ',';_fs->PopTarget(), Lex(), CommaExpr(warningAssign));
  812. }
  813. void ErrorIfConst(){
  814. SQLocalVarInfo &vsrc = _fs->_vlocals[_fs->TopTarget()];
  815. //printf("%d %d %d %d %s\n", __LINE__, vsrc._scope, vsrc._type, vsrc._pos, vsrc._name._unVal.pString ? _stringval(vsrc._name) : "?");
  816. if(vsrc._type & _VAR_CONST) Error(_SC("can't assign to a const variable"));
  817. }
  818. void Expression(bool warningAssign=false)
  819. {
  820. SQExpState es = _es;
  821. _es.etype = EXPR;
  822. _es.epos = -1;
  823. _es.donot_get = false;
  824. SQObject id;
  825. if((_token == TK_IDENTIFIER) && (es.etype == EXPR_STATEMENT))
  826. {
  827. id = _fs->CreateString(_lex.data->svalue);
  828. }
  829. //else id = {}; //old compilers do not allow this
  830. else id._type = OT_NULL; //segfault without it with gcc -O3
  831. LogicalOrExp();
  832. switch(_token) {
  833. case _SC('='):
  834. case TK_NEWSLOT:
  835. case TK_MINUSEQ:
  836. case TK_PLUSEQ:
  837. case TK_MULEQ:
  838. case TK_DIVEQ:
  839. case TK_MODEQ:{
  840. SQInteger op = _token;
  841. SQInteger ds = _es.etype;
  842. SQInteger pos = _es.epos;
  843. if(ds == EXPR) Error(_SC("can't assign expression"));
  844. else if(ds == BASE) Error(_SC("'base' cannot be modified"));
  845. Lex(); Expression();
  846. switch(op){
  847. case TK_NEWSLOT:
  848. if(ds == OBJECT || ds == BASE) {
  849. EmitDerefOp(_OP_NEWSLOT);
  850. if((_es.epos == -1) && (es.etype == EXPR_STATEMENT)
  851. && (type(id) == OT_STRING) ) AddGlobalName(id);
  852. }
  853. else //if _derefstate != DEREF_NO_DEREF && DEREF_FIELD so is the index of a local
  854. Error(_SC("can't 'create' a local slot"));
  855. break;
  856. case _SC('='): //ASSIGN
  857. if(warningAssign) Warning(_SC("%s:%d:%d warning making assignment, maybe it's not what you want\n"),
  858. _stringval(_sourcename), _lex.data->currentline, _lex.data->currentcolumn);
  859. switch(ds) {
  860. case LOCAL:
  861. {
  862. SQInteger src = _fs->PopTarget();
  863. SQInteger dst = _fs->TopTarget();
  864. _fs->AddInstruction(_OP_MOVE, dst, src);
  865. }
  866. break;
  867. case OBJECT:
  868. case BASE:
  869. EmitDerefOp(_OP_SET);
  870. break;
  871. case OUTER:
  872. {
  873. SQInteger src = _fs->PopTarget();
  874. SQInteger dst = _fs->PushTarget();
  875. _fs->AddInstruction(_OP_SETOUTER, dst, pos, src);
  876. }
  877. }
  878. break;
  879. case TK_MINUSEQ:
  880. case TK_PLUSEQ:
  881. case TK_MULEQ:
  882. case TK_DIVEQ:
  883. case TK_MODEQ:
  884. EmitCompoundArith(op, ds, pos);
  885. break;
  886. }
  887. }
  888. break;
  889. case _SC('?'): {
  890. Lex();
  891. _fs->AddInstruction(_OP_JZ, _fs->PopTarget());
  892. SQInteger jzpos = _fs->GetCurrentPos();
  893. SQInteger trg = _fs->PushTarget();
  894. Expression();
  895. SQInteger first_exp = _fs->PopTarget();
  896. if(trg != first_exp) _fs->AddInstruction(_OP_MOVE, trg, first_exp);
  897. SQInteger endfirstexp = _fs->GetCurrentPos();
  898. _fs->AddInstruction(_OP_JMP, 0, 0);
  899. Expect(_SC(':'));
  900. SQInteger jmppos = _fs->GetCurrentPos();
  901. Expression();
  902. SQInteger second_exp = _fs->PopTarget();
  903. if(trg != second_exp) _fs->AddInstruction(_OP_MOVE, trg, second_exp);
  904. _fs->SetIntructionParam(jmppos, 1, _fs->GetCurrentPos() - jmppos);
  905. _fs->SetIntructionParam(jzpos, 1, endfirstexp - jzpos + 1);
  906. _fs->SnoozeOpt();
  907. }
  908. break;
  909. }
  910. _es = es;
  911. }
  912. template<typename T> void INVOKE_EXP(T f)
  913. {
  914. SQExpState es = _es;
  915. _es.etype = EXPR;
  916. _es.epos = -1;
  917. _es.donot_get = false;
  918. (this->*f)();
  919. _es = es;
  920. }
  921. template<typename T> void BIN_EXP(SQOpcode op, T f,SQInteger op3 = 0)
  922. {
  923. Lex();
  924. INVOKE_EXP(f);
  925. SQInteger op1 = _fs->PopTarget();SQInteger op2 = _fs->PopTarget();
  926. _fs->AddInstruction(op, _fs->PushTarget(), op1, op2, op3);
  927. }
  928. void LogicalOrExp()
  929. {
  930. LogicalAndExp();
  931. for(;;) if(_token == TK_OR) {
  932. SQInteger first_exp = _fs->PopTarget();
  933. SQInteger trg = _fs->PushTarget();
  934. _fs->AddInstruction(_OP_OR, trg, 0, first_exp, 0);
  935. SQInteger jpos = _fs->GetCurrentPos();
  936. if(trg != first_exp) _fs->AddInstruction(_OP_MOVE, trg, first_exp);
  937. Lex(); INVOKE_EXP(&SQCompiler::LogicalOrExp);
  938. _fs->SnoozeOpt();
  939. SQInteger second_exp = _fs->PopTarget();
  940. if(trg != second_exp) _fs->AddInstruction(_OP_MOVE, trg, second_exp);
  941. _fs->SnoozeOpt();
  942. _fs->SetIntructionParam(jpos, 1, (_fs->GetCurrentPos() - jpos));
  943. break;
  944. }else return;
  945. }
  946. void LogicalAndExp()
  947. {
  948. BitwiseOrExp();
  949. for(;;) switch(_token) {
  950. case TK_AND: {
  951. SQInteger first_exp = _fs->PopTarget();
  952. SQInteger trg = _fs->PushTarget();
  953. _fs->AddInstruction(_OP_AND, trg, 0, first_exp, 0);
  954. SQInteger jpos = _fs->GetCurrentPos();
  955. if(trg != first_exp) _fs->AddInstruction(_OP_MOVE, trg, first_exp);
  956. Lex(); INVOKE_EXP(&SQCompiler::LogicalAndExp);
  957. _fs->SnoozeOpt();
  958. SQInteger second_exp = _fs->PopTarget();
  959. if(trg != second_exp) _fs->AddInstruction(_OP_MOVE, trg, second_exp);
  960. _fs->SnoozeOpt();
  961. _fs->SetIntructionParam(jpos, 1, (_fs->GetCurrentPos() - jpos));
  962. break;
  963. }
  964. default:
  965. return;
  966. }
  967. }
  968. void BitwiseOrExp()
  969. {
  970. BitwiseXorExp();
  971. for(;;) if(_token == _SC('|'))
  972. {BIN_EXP(_OP_BITW, &SQCompiler::BitwiseXorExp,BW_OR);
  973. }else return;
  974. }
  975. void BitwiseXorExp()
  976. {
  977. BitwiseAndExp();
  978. for(;;) if(_token == _SC('^'))
  979. {BIN_EXP(_OP_BITW, &SQCompiler::BitwiseAndExp,BW_XOR);
  980. }else return;
  981. }
  982. void BitwiseAndExp()
  983. {
  984. EqExp();
  985. for(;;) if(_token == _SC('&'))
  986. {BIN_EXP(_OP_BITW, &SQCompiler::EqExp,BW_AND);
  987. }else return;
  988. }
  989. void EqExp()
  990. {
  991. CompExp();
  992. for(;;) switch(_token) {
  993. case TK_EQ: BIN_EXP(_OP_EQ, &SQCompiler::CompExp); break;
  994. case TK_EQ_IDENTITY :BIN_EXP(_OP_EQI, &SQCompiler::CompExp); break;
  995. case TK_NE: BIN_EXP(_OP_NE, &SQCompiler::CompExp); break;
  996. case TK_NE_IDENTITY: BIN_EXP(_OP_NEI, &SQCompiler::CompExp); break;
  997. case TK_3WAYSCMP: BIN_EXP(_OP_CMP, &SQCompiler::CompExp,CMP_3W); break;
  998. default: return;
  999. }
  1000. }
  1001. void CompExp()
  1002. {
  1003. ShiftExp();
  1004. for(;;) switch(_token) {
  1005. case _SC('>'): BIN_EXP(_OP_CMP, &SQCompiler::ShiftExp,CMP_G); break;
  1006. case _SC('<'): BIN_EXP(_OP_CMP, &SQCompiler::ShiftExp,CMP_L); break;
  1007. case TK_GE: BIN_EXP(_OP_CMP, &SQCompiler::ShiftExp,CMP_GE); break;
  1008. case TK_LE: BIN_EXP(_OP_CMP, &SQCompiler::ShiftExp,CMP_LE); break;
  1009. case TK_IN: BIN_EXP(_OP_EXISTS, &SQCompiler::ShiftExp); break;
  1010. case TK_INSTANCEOF: BIN_EXP(_OP_INSTANCEOF, &SQCompiler::ShiftExp); break;
  1011. default: return;
  1012. }
  1013. }
  1014. void ShiftExp()
  1015. {
  1016. PlusExp();
  1017. for(;;) switch(_token) {
  1018. case TK_USHIFTR: BIN_EXP(_OP_BITW, &SQCompiler::PlusExp,BW_USHIFTR); break;
  1019. case TK_SHIFTL: BIN_EXP(_OP_BITW, &SQCompiler::PlusExp,BW_SHIFTL); break;
  1020. case TK_SHIFTR: BIN_EXP(_OP_BITW, &SQCompiler::PlusExp,BW_SHIFTR); break;
  1021. default: return;
  1022. }
  1023. }
  1024. SQOpcode ChooseArithOpByToken(SQInteger tok)
  1025. {
  1026. switch(tok) {
  1027. case TK_PLUSEQ: case '+': return _OP_ADD;
  1028. case TK_MINUSEQ: case '-': return _OP_SUB;
  1029. case TK_MULEQ: case '*': return _OP_MUL;
  1030. case TK_DIVEQ: case '/': return _OP_DIV;
  1031. case TK_MODEQ: case '%': return _OP_MOD;
  1032. default: assert(0);
  1033. }
  1034. return _OP_ADD;
  1035. }
  1036. SQInteger ChooseCompArithCharByToken(SQInteger tok)
  1037. {
  1038. SQInteger oper;
  1039. switch(tok){
  1040. case TK_MINUSEQ: oper = '-'; break;
  1041. case TK_PLUSEQ: oper = '+'; break;
  1042. case TK_MULEQ: oper = '*'; break;
  1043. case TK_DIVEQ: oper = '/'; break;
  1044. case TK_MODEQ: oper = '%'; break;
  1045. default: oper = 0; //shut up compiler
  1046. assert(0); break;
  1047. };
  1048. return oper;
  1049. }
  1050. void PlusExp()
  1051. {
  1052. MultExp();
  1053. for(;;) switch(_token) {
  1054. case _SC('+'): case _SC('-'):
  1055. BIN_EXP(ChooseArithOpByToken(_token), &SQCompiler::MultExp); break;
  1056. default: return;
  1057. }
  1058. }
  1059. void MultExp()
  1060. {
  1061. PrefixedExpr();
  1062. for(;;) switch(_token) {
  1063. case _SC('*'): case _SC('/'): case _SC('%'):
  1064. BIN_EXP(ChooseArithOpByToken(_token), &SQCompiler::PrefixedExpr); break;
  1065. default: return;
  1066. }
  1067. }
  1068. //if 'pos' != -1 the previous variable is a local variable
  1069. void PrefixedExpr()
  1070. {
  1071. SQInteger pos = Factor();
  1072. for(;;) {
  1073. switch(_token) {
  1074. case _SC('.'):
  1075. pos = -1;
  1076. Lex();
  1077. _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(Expect(TK_IDENTIFIER)));
  1078. if(_es.etype==BASE) {
  1079. Emit2ArgsOP(_OP_GET);
  1080. pos = _fs->TopTarget();
  1081. _es.etype = EXPR;
  1082. _es.epos = pos;
  1083. }
  1084. else {
  1085. if(NeedGet()) {
  1086. Emit2ArgsOP(_OP_GET);
  1087. }
  1088. _es.etype = OBJECT;
  1089. }
  1090. break;
  1091. case _SC('['):
  1092. if(_lex.data->prevtoken == _SC('\n')) Error(_SC("cannot brake deref/or comma needed after [exp]=exp slot declaration"));
  1093. Lex(); Expression(); Expect(_SC(']'));
  1094. pos = -1;
  1095. if(_es.etype==BASE) {
  1096. Emit2ArgsOP(_OP_GET);
  1097. pos = _fs->TopTarget();
  1098. _es.etype = EXPR;
  1099. _es.epos = pos;
  1100. }
  1101. else {
  1102. if(NeedGet()) {
  1103. Emit2ArgsOP(_OP_GET);
  1104. }
  1105. _es.etype = OBJECT;
  1106. }
  1107. break;
  1108. case TK_MINUSMINUS:
  1109. case TK_PLUSPLUS:
  1110. {
  1111. if(IsEndOfStatement()) return;
  1112. SQInteger diff = (_token==TK_MINUSMINUS) ? -1 : 1;
  1113. Lex();
  1114. switch(_es.etype)
  1115. {
  1116. case EXPR: Error(_SC("can't '++' or '--' an expression")); break;
  1117. case OBJECT:
  1118. case BASE:
  1119. if(_es.donot_get == true) { Error(_SC("can't '++' or '--' an expression")); break; } //mmh dor this make sense?
  1120. Emit2ArgsOP(_OP_PINC, diff);
  1121. break;
  1122. case LOCAL: {
  1123. SQInteger src = _fs->PopTarget();
  1124. _fs->AddInstruction(_OP_PINCL, _fs->PushTarget(), src, 0, diff);
  1125. }
  1126. break;
  1127. case OUTER: {
  1128. SQInteger tmp1 = _fs->PushTarget();
  1129. SQInteger tmp2 = _fs->PushTarget();
  1130. _fs->AddInstruction(_OP_GETOUTER, tmp2, _es.epos);
  1131. _fs->AddInstruction(_OP_PINCL, tmp1, tmp2, 0, diff);
  1132. _fs->AddInstruction(_OP_SETOUTER, tmp2, _es.epos, tmp2);
  1133. _fs->PopTarget();
  1134. }
  1135. }
  1136. }
  1137. return;
  1138. break;
  1139. case _SC('('):
  1140. switch(_es.etype) {
  1141. case OBJECT: {
  1142. SQInteger key = _fs->PopTarget(); /* location of the key */
  1143. SQInteger table = _fs->PopTarget(); /* location of the object */
  1144. SQInteger closure = _fs->PushTarget(); /* location for the closure */
  1145. SQInteger ttarget = _fs->PushTarget(); /* location for 'this' pointer */
  1146. _fs->AddInstruction(_OP_PREPCALL, closure, key, table, ttarget);
  1147. }
  1148. break;
  1149. case BASE:
  1150. //Emit2ArgsOP(_OP_GET);
  1151. _fs->AddInstruction(_OP_MOVE, _fs->PushTarget(), 0);
  1152. break;
  1153. case OUTER:
  1154. _fs->AddInstruction(_OP_GETOUTER, _fs->PushTarget(), _es.epos);
  1155. _fs->AddInstruction(_OP_MOVE, _fs->PushTarget(), 0);
  1156. break;
  1157. default:
  1158. _fs->AddInstruction(_OP_MOVE, _fs->PushTarget(), 0);
  1159. }
  1160. _es.etype = EXPR;
  1161. Lex();
  1162. FunctionCallArgs();
  1163. break;
  1164. case TK_AS:
  1165. {
  1166. Lex();
  1167. ExpectTypeToken(); //ignore for now
  1168. }
  1169. default: return;
  1170. }
  1171. }
  1172. }
  1173. SQInteger Factor()
  1174. {
  1175. _es.etype = EXPR;
  1176. switch(_token)
  1177. {
  1178. case TK_STRING_LITERAL:
  1179. _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(_fs->CreateString(_lex.data->svalue,_lex.data->longstr.size()-1)));
  1180. Lex();
  1181. break;
  1182. case TK_BASE:
  1183. Lex();
  1184. _fs->AddInstruction(_OP_GETBASE, _fs->PushTarget());
  1185. _es.etype = BASE;
  1186. _es.epos = _fs->TopTarget();
  1187. return (_es.epos);
  1188. break;
  1189. case TK_IDENTIFIER:
  1190. case TK_CONSTRUCTOR:
  1191. case TK_DESTRUCTOR:
  1192. case TK_THIS:{
  1193. SQObject id;
  1194. SQObject constant;
  1195. switch(_token) {
  1196. case TK_IDENTIFIER: id = _fs->CreateString(_lex.data->svalue); break;
  1197. case TK_THIS: id = _fs->CreateString(_SC("this"), 4); break;
  1198. case TK_CONSTRUCTOR: id = _fs->CreateString(_SC("constructor"), 11); break;
  1199. case TK_DESTRUCTOR: id = _fs->CreateString(_SC("destructor"), 10); break;
  1200. }
  1201. SQInteger pos = -1;
  1202. Lex();
  1203. if((pos = _fs->GetLocalVariable(id)) != -1) {
  1204. /* Handle a local variable (includes 'this') */
  1205. _fs->PushTarget(pos);
  1206. _es.etype = LOCAL;
  1207. _es.epos = pos;
  1208. }
  1209. else if((pos = _fs->GetOuterVariable(id)) != -1) {
  1210. /* Handle a free var */
  1211. if(NeedGet()) {
  1212. _es.epos = _fs->PushTarget();
  1213. _fs->AddInstruction(_OP_GETOUTER, _es.epos, pos);
  1214. /* _es.etype = EXPR; already default value */
  1215. }
  1216. else {
  1217. _es.etype = OUTER;
  1218. _es.epos = pos;
  1219. }
  1220. }
  1221. else if(IsConstant(id, constant)) {
  1222. /* Handle named constant */
  1223. SQObjectPtr constval;
  1224. SQObject constid;
  1225. if(type(constant) == OT_TABLE) {
  1226. Expect('.');
  1227. constid = Expect(TK_IDENTIFIER);
  1228. if(!_table(constant)->Get(constid, constval)) {
  1229. constval.Null();
  1230. Error(_SC("invalid constant [%s.%s]"), _stringval(id), _stringval(constid));
  1231. }
  1232. }
  1233. else {
  1234. constval = constant;
  1235. }
  1236. _es.epos = _fs->PushTarget();
  1237. /* generate direct or literal function depending on size */
  1238. SQObjectType ctype = type(constval);
  1239. switch(ctype) {
  1240. case OT_INTEGER: EmitLoadConstInt(_integer(constval),_es.epos); break;
  1241. case OT_FLOAT: EmitLoadConstFloat(_float(constval),_es.epos); break;
  1242. case OT_BOOL: _fs->AddInstruction(_OP_LOADBOOL, _es.epos, _integer(constval)); break;
  1243. default: _fs->AddInstruction(_OP_LOAD,_es.epos,_fs->GetConstant(constval)); break;
  1244. }
  1245. _es.etype = EXPR;
  1246. }
  1247. else {
  1248. /* Handle a non-local variable, aka a field. Push the 'this' pointer on
  1249. * the virtual stack (always found in offset 0, so no instruction needs to
  1250. * be generated), and push the key next. Generate an _OP_LOAD instruction
  1251. * for the latter. If we are not using the variable as a dref expr, generate
  1252. * the _OP_GET instruction.
  1253. */
  1254. if(CheckNameIsType(id)) EatTemplateInitialization();
  1255. _fs->PushTarget(0);
  1256. _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(id));
  1257. if(NeedGet()) {
  1258. Emit2ArgsOP(_OP_GET);
  1259. }
  1260. _es.etype = OBJECT;
  1261. }
  1262. return _es.epos;
  1263. }
  1264. break;
  1265. case TK_DOUBLE_COLON: // "::"
  1266. _fs->AddInstruction(_OP_LOADROOT, _fs->PushTarget());
  1267. _es.etype = OBJECT;
  1268. _token = _SC('.'); /* hack: drop into PrefixExpr, case '.'*/
  1269. _es.epos = -1;
  1270. return _es.epos;
  1271. break;
  1272. case TK_NULL:
  1273. _fs->AddInstruction(_OP_LOADNULLS, _fs->PushTarget(),1);
  1274. Lex();
  1275. break;
  1276. case TK_INTEGER: EmitLoadConstInt(_lex.data->nvalue,-1); Lex(); break;
  1277. case TK_FLOAT: EmitLoadConstFloat(_lex.data->fvalue,-1); Lex(); break;
  1278. case TK_TRUE: case TK_FALSE:
  1279. _fs->AddInstruction(_OP_LOADBOOL, _fs->PushTarget(),_token == TK_TRUE?1:0);
  1280. Lex();
  1281. break;
  1282. case _SC('['): {
  1283. _fs->AddInstruction(_OP_NEWOBJ, _fs->PushTarget(),0,0,NOT_ARRAY);
  1284. SQInteger apos = _fs->GetCurrentPos(),key = 0;
  1285. Lex();
  1286. while(_token != _SC(']')) {
  1287. Expression();
  1288. if(_token == _SC(',')) Lex();
  1289. SQInteger val = _fs->PopTarget();
  1290. SQInteger array = _fs->TopTarget();
  1291. _fs->AddInstruction(_OP_APPENDARRAY, array, val, AAT_STACK);
  1292. key++;
  1293. }
  1294. _fs->SetIntructionParam(apos, 1, key);
  1295. Lex();
  1296. }
  1297. break;
  1298. case _SC('{'):
  1299. _fs->AddInstruction(_OP_NEWOBJ, _fs->PushTarget(),0,NOT_TABLE);
  1300. Lex();ParseTableOrClass(_SC(','),_SC('}'));
  1301. break;
  1302. case TK_FUNCTION: FunctionExp(_token);break;
  1303. case _SC('@'): FunctionExp(_token,true);break;
  1304. case TK_STRUCT:
  1305. case TK_CLASS: Lex(); ClassExp(NULL);break;
  1306. case _SC('-'):
  1307. Lex();
  1308. switch(_token) {
  1309. case TK_INTEGER: EmitLoadConstInt(-_lex.data->nvalue,-1); Lex(); break;
  1310. case TK_FLOAT: EmitLoadConstFloat(-_lex.data->fvalue,-1); Lex(); break;
  1311. default: UnaryOP(_OP_NEG);
  1312. }
  1313. break;
  1314. case _SC('!'): Lex(); UnaryOP(_OP_NOT); break;
  1315. case _SC('~'):
  1316. Lex();
  1317. if(_token == TK_INTEGER) { EmitLoadConstInt(~_lex.data->nvalue,-1); Lex(); break; }
  1318. UnaryOP(_OP_BWNOT);
  1319. break;
  1320. case TK_TYPEOF : Lex() ;UnaryOP(_OP_TYPEOF); break;
  1321. case TK_RESUME : Lex(); UnaryOP(_OP_RESUME); break;
  1322. case TK_CLONE : Lex(); UnaryOP(_OP_CLONE); break;
  1323. case TK_RAWCALL: Lex(); Expect('('); FunctionCallArgs(true); break;
  1324. case TK_MINUSMINUS :
  1325. case TK_PLUSPLUS :PrefixIncDec(_token); break;
  1326. case TK_DELETE : DeleteExpr(); break;
  1327. case _SC('('):
  1328. Lex();
  1329. if(_token == TK_IDENTIFIER)
  1330. {
  1331. //check C/C++ cast
  1332. SQObject id = _fs->CreateString(_lex.data->svalue);
  1333. if(CheckTypeName(id)) //C/C++ type declaration;
  1334. {
  1335. SQInteger lhtk = _lex.LookaheadLex();
  1336. if(lhtk == _SC(')'))
  1337. {
  1338. Lex(); //eat TK_IDENTIFIER
  1339. Lex(); //eat ')'
  1340. CommaExpr();
  1341. break;
  1342. }
  1343. }
  1344. }
  1345. CommaExpr();
  1346. Expect(_SC(')'));
  1347. break;
  1348. case TK___LINE__: EmitLoadConstInt(_lex.data->currentline,-1); Lex(); break;
  1349. case TK___FILE__:
  1350. _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(_sourcename));
  1351. Lex();
  1352. break;
  1353. case TK___FUNCTION__:
  1354. _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(_fs->_name));
  1355. Lex();
  1356. break;
  1357. case TK_IGNORE:
  1358. //Warning("Keyword ignored \"%s\" at line %d:%d\n", _lex.Tok2Str(_token),
  1359. // _lex.data->currentline, _lex.data->currentcolumn);
  1360. Lex(); Factor();
  1361. break;
  1362. default: Error(_SC("expression expected"));
  1363. }
  1364. return -1;
  1365. }
  1366. void EmitLoadConstInt(SQInteger value,SQInteger target)
  1367. {
  1368. if(target < 0) {
  1369. target = _fs->PushTarget();
  1370. }
  1371. //with the line bellow we get wrong result for -1
  1372. //if(value <= INT_MAX && value > INT_MIN) { //does it fit in 32 bits?
  1373. if((value & (~((SQInteger)0xFFFFFFFF))) == 0) { //does it fit in 32 bits?
  1374. _fs->AddInstruction(_OP_LOADINT, target,value);
  1375. }
  1376. else {
  1377. _fs->AddInstruction(_OP_LOAD, target, _fs->GetNumericConstant(value));
  1378. }
  1379. }
  1380. void EmitLoadConstFloat(SQFloat value,SQInteger target)
  1381. {
  1382. if(target < 0) {
  1383. target = _fs->PushTarget();
  1384. }
  1385. if(sizeof(SQFloat) == sizeof(SQInt32)) {
  1386. _fs->AddInstruction(_OP_LOADFLOAT, target,*((SQInt32 *)&value));
  1387. }
  1388. else {
  1389. _fs->AddInstruction(_OP_LOAD, target, _fs->GetNumericConstant(value));
  1390. }
  1391. }
  1392. void UnaryOP(SQOpcode op)
  1393. {
  1394. PrefixedExpr();
  1395. SQInteger src = _fs->PopTarget();
  1396. _fs->AddInstruction(op, _fs->PushTarget(), src);
  1397. }
  1398. bool NeedGet()
  1399. {
  1400. switch(_token) {
  1401. case _SC('='): case _SC('('): case TK_NEWSLOT: case TK_MODEQ: case TK_MULEQ:
  1402. case TK_DIVEQ: case TK_MINUSEQ: case TK_PLUSEQ:
  1403. return false;
  1404. case TK_PLUSPLUS: case TK_MINUSMINUS:
  1405. if (!IsEndOfStatement()) {
  1406. return false;
  1407. }
  1408. break;
  1409. }
  1410. return (!_es.donot_get || ( _es.donot_get && (_token == _SC('.') || _token == _SC('['))));
  1411. }
  1412. void FunctionCallArgs(bool rawcall = false)
  1413. {
  1414. SQInteger nargs = 1;//this
  1415. while(_token != _SC(')')) {
  1416. Expression();
  1417. MoveIfCurrentTargetIsLocal();
  1418. nargs++;
  1419. if(_token == _SC(',')){
  1420. Lex();
  1421. if(_token == ')') Error(_SC("expression expected, found ')'"));
  1422. }
  1423. }
  1424. Lex();
  1425. if (rawcall) {
  1426. if (nargs < 3) Error(_SC("rawcall requires at least 2 parameters (callee and this)"));
  1427. nargs -= 2; //removes callee and this from count
  1428. }
  1429. for(SQInteger i = 0; i < (nargs - 1); i++) _fs->PopTarget();
  1430. SQInteger stackbase = _fs->PopTarget();
  1431. SQInteger closure = _fs->PopTarget();
  1432. _fs->AddInstruction(_OP_CALL, _fs->PushTarget(), closure, stackbase, nargs);
  1433. }
  1434. void AddClassMemberExists(SQObjectPtr &member_names, SQObject &name)
  1435. {
  1436. SQObjectPtr oname = name, otrue = true;
  1437. _table(member_names)->NewSlot(oname, otrue);
  1438. }
  1439. void CheckClassMemberExists(SQObjectPtr &member_names, SQObject &name, bool addIfNotExists=true)
  1440. {
  1441. if(_table(member_names)->Exists(name))
  1442. {
  1443. Error(_SC("class already has a member named: %s"), _stringval(name));
  1444. }
  1445. if(addIfNotExists) AddClassMemberExists(member_names, name);
  1446. }
  1447. void CheckClassMemberExists(SQObjectPtr &member_names, const SQChar *name)
  1448. {
  1449. SQObject oname = _fs->CreateString(name);
  1450. CheckClassMemberExists(member_names, oname);
  1451. }
  1452. void ParseTableOrClass(SQInteger separator,SQInteger terminator, SQObjectPtr *class_name=NULL)
  1453. {
  1454. SQObjectPtr member_names;
  1455. SQInteger saved_tok, tpos = _fs->GetCurrentPos(),nkeys = 0;
  1456. SQObject type_name, obj_id;
  1457. bool isClass = separator == ';'; //hack recognizes a table/class from the separator
  1458. if(isClass)
  1459. {
  1460. member_names = SQTable::Create(_ss(_vm),0);
  1461. }
  1462. int addClassMember = 0;
  1463. while(_token != terminator) {
  1464. bool hasattrs = false;
  1465. bool isstatic = false;
  1466. bool cppDestructor = false;
  1467. //bool isvirtual = false;
  1468. //bool isprivate = false;
  1469. const SQChar *membertypename = 0;
  1470. //check if is an attribute
  1471. if(isClass) {
  1472. if(_token == TK_ATTR_OPEN) {
  1473. _fs->AddInstruction(_OP_NEWOBJ, _fs->PushTarget(),0,NOT_TABLE); Lex();
  1474. ParseTableOrClass(',',TK_ATTR_CLOSE);
  1475. hasattrs = true;
  1476. }
  1477. if(_token == TK_STATIC) {
  1478. isstatic = true;
  1479. Lex();
  1480. }
  1481. if(_token == TK_VIRTUAL) {
  1482. //isvirtual = true;
  1483. Lex();
  1484. }
  1485. else if(_token == TK_PUBLIC) {
  1486. Lex();
  1487. }
  1488. else if(_token == TK_PRIVATE) {
  1489. //isprivate = true;
  1490. Lex();
  1491. }
  1492. else if(_token == TK_PROTECTED) {
  1493. //isprivate = true;
  1494. Lex();
  1495. }
  1496. else if(_token == TK_INLINE) {
  1497. Lex();
  1498. }
  1499. else if(_token == TK_CONST) {
  1500. Lex();
  1501. }
  1502. else if(_token == TK_TEMPLATE) {
  1503. TemplateStatement();
  1504. }
  1505. }
  1506. member_has_type:
  1507. switch(_token) {
  1508. case TK_FUNCTION:
  1509. case TK_CONSTRUCTOR:
  1510. case TK_DESTRUCTOR:{
  1511. saved_tok = _token;
  1512. Lex();
  1513. obj_id = saved_tok == TK_FUNCTION ? Expect(TK_IDENTIFIER) :
  1514. _fs->CreateString(saved_tok == TK_CONSTRUCTOR ? _SC("constructor") : _SC("destructor"));
  1515. CheckClassMemberExists(member_names, obj_id);
  1516. Expect(_SC('('));
  1517. function_params_decl:
  1518. _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(obj_id));
  1519. CreateFunction(obj_id, eFunctionType_member);
  1520. _fs->AddInstruction(_OP_CLOSURE, _fs->PushTarget(), _fs->_functions.size() - 1, 0);
  1521. }
  1522. break;
  1523. case _SC('['):
  1524. Lex(); CommaExpr(); Expect(_SC(']'));
  1525. Expect(_SC('=')); Expression();
  1526. break;
  1527. case TK_STRING_LITERAL: //JSON
  1528. if(isClass)
  1529. {
  1530. Error(_SC("unexpected string literal in class declaration"));
  1531. }
  1532. case TK_IDENTIFIER: {//JSON
  1533. obj_id = GetTokenObject(_token);
  1534. SQInteger next_token = _SC('=');
  1535. if(isClass)
  1536. {
  1537. CheckClassMemberExists(member_names, obj_id, false);
  1538. addClassMember = 0;
  1539. EatTemplateInitialization();
  1540. switch(_token)
  1541. {
  1542. case _SC('('): //C/C++ style function declaration
  1543. Lex();
  1544. if(class_name)
  1545. {
  1546. //printf("ClassMember %d : %s : %s\n", (int)cppDestructor, _stringval(*class_name), _stringval(obj_id));
  1547. if(memcmp(_stringval(*class_name), _stringval(obj_id), _string(*class_name)->_len) == 0)
  1548. {
  1549. //C++ style constructor/destructor declaration
  1550. obj_id = _fs->CreateString(cppDestructor ? _SC("destructor") : _SC("constructor"));
  1551. cppDestructor = false;
  1552. }
  1553. }
  1554. AddClassMemberExists(member_names, obj_id);
  1555. goto function_params_decl;
  1556. break;
  1557. case _SC(':'): //typescript field with type annotation
  1558. if(membertypename)
  1559. {
  1560. Error(_SC("member type already declared before %s"), _stringval(obj_id));
  1561. }
  1562. Lex();
  1563. type_name = ExpectTypeToken();
  1564. addClassMember = 1;
  1565. break;
  1566. case _SC(','):
  1567. ++addClassMember;
  1568. case _SC(';'): //member variable declaration without explicit initialization
  1569. Lex();
  1570. ++addClassMember;
  1571. break;
  1572. case TK_IDENTIFIER: //if 2 identifier found the first should be a type
  1573. if(CheckNameIsType(obj_id)) //Struct/Class/Typedef names
  1574. {
  1575. membertypename = _stringval(obj_id);
  1576. goto member_has_type;
  1577. }
  1578. }
  1579. if(addClassMember)
  1580. {
  1581. AddClassMemberExists(member_names, obj_id);
  1582. if(_token != _SC('='))
  1583. {
  1584. _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(obj_id));
  1585. _fs->AddInstruction(_OP_LOADNULLS, _fs->PushTarget(), 1);
  1586. break;
  1587. }
  1588. }
  1589. }
  1590. if(_token == _SC(':')){
  1591. next_token = _token;
  1592. }
  1593. Expect(next_token);
  1594. _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(obj_id));
  1595. Expression();
  1596. break;
  1597. }
  1598. CASE_TK_LOCAL_TYPES: //class member variables;
  1599. case TK_LOCAL:
  1600. if(isClass)
  1601. {
  1602. membertypename = _lex.GetTokenName(_token);
  1603. Lex();
  1604. goto member_has_type;
  1605. }
  1606. case _SC('~'): //C++ style destructor declaration
  1607. if(isClass)
  1608. {
  1609. cppDestructor = true;
  1610. Lex();
  1611. goto member_has_type;
  1612. }
  1613. case TK_CONSTEXPR:
  1614. if(isClass)
  1615. {
  1616. Lex();
  1617. continue;
  1618. }
  1619. //else fallthrough
  1620. default :
  1621. ErrorIfNotToken(TK_IDENTIFIER);
  1622. }
  1623. if(_token == separator) Lex();//optional comma/semicolon
  1624. nkeys++;
  1625. SQInteger val = _fs->PopTarget();
  1626. SQInteger key = _fs->PopTarget();
  1627. SQInteger attrs = hasattrs ? _fs->PopTarget():-1;
  1628. assert((hasattrs && (attrs == key-1)) || !hasattrs);
  1629. (void)attrs; // UNUSED
  1630. unsigned char flags = (hasattrs?NEW_SLOT_ATTRIBUTES_FLAG:0)|(isstatic?NEW_SLOT_STATIC_FLAG:0);
  1631. SQInteger table = _fs->TopTarget(); //<<BECAUSE OF THIS NO COMMON EMIT FUNC IS POSSIBLE
  1632. if(isClass) {
  1633. _fs->AddInstruction(_OP_NEWSLOTA, flags, table, key, val); //this for classes only as it invokes _newmember
  1634. if(addClassMember > 1)
  1635. {
  1636. addClassMember = 0;
  1637. goto member_has_type;
  1638. }
  1639. }
  1640. else {
  1641. _fs->AddInstruction(_OP_NEWSLOT, 0xFF, table, key, val);
  1642. }
  1643. }
  1644. if(separator == _SC(',')) //hack recognizes a table from the separator
  1645. _fs->SetIntructionParam(tpos, 1, nkeys);
  1646. Lex();
  1647. }
  1648. void ExternDeclStatement()
  1649. {
  1650. _is_parsing_extern = true;
  1651. Lex();
  1652. LocalDeclStatement();
  1653. _is_parsing_extern = false;
  1654. }
  1655. void EatTemplateInitialization()
  1656. {
  1657. //C++/Java template instantiation
  1658. if(_token == _SC('<')) {
  1659. Lex();
  1660. ExpectTypeToken();
  1661. if(_token != _SC('>')) Error(_SC("template instantiation declaration expected <TYPE>"));
  1662. Lex();
  1663. }
  1664. }
  1665. #define CHECK_REF_DECLARATION(tk) if(tk == _SC('&')){is_reference_declaration = true;Lex();}
  1666. void LocalDeclStatement()
  1667. {
  1668. SQObject varname;
  1669. //SQChar *param_type_name;
  1670. bool is_void_declaration = _token == TK_VOID;
  1671. bool is_const_declaration = _token == TK_CONST;
  1672. bool is_reference_declaration = false;
  1673. //bool is_instance_declaration = _token == TK_IDENTIFIER;
  1674. SQInteger declType = _token;
  1675. Lex();
  1676. EatTemplateInitialization();
  1677. if( _token == TK_FUNCTION) {
  1678. Lex();
  1679. varname = Expect(TK_IDENTIFIER);
  1680. CheckLocalNameScope(varname, _scope.nested);
  1681. Expect(_SC('('));
  1682. function_params_decl:
  1683. #if 1 //doing this way works but prevents garbage collection when doing multiple reloads on the same vm
  1684. //the following is an attempt to allow local declared functions be called recursivelly
  1685. SQInteger old_pos = _fs->GetCurrentPos(); //save current instructions position
  1686. _fs->PushLocalVariable(varname, _scope.nested, _VAR_CLOSURE); //add function name to find it as outer var if needed
  1687. //-1 to compensate default parameters when relocating
  1688. CreateFunction(varname, eFunctionType_local, -1);
  1689. if(_is_parsing_extern) {
  1690. if(_token == _SC(';')) //to parse thinscript
  1691. {
  1692. //Expect(_SC(';'));
  1693. CheckExternName(varname, true);
  1694. return;
  1695. }
  1696. else _is_parsing_extern = false;
  1697. }
  1698. _fs->AddInstruction(_OP_CLOSURE, _fs->PushTarget(), _fs->_functions.size() - 1, 0);
  1699. //rellocate any stack operation (default parameters & _OP_Closure)
  1700. for(int i=old_pos+1, curr_pos = _fs->GetCurrentPos(); i <= curr_pos; ++i){
  1701. SQInstruction & inst = _fs->GetInstruction(i);
  1702. _fs->SetIntructionParam(i, 0, inst._arg0 -1);
  1703. }
  1704. _fs->PopTarget();
  1705. #else
  1706. CreateFunction(varname,eFunctionType::local);
  1707. _fs->AddInstruction(_OP_CLOSURE, _fs->PushTarget(), _fs->_functions.size() - 1, 0);
  1708. _fs->PopTarget();
  1709. _fs->PushLocalVariable(varname, _scope.nested, _VAR_CLOSURE);
  1710. #endif
  1711. return;
  1712. }
  1713. do {
  1714. //param_type_name = NULL;
  1715. CHECK_REF_DECLARATION(_token);
  1716. varname = Expect(TK_IDENTIFIER);
  1717. if(CheckNameIsType(varname))
  1718. {
  1719. //ignore custom types for now
  1720. //param_type_name = _stringval(varname);
  1721. declType = _VAR_ANY;
  1722. CHECK_REF_DECLARATION(_token);
  1723. varname = Expect(TK_IDENTIFIER);
  1724. }
  1725. CheckLocalNameScope(varname, _scope.nested);
  1726. EatTemplateInitialization();
  1727. if(_token == _SC('(')) {
  1728. //C/C++ style function declaration
  1729. Lex();
  1730. goto function_params_decl;
  1731. }
  1732. else if(_token == _SC(':')) {
  1733. //type specifier like typescript
  1734. Lex();
  1735. ExpectTypeToken(); //ignore for now
  1736. }
  1737. if(is_void_declaration)
  1738. {
  1739. Error(_SC("void type is invalid here"));
  1740. }
  1741. else if(_token == _SC('=')) {
  1742. if(_is_parsing_extern)
  1743. {
  1744. Error(_SC("can not make assignment in external declarations"));
  1745. }
  1746. Lex(); Expression();
  1747. SQInteger src = _fs->PopTarget();
  1748. SQInteger dest = _fs->PushTarget();
  1749. if(dest != src) _fs->AddInstruction(_OP_MOVE, dest, src);
  1750. declType = _VAR_ANY;
  1751. }
  1752. else if(is_const_declaration || is_reference_declaration)
  1753. Error(_SC("const/reference '%s' need an initializer"), _stringval(varname));
  1754. else {
  1755. //check_param_type:
  1756. if(!_is_parsing_extern) {
  1757. SQInteger dest = _fs->PushTarget();
  1758. switch(declType){
  1759. CASE_TK_LOCAL_CHAR_TYPES:
  1760. _fs->AddInstruction(_OP_LOADNULLS, dest,1);
  1761. declType = _VAR_STRING;
  1762. break;
  1763. case TK_LOCAL_BOOL_T:
  1764. //default value false
  1765. _fs->AddInstruction(_OP_LOADBOOL, dest,0);
  1766. declType = _VAR_BOOL;
  1767. break;
  1768. case TK_LOCAL_TABLE_T:
  1769. _fs->AddInstruction(_OP_LOADNULLS, dest,1);
  1770. declType = _VAR_TABLE;
  1771. break;
  1772. case TK_LOCAL_ARRAY_T:
  1773. _fs->AddInstruction(_OP_LOADNULLS, dest,1);
  1774. declType = _VAR_ARRAY;
  1775. break;
  1776. CASE_TK_LOCAL_INT_TYPES:
  1777. //default value 0
  1778. _fs->AddInstruction(_OP_LOADINT, dest,0);
  1779. declType = _VAR_INTEGER;
  1780. break;
  1781. CASE_TK_LOCAL_FLOAT_TYPES:
  1782. case TK_LOCAL_NUMBER_T: //start numbers as floats
  1783. //default value 0.0
  1784. //_OP_LOADFLOAT is only valid when SQFloat size == SQInt32 size
  1785. _fs->AddInstruction(_OP_LOADINT, dest,0);
  1786. declType = _VAR_FLOAT;
  1787. break;
  1788. //case TK_LOCAL:
  1789. default:
  1790. //default value null
  1791. _fs->AddInstruction(_OP_LOADNULLS, dest,1);
  1792. declType = _VAR_ANY;
  1793. }
  1794. }
  1795. }
  1796. if(_is_parsing_extern) {
  1797. if(CheckExternName(varname, true))
  1798. {
  1799. Error(_SC("extern %s already declared"), varname);
  1800. }
  1801. }
  1802. else {
  1803. _fs->PopTarget();
  1804. _fs->PushLocalVariable(varname, _scope.nested, (is_const_declaration ? _VAR_CONST : declType)
  1805. | (is_reference_declaration ? _VAR_REFERENCE : 0));
  1806. }
  1807. if(_token == _SC(',')) Lex(); else break;
  1808. } while(1);
  1809. }
  1810. void IfBlock()
  1811. {
  1812. if (_token == _SC('{'))
  1813. {
  1814. BEGIN_SCOPE();
  1815. Lex();
  1816. Statements();
  1817. Expect(_SC('}'));
  1818. if (true) {
  1819. END_SCOPE();
  1820. }
  1821. else {
  1822. END_SCOPE_NO_CLOSE();
  1823. }
  1824. }
  1825. else {
  1826. //BEGIN_SCOPE();
  1827. Statement();
  1828. if (_lex.data->prevtoken != _SC('}') && _lex.data->prevtoken != _SC(';')) OptionalSemicolon();
  1829. //END_SCOPE();
  1830. }
  1831. }
  1832. void IfStatement()
  1833. {
  1834. SQInteger jmppos;
  1835. bool haselse = false;
  1836. Lex(); Expect(_SC('(')); CommaExpr(true); Expect(_SC(')'));
  1837. _fs->AddInstruction(_OP_JZ, _fs->PopTarget());
  1838. SQInteger jnepos = _fs->GetCurrentPos();
  1839. BEGIN_SCOPE();
  1840. //there is a situation where the if statement has a statement enclosed by {}
  1841. //and after the closing "}" there is no newline or semicolom
  1842. //it's a valid construction but the compiler was complaining about it
  1843. //for now added "&& _token != TK_IDENTIFIER" to the check after "Statement()" call
  1844. /*
  1845. local color = "blue";
  1846. if(color == "yellow"){
  1847. print(color);
  1848. } print("Waht color is it ?");
  1849. */
  1850. Statement();
  1851. //
  1852. if(_token != _SC('}') && _token != TK_ELSE && _token != TK_IDENTIFIER) OptionalSemicolon();
  1853. END_SCOPE();
  1854. SQInteger endifblock = _fs->GetCurrentPos();
  1855. if(_token == TK_ELSE){
  1856. haselse = true;
  1857. BEGIN_SCOPE();
  1858. _fs->AddInstruction(_OP_JMP);
  1859. jmppos = _fs->GetCurrentPos();
  1860. Lex();
  1861. Statement(); if(_token != TK_IDENTIFIER) OptionalSemicolon();
  1862. END_SCOPE();
  1863. _fs->SetIntructionParam(jmppos, 1, _fs->GetCurrentPos() - jmppos);
  1864. }
  1865. _fs->SetIntructionParam(jnepos, 1, endifblock - jnepos + (haselse?1:0));
  1866. }
  1867. void WhileStatement()
  1868. {
  1869. SQInteger jzpos, jmppos;
  1870. jmppos = _fs->GetCurrentPos();
  1871. Lex(); Expect(_SC('(')); CommaExpr(true); Expect(_SC(')'));
  1872. BEGIN_BREAKBLE_BLOCK();
  1873. _fs->AddInstruction(_OP_JZ, _fs->PopTarget());
  1874. jzpos = _fs->GetCurrentPos();
  1875. BEGIN_SCOPE();
  1876. Statement();
  1877. END_SCOPE();
  1878. _fs->AddInstruction(_OP_JMP, 0, jmppos - _fs->GetCurrentPos() - 1);
  1879. _fs->SetIntructionParam(jzpos, 1, _fs->GetCurrentPos() - jzpos);
  1880. END_BREAKBLE_BLOCK(jmppos);
  1881. }
  1882. void DoWhileStatement()
  1883. {
  1884. Lex();
  1885. SQInteger jmptrg = _fs->GetCurrentPos();
  1886. BEGIN_BREAKBLE_BLOCK()
  1887. BEGIN_SCOPE();
  1888. Statement();
  1889. END_SCOPE();
  1890. //fix proposed by frosch to correct line number info in stack dumps
  1891. _fs->AddLineInfos(_lex.data->currentline, _lineinfo, true);
  1892. Expect(TK_WHILE);
  1893. SQInteger continuetrg = _fs->GetCurrentPos();
  1894. Expect(_SC('(')); CommaExpr(true); Expect(_SC(')'));
  1895. _fs->AddInstruction(_OP_JZ, _fs->PopTarget(), 1);
  1896. _fs->AddInstruction(_OP_JMP, 0, jmptrg - _fs->GetCurrentPos() - 1);
  1897. END_BREAKBLE_BLOCK(continuetrg);
  1898. }
  1899. void ForStatement()
  1900. {
  1901. Lex();
  1902. BEGIN_SCOPE();
  1903. Expect(_SC('('));
  1904. switch(_token){
  1905. case TK_IDENTIFIER:{
  1906. SQObject id = _fs->CreateString(_lex.data->svalue);
  1907. if(!CheckTypeName(id)) goto lbl_commaexpr;//not a C/C++ type declaration;
  1908. }
  1909. //ignore custom types for now
  1910. //fallthrough as local declaration
  1911. CASE_TK_LOCAL_TYPES:
  1912. case TK_LOCAL:
  1913. LocalDeclStatement();
  1914. break;
  1915. default:
  1916. lbl_commaexpr:
  1917. if(_token != _SC(';')){
  1918. CommaExpr();
  1919. _fs->PopTarget();
  1920. }
  1921. }
  1922. Expect(_SC(';'));
  1923. _fs->SnoozeOpt();
  1924. SQInteger jmppos = _fs->GetCurrentPos();
  1925. SQInteger jzpos = -1;
  1926. if(_token != _SC(';')) { CommaExpr(); _fs->AddInstruction(_OP_JZ, _fs->PopTarget()); jzpos = _fs->GetCurrentPos(); }
  1927. Expect(_SC(';'));
  1928. _fs->SnoozeOpt();
  1929. SQInteger expstart = _fs->GetCurrentPos() + 1;
  1930. if(_token != _SC(')')) {
  1931. CommaExpr();
  1932. _fs->PopTarget();
  1933. }
  1934. Expect(_SC(')'));
  1935. _fs->SnoozeOpt();
  1936. SQInteger expend = _fs->GetCurrentPos();
  1937. SQInteger expsize = (expend - expstart) + 1;
  1938. SQInstructionVec exp;
  1939. if(expsize > 0) {
  1940. for(SQInteger i = 0; i < expsize; i++)
  1941. exp.push_back(_fs->GetInstruction(expstart + i));
  1942. _fs->PopInstructions(expsize);
  1943. }
  1944. BEGIN_BREAKBLE_BLOCK()
  1945. Statement();
  1946. SQInteger continuetrg = _fs->GetCurrentPos();
  1947. if(expsize > 0) {
  1948. for(SQInteger i = 0; i < expsize; i++)
  1949. _fs->AddInstruction(exp[i]);
  1950. }
  1951. _fs->AddInstruction(_OP_JMP, 0, jmppos - _fs->GetCurrentPos() - 1, 0);
  1952. if(jzpos> 0) _fs->SetIntructionParam(jzpos, 1, _fs->GetCurrentPos() - jzpos);
  1953. END_SCOPE();
  1954. END_BREAKBLE_BLOCK(continuetrg);
  1955. }
  1956. void ForEachStatement()
  1957. {
  1958. SQObject idxname, valname;
  1959. Lex(); Expect(_SC('(')); valname = Expect(TK_IDENTIFIER);
  1960. if(_token == _SC(',')) {
  1961. idxname = valname;
  1962. Lex(); valname = Expect(TK_IDENTIFIER);
  1963. }
  1964. else{
  1965. idxname = _fs->CreateString(_SC("@INDEX@"));
  1966. }
  1967. Expect(TK_IN);
  1968. //save the stack size
  1969. BEGIN_SCOPE();
  1970. //put the table in the stack(evaluate the table expression)
  1971. Expression(); Expect(_SC(')'));
  1972. SQInteger container = _fs->TopTarget();
  1973. //push the index local var
  1974. SQInteger indexpos = _fs->PushLocalVariable(idxname, _scope.nested);
  1975. _fs->AddInstruction(_OP_LOADNULLS, indexpos,1);
  1976. //push the value local var
  1977. SQInteger valuepos = _fs->PushLocalVariable(valname, _scope.nested);
  1978. _fs->AddInstruction(_OP_LOADNULLS, valuepos,1);
  1979. //push reference index
  1980. SQInteger itrpos = _fs->PushLocalVariable(_fs->CreateString(_SC("@ITERATOR@")), _scope.nested); //use invalid id to make it inaccessible
  1981. _fs->AddInstruction(_OP_LOADNULLS, itrpos,1);
  1982. SQInteger jmppos = _fs->GetCurrentPos();
  1983. _fs->AddInstruction(_OP_FOREACH, container, 0, indexpos);
  1984. SQInteger foreachpos = _fs->GetCurrentPos();
  1985. _fs->AddInstruction(_OP_POSTFOREACH, container, 0, indexpos);
  1986. //generate the statement code
  1987. BEGIN_BREAKBLE_BLOCK()
  1988. Statement();
  1989. _fs->AddInstruction(_OP_JMP, 0, jmppos - _fs->GetCurrentPos() - 1);
  1990. _fs->SetIntructionParam(foreachpos, 1, _fs->GetCurrentPos() - foreachpos);
  1991. _fs->SetIntructionParam(foreachpos + 1, 1, _fs->GetCurrentPos() - foreachpos);
  1992. END_BREAKBLE_BLOCK(foreachpos - 1);
  1993. //restore the local variable stack(remove index,val and ref idx)
  1994. _fs->PopTarget();
  1995. END_SCOPE();
  1996. }
  1997. void SwitchStatement()
  1998. {
  1999. Lex(); Expect(_SC('(')); CommaExpr(true); Expect(_SC(')'));
  2000. Expect(_SC('{'));
  2001. SQInteger expr = _fs->TopTarget();
  2002. bool bfirst = true;
  2003. SQInteger tonextcondjmp = -1;
  2004. SQInteger skipcondjmp = -1;
  2005. SQInteger __nbreaks__ = _fs->_unresolvedbreaks.size();
  2006. _fs->_breaktargets.push_back(0);
  2007. while(_token == TK_CASE) {
  2008. if(!bfirst) {
  2009. _fs->AddInstruction(_OP_JMP, 0, 0);
  2010. skipcondjmp = _fs->GetCurrentPos();
  2011. _fs->SetIntructionParam(tonextcondjmp, 1, _fs->GetCurrentPos() - tonextcondjmp);
  2012. }
  2013. //condition
  2014. Lex(); ExpressionScalar() /*Expression()*/; Expect(_SC(':'));
  2015. SQInteger trg = _fs->PopTarget();
  2016. SQInteger eqtarget = trg;
  2017. bool local = _fs->IsLocal(trg);
  2018. if(local) {
  2019. eqtarget = _fs->PushTarget(); //we need to allocate a extra reg
  2020. }
  2021. _fs->AddInstruction(_OP_EQ, eqtarget, trg, expr);
  2022. _fs->AddInstruction(_OP_JZ, eqtarget, 0);
  2023. if(local) {
  2024. _fs->PopTarget();
  2025. }
  2026. //end condition
  2027. if(skipcondjmp != -1) {
  2028. _fs->SetIntructionParam(skipcondjmp, 1, (_fs->GetCurrentPos() - skipcondjmp));
  2029. }
  2030. tonextcondjmp = _fs->GetCurrentPos();
  2031. BEGIN_SCOPE();
  2032. Statements();
  2033. END_SCOPE();
  2034. bfirst = false;
  2035. }
  2036. if(tonextcondjmp != -1)
  2037. _fs->SetIntructionParam(tonextcondjmp, 1, _fs->GetCurrentPos() - tonextcondjmp);
  2038. if(_token == TK_DEFAULT) {
  2039. Lex(); Expect(_SC(':'));
  2040. BEGIN_SCOPE();
  2041. Statements();
  2042. END_SCOPE();
  2043. }
  2044. Expect(_SC('}'));
  2045. _fs->PopTarget();
  2046. __nbreaks__ = _fs->_unresolvedbreaks.size() - __nbreaks__;
  2047. if(__nbreaks__ > 0)ResolveBreaks(_fs, __nbreaks__);
  2048. _fs->_breaktargets.pop_back();
  2049. }
  2050. void TemplateStatement()
  2051. {
  2052. Lex(); //ignore for now
  2053. Expect(_SC('<'));
  2054. int nest_count = 1;
  2055. while(_token > 0 && nest_count > 0){
  2056. Lex();
  2057. switch(_token)
  2058. {
  2059. case _SC('>'):
  2060. --nest_count;
  2061. break;
  2062. case _SC('<'):
  2063. nest_count++;
  2064. }
  2065. }
  2066. if(nest_count == 0) Lex(); //last '>' ignore for now
  2067. }
  2068. void FunctionStatement()
  2069. {
  2070. SQObject id;
  2071. Lex(); id = Expect(TK_IDENTIFIER);
  2072. _fs->PushTarget(0);
  2073. _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(id));
  2074. if(_token == TK_DOUBLE_COLON) Emit2ArgsOP(_OP_GET);
  2075. else CheckGlobalName(id, true);
  2076. while(_token == TK_DOUBLE_COLON) {
  2077. Lex();
  2078. id = Expect(TK_IDENTIFIER);
  2079. //todo check if class function already exists
  2080. _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(id));
  2081. if(_token == TK_DOUBLE_COLON) Emit2ArgsOP(_OP_GET);
  2082. }
  2083. Expect(_SC('('));
  2084. CreateFunction(id, eFunctionType_global);
  2085. _fs->AddInstruction(_OP_CLOSURE, _fs->PushTarget(), _fs->_functions.size() - 1, 0);
  2086. EmitDerefOp(_OP_NEWSLOT);
  2087. _fs->PopTarget();
  2088. }
  2089. void ClassStatement()
  2090. {
  2091. SQExpState es;
  2092. SQObjectPtr class_name;
  2093. Lex();
  2094. if(_token == TK_IDENTIFIER) {
  2095. class_name = SQString::Create(_ss(_vm), _lex.data->svalue);
  2096. CheckGlobalName(class_name, true);
  2097. CheckTypeName(class_name, true); //to allow C/C++ style instance declarations
  2098. }
  2099. es = _es;
  2100. _es.donot_get = true;
  2101. PrefixedExpr();
  2102. if(_token == _SC(';')) //class forward declaration
  2103. {
  2104. //return;
  2105. }
  2106. if(_es.etype == EXPR) {
  2107. Error(_SC("invalid class name"));
  2108. }
  2109. else if(_es.etype == OBJECT || _es.etype == BASE) {
  2110. ClassExp(&class_name);
  2111. EmitDerefOp(_OP_NEWSLOT);
  2112. _fs->PopTarget();
  2113. }
  2114. else {
  2115. Error(_SC("cannot create a class in a local with the syntax(class <local>)"));
  2116. }
  2117. _es = es;
  2118. }
  2119. SQObject ExpectScalar()
  2120. {
  2121. SQObject val;
  2122. val._type = OT_NULL; val._unVal.nInteger = 0; //shut up GCC 4.x
  2123. switch(_token) {
  2124. case TK_INTEGER:
  2125. val._type = OT_INTEGER;
  2126. val._unVal.nInteger = _lex.data->nvalue;
  2127. break;
  2128. case TK_FLOAT:
  2129. val._type = OT_FLOAT;
  2130. val._unVal.fFloat = _lex.data->fvalue;
  2131. break;
  2132. case TK_STRING_LITERAL:
  2133. val = _fs->CreateString(_lex.data->svalue,_lex.data->longstr.size()-1);
  2134. break;
  2135. case TK_TRUE:
  2136. case TK_FALSE:
  2137. val._type = OT_BOOL;
  2138. val._unVal.nInteger = _token == TK_TRUE ? 1 : 0;
  2139. break;
  2140. case '-':
  2141. Lex();
  2142. switch(_token)
  2143. {
  2144. case TK_INTEGER:
  2145. val._type = OT_INTEGER;
  2146. val._unVal.nInteger = -_lex.data->nvalue;
  2147. break;
  2148. case TK_FLOAT:
  2149. val._type = OT_FLOAT;
  2150. val._unVal.fFloat = -_lex.data->fvalue;
  2151. break;
  2152. default:
  2153. Error(_SC("scalar expected : integer, float"));
  2154. }
  2155. break;
  2156. default:
  2157. Error(_SC("scalar expected : integer, float or string"));
  2158. }
  2159. Lex();
  2160. return val;
  2161. }
  2162. SQInteger ExpressionScalar()
  2163. {
  2164. SQInteger tk_type = _token;
  2165. switch(_token) {
  2166. case TK_INTEGER:
  2167. EmitLoadConstInt(_lex.data->nvalue,-1);
  2168. break;
  2169. case TK_FLOAT:
  2170. EmitLoadConstFloat(_lex.data->fvalue,-1);
  2171. break;
  2172. case TK_STRING_LITERAL:
  2173. _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(_fs->CreateString(_lex.data->svalue,_lex.data->longstr.size()-1)));
  2174. break;
  2175. case TK_IDENTIFIER: {
  2176. SQObject id = _fs->CreateString(_lex.data->svalue);
  2177. Lex();
  2178. ExpressionConstant(id);
  2179. return tk_type;
  2180. }
  2181. break;
  2182. case '-':
  2183. Lex();
  2184. tk_type = _token;
  2185. switch(_token)
  2186. {
  2187. case TK_INTEGER:
  2188. EmitLoadConstInt(-_lex.data->nvalue,-1);
  2189. break;
  2190. case TK_FLOAT:
  2191. EmitLoadConstFloat(-_lex.data->fvalue,-1);
  2192. break;
  2193. default:
  2194. Error(_SC("scalar expected : integer, float"));
  2195. }
  2196. break;
  2197. default:
  2198. goto error;
  2199. }
  2200. Lex();
  2201. return tk_type;
  2202. error:
  2203. Error(_SC("constant or scalar expected : integer, float or string"));
  2204. return 0;
  2205. }
  2206. void EnumStatement()
  2207. {
  2208. Lex();
  2209. SQObject id = Expect(TK_IDENTIFIER);
  2210. Expect(_SC('{'));
  2211. //CheckLocalNameScope(id, _scope.nested);
  2212. SQObjectPtr strongid = id;
  2213. CheckLocalNameScope(id, _scope.nested);
  2214. SQObject table = _fs->CreateTable();
  2215. //_fs->AddInstruction(_OP_NEWOBJ, _fs->PushTarget(),0,NOT_TABLE);
  2216. SQInteger nval = 0;
  2217. while(_token != _SC('}')) {
  2218. SQObject key = Expect(TK_IDENTIFIER);
  2219. SQObject val;
  2220. if(_token == _SC('=')) {
  2221. Lex();
  2222. val = ExpectScalar();
  2223. }
  2224. else {
  2225. val._type = OT_INTEGER;
  2226. val._unVal.nInteger = nval++;
  2227. }
  2228. //SQInteger table = _fs->TopTarget(); //<<BECAUSE OF THIS NO COMMON EMIT FUNC IS POSSIBLE
  2229. //_fs->AddInstruction(_OP_NEWSLOT, 0xFF, table, key, val);
  2230. _table(table)->NewSlot(SQObjectPtr(key),SQObjectPtr(val));
  2231. if(_token == ',') Lex();
  2232. }
  2233. ConstsNewSlot(SQObjectPtr(strongid),SQObjectPtr(table));
  2234. strongid.Null();
  2235. Lex();
  2236. }
  2237. void TryCatchStatement()
  2238. {
  2239. SQObject exid;
  2240. Lex();
  2241. _fs->AddInstruction(_OP_PUSHTRAP,0,0);
  2242. _fs->_traps++;
  2243. if(_fs->_breaktargets.size()) _fs->_breaktargets.top()++;
  2244. if(_fs->_continuetargets.size()) _fs->_continuetargets.top()++;
  2245. SQInteger trappos = _fs->GetCurrentPos();
  2246. {
  2247. BEGIN_SCOPE();
  2248. Statement();
  2249. END_SCOPE();
  2250. }
  2251. _fs->_traps--;
  2252. _fs->AddInstruction(_OP_POPTRAP, 1, 0);
  2253. if(_fs->_breaktargets.size()) _fs->_breaktargets.top()--;
  2254. if(_fs->_continuetargets.size()) _fs->_continuetargets.top()--;
  2255. _fs->AddInstruction(_OP_JMP, 0, 0);
  2256. SQInteger jmppos = _fs->GetCurrentPos();
  2257. _fs->SetIntructionParam(trappos, 1, (_fs->GetCurrentPos() - trappos));
  2258. Expect(TK_CATCH); Expect(_SC('(')); exid = Expect(TK_IDENTIFIER); Expect(_SC(')'));
  2259. {
  2260. BEGIN_SCOPE();
  2261. SQInteger ex_target = _fs->PushLocalVariable(exid, _scope.nested);
  2262. _fs->SetIntructionParam(trappos, 0, ex_target);
  2263. Statement();
  2264. _fs->SetIntructionParams(jmppos, 0, (_fs->GetCurrentPos() - jmppos), 0);
  2265. END_SCOPE();
  2266. }
  2267. }
  2268. void FunctionExp(SQInteger ftype,bool lambda = false)
  2269. {
  2270. Lex(); Expect(_SC('('));
  2271. SQObjectPtr dummy;
  2272. CreateFunction(dummy, lambda ? eFunctionType_lambda : eFunctionType_anonymous);
  2273. _fs->AddInstruction(_OP_CLOSURE, _fs->PushTarget(), _fs->_functions.size() - 1, ftype == TK_FUNCTION?0:1);
  2274. }
  2275. void ClassExp(SQObjectPtr *class_name)
  2276. {
  2277. SQInteger base = -1;
  2278. SQInteger attrs = -1;
  2279. bool hasInheritance = false;
  2280. switch(_token)
  2281. {
  2282. case _SC(':'): //C++ style class derivation
  2283. case TK_EXTENDS: //squirrel style class derivation
  2284. Lex();
  2285. hasInheritance = true;
  2286. switch(_token)
  2287. {
  2288. case TK_PRIVATE:
  2289. case TK_PROTECTED:
  2290. case TK_PUBLIC:
  2291. Lex(); //ignore, accepted only to compile a subset of C++
  2292. }
  2293. }
  2294. if(hasInheritance) {
  2295. Expression();
  2296. base = _fs->TopTarget();
  2297. }
  2298. if(_token == TK_ATTR_OPEN) {
  2299. Lex();
  2300. _fs->AddInstruction(_OP_NEWOBJ, _fs->PushTarget(),0,NOT_TABLE);
  2301. ParseTableOrClass(_SC(','),TK_ATTR_CLOSE);
  2302. attrs = _fs->TopTarget();
  2303. }
  2304. Expect(_SC('{'));
  2305. if(attrs != -1) _fs->PopTarget();
  2306. if(base != -1) _fs->PopTarget();
  2307. _fs->AddInstruction(_OP_NEWOBJ, _fs->PushTarget(), base, attrs,NOT_CLASS);
  2308. ParseTableOrClass(_SC(';'),_SC('}'), class_name);
  2309. }
  2310. void DeleteExpr()
  2311. {
  2312. SQExpState es;
  2313. Lex();
  2314. es = _es;
  2315. _es.donot_get = true;
  2316. PrefixedExpr();
  2317. if(_es.etype==EXPR) Error(_SC("can't delete an expression"));
  2318. if(_es.etype==OBJECT || _es.etype==BASE) {
  2319. Emit2ArgsOP(_OP_DELETE);
  2320. }
  2321. else {
  2322. Error(_SC("cannot delete an (outer) local"));
  2323. }
  2324. _es = es;
  2325. }
  2326. void PrefixIncDec(SQInteger token)
  2327. {
  2328. SQExpState es;
  2329. SQInteger diff = (token==TK_MINUSMINUS) ? -1 : 1;
  2330. Lex();
  2331. es = _es;
  2332. _es.donot_get = true;
  2333. PrefixedExpr();
  2334. if(_es.etype==EXPR) {
  2335. Error(_SC("can't '++' or '--' an expression"));
  2336. }
  2337. else if(_es.etype==OBJECT || _es.etype==BASE) {
  2338. Emit2ArgsOP(_OP_INC, diff);
  2339. }
  2340. else if(_es.etype==LOCAL) {
  2341. SQInteger src = _fs->TopTarget();
  2342. _fs->AddInstruction(_OP_INCL, src, src, 0, diff);
  2343. }
  2344. else if(_es.etype==OUTER) {
  2345. SQInteger tmp = _fs->PushTarget();
  2346. _fs->AddInstruction(_OP_GETOUTER, tmp, _es.epos);
  2347. _fs->AddInstruction(_OP_INCL, tmp, tmp, 0, diff);
  2348. _fs->AddInstruction(_OP_SETOUTER, tmp, _es.epos, tmp);
  2349. }
  2350. _es = es;
  2351. }
  2352. void CreateFunction(SQObject &name, int ftype, int stack_offset=0)
  2353. {
  2354. SQFuncState *funcstate = _fs->PushChildState(_ss(_vm));
  2355. funcstate->_name = name;
  2356. SQObject paramname, type_name;
  2357. funcstate->AddParameter(_fs->CreateString(_SC("this")), _scope.nested+1);
  2358. funcstate->_sourcename = _sourcename;
  2359. SQInteger defparams = 0;
  2360. bool is_reference_declaration = 0;
  2361. const SQChar *param_type_name = 0;
  2362. bool isVoid = false;
  2363. while(_token!=_SC(')')) {
  2364. if(isVoid)
  2365. {
  2366. Error(_SC("void type is invalid here"));
  2367. }
  2368. is_reference_declaration = 0; //reset is_reference_declaration
  2369. param_type_name = 0; //rest for each parameter
  2370. if(_token == TK_VOID)
  2371. {
  2372. isVoid = true;
  2373. Lex();
  2374. continue;
  2375. }
  2376. if(_token == TK_VARPARAMS) {
  2377. if(defparams > 0) Error(_SC("function with default parameters cannot have variable number of parameters"));
  2378. funcstate->AddParameter(_fs->CreateString(_SC("vargv")), _scope.nested+1);
  2379. funcstate->_varparams = true;
  2380. Lex();
  2381. if(_token != _SC(')')) Error(_SC("expected ')'"));
  2382. break;
  2383. }
  2384. else {
  2385. if(_token == TK_CONST) Lex(); //C/C++ const cualifiers for now
  2386. switch(_token)
  2387. {
  2388. CASE_TK_LOCAL_TYPES: //accept C/C++ type parameter declarations
  2389. param_type_name = _lex.GetTokenName(_token);
  2390. Lex();
  2391. }
  2392. CHECK_REF_DECLARATION(_token);
  2393. paramname = Expect(TK_IDENTIFIER);
  2394. if(CheckNameIsType(paramname))
  2395. {
  2396. //ignore custom types for now
  2397. param_type_name = _stringval(paramname);
  2398. CHECK_REF_DECLARATION(_token);
  2399. EatTemplateInitialization();
  2400. paramname = Expect(TK_IDENTIFIER);
  2401. }
  2402. funcstate->AddParameter(paramname, _scope.nested+1, is_reference_declaration ? _VAR_REFERENCE : _VAR_ANY);
  2403. if(param_type_name)
  2404. {
  2405. funcstate->AddParameterTypeName(param_type_name);
  2406. }
  2407. if(_token == _SC('=')) {
  2408. if(is_reference_declaration) Error(_SC("parameter passed by reference can't have default value"));
  2409. Lex();
  2410. if(_token == _SC('[') || _token == _SC('{')) Error(_SC("default parameter with array/table values not supported"));
  2411. Expression();
  2412. //stack_offset to compensate for local functions been relocated to allow recursion
  2413. funcstate->AddDefaultParam(_fs->TopTarget()+stack_offset);
  2414. defparams++;
  2415. }
  2416. else if(_token == _SC(':')){
  2417. //param type specifier like typescript
  2418. if(param_type_name)
  2419. {
  2420. Error(_SC("parameter type already declared before %s"), _string(paramname));
  2421. }
  2422. Lex();
  2423. type_name = ExpectTypeToken();
  2424. funcstate->AddParameterTypeName(type_name);
  2425. //printf("%d %s\n", __LINE__, _stringval(type_name));
  2426. }
  2427. else {
  2428. if(defparams > 0) Error(_SC("expected '='"));
  2429. }
  2430. if(_token == _SC(',')) Lex();
  2431. else if(_token != _SC(')')) Error(_SC("expected ')' or ','"));
  2432. }
  2433. }
  2434. Expect(_SC(')'));
  2435. switch(_token)
  2436. {
  2437. case _SC(':'):{
  2438. //return type specifier like typescript
  2439. Lex();
  2440. type_name = ExpectTypeToken();
  2441. funcstate->_return_type = type_name;
  2442. //printf("%d %s\n", __LINE__, _stringval(type_name));
  2443. }
  2444. break;
  2445. case TK_CONST:
  2446. case TK_NOEXCEPT:
  2447. case TK_VOLATILE:
  2448. Lex(); //accept and ignore
  2449. break;
  2450. case TK_IDENTIFIER:
  2451. if(ftype == eFunctionType_member)
  2452. {
  2453. if( (scstrcmp(_lex.data->svalue, _SC("final")) == 0) ||
  2454. (scstrcmp(_lex.data->svalue, _SC("override")) == 0))
  2455. {
  2456. Lex(); //accept but ignore then
  2457. }
  2458. }
  2459. }
  2460. for(SQInteger n = 0; n < defparams; n++) {
  2461. _fs->PopTarget();
  2462. }
  2463. if(_is_parsing_extern) {
  2464. if(_token == _SC(';')) //to parse thinscript
  2465. {
  2466. _fs->PopChildState();
  2467. return;
  2468. }
  2469. else _is_parsing_extern = false;
  2470. }
  2471. SQFuncState *currchunk = _fs;
  2472. _fs = funcstate;
  2473. if(ftype == eFunctionType_lambda) {
  2474. Expression();
  2475. _fs->AddInstruction(_OP_RETURN, 1, _fs->PopTarget());}
  2476. else {
  2477. Statement(false);
  2478. }
  2479. ResolveGotos();
  2480. funcstate->AddLineInfos(_lex.data->prevtoken == _SC('\n')?_lex.data->lasttokenline:_lex.data->currentline, _lineinfo, true);
  2481. funcstate->AddInstruction(_OP_RETURN, -1);
  2482. funcstate->SetStackSize(0);
  2483. SQFunctionProto *func = funcstate->BuildProto();
  2484. #ifdef _DEBUG_DUMP
  2485. funcstate->Dump(func);
  2486. #endif
  2487. _fs = currchunk;
  2488. _fs->_functions.push_back(func);
  2489. _fs->PopChildState();
  2490. }
  2491. void AdjustGotoInstruction(const SQGotoLabelsInfo &goto_info, const SQGotoLabelsInfo &label)
  2492. {
  2493. SQInteger target_pos = label.pos;
  2494. SQInteger jump_pos = target_pos - goto_info.pos;
  2495. //set the jmp instruction
  2496. SQInteger poptraps = goto_info.traps - label.traps;
  2497. SQInt32 ioffset = 1;
  2498. while(poptraps)
  2499. {
  2500. SQInstruction &i_op_poptrap = _fs->GetInstruction(goto_info.pos-ioffset);
  2501. if(i_op_poptrap.op != _OP_NOP)
  2502. {
  2503. if(ioffset < 2)
  2504. {
  2505. ++ioffset;
  2506. continue;
  2507. }
  2508. Error(_SC("Compiler: This should not happen i_op_poptrap.op != _OP_NOP"));
  2509. }
  2510. //printf("poptraps for %s = %d : %d\n", _stringval(label.name), (int)poptraps, (int)i_op_poptrap._arg0);
  2511. i_op_poptrap.op = _OP_POPTRAP;
  2512. i_op_poptrap._arg0 = poptraps;
  2513. break;
  2514. }
  2515. _fs->SetIntructionParams(goto_info.pos, 0, jump_pos, 0);
  2516. }
  2517. void ResolveGotos()
  2518. {
  2519. //first we walk through the labels and when we come back
  2520. //nested labels we remove then because we only allow
  2521. //goto/joump out of blocks/scopes
  2522. SQInt16 last_nested = -1;
  2523. for(SQUnsignedInteger idxlabel = 0; idxlabel < _fs->_gototargets.size(); ++idxlabel) {
  2524. SQGotoLabelsInfo &label = _fs->_gototargets[idxlabel];
  2525. SQUnsignedInteger resolved = 0;
  2526. for(SQUnsignedInteger idxGoto = 0; idxGoto < _fs->_unresolvedgotos.size(); ++idxGoto) {
  2527. SQGotoLabelsInfo &goto_info = _fs->_unresolvedgotos[idxGoto];
  2528. if(_string(label.name) == _string(goto_info.name))
  2529. {
  2530. if(label.nested > goto_info.nested)
  2531. {
  2532. _lex.data->currentline = goto_info.line;
  2533. Error(_SC("Goto can not jump inside nested block/scope '%s'"), _stringval(goto_info.name));
  2534. }
  2535. AdjustGotoInstruction(goto_info, label);
  2536. //we dcrease idxGoto here to compensate the removal inside the for loop
  2537. _fs->_unresolvedgotos.remove(idxGoto--);
  2538. ++resolved;
  2539. }
  2540. }
  2541. if(!resolved)
  2542. {
  2543. //if a label isn't resolved we have an unused label
  2544. _lex.data->currentline = label.line;
  2545. Error(_SC("Label not resolved at this point '%s'"), _stringval(label.name));
  2546. }
  2547. if(label.nested < last_nested)
  2548. {
  2549. //remove any previous nested label to prevent goto/jump inside blocks/scopes
  2550. SQUnsignedInteger i = idxlabel - 1;
  2551. for(; i > 0; --i)
  2552. {
  2553. if(_fs->_gototargets[i].nested <= label.nested) break;
  2554. _fs->_gototargets.remove(i);
  2555. //we decrease the index idxlabel here due to remove before
  2556. //the current idxlabel
  2557. --idxlabel;
  2558. }
  2559. }
  2560. last_nested = label.nested;
  2561. }
  2562. if(_fs->_unresolvedgotos.size())
  2563. {
  2564. //if we still have any unresolved goto it's an error, let's show the first
  2565. SQGotoLabelsInfo &goto_info = _fs->_unresolvedgotos[0];
  2566. _lex.data->currentline = goto_info.line;
  2567. Error(_SC("Goto can not jump to undeclared label '%s'"), _stringval(goto_info.name));
  2568. }
  2569. }
  2570. void ResolveBreaks(SQFuncState *funcstate, SQInteger ntoresolve)
  2571. {
  2572. while(ntoresolve > 0) {
  2573. SQInteger pos = funcstate->_unresolvedbreaks.back();
  2574. funcstate->_unresolvedbreaks.pop_back();
  2575. //set the jmp instruction
  2576. funcstate->SetIntructionParams(pos, 0, funcstate->GetCurrentPos() - pos, 0);
  2577. ntoresolve--;
  2578. }
  2579. }
  2580. void ResolveContinues(SQFuncState *funcstate, SQInteger ntoresolve, SQInteger targetpos)
  2581. {
  2582. while(ntoresolve > 0) {
  2583. SQInteger pos = funcstate->_unresolvedcontinues.back();
  2584. funcstate->_unresolvedcontinues.pop_back();
  2585. //set the jmp instruction
  2586. funcstate->SetIntructionParams(pos, 0, targetpos - pos, 0);
  2587. ntoresolve--;
  2588. }
  2589. }
  2590. private:
  2591. SQInteger _token;
  2592. SQFuncState *_fs;
  2593. SQObjectPtr _sourcename;
  2594. SQLexer _lex;
  2595. bool _lineinfo;
  2596. bool _raiseerror;
  2597. bool _show_warnings;
  2598. bool _is_parsing_extern;
  2599. SQInteger _debugline;
  2600. SQInteger _debugop;
  2601. SQExpState _es;
  2602. SQScope _scope;
  2603. SQChar *_compilererror;
  2604. jmp_buf _errorjmp;
  2605. SQVM *_vm;
  2606. SQObjectPtrVec _scope_consts;
  2607. SQObjectPtr _globals;
  2608. SQObjectPtr _type_names; //to allow C/C++ style instance declarations
  2609. SQObjectPtr _extern_names; //to allow C/C++ style extern declarations
  2610. SQChar error_buf[MAX_COMPILER_ERROR_LEN];
  2611. SQInteger _max_nested_includes, _nested_includes_count;
  2612. };
  2613. bool Compile(SQVM *vm,SQLEXREADFUNC rg, SQUserPointer up, const SQChar *sourcename, SQObjectPtr &out,
  2614. bool raiseerror, bool lineinfo, bool show_warnings, SQInteger max_nested_includes)
  2615. {
  2616. SQCompiler p(vm, rg, up, sourcename, raiseerror, lineinfo, show_warnings, max_nested_includes);
  2617. return p.Compile(out);
  2618. }
  2619. #endif