sqcompiler.cpp 116 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519
  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. {
  32. SQInteger etype; /* expr. type; one of EXPR, OBJECT, BASE, OUTER, LOCAL or EXPR_STATEMENT */
  33. SQInteger epos; /* expr. location on stack; -1 for OBJECT and BASE */
  34. bool donot_get; /* signal not to deref the next value */
  35. };
  36. #define MAX_COMPILER_ERROR_LEN 256
  37. struct SQScope
  38. {
  39. SQInt32 stacksize;
  40. SQInt16 outers;
  41. SQInt16 nested;
  42. };
  43. #define BEGIN_SCOPE() SQScope __oldscope__ = _scope; \
  44. ++_scope.nested; \
  45. _scope.outers = (SQInt16)_fs->_outers; \
  46. _scope.stacksize = (SQInt32)_fs->GetStackSize();\
  47. _scope_types.push_back(SQTable::Create(_ss(_vm),0)); \
  48. _scope_consts.push_back(SQTable::Create(_ss(_vm),0));
  49. #define RESOLVE_OUTERS() if(_fs->GetStackSize() != _scope.stacksize) { \
  50. if(_fs->CountOuters(_scope.stacksize)) { \
  51. _fs->AddInstruction(_OP_CLOSE,0,_scope.stacksize); \
  52. } \
  53. }
  54. #define END_SCOPE_NO_CLOSE() { if(_fs->GetStackSize() != _scope.stacksize) { \
  55. _fs->SetStackSize(_scope.stacksize); \
  56. } \
  57. _scope = __oldscope__; \
  58. _scope_types.pop_back();\
  59. _scope_consts.pop_back();\
  60. }
  61. #define END_SCOPE() { SQInteger oldouters = _fs->_outers;\
  62. if(_fs->GetStackSize() != _scope.stacksize) { \
  63. _fs->SetStackSize(_scope.stacksize); \
  64. if(oldouters != _fs->_outers) { \
  65. _fs->AddInstruction(_OP_CLOSE,0,_scope.stacksize); \
  66. } \
  67. } \
  68. _scope = __oldscope__; \
  69. }
  70. #define BEGIN_BREAKBLE_BLOCK() SQInteger __nbreaks__=_fs->_unresolvedbreaks.size(); \
  71. SQInteger __ncontinues__=_fs->_unresolvedcontinues.size(); \
  72. _fs->_breaktargets.push_back(0);_fs->_continuetargets.push_back(0);
  73. #define END_BREAKBLE_BLOCK(continue_target) {__nbreaks__=_fs->_unresolvedbreaks.size()-__nbreaks__; \
  74. __ncontinues__=_fs->_unresolvedcontinues.size()-__ncontinues__; \
  75. if(__ncontinues__>0)ResolveContinues(_fs,__ncontinues__,continue_target); \
  76. if(__nbreaks__>0)ResolveBreaks(_fs,__nbreaks__); \
  77. _fs->_breaktargets.pop_back();_fs->_continuetargets.pop_back();}
  78. #define CASE_TK_LOCAL_CHAR_TYPES \
  79. case TK_LOCAL_STRING_T: \
  80. case TK_LOCAL_CHAR_T: \
  81. case TK_LOCAL_WCHAR_T
  82. #define CASE_TK_LOCAL_INT_TYPES \
  83. case TK_LOCAL_INT8_T: \
  84. case TK_LOCAL_INT16_T: \
  85. case TK_LOCAL_INT32_T: \
  86. case TK_LOCAL_INT64_T: \
  87. case TK_LOCAL_INT_T: \
  88. case TK_LOCAL_UINT8_T: \
  89. case TK_LOCAL_UINT16_T: \
  90. case TK_LOCAL_UINT32_T: \
  91. case TK_LOCAL_UINT64_T: \
  92. case TK_LOCAL_UINT_T: \
  93. case TK_LOCAL_SIZE_T: \
  94. case TK_LOCAL_SSIZE_T
  95. #define CASE_TK_LOCAL_FLOAT_TYPES \
  96. case TK_LOCAL_FLOAT_T: \
  97. case TK_LOCAL_DOUBLE_T: \
  98. case TK_LOCAL_LONG_DOUBLE_T
  99. #define CASE_TK_NUMBER_TYPES \
  100. CASE_TK_LOCAL_FLOAT_TYPES: \
  101. CASE_TK_LOCAL_INT_TYPES: \
  102. case TK_LOCAL_NUMBER_T
  103. #define CASE_CONST_TYPES \
  104. CASE_TK_LOCAL_CHAR_TYPES: \
  105. CASE_TK_NUMBER_TYPES: \
  106. case TK_LOCAL_BOOL_T: \
  107. case TK_LOCAL_VOIDPTR_T
  108. #define CASE_TK_LOCAL_TYPES \
  109. CASE_CONST_TYPES: \
  110. case TK_LOCAL_TABLE_T: \
  111. case TK_LOCAL_ANY_T: \
  112. case TK_LOCAL_ARRAY_T: \
  113. case TK_VOID: \
  114. case TK_LOCAL_WEAKREF_T
  115. #define CASE_TK_INTEGER \
  116. case TK_INTEGER: \
  117. case TK_UNSIGNED_INTEGER: \
  118. case TK_SHORT_INTEGER: \
  119. case TK_UNSIGNED_SHORT_INTEGER: \
  120. case TK_LONG_INTEGER: \
  121. case TK_UNSIGNED_LONG_INTEGER: \
  122. case TK_LONG_LONG_INTEGER: \
  123. case TK_UNSIGNED_LONG_LONG_INTEGER
  124. static SQInteger compilerReadFunc(SQUserPointer fp)
  125. {
  126. SQInteger c = fgetc((FILE *)fp);
  127. if(c == EOF)
  128. {
  129. return 0;
  130. }
  131. return c;
  132. }
  133. class SQCompiler
  134. {
  135. public:
  136. SQCompiler(SQVM *v, SQLEXREADFUNC rg, SQUserPointer up, const SQChar* sourcename,
  137. bool raiseerror, bool lineinfo, bool show_warnings, SQInteger max_nested_includes)
  138. {
  139. _vm=v;
  140. _lex.Init(_ss(v), rg, up,ThrowError,this);
  141. _sourcename = SQString::Create(_ss(v), sourcename);
  142. _lineinfo = lineinfo;
  143. _raiseerror = raiseerror;
  144. _show_warnings = show_warnings;
  145. _scope.outers = 0;
  146. _scope.stacksize = 0;
  147. _scope.nested = 0;
  148. _compilererror = NULL;
  149. _globals = SQTable::Create(_ss(_vm),0);
  150. _type_names = SQTable::Create(_ss(_vm),0);
  151. _extern_names = SQTable::Create(_ss(_vm),0);
  152. _max_nested_includes = max_nested_includes;
  153. _nested_includes_count = 0;
  154. _is_parsing_extern = false;
  155. _ifdef_exclude = 0;
  156. _ifdef_line = 0;
  157. _inside_ifdef = 0;
  158. squilu_lib_path = NULL;
  159. _inside_constructor = false;
  160. }
  161. ~SQCompiler()
  162. {
  163. _table(_globals)->Finalize();
  164. _globals.Null();
  165. }
  166. static void ThrowError(void *ud, const SQChar *s)
  167. {
  168. SQCompiler *c = (SQCompiler *)ud;
  169. c->Error(_SC("%s"), s);
  170. }
  171. void Error(const SQChar *s, ...) __attribute__((format(printf, 2, 3)))
  172. {
  173. va_list vl;
  174. va_start(vl, s);
  175. scvsprintf(error_buf, sizeof(error_buf), s, vl);
  176. va_end(vl);
  177. _compilererror = error_buf;
  178. longjmp(_errorjmp,1);
  179. }
  180. void Warning(const SQChar *s, ...) __attribute__((format(printf, 2, 3)))
  181. {
  182. if(!_show_warnings) return;
  183. va_list vl;
  184. va_start(vl, s);
  185. scvfprintf(stderr, s, vl);
  186. va_end(vl);
  187. }
  188. void ErrorNameAlreadyDeclared(const SQObject &name)
  189. {
  190. //Error(_SC("Name '%s' already declared"), _stringval(name));
  191. }
  192. bool CheckNameIsType(const SQObject &name)
  193. {
  194. for(int i=_scope.nested-1; i >= 0; --i)
  195. {
  196. if(_table(_scope_types[i])->Exists(name)) return true;
  197. }
  198. return _table(_type_names)->Exists(name);
  199. }
  200. bool CheckTypeName(const SQObject &name, bool addIfNotExists=false)
  201. {
  202. bool found = CheckNameIsType(name);
  203. if(addIfNotExists && !found)
  204. {
  205. SQObjectPtr oname = name, otrue = true;
  206. _table(_type_names)->NewSlot(oname, otrue);
  207. }
  208. return found;
  209. }
  210. bool TypesGet(const SQObjectPtr &key,SQObjectPtr &val)
  211. {
  212. for(int i=_scope.nested-1; i >= 0; --i)
  213. {
  214. if(_table(_scope_types[i])->Get(key,val)) return true;
  215. }
  216. return _table(_type_names)->NewSlot(key,val);
  217. }
  218. bool TypesNewSlot(const SQObjectPtr &key, const SQObjectPtr &val)
  219. {
  220. if(_scope.nested) return _table(_scope_types[_scope.nested-1])->NewSlot(key,val);
  221. return _table(_type_names)->NewSlot(key,val);
  222. }
  223. SQInteger CheckExternName(const SQObject &name, bool addIfNotExists=false)
  224. {
  225. SQObjectPtr orefcount;
  226. int found = _table(_extern_names)->Get(name, orefcount);
  227. if(addIfNotExists && !found)
  228. {
  229. SQObjectPtr oname = name;
  230. _integer(orefcount) = 1; //1 means only declared, 2 declared and assigned
  231. _table(_extern_names)->NewSlot(oname, orefcount);
  232. _table(_globals)->NewSlot(oname, orefcount);
  233. }
  234. return found ? _integer(orefcount) : 0;
  235. }
  236. SQInteger ExternNameSetRefCount(const SQObject &name)
  237. {
  238. SQInteger ref_count = CheckExternName(name);
  239. if(ref_count == 1)
  240. {
  241. SQObjectPtr orefcount;
  242. _integer(orefcount) = ref_count = 2;
  243. _table(_extern_names)->Set(name, orefcount);
  244. }
  245. return ref_count;
  246. }
  247. void AddGlobalName(const SQObject &name)
  248. {
  249. SQObjectPtr oname = name, otrue = true;
  250. _table(_globals)->NewSlot(oname, otrue);
  251. }
  252. void CheckGlobalName(const SQObject &name, bool addIfNotExists=false, bool checkLocals=true)
  253. {
  254. const SQChar *found = NULL;
  255. if(CheckExternName(name))
  256. {
  257. found = _SC("extern");
  258. }
  259. else if(_table(_globals)->Exists(name))
  260. {
  261. found = _SC("global");
  262. }
  263. if(found)
  264. {
  265. if(checkLocals) Error(_SC("%s '%s' already declared"), found, _stringval(name));
  266. else Warning(_SC("%s:%d:%d warning %s '%s' already declared will be shadowed\n"),
  267. _stringval(_sourcename), (int)_lex.data->currentline, (int)_lex.data->currentcolumn,
  268. found, _stringval(name));
  269. }
  270. else if(checkLocals) CheckLocalNameScope(name, -1, false);
  271. if(addIfNotExists)
  272. {
  273. AddGlobalName(name);
  274. }
  275. }
  276. void CheckLocalNameScope(const SQObject &name, SQInteger scope, bool checkGlobals=true)
  277. {
  278. SQInteger found = _fs->GetLocalVariable(name);
  279. if(found >= 0)
  280. {
  281. SQLocalVarInfo &lvi = _fs->_vlocals[found];
  282. if(lvi._scope == scope)
  283. Error(_SC("local '%s' already declared"), _stringval(name));
  284. else
  285. Warning(_SC("%s:%d:%d warning local '%s' already declared will be shadowed\n"),
  286. _stringval(_sourcename), (int)_lex.data->currentline, (int)_lex.data->currentcolumn, _stringval(name));
  287. }
  288. else
  289. {
  290. found = _fs->FindOuterVariable(name);
  291. if(found >= 0) Warning(_SC("%s:%d:%d warning outer variable '%s' already declared will be shadowed\n"),
  292. _stringval(_sourcename), (int)_lex.data->currentline, (int)_lex.data->currentcolumn, _stringval(name));
  293. }
  294. if(checkGlobals) CheckGlobalName(name, false, false);
  295. SQObjectPtr strongid = name;
  296. CheckConstsExists(strongid);
  297. }
  298. bool IsConstant(const SQObject &name,SQObject &e)
  299. {
  300. SQObjectPtr val;
  301. if(ConstsGet(name, val))
  302. {
  303. e = val;
  304. return true;
  305. }
  306. return false;
  307. }
  308. SQInteger ExpressionConstant(SQObject id)
  309. {
  310. SQObject constant;
  311. SQInteger epos = 0;
  312. if(IsConstant(id, constant))
  313. {
  314. /* Handle named constant */
  315. SQObjectPtr constval;
  316. SQObject constid;
  317. if(sq_type(constant) == OT_TABLE)
  318. {
  319. Expect('.');
  320. constid = Expect(TK_IDENTIFIER);
  321. if(!_table(constant)->Get(constid, constval))
  322. {
  323. constval.Null();
  324. Error(_SC("invalid constant [%s.%s]"), _stringval(id), _stringval(constid));
  325. }
  326. }
  327. else
  328. {
  329. constval = constant;
  330. }
  331. epos = _fs->PushTarget();
  332. /* generate direct or literal function depending on size */
  333. SQObjectType ctype = sq_type(constval);
  334. switch(ctype)
  335. {
  336. case OT_INTEGER:
  337. EmitLoadConstInt(_integer(constval),epos);
  338. break;
  339. case OT_FLOAT:
  340. EmitLoadConstFloat(_float(constval),epos);
  341. break;
  342. default:
  343. _fs->AddInstruction(_OP_LOAD,epos,_fs->GetConstant(constval));
  344. break;
  345. }
  346. }
  347. else
  348. {
  349. Error(_SC("invalid constant [%s]"), _stringval(id));
  350. }
  351. return epos;
  352. }
  353. void CheckConstsExists(const SQObjectPtr &key)
  354. {
  355. int found = -1;
  356. for(int i=_scope.nested-1; i >= 0; --i)
  357. {
  358. if(_table(_scope_consts[i])->Exists(key))
  359. {
  360. found = i+1;
  361. break;
  362. }
  363. }
  364. if(found < 0 && _table(_ss(_vm)->_consts)->Exists(key)) found = 0;
  365. if(found == _scope.nested)
  366. {
  367. Error(_SC("constant '%s' already exists\n"), _stringval(key));
  368. }
  369. if(found >= 0) Warning(_SC("%s:%d:%d warning an already defined constant '%s' will be shadowed\n"),
  370. _stringval(_sourcename), (int)_lex.data->currentline, (int)_lex.data->currentcolumn, _stringval(key));
  371. }
  372. bool ConstsGet(const SQObjectPtr &key,SQObjectPtr &val)
  373. {
  374. for(int i=_scope.nested-1; i >= 0; --i)
  375. {
  376. if(_table(_scope_consts[i])->Get(key,val)) return true;
  377. }
  378. return _table(_ss(_vm)->_consts)->Get(key,val);
  379. }
  380. void CompileTimeCheckConstsGet(const SQObjectPtr &key,SQObjectPtr &val)
  381. {
  382. if(!ConstsGet(key, val))
  383. {
  384. Error(_SC("compile time checked constant fail \"%s\""), _stringval(key));
  385. }
  386. }
  387. bool ConstsNewSlot(const SQObjectPtr &key, const SQObjectPtr &val)
  388. {
  389. if(_scope.nested) return _table(_scope_consts[_scope.nested-1])->NewSlot(key,val);
  390. return _table(_ss(_vm)->_consts)->NewSlot(key,val);
  391. }
  392. void Lex(bool dontThrowIntegerOverflow=false)
  393. {
  394. _token = _lex.Lex(dontThrowIntegerOverflow);
  395. }
  396. SQObjectPtr GetTokenObject(SQInteger tok, bool doLex=true)
  397. {
  398. SQObjectPtr ret;
  399. switch(tok)
  400. {
  401. case TK_IDENTIFIER:
  402. ret = _fs->CreateString(_lex.data->svalue);
  403. break;
  404. case TK_STRING_LITERAL:
  405. ret = _fs->CreateString(_lex.data->svalue,_lex.data->longstr.size()-1);
  406. break;
  407. CASE_TK_INTEGER:
  408. ret = SQObjectPtr(_lex.data->nvalue);
  409. break;
  410. case TK_FLOAT:
  411. ret = SQObjectPtr(_lex.data->fvalue);
  412. break;
  413. default:
  414. ret = _fs->CreateString(_lex.GetTokenName(_token));
  415. }
  416. if(doLex) Lex();
  417. return ret;
  418. }
  419. void ErrorIfNotToken(SQInteger tok)
  420. {
  421. if(_token != tok)
  422. {
  423. if(((_token == TK_CONSTRUCTOR) || (_token == TK_DESTRUCTOR)) && tok == TK_IDENTIFIER)
  424. {
  425. //do nothing
  426. }
  427. else
  428. {
  429. const SQChar *etypename;
  430. if(tok > 255)
  431. {
  432. switch(tok)
  433. {
  434. case TK_IDENTIFIER:
  435. etypename = _SC("IDENTIFIER");
  436. break;
  437. case TK_STRING_LITERAL:
  438. etypename = _SC("STRING_LITERAL");
  439. break;
  440. CASE_TK_INTEGER:
  441. etypename = _SC("INTEGER");
  442. break;
  443. case TK_FLOAT:
  444. etypename = _SC("FLOAT");
  445. break;
  446. default:
  447. etypename = _lex.Tok2Str(tok);
  448. }
  449. Error(_SC("expected '%s'"), etypename);
  450. }
  451. Error(_SC("expected '%c'"), (int)tok);
  452. }
  453. }
  454. }
  455. SQObject Expect(SQInteger tok, bool doLex=true)
  456. {
  457. ErrorIfNotToken(tok);
  458. return GetTokenObject(tok, doLex);
  459. }
  460. SQObject ExpectTypeToken()
  461. {
  462. switch(_token)
  463. {
  464. CASE_TK_LOCAL_TYPES:
  465. return GetTokenObject(_token);
  466. case TK_IDENTIFIER:
  467. break;
  468. default:
  469. ErrorIfNotToken(TK_IDENTIFIER);
  470. }
  471. return GetTokenObject(TK_IDENTIFIER);
  472. }
  473. bool IsEndOfStatement()
  474. {
  475. return ((_lex.data->prevtoken == _SC('\n')) || (_token == SQUIRREL_EOB) || (_token == _SC('}')) || (_token == _SC(';')));
  476. }
  477. void OptionalSemicolon()
  478. {
  479. if(_token == _SC(';'))
  480. {
  481. Lex();
  482. return;
  483. }
  484. if(!IsEndOfStatement())
  485. {
  486. Error(_SC("end of statement expected (; or lf) found (%d)"), (int)_token);
  487. }
  488. }
  489. void MoveIfCurrentTargetIsLocal()
  490. {
  491. SQInteger trg = _fs->TopTarget();
  492. if(_fs->IsLocal(trg))
  493. {
  494. trg = _fs->PopTarget(); //pops the target and moves it
  495. _fs->AddInstruction(_OP_MOVE, _fs->PushTarget(), trg);
  496. }
  497. }
  498. void Pragma()
  499. {
  500. SQInteger line = _lex.data->currentline;
  501. SQInteger column = _lex.data->currentcolumn;
  502. Lex();
  503. if(_token == TK_ELSE)
  504. {
  505. if(!_inside_ifdef) Error(_SC("'#else' without '#ifdef'"));
  506. Lex();
  507. //if we were not been excluding we start excluding
  508. if(_ifdef_exclude == 0) ++_ifdef_exclude;
  509. else if(_ifdef_exclude == 1) --_ifdef_exclude;
  510. return;
  511. }
  512. SQObject id = Expect(TK_IDENTIFIER);
  513. if(scstrcmp(_stringval(id), _SC("include")) == 0)
  514. {
  515. #if SQ_ENABLE_INCLUDES
  516. SQInteger nested_count = _nested_includes_count + 1;
  517. if((_max_nested_includes <= 0) || (nested_count > _max_nested_includes))
  518. {
  519. Error(_SC("Error: too many nested includes %d %s\n"), (int)nested_count, _stringval(id));
  520. }
  521. //C/C++ system includes we do not use it in SquiLu
  522. if(_ifdef_exclude && (_token == _SC('<')))
  523. {
  524. while(_token > 0)
  525. {
  526. if(_token == _SC('>'))
  527. {
  528. Lex();
  529. return;
  530. }
  531. Lex();
  532. }
  533. return;
  534. }
  535. //do not call Lex() since next token can try search for id defined in the include
  536. //and then go global instead of local, we'll call it after including
  537. id = Expect(TK_STRING_LITERAL, false);
  538. if(_ifdef_exclude)
  539. {
  540. Lex();
  541. return;
  542. }
  543. //Warning(_SC("%s:%d:%d warning pragma include %s\n"),
  544. // _stringval(_sourcename), line, column, _stringval(id));
  545. const SQChar *lib_path = NULL;
  546. //first try the filename alone
  547. FILE *fp = scfopen(_stringval(id), "r");
  548. while(!fp)
  549. {
  550. //now if we have an environment path let's try it
  551. if(!lib_path)
  552. {
  553. if(!squilu_lib_path) squilu_lib_path = _vm->GetIncludePath();
  554. lib_path = squilu_lib_path;
  555. if(!lib_path) break;
  556. }
  557. while (*lib_path == *SQUILU_PATH_SEP) lib_path++; /* skip separators */
  558. if (*lib_path == '\0') break; /* no more templates */
  559. const SQChar *l = scstrchr(lib_path, *SQUILU_PATH_SEP); /* find next separator */
  560. if (l == NULL) l = lib_path + scstrlen(lib_path);
  561. size_t path_len = l - lib_path;
  562. SQChar file_path_name[SQUILU_MAX_PATH];
  563. if(path_len < sizeof(file_path_name)) //enough room to work ?
  564. {
  565. scsprintf(file_path_name, sizeof(file_path_name), "%s", lib_path);
  566. scsprintf(file_path_name + path_len, sizeof(file_path_name) - path_len,
  567. "%s%s", SQUILU_DIRSEP, _stringval(id));
  568. //printf("file_path_name = %s\n", file_path_name);
  569. fp = scfopen(file_path_name, "r");
  570. lib_path += path_len; //move the pointer to the next path
  571. }
  572. }
  573. if(fp != NULL)
  574. {
  575. //increment nested count
  576. ++_nested_includes_count;
  577. //save current source file and lex state
  578. SQUserPointer saved_up = _lex._up; //current userpointer
  579. SQLEXREADFUNC saved_readf = _lex._readf; //current readfunction
  580. SQInteger saved_line = _lex.data->currentline;
  581. SQInteger saved_column = _lex.data->currentcolumn;
  582. SQInteger saved_curdata = _lex.data->currdata;
  583. SQInteger saved_prevtoken = _lex.data->prevtoken;
  584. SQInteger saved_token = _token;
  585. SQInteger saved__inside_ifdef = _inside_ifdef;
  586. SQObjectPtr saved_source_name = _sourcename;
  587. //set new source file
  588. _fs->_sourcename = id;
  589. _sourcename = id;
  590. _lex.ResetReader(compilerReadFunc, fp, 1);
  591. _inside_ifdef = 0;
  592. //compile the include file
  593. Lex();
  594. while(_token > 0)
  595. {
  596. Statement();
  597. if(_lex.data->prevtoken != _SC('}') && _lex.data->prevtoken != _SC(';')) OptionalSemicolon();
  598. }
  599. //close file
  600. fclose(fp);
  601. if(_inside_ifdef) Error(_SC("unterminated #ifdef starting on line %d"), (int)_ifdef_line);
  602. //restore saved source file and lex state
  603. _fs->_sourcename = saved_source_name;
  604. _sourcename = saved_source_name;
  605. _token = saved_token;
  606. _lex.data->currdata = saved_curdata;
  607. _lex.data->prevtoken = saved_prevtoken;
  608. _lex.data->currentcolumn = saved_column;
  609. _lex.data->currentline = saved_line;
  610. _lex._readf = saved_readf;
  611. _lex._up = saved_up;
  612. --_nested_includes_count;
  613. _inside_ifdef = saved__inside_ifdef;
  614. //done let's continue working
  615. //Now we do the Lex() call skiped before compile the include file
  616. Lex();
  617. }
  618. else
  619. {
  620. Error(_SC("Error: opening include file %s\n"), _stringval(id));
  621. }
  622. #else
  623. Error(_SC("Error: includes are not enabled\n"));
  624. #endif
  625. }
  626. else if(scstrcmp(_stringval(id), _SC("endif")) == 0)
  627. {
  628. if(!_inside_ifdef) Error(_SC("'#endif' without '#ifdef'"));
  629. if(_ifdef_exclude) --_ifdef_exclude;
  630. --_inside_ifdef;
  631. }
  632. else if((scstrcmp(_stringval(id), _SC("ifdef")) == 0)
  633. || (scstrcmp(_stringval(id), _SC("ifndef")) == 0))
  634. {
  635. ++_inside_ifdef;
  636. bool isIfndef = _stringval(id)[2] == _SC('n');
  637. id = Expect(TK_IDENTIFIER);
  638. if(_ifdef_exclude) ++_ifdef_exclude;
  639. else
  640. {
  641. _ifdef_line = line;
  642. bool isDefined = _vm->IsDefined(_stringval(id));
  643. if(isIfndef) isDefined = !isDefined;
  644. _ifdef_exclude = (isDefined ? 0 : 1);
  645. }
  646. }
  647. else if(scstrcmp(_stringval(id), _SC("define")) == 0)
  648. {
  649. id = Expect(TK_IDENTIFIER);
  650. if(_ifdef_exclude == 0)
  651. {
  652. if(_vm->IsDefined(_stringval(id)))
  653. Warning(_SC("%s:%d:%d warning '%s' redefined\n"),
  654. _stringval(_sourcename), (int)line, (int)column, _stringval(id));
  655. _vm->AddDefined(_stringval(id));
  656. }
  657. }
  658. else if(scstrcmp(_stringval(id), _SC("undef")) == 0)
  659. {
  660. id = Expect(TK_IDENTIFIER);
  661. if(_ifdef_exclude == 0) _vm->RemoveDefined(_stringval(id));
  662. }
  663. else
  664. {
  665. _lex.data->currentline = line;
  666. Error(_SC("Error: unknown pragma %s\n"), _stringval(id));
  667. }
  668. }
  669. bool Compile(SQObjectPtr &o)
  670. {
  671. _debugline = 1;
  672. _debugop = 0;
  673. SQFuncState funcstate(_ss(_vm), NULL,ThrowError,this);
  674. funcstate._name = SQString::Create(_ss(_vm), _SC("main"));
  675. _fs = &funcstate;
  676. SQObject strTemp = _fs->CreateString(_SC("this"));
  677. if(_fs->AddParameter(strTemp, _scope.nested+1) < 0)
  678. ErrorNameAlreadyDeclared(strTemp);
  679. strTemp = _fs->CreateString(_SC("vargv"));
  680. if(_fs->AddParameter(strTemp, _scope.nested+1) < 0)
  681. ErrorNameAlreadyDeclared(strTemp);
  682. _fs->_varparams = true;
  683. _fs->_sourcename = _sourcename;
  684. SQInteger stacksize = _fs->GetStackSize();
  685. if(setjmp(_errorjmp) == 0)
  686. {
  687. Lex();
  688. while(_token > 0)
  689. {
  690. Statement();
  691. if(_lex.data->prevtoken != _SC('}') && _lex.data->prevtoken != _SC(';')) OptionalSemicolon();
  692. }
  693. if(_inside_ifdef) Error(_SC("unterminated #ifdef starting on line %d"), (int)_ifdef_line);
  694. _fs->SetStackSize(stacksize);
  695. _fs->AddLineInfos(_lex.data->currentline, _lineinfo, true);
  696. _fs->AddInstruction(_OP_RETURN, 0xFF);
  697. _fs->SetStackSize(0);
  698. o =_fs->BuildProto();
  699. #ifdef _DEBUG_DUMP
  700. _fs->Dump(_funcproto(o));
  701. #endif
  702. }
  703. else
  704. {
  705. if(_raiseerror && _ss(_vm)->_compilererrorhandler)
  706. {
  707. _ss(_vm)->_compilererrorhandler(_vm, _compilererror, sq_type(_sourcename) == OT_STRING?_stringval(_sourcename):_SC("unknown"),
  708. _lex.data->currentline, _lex.data->currentcolumn);
  709. }
  710. _vm->_lasterror = SQString::Create(_ss(_vm), _compilererror, -1);
  711. _vm->_lasterror_line = _lex.data->currentline;
  712. _vm->_lasterror_column = _lex.data->currentcolumn;
  713. return false;
  714. }
  715. return true;
  716. }
  717. void Statements()
  718. {
  719. while(_token != _SC('}') && _token != TK_DEFAULT && _token != TK_CASE)
  720. {
  721. Statement();
  722. if(_lex.data->prevtoken != _SC('}') && _lex.data->prevtoken != _SC(';')) OptionalSemicolon();
  723. }
  724. }
  725. void Statement(bool closeframe = true)
  726. {
  727. _es.etype = EXPR_STATEMENT;
  728. SQObject id;
  729. _fs->AddLineInfos(_lex.data->currentline, _lineinfo);
  730. while(_ifdef_exclude && (_token != TK_PRAGMA))
  731. {
  732. Lex(true); //dontThrowIntegerOverflow for when in 32bits parsing 64bits integer inside excluded ifdef
  733. if(_token <= 0) Error(_SC("'#endif' expected to close '#ifdef' started at %d"), (int)_ifdef_line);
  734. }
  735. start_again:
  736. switch(_token)
  737. {
  738. case _SC(';'):
  739. Lex();
  740. break;
  741. case TK_IF:
  742. IfStatement();
  743. break;
  744. case TK_WHILE:
  745. WhileStatement();
  746. break;
  747. case TK_DO:
  748. DoWhileStatement();
  749. break;
  750. case TK_FOR:
  751. ForStatement();
  752. break;
  753. case TK_FOREACH:
  754. ForEachStatement();
  755. break;
  756. case TK_SWITCH:
  757. SwitchStatement();
  758. break;
  759. case TK_LOCAL:
  760. //case TK_CONST:
  761. CASE_TK_LOCAL_TYPES:
  762. LocalDeclStatement();
  763. break;
  764. case TK_RETURN:
  765. case TK_YIELD:
  766. {
  767. SQOpcode op;
  768. if(_token == TK_RETURN)
  769. {
  770. op = _OP_RETURN;
  771. }
  772. else
  773. {
  774. op = _OP_YIELD;
  775. _fs->_bgenerator = true;
  776. }
  777. Lex();
  778. if(!IsEndOfStatement())
  779. {
  780. SQInteger retexp = _fs->GetCurrentPos()+1;
  781. CommaExpr(true);
  782. if(op == _OP_RETURN && _fs->_traps > 0)
  783. _fs->AddInstruction(_OP_POPTRAP, _fs->_traps, 0);
  784. _fs->_returnexp = retexp;
  785. _fs->AddInstruction(op, 1, _fs->PopTarget(),_fs->GetStackSize());
  786. }
  787. else
  788. {
  789. if(op == _OP_RETURN && _fs->_traps > 0)
  790. _fs->AddInstruction(_OP_POPTRAP, _fs->_traps ,0);
  791. _fs->_returnexp = -1;
  792. _fs->AddInstruction(op, 0xFF,0,_fs->GetStackSize());
  793. }
  794. break;
  795. }
  796. case TK_GOTO:
  797. {
  798. //error if outside any function
  799. //if(!_fs->_parent) Error(_SC("'goto' has to be in a function block"));
  800. Lex(); //ignore for now
  801. id = Expect(TK_IDENTIFIER);
  802. SQGotoLabelsInfo info;
  803. info.name = id;
  804. info.line = _lex.data->currentline; //need to get line number here
  805. info.traps = _fs->_traps;
  806. info.nested = _scope.nested;
  807. //Expect(_SC(';'));
  808. if(info.traps) _fs->AddInstruction(_OP_NOP, 0, 0); //for _OP_POPTRAP
  809. RESOLVE_OUTERS();
  810. _fs->AddInstruction(_OP_JMP, 0, -1234);
  811. //instruction pos
  812. info.pos = _fs->GetCurrentPos();
  813. _fs->_unresolvedgotos.push_back(info);
  814. }
  815. break;
  816. case TK_BREAK:
  817. if(_fs->_breaktargets.size() <= 0)Error(_SC("'break' has to be in a loop block"));
  818. if(_fs->_breaktargets.top() > 0)
  819. {
  820. _fs->AddInstruction(_OP_POPTRAP, _fs->_breaktargets.top(), 0);
  821. }
  822. RESOLVE_OUTERS();
  823. _fs->AddInstruction(_OP_JMP, 0, -1234);
  824. _fs->_unresolvedbreaks.push_back(_fs->GetCurrentPos());
  825. Lex();
  826. break;
  827. case TK_CONTINUE:
  828. if(_fs->_continuetargets.size() <= 0)Error(_SC("'continue' has to be in a loop block"));
  829. if(_fs->_continuetargets.top() > 0)
  830. {
  831. _fs->AddInstruction(_OP_POPTRAP, _fs->_continuetargets.top(), 0);
  832. }
  833. RESOLVE_OUTERS();
  834. _fs->AddInstruction(_OP_JMP, 0, -1234);
  835. _fs->_unresolvedcontinues.push_back(_fs->GetCurrentPos());
  836. Lex();
  837. break;
  838. case TK_FUNCTION:
  839. FunctionStatement();
  840. break;
  841. case TK_CLASS:
  842. case TK_STRUCT:
  843. case TK_INTERFACE:
  844. ClassStatement();
  845. break;
  846. case TK_ENUM:
  847. EnumStatement();
  848. break;
  849. case _SC('{'):
  850. {
  851. BEGIN_SCOPE();
  852. Lex();
  853. Statements();
  854. Expect(_SC('}'));
  855. if(closeframe)
  856. {
  857. END_SCOPE();
  858. }
  859. else
  860. {
  861. END_SCOPE_NO_CLOSE();
  862. }
  863. }
  864. break;
  865. case TK_TRY:
  866. TryCatchStatement();
  867. break;
  868. case TK_THROW:
  869. Lex();
  870. CommaExpr();
  871. _fs->AddInstruction(_OP_THROW, _fs->PopTarget());
  872. break;
  873. case TK_CONST:
  874. {
  875. Lex();
  876. if(_token == _SC('$'))
  877. {
  878. //string const
  879. Lex();
  880. id = Expect(TK_IDENTIFIER);
  881. SQObjectPtr strongid = id;
  882. CheckLocalNameScope(id, _scope.nested);
  883. //CheckConstsExists(strongid);
  884. OptionalSemicolon();
  885. ConstsNewSlot(strongid,SQObjectPtr(id));
  886. break;
  887. }
  888. ////C/C++ type declaration using local types;
  889. switch(_token){
  890. CASE_CONST_TYPES:
  891. Lex(); //ignore for now
  892. break;
  893. }
  894. if(_token == TK_IDENTIFIER)
  895. {
  896. id = _fs->CreateString(_lex.data->svalue);
  897. if(CheckTypeName(id)) //C/C++ type declaration;
  898. {
  899. goto start_again;
  900. }
  901. Lex();
  902. }
  903. else id = Expect(TK_IDENTIFIER);
  904. if(_token == _SC(':'))
  905. {
  906. //type specifier like typescript
  907. Lex();
  908. ExpectTypeToken(); //ignore for now
  909. }
  910. Expect(_SC('='));
  911. SQObjectPtr strongid = id;
  912. CheckLocalNameScope(id, _scope.nested);
  913. //CheckConstsExists(strongid);
  914. SQObject val = ExpectScalar();
  915. OptionalSemicolon();
  916. ConstsNewSlot(strongid,SQObjectPtr(val));
  917. }
  918. break;
  919. case TK_INLINE: //accept and ignore
  920. case TK_CONSTEXPR: //accept and ignore
  921. case TK_FRIEND:
  922. case TK_VOLATILE:
  923. Lex();
  924. goto start_again;
  925. case TK_STATIC:
  926. if(_scope.nested)
  927. {
  928. Warning(_SC("%s:%d:%d warning static cualifier is ignored\n"),
  929. _stringval(_sourcename), (int)_lex.data->currentline, (int)_lex.data->currentcolumn);
  930. }
  931. Lex(); //ignore it only to allow run some C/C++ code
  932. goto start_again;
  933. case TK_DECLARE: //parse as extern
  934. case TK_EXTERN:
  935. ExternDeclStatement();
  936. break;
  937. case TK_TYPEDEF:
  938. {
  939. Lex();
  940. SQObject type_val = ExpectTypeToken();
  941. id = ExpectTypeToken();
  942. SQObjectPtr strongid = id;
  943. CheckLocalNameScope(id, _scope.nested);
  944. //Expect(_SC(';'));
  945. TypesNewSlot(strongid,SQObjectPtr(type_val));
  946. if(_token == _SC('[')) //fixed size array
  947. {
  948. Lex();
  949. Expect(TK_INTEGER);
  950. Expect(_SC(']'));
  951. }
  952. }
  953. break;
  954. case TK_USING:
  955. {
  956. Lex();
  957. id = Expect(TK_IDENTIFIER);
  958. Expect('=');
  959. SQObjectPtr strongid = id;
  960. CheckLocalNameScope(id, _scope.nested);
  961. SQObject type_val = ExpectTypeToken();
  962. //Expect(_SC(';'));
  963. TypesNewSlot(strongid,SQObjectPtr(type_val));
  964. }
  965. break;
  966. case TK_PRAGMA:
  967. Pragma();
  968. break;
  969. case TK_UNSAFE:
  970. Lex(); //ignore for now
  971. goto start_again;
  972. case TK_TEMPLATE:
  973. TemplateStatement();
  974. break;
  975. case TK_IDENTIFIER:
  976. {
  977. id = _fs->CreateString(_lex.data->svalue);
  978. SQInteger lhtk = _lex.LookaheadLex();
  979. if(lhtk == _SC(':'))
  980. {
  981. //if(!_fs->_parent) Error(_SC("'label' has to be inside a function block"));
  982. if(!_fs->AddGotoTarget(id, _lex.data->currentline, _fs->_traps, _scope.nested))
  983. {
  984. Error(_SC("Label already declared"));
  985. }
  986. Lex(); //eat ':'
  987. Lex();
  988. break;
  989. }
  990. if(CheckTypeName(id)) //C/C++ type declaration;
  991. {
  992. if(lhtk != _SC('.'))
  993. {
  994. LocalDeclStatement();
  995. break;
  996. }
  997. }
  998. }
  999. default:
  1000. CommaExpr();
  1001. _fs->DiscardTarget();
  1002. /*
  1003. //Fagiano says that this is not a bug
  1004. //and with this modification the stack grow by one lement all the time
  1005. if(_token == TK_IDENTIFIER){
  1006. CommaExpr();
  1007. if(_token == TK_IDENTIFIER){
  1008. Error(_SC(" '=' expected near '%s'"), _lex.data->svalue);
  1009. }
  1010. }
  1011. */
  1012. //_fs->PopTarget();
  1013. break;
  1014. }
  1015. _fs->SnoozeOpt();
  1016. }
  1017. void EmitDerefOp(SQOpcode op)
  1018. {
  1019. SQInteger val = _fs->PopTarget();
  1020. SQInteger key = _fs->PopTarget();
  1021. SQInteger src = _fs->PopTarget();
  1022. _fs->AddInstruction(op,_fs->PushTarget(),src,key,val);
  1023. }
  1024. void Emit2ArgsOP(SQOpcode op, SQInteger p3 = 0)
  1025. {
  1026. SQInteger p2 = _fs->PopTarget(); //src in OP_GET
  1027. SQInteger p1 = _fs->PopTarget(); //key in OP_GET
  1028. _fs->AddInstruction(op,_fs->PushTarget(), p1, p2, p3);
  1029. }
  1030. void EmitCompoundArith(SQInteger tok, SQInteger etype, SQInteger pos)
  1031. {
  1032. /* Generate code depending on the expression type */
  1033. switch(etype)
  1034. {
  1035. case LOCAL:
  1036. {
  1037. SQOpcode op = ChooseArithOpByToken(tok);
  1038. SQInteger p3 = 0;
  1039. SQInteger p2 = _fs->PopTarget(); //src in OP_GET
  1040. SQInteger p1 = _fs->PopTarget(); //key in OP_GET
  1041. _fs->PushTarget(p1);
  1042. //EmitCompArithLocal(tok, p1, p1, p2);
  1043. if(op == _OP_BITW)
  1044. {
  1045. p3 = ChooseBitwOpByToken(tok);
  1046. }
  1047. _fs->AddInstruction(op,p1, p2, p1, p3);
  1048. _fs->SnoozeOpt(); //FIX: stop optimizer in retargeting opcode
  1049. }
  1050. break;
  1051. case OBJECT:
  1052. case BASE:
  1053. {
  1054. SQInteger val = _fs->PopTarget();
  1055. SQInteger key = _fs->PopTarget();
  1056. SQInteger src = _fs->PopTarget();
  1057. /* _OP_COMPARITH mixes dest obj and source val in the arg1 */
  1058. _fs->AddInstruction(_OP_COMPARITH, _fs->PushTarget(), (src<<16)|val, key, ChooseCompArithCharByToken(tok));
  1059. }
  1060. break;
  1061. case OUTER:
  1062. {
  1063. SQInteger val = _fs->TopTarget();
  1064. SQInteger tmp = _fs->PushTarget();
  1065. _fs->AddInstruction(_OP_GETOUTER, tmp, pos);
  1066. _fs->AddInstruction(ChooseArithOpByToken(tok), tmp, val, tmp, 0);
  1067. _fs->PopTarget();
  1068. _fs->PopTarget();
  1069. _fs->AddInstruction(_OP_SETOUTER, _fs->PushTarget(), pos, tmp);
  1070. }
  1071. break;
  1072. }
  1073. }
  1074. void CommaExpr(bool warningAssign=false)
  1075. {
  1076. for(Expression(warningAssign); _token == ','; _fs->PopTarget(), Lex(), CommaExpr(warningAssign));
  1077. }
  1078. void ErrorIfConst()
  1079. {
  1080. SQLocalVarInfo &vsrc = _fs->_vlocals[_fs->TopTarget()];
  1081. //printf("%d %d %d %d %s\n", __LINE__, vsrc._scope, vsrc._type, vsrc._pos, vsrc._name._unVal.pString ? _stringval(vsrc._name) : "?");
  1082. if(vsrc._type & _VAR_CONST) Error(_SC("can't assign to a const variable"));
  1083. }
  1084. void Expression(bool warningAssign=false)
  1085. {
  1086. SQExpState es = _es;
  1087. _es.etype = EXPR;
  1088. _es.epos = -1;
  1089. _es.donot_get = false;
  1090. SQObject id;
  1091. if((_token == TK_IDENTIFIER) && (es.etype == EXPR_STATEMENT))
  1092. {
  1093. id = _fs->CreateString(_lex.data->svalue);
  1094. }
  1095. //else id = {}; //old compilers do not allow this
  1096. else id._type = OT_NULL; //segfault without it with gcc -O3
  1097. LogicalOrExp();
  1098. switch(_token)
  1099. {
  1100. case _SC('='):
  1101. case TK_NEWSLOT:
  1102. case TK_MINUSEQ:
  1103. case TK_PLUSEQ:
  1104. case TK_MULEQ:
  1105. case TK_DIVEQ:
  1106. case TK_MODEQ:
  1107. case TK_BIT_XOR_EQ:
  1108. case TK_BIT_AND_EQ:
  1109. case TK_BIT_OR_EQ:
  1110. case TK_BIT_SHIFT_LEFT_EQ:
  1111. case TK_BIT_SHIFT_RIGHT_EQ:
  1112. {
  1113. SQInteger op = _token;
  1114. SQInteger ds = _es.etype;
  1115. SQInteger pos = _es.epos;
  1116. if(ds == EXPR) Error(_SC("can't assign expression"));
  1117. else if(ds == BASE) Error(_SC("'base' cannot be modified"));
  1118. Lex();
  1119. Expression();
  1120. switch(op)
  1121. {
  1122. case TK_NEWSLOT:
  1123. if(ds == OBJECT || ds == BASE)
  1124. {
  1125. EmitDerefOp(_OP_NEWSLOT);
  1126. if((_es.epos == -1) && (es.etype == EXPR_STATEMENT)
  1127. && (sq_type(id) == OT_STRING) ) AddGlobalName(id);
  1128. }
  1129. else //if _derefstate != DEREF_NO_DEREF && DEREF_FIELD so is the index of a local
  1130. Error(_SC("can't 'create' a local slot"));
  1131. break;
  1132. case _SC('='): //ASSIGN
  1133. if(warningAssign) Warning(_SC("%s:%d:%d warning making assignment, maybe it's not what you want\n"),
  1134. _stringval(_sourcename), (int)_lex.data->currentline, (int)_lex.data->currentcolumn);
  1135. switch(ds)
  1136. {
  1137. case LOCAL:
  1138. {
  1139. SQInteger src = _fs->PopTarget();
  1140. SQInteger dst = _fs->TopTarget();
  1141. _fs->AddInstruction(_OP_MOVE, dst, src);
  1142. }
  1143. break;
  1144. case OBJECT:
  1145. case BASE:
  1146. EmitDerefOp(_OP_SET);
  1147. break;
  1148. case OUTER:
  1149. {
  1150. SQInteger src = _fs->PopTarget();
  1151. SQInteger dst = _fs->PushTarget();
  1152. _fs->AddInstruction(_OP_SETOUTER, dst, pos, src);
  1153. }
  1154. }
  1155. break;
  1156. case TK_MINUSEQ:
  1157. case TK_PLUSEQ:
  1158. case TK_MULEQ:
  1159. case TK_DIVEQ:
  1160. case TK_MODEQ:
  1161. case TK_BIT_XOR_EQ:
  1162. case TK_BIT_AND_EQ:
  1163. case TK_BIT_OR_EQ:
  1164. case TK_BIT_SHIFT_LEFT_EQ:
  1165. case TK_BIT_SHIFT_RIGHT_EQ:
  1166. EmitCompoundArith(op, ds, pos);
  1167. break;
  1168. }
  1169. }
  1170. break;
  1171. case _SC('?'):
  1172. {
  1173. Lex();
  1174. _fs->AddInstruction(_OP_JZ, _fs->PopTarget());
  1175. SQInteger jzpos = _fs->GetCurrentPos();
  1176. SQInteger trg = _fs->PushTarget();
  1177. Expression();
  1178. SQInteger first_exp = _fs->PopTarget();
  1179. if(trg != first_exp) _fs->AddInstruction(_OP_MOVE, trg, first_exp);
  1180. SQInteger endfirstexp = _fs->GetCurrentPos();
  1181. _fs->AddInstruction(_OP_JMP, 0, 0);
  1182. Expect(_SC(':'));
  1183. SQInteger jmppos = _fs->GetCurrentPos();
  1184. Expression();
  1185. SQInteger second_exp = _fs->PopTarget();
  1186. if(trg != second_exp) _fs->AddInstruction(_OP_MOVE, trg, second_exp);
  1187. _fs->SetInstructionParam(jmppos, 1, _fs->GetCurrentPos() - jmppos);
  1188. _fs->SetInstructionParam(jzpos, 1, endfirstexp - jzpos + 1);
  1189. _fs->SnoozeOpt();
  1190. }
  1191. break;
  1192. }
  1193. _es = es;
  1194. }
  1195. template<typename T> void INVOKE_EXP(T f)
  1196. {
  1197. SQExpState es = _es;
  1198. _es.etype = EXPR;
  1199. _es.epos = -1;
  1200. _es.donot_get = false;
  1201. (this->*f)();
  1202. _es = es;
  1203. }
  1204. template<typename T> void BIN_EXP(SQOpcode op, T f,SQInteger op3 = 0)
  1205. {
  1206. Lex();
  1207. INVOKE_EXP(f);
  1208. SQInteger op1 = _fs->PopTarget();
  1209. SQInteger op2 = _fs->PopTarget();
  1210. _fs->AddInstruction(op, _fs->PushTarget(), op1, op2, op3);
  1211. _es.etype = EXPR;
  1212. }
  1213. void LogicalOrExp()
  1214. {
  1215. LogicalAndExp();
  1216. for(;;) if(_token == TK_OR)
  1217. {
  1218. SQInteger first_exp = _fs->PopTarget();
  1219. SQInteger trg = _fs->PushTarget();
  1220. _fs->AddInstruction(_OP_OR, trg, 0, first_exp, 0);
  1221. SQInteger jpos = _fs->GetCurrentPos();
  1222. if(trg != first_exp) _fs->AddInstruction(_OP_MOVE, trg, first_exp);
  1223. Lex();
  1224. INVOKE_EXP(&SQCompiler::LogicalOrExp);
  1225. _fs->SnoozeOpt();
  1226. SQInteger second_exp = _fs->PopTarget();
  1227. if(trg != second_exp) _fs->AddInstruction(_OP_MOVE, trg, second_exp);
  1228. _fs->SnoozeOpt();
  1229. _fs->SetInstructionParam(jpos, 1, (_fs->GetCurrentPos() - jpos));
  1230. _es.etype = EXPR;
  1231. break;
  1232. }
  1233. else return;
  1234. }
  1235. void LogicalAndExp()
  1236. {
  1237. BitwiseOrExp();
  1238. for(;;) switch(_token)
  1239. {
  1240. case TK_AND:
  1241. {
  1242. SQInteger first_exp = _fs->PopTarget();
  1243. SQInteger trg = _fs->PushTarget();
  1244. _fs->AddInstruction(_OP_AND, trg, 0, first_exp, 0);
  1245. SQInteger jpos = _fs->GetCurrentPos();
  1246. if(trg != first_exp) _fs->AddInstruction(_OP_MOVE, trg, first_exp);
  1247. Lex();
  1248. INVOKE_EXP(&SQCompiler::LogicalAndExp);
  1249. _fs->SnoozeOpt();
  1250. SQInteger second_exp = _fs->PopTarget();
  1251. if(trg != second_exp) _fs->AddInstruction(_OP_MOVE, trg, second_exp);
  1252. _fs->SnoozeOpt();
  1253. _fs->SetInstructionParam(jpos, 1, (_fs->GetCurrentPos() - jpos));
  1254. _es.etype = EXPR;
  1255. break;
  1256. }
  1257. default:
  1258. return;
  1259. }
  1260. }
  1261. void BitwiseOrExp()
  1262. {
  1263. BitwiseXorExp();
  1264. for(;;) if(_token == _SC('|'))
  1265. {
  1266. BIN_EXP(_OP_BITW, &SQCompiler::BitwiseXorExp,BW_OR);
  1267. }
  1268. else return;
  1269. }
  1270. void BitwiseXorExp()
  1271. {
  1272. BitwiseAndExp();
  1273. for(;;) if(_token == _SC('^'))
  1274. {
  1275. BIN_EXP(_OP_BITW, &SQCompiler::BitwiseAndExp,BW_XOR);
  1276. }
  1277. else return;
  1278. }
  1279. void BitwiseAndExp()
  1280. {
  1281. EqExp();
  1282. for(;;) if(_token == _SC('&'))
  1283. {
  1284. BIN_EXP(_OP_BITW, &SQCompiler::EqExp,BW_AND);
  1285. }
  1286. else return;
  1287. }
  1288. void EqExp()
  1289. {
  1290. CompExp();
  1291. for(;;) switch(_token)
  1292. {
  1293. case TK_EQ:
  1294. BIN_EXP(_OP_EQ, &SQCompiler::CompExp);
  1295. break;
  1296. case TK_EQ_IDENTITY :
  1297. BIN_EXP(_OP_EQI, &SQCompiler::CompExp);
  1298. break;
  1299. case TK_NE:
  1300. BIN_EXP(_OP_NE, &SQCompiler::CompExp);
  1301. break;
  1302. case TK_NE_IDENTITY:
  1303. BIN_EXP(_OP_NEI, &SQCompiler::CompExp);
  1304. break;
  1305. case TK_3WAYSCMP:
  1306. BIN_EXP(_OP_CMP, &SQCompiler::CompExp,CMP_3W);
  1307. break;
  1308. default:
  1309. return;
  1310. }
  1311. }
  1312. void CompExp()
  1313. {
  1314. ShiftExp();
  1315. for(;;) switch(_token)
  1316. {
  1317. case _SC('>'):
  1318. BIN_EXP(_OP_CMP, &SQCompiler::ShiftExp,CMP_G);
  1319. break;
  1320. case _SC('<'):
  1321. BIN_EXP(_OP_CMP, &SQCompiler::ShiftExp,CMP_L);
  1322. break;
  1323. case TK_GE:
  1324. BIN_EXP(_OP_CMP, &SQCompiler::ShiftExp,CMP_GE);
  1325. break;
  1326. case TK_LE:
  1327. BIN_EXP(_OP_CMP, &SQCompiler::ShiftExp,CMP_LE);
  1328. break;
  1329. case TK_IN:
  1330. BIN_EXP(_OP_EXISTS, &SQCompiler::ShiftExp);
  1331. break;
  1332. case TK_INSTANCEOF:
  1333. BIN_EXP(_OP_INSTANCEOF, &SQCompiler::ShiftExp);
  1334. break;
  1335. default:
  1336. return;
  1337. }
  1338. }
  1339. void ShiftExp()
  1340. {
  1341. PlusExp();
  1342. for(;;) switch(_token)
  1343. {
  1344. case TK_USHIFTR:
  1345. BIN_EXP(_OP_BITW, &SQCompiler::PlusExp,BW_USHIFTR);
  1346. break;
  1347. case TK_SHIFTL:
  1348. BIN_EXP(_OP_BITW, &SQCompiler::PlusExp,BW_SHIFTL);
  1349. break;
  1350. case TK_SHIFTR:
  1351. BIN_EXP(_OP_BITW, &SQCompiler::PlusExp,BW_SHIFTR);
  1352. break;
  1353. default:
  1354. return;
  1355. }
  1356. }
  1357. SQOpcode ChooseArithOpByToken(SQInteger tok)
  1358. {
  1359. switch(tok)
  1360. {
  1361. case TK_PLUSEQ:
  1362. case _SC('+'):
  1363. return _OP_ADD;
  1364. case TK_MINUSEQ:
  1365. case _SC('-'):
  1366. return _OP_SUB;
  1367. case TK_MULEQ:
  1368. case _SC('*'):
  1369. return _OP_MUL;
  1370. case TK_DIVEQ:
  1371. case _SC('/'):
  1372. return _OP_DIV;
  1373. case TK_MODEQ:
  1374. case _SC('%'):
  1375. return _OP_MOD;
  1376. case TK_BIT_XOR_EQ:
  1377. case TK_BIT_AND_EQ:
  1378. case TK_BIT_OR_EQ:
  1379. case TK_BIT_SHIFT_LEFT_EQ:
  1380. case TK_BIT_SHIFT_RIGHT_EQ:
  1381. return _OP_BITW;
  1382. default:
  1383. assert(0);
  1384. }
  1385. return _OP_ADD;
  1386. }
  1387. BitWiseOP ChooseBitwOpByToken(SQInteger tok)
  1388. {
  1389. switch(tok)
  1390. {
  1391. case TK_BIT_XOR_EQ:
  1392. return BW_XOR;
  1393. case TK_BIT_AND_EQ:
  1394. return BW_AND;
  1395. case TK_BIT_OR_EQ:
  1396. return BW_OR;
  1397. case TK_BIT_SHIFT_LEFT_EQ:
  1398. return BW_SHIFTL;
  1399. case TK_BIT_SHIFT_RIGHT_EQ:
  1400. return BW_SHIFTR;
  1401. default:
  1402. Error(_SC("unknown bitwise token"));
  1403. }
  1404. return BW_AND;
  1405. }
  1406. SQInteger ChooseCompArithCharByToken(SQInteger tok)
  1407. {
  1408. SQInteger oper;
  1409. switch(tok)
  1410. {
  1411. case TK_MINUSEQ:
  1412. oper = _SC('-');
  1413. break;
  1414. case TK_PLUSEQ:
  1415. oper = _SC('+');
  1416. break;
  1417. case TK_MULEQ:
  1418. oper = _SC('*');
  1419. break;
  1420. case TK_DIVEQ:
  1421. oper = _SC('/');
  1422. break;
  1423. case TK_MODEQ:
  1424. oper = _SC('%');
  1425. break;
  1426. default:
  1427. oper = 0; //shut up compiler
  1428. assert(0);
  1429. break;
  1430. };
  1431. return oper;
  1432. }
  1433. void PlusExp()
  1434. {
  1435. MultExp();
  1436. for(;;) switch(_token)
  1437. {
  1438. case _SC('+'):
  1439. case _SC('-'):
  1440. BIN_EXP(ChooseArithOpByToken(_token), &SQCompiler::MultExp);
  1441. break;
  1442. default:
  1443. return;
  1444. }
  1445. }
  1446. void MultExp()
  1447. {
  1448. PrefixedExpr();
  1449. for(;;) switch(_token)
  1450. {
  1451. case _SC('*'):
  1452. case _SC('/'):
  1453. case _SC('%'):
  1454. BIN_EXP(ChooseArithOpByToken(_token), &SQCompiler::PrefixedExpr);
  1455. break;
  1456. default:
  1457. return;
  1458. }
  1459. }
  1460. //if 'pos' != -1 the previous variable is a local variable
  1461. void PrefixedExpr()
  1462. {
  1463. SQInteger pos = Factor();
  1464. for(;;)
  1465. {
  1466. switch(_token)
  1467. {
  1468. case TK_DOUBLE_COLON: //C++ style namespace
  1469. if(IsEndOfStatement()) return;
  1470. //else fall trough
  1471. case _SC('.'):
  1472. pos = -1;
  1473. Lex();
  1474. _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(Expect(TK_IDENTIFIER)));
  1475. if(_es.etype==BASE)
  1476. {
  1477. Emit2ArgsOP(_OP_GET);
  1478. pos = _fs->TopTarget();
  1479. _es.etype = EXPR;
  1480. _es.epos = pos;
  1481. }
  1482. else
  1483. {
  1484. if(NeedGet())
  1485. {
  1486. Emit2ArgsOP(_OP_GET);
  1487. }
  1488. _es.etype = OBJECT;
  1489. }
  1490. break;
  1491. case _SC('['):
  1492. if(_lex.data->prevtoken == _SC('\n')) Error(_SC("cannot breake deref/or comma needed after [exp]=exp slot declaration"));
  1493. Lex();
  1494. Expression();
  1495. Expect(_SC(']'));
  1496. pos = -1;
  1497. if(_es.etype==BASE)
  1498. {
  1499. Emit2ArgsOP(_OP_GET);
  1500. pos = _fs->TopTarget();
  1501. _es.etype = EXPR;
  1502. _es.epos = pos;
  1503. }
  1504. else
  1505. {
  1506. if(NeedGet())
  1507. {
  1508. Emit2ArgsOP(_OP_GET);
  1509. }
  1510. _es.etype = OBJECT;
  1511. }
  1512. break;
  1513. case TK_MINUSMINUS:
  1514. case TK_PLUSPLUS:
  1515. {
  1516. if(IsEndOfStatement()) return;
  1517. SQInteger diff = (_token==TK_MINUSMINUS) ? -1 : 1;
  1518. Lex();
  1519. switch(_es.etype)
  1520. {
  1521. case EXPR:
  1522. Error(_SC("can't '++' or '--' an expression"));
  1523. break;
  1524. case OBJECT:
  1525. case BASE:
  1526. if(_es.donot_get == true)
  1527. {
  1528. Error(_SC("can't '++' or '--' an expression")); //mmh dor this make sense?
  1529. break;
  1530. }
  1531. Emit2ArgsOP(_OP_PINC, diff);
  1532. break;
  1533. case LOCAL:
  1534. {
  1535. SQInteger src = _fs->PopTarget();
  1536. _fs->AddInstruction(_OP_PINCL, _fs->PushTarget(), src, 0, diff);
  1537. }
  1538. break;
  1539. case OUTER:
  1540. {
  1541. SQInteger tmp1 = _fs->PushTarget();
  1542. SQInteger tmp2 = _fs->PushTarget();
  1543. _fs->AddInstruction(_OP_GETOUTER, tmp2, _es.epos);
  1544. _fs->AddInstruction(_OP_PINCL, tmp1, tmp2, 0, diff);
  1545. _fs->AddInstruction(_OP_SETOUTER, tmp2, _es.epos, tmp2);
  1546. _fs->PopTarget();
  1547. }
  1548. }
  1549. }
  1550. return;
  1551. case _SC('('):
  1552. switch(_es.etype)
  1553. {
  1554. case OBJECT:
  1555. {
  1556. SQInteger key = _fs->PopTarget(); /* location of the key */
  1557. SQInteger table = _fs->PopTarget(); /* location of the object */
  1558. SQInteger closure = _fs->PushTarget(); /* location for the closure */
  1559. SQInteger ttarget = _fs->PushTarget(); /* location for 'this' pointer */
  1560. _fs->AddInstruction(_OP_PREPCALL, closure, key, table, ttarget);
  1561. }
  1562. break;
  1563. case BASE:
  1564. //Emit2ArgsOP(_OP_GET);
  1565. _fs->AddInstruction(_OP_MOVE, _fs->PushTarget(), 0);
  1566. break;
  1567. case OUTER:
  1568. _fs->AddInstruction(_OP_GETOUTER, _fs->PushTarget(), _es.epos);
  1569. _fs->AddInstruction(_OP_MOVE, _fs->PushTarget(), 0);
  1570. break;
  1571. default:
  1572. _fs->AddInstruction(_OP_MOVE, _fs->PushTarget(), 0);
  1573. }
  1574. _es.etype = EXPR;
  1575. Lex();
  1576. FunctionCallArgs();
  1577. break;
  1578. case TK_AS:
  1579. {
  1580. Lex();
  1581. ExpectTypeToken(); //ignore for now
  1582. }
  1583. default:
  1584. return;
  1585. }
  1586. }
  1587. }
  1588. SQInteger Factor()
  1589. {
  1590. _es.etype = EXPR;
  1591. bool compileTimeCheckedConstant = false;
  1592. switch(_token)
  1593. {
  1594. case TK_STRING_LITERAL:
  1595. {
  1596. SQInteger lhtk = _lex.LookaheadLex();
  1597. if(lhtk == TK_STRING_LITERAL) //C/C++ two consecutive strings
  1598. {
  1599. SQCharBuf buf;
  1600. //save the current string
  1601. buf.append(_lex.data->svalue,_lex.data->longstr.size()-1);
  1602. while(lhtk == TK_STRING_LITERAL)
  1603. {
  1604. Lex(); //get lookahead token
  1605. buf.append(_lex.data->svalue,_lex.data->longstr.size()-1);
  1606. lhtk = _lex.LookaheadLex();
  1607. }
  1608. _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(
  1609. _fs->CreateString(buf.data(),buf.size())));
  1610. }
  1611. else
  1612. {
  1613. _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(
  1614. _fs->CreateString(_lex.data->svalue,_lex.data->longstr.size()-1)));
  1615. }
  1616. Lex();
  1617. }
  1618. break;
  1619. case TK_BASE:
  1620. Lex();
  1621. if(_token == '(' && _inside_constructor)
  1622. Warning(_SC("calling 'base' as a function; did you mean base.constructor()?\n"));
  1623. _fs->AddInstruction(_OP_GETBASE, _fs->PushTarget());
  1624. _es.etype = BASE;
  1625. _es.epos = _fs->TopTarget();
  1626. return (_es.epos);
  1627. case _SC('$'): //compile time checked constant
  1628. Lex();
  1629. ErrorIfNotToken(TK_IDENTIFIER);
  1630. compileTimeCheckedConstant = true;
  1631. //fallthrough to TK_IDENTIFIER
  1632. case TK_IDENTIFIER:
  1633. case TK_CONSTRUCTOR:
  1634. case TK_DESTRUCTOR:
  1635. case TK_THIS:
  1636. {
  1637. SQObject id;
  1638. SQObject constant;
  1639. switch(_token)
  1640. {
  1641. case TK_IDENTIFIER:
  1642. id = _fs->CreateString(_lex.data->svalue);
  1643. if(compileTimeCheckedConstant)
  1644. {
  1645. SQObjectPtr val;
  1646. CompileTimeCheckConstsGet(id, val);
  1647. }
  1648. break;
  1649. case TK_THIS:
  1650. id = _fs->CreateString(_SC("this"), 4);
  1651. break;
  1652. case TK_CONSTRUCTOR:
  1653. id = _fs->CreateString(_SC("constructor"), 11);
  1654. break;
  1655. case TK_DESTRUCTOR:
  1656. id = _fs->CreateString(_SC("destructor"), 10);
  1657. break;
  1658. }
  1659. SQInteger pos = -1;
  1660. Lex();
  1661. if((pos = _fs->GetLocalVariable(id)) != -1)
  1662. {
  1663. /* Handle a local variable (includes 'this') */
  1664. _fs->PushTarget(pos);
  1665. _es.etype = LOCAL;
  1666. _es.epos = pos;
  1667. }
  1668. else if((pos = _fs->GetOuterVariable(id)) != -1)
  1669. {
  1670. /* Handle a free var */
  1671. if(NeedGet())
  1672. {
  1673. _es.epos = _fs->PushTarget();
  1674. _fs->AddInstruction(_OP_GETOUTER, _es.epos, pos);
  1675. /* _es.etype = EXPR; already default value */
  1676. }
  1677. else
  1678. {
  1679. _es.etype = OUTER;
  1680. _es.epos = pos;
  1681. }
  1682. }
  1683. else if(IsConstant(id, constant))
  1684. {
  1685. /* Handle named constant */
  1686. SQObjectPtr constval;
  1687. SQObject constid;
  1688. if(sq_type(constant) == OT_TABLE)
  1689. {
  1690. Expect('.');
  1691. constid = Expect(TK_IDENTIFIER);
  1692. if(!_table(constant)->Get(constid, constval))
  1693. {
  1694. constval.Null();
  1695. Error(_SC("invalid constant [%s.%s]"), _stringval(id), _stringval(constid));
  1696. }
  1697. }
  1698. else
  1699. {
  1700. constval = constant;
  1701. }
  1702. _es.epos = _fs->PushTarget();
  1703. /* generate direct or literal function depending on size */
  1704. SQObjectType ctype = sq_type(constval);
  1705. switch(ctype)
  1706. {
  1707. case OT_INTEGER:
  1708. EmitLoadConstInt(_integer(constval),_es.epos);
  1709. break;
  1710. case OT_FLOAT:
  1711. EmitLoadConstFloat(_float(constval),_es.epos);
  1712. break;
  1713. case OT_BOOL:
  1714. _fs->AddInstruction(_OP_LOADBOOL, _es.epos, _integer(constval));
  1715. break;
  1716. default:
  1717. _fs->AddInstruction(_OP_LOAD,_es.epos,_fs->GetConstant(constval));
  1718. break;
  1719. }
  1720. _es.etype = EXPR;
  1721. }
  1722. else
  1723. {
  1724. /* Handle a non-local variable, aka a field. Push the 'this' pointer on
  1725. * the virtual stack (always found in offset 0, so no instruction needs to
  1726. * be generated), and push the key next. Generate an _OP_LOAD instruction
  1727. * for the latter. If we are not using the variable as a dref expr, generate
  1728. * the _OP_GET instruction.
  1729. */
  1730. if(CheckNameIsType(id)) EatTemplateInitialization();
  1731. _fs->PushTarget(0);
  1732. _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(id));
  1733. if(NeedGet())
  1734. {
  1735. Emit2ArgsOP(_OP_GET);
  1736. }
  1737. _es.etype = OBJECT;
  1738. }
  1739. return _es.epos;
  1740. }
  1741. break;
  1742. case TK_DOUBLE_COLON: // "::"
  1743. _fs->AddInstruction(_OP_LOADROOT, _fs->PushTarget());
  1744. _es.etype = OBJECT;
  1745. _token = _SC('.'); /* hack: drop into PrefixExpr, case '.'*/
  1746. _es.epos = -1;
  1747. return _es.epos;
  1748. case TK_NULL:
  1749. _fs->AddInstruction(_OP_LOADNULLS, _fs->PushTarget(),1);
  1750. Lex();
  1751. break;
  1752. CASE_TK_INTEGER:
  1753. EmitLoadConstInt(_lex.data->nvalue,-1);
  1754. Lex();
  1755. break;
  1756. case TK_FLOAT:
  1757. EmitLoadConstFloat(_lex.data->fvalue,-1);
  1758. Lex();
  1759. break;
  1760. case TK_TRUE:
  1761. case TK_FALSE:
  1762. _fs->AddInstruction(_OP_LOADBOOL, _fs->PushTarget(),_token == TK_TRUE?1:0);
  1763. Lex();
  1764. break;
  1765. case _SC('['):
  1766. {
  1767. _fs->AddInstruction(_OP_NEWOBJ, _fs->PushTarget(),0,0,NOT_ARRAY);
  1768. SQInteger apos = _fs->GetCurrentPos(),key = 0;
  1769. Lex();
  1770. while(_token != _SC(']'))
  1771. {
  1772. Expression();
  1773. if(_token == _SC(',')) Lex();
  1774. SQInteger val = _fs->PopTarget();
  1775. SQInteger array = _fs->TopTarget();
  1776. _fs->AddInstruction(_OP_APPENDARRAY, array, val, AAT_STACK);
  1777. key++;
  1778. }
  1779. _fs->SetInstructionParam(apos, 1, key);
  1780. Lex();
  1781. }
  1782. break;
  1783. case _SC('{'):
  1784. _fs->AddInstruction(_OP_NEWOBJ, _fs->PushTarget(),0,NOT_TABLE);
  1785. Lex();
  1786. ParseTableOrClass(_SC(','),_SC('}'));
  1787. break;
  1788. case TK_FUNCTION:
  1789. FunctionExp(_token);
  1790. break;
  1791. case _SC('@'):
  1792. FunctionExp(_token,true);
  1793. break;
  1794. case TK_STRUCT:
  1795. case TK_CLASS:
  1796. case TK_INTERFACE:
  1797. Lex();
  1798. ClassExp(NULL);
  1799. break;
  1800. case _SC('-'):
  1801. Lex();
  1802. switch(_token)
  1803. {
  1804. CASE_TK_INTEGER:
  1805. EmitLoadConstInt(-_lex.data->nvalue,-1);
  1806. Lex();
  1807. break;
  1808. case TK_FLOAT:
  1809. EmitLoadConstFloat(-_lex.data->fvalue,-1);
  1810. Lex();
  1811. break;
  1812. default:
  1813. UnaryOP(_OP_NEG);
  1814. }
  1815. break;
  1816. case _SC('!'):
  1817. Lex();
  1818. UnaryOP(_OP_NOT);
  1819. break;
  1820. case _SC('~'):
  1821. Lex();
  1822. bool isInteger;
  1823. switch(_token)
  1824. {
  1825. CASE_TK_INTEGER:
  1826. isInteger = true;
  1827. default:
  1828. isInteger = false;
  1829. }
  1830. if(isInteger)
  1831. {
  1832. EmitLoadConstInt(~_lex.data->nvalue,-1);
  1833. Lex();
  1834. break;
  1835. }
  1836. UnaryOP(_OP_BWNOT);
  1837. break;
  1838. case TK_TYPEOF :
  1839. Lex() ;
  1840. UnaryOP(_OP_TYPEOF);
  1841. break;
  1842. case TK_RESUME :
  1843. Lex();
  1844. UnaryOP(_OP_RESUME);
  1845. break;
  1846. case TK_CLONE :
  1847. Lex();
  1848. UnaryOP(_OP_CLONE);
  1849. break;
  1850. case TK_RAWCALL:
  1851. Lex();
  1852. Expect('(');
  1853. FunctionCallArgs(true);
  1854. break;
  1855. case TK_MINUSMINUS :
  1856. case TK_PLUSPLUS :
  1857. PrefixIncDec(_token);
  1858. break;
  1859. case TK_DELETE :
  1860. DeleteExpr();
  1861. break;
  1862. case _SC('('):
  1863. Lex();
  1864. if(_token == TK_IDENTIFIER)
  1865. {
  1866. //check C/C++ cast
  1867. SQObject id = _fs->CreateString(_lex.data->svalue);
  1868. if(CheckTypeName(id)) //C/C++ type declaration;
  1869. {
  1870. SQInteger lhtk = _lex.LookaheadLex();
  1871. if(lhtk == _SC(')'))
  1872. {
  1873. Lex(); //eat TK_IDENTIFIER
  1874. Lex(); //eat ')'
  1875. CommaExpr();
  1876. break;
  1877. }
  1878. }
  1879. }
  1880. CommaExpr();
  1881. Expect(_SC(')'));
  1882. break;
  1883. case TK___LINE__:
  1884. EmitLoadConstInt(_lex.data->currentline,-1);
  1885. Lex();
  1886. break;
  1887. case TK___FILE__:
  1888. _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(_sourcename));
  1889. Lex();
  1890. break;
  1891. case TK___FUNCTION__:
  1892. _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(_fs->_name));
  1893. Lex();
  1894. break;
  1895. case TK_IGNORE:
  1896. //Warning("Keyword ignored \"%s\" at line %d:%d\n", _lex.Tok2Str(_token),
  1897. // _lex.data->currentline, _lex.data->currentcolumn);
  1898. Lex();
  1899. return Factor();
  1900. default:
  1901. Error(_SC("expression expected"));
  1902. }
  1903. _es.etype = EXPR;
  1904. return -1;
  1905. }
  1906. void EmitLoadConstInt(SQInteger value,SQInteger target)
  1907. {
  1908. if(target < 0)
  1909. {
  1910. target = _fs->PushTarget();
  1911. }
  1912. //with the line bellow we get wrong result for -1
  1913. //if(value <= INT_MAX && value > INT_MIN) { //does it fit in 32 bits?
  1914. if((value & (~((SQInteger)0xFFFFFFFF))) == 0) //does it fit in 32 bits?
  1915. {
  1916. _fs->AddInstruction(_OP_LOADINT, target,value);
  1917. }
  1918. else
  1919. {
  1920. _fs->AddInstruction(_OP_LOAD, target, _fs->GetNumericConstant(value));
  1921. }
  1922. }
  1923. void EmitLoadConstFloat(SQFloat value,SQInteger target)
  1924. {
  1925. if(target < 0)
  1926. {
  1927. target = _fs->PushTarget();
  1928. }
  1929. SQFloat32 f32 = value;
  1930. if(f32 == value) //same value no overflow/underflow/infinity
  1931. {
  1932. _fs->AddInstruction(_OP_LOADFLOAT, target,*((SQInt32 *)&f32));
  1933. }
  1934. else _fs->AddInstruction(_OP_LOAD, target, _fs->GetNumericConstant(value));
  1935. }
  1936. void UnaryOP(SQOpcode op)
  1937. {
  1938. PrefixedExpr();
  1939. SQInteger src = _fs->PopTarget();
  1940. _fs->AddInstruction(op, _fs->PushTarget(), src);
  1941. }
  1942. bool NeedGet()
  1943. {
  1944. switch(_token)
  1945. {
  1946. case _SC('='):
  1947. case _SC('('):
  1948. case TK_NEWSLOT:
  1949. case TK_MODEQ:
  1950. case TK_MULEQ:
  1951. case TK_DIVEQ:
  1952. case TK_MINUSEQ:
  1953. case TK_PLUSEQ:
  1954. case TK_BIT_XOR_EQ:
  1955. case TK_BIT_AND_EQ:
  1956. case TK_BIT_OR_EQ:
  1957. case TK_BIT_SHIFT_LEFT_EQ:
  1958. case TK_BIT_SHIFT_RIGHT_EQ:
  1959. return false;
  1960. case TK_PLUSPLUS:
  1961. case TK_MINUSMINUS:
  1962. if (!IsEndOfStatement())
  1963. {
  1964. return false;
  1965. }
  1966. break;
  1967. }
  1968. return (!_es.donot_get || ( _es.donot_get && (_token == _SC('.') || _token == _SC('[') || _token == TK_DOUBLE_COLON)));
  1969. }
  1970. void FunctionCallArgs(bool rawcall = false)
  1971. {
  1972. SQInteger nargs = 1;//this
  1973. while(_token != _SC(')'))
  1974. {
  1975. Expression();
  1976. MoveIfCurrentTargetIsLocal();
  1977. nargs++;
  1978. if(_token == _SC(','))
  1979. {
  1980. Lex();
  1981. if(_token == ')') Error(_SC("expression expected, found ')'"));
  1982. }
  1983. }
  1984. Lex();
  1985. if (rawcall)
  1986. {
  1987. if (nargs < 3) Error(_SC("rawcall requires at least 2 parameters (callee and this)"));
  1988. nargs -= 2; //removes callee and this from count
  1989. }
  1990. for(SQInteger i = 0; i < (nargs - 1); i++) _fs->PopTarget();
  1991. SQInteger stackbase = _fs->PopTarget();
  1992. SQInteger closure = _fs->PopTarget();
  1993. SQInteger target = _fs->PushTarget();
  1994. assert(target >= -1);
  1995. assert(target < 255);
  1996. /*
  1997. const char *fname = _stringval(_fs->_name);
  1998. SQObjectPtr optr = _fs->_functions[closure];
  1999. SQFunctionProto *f = _funcproto(optr);
  2000. for(int i=0; i < f->_nparameters; ++i)
  2001. {
  2002. printf("Func %s params %d %d\n", _stringval(f->_name), i, (int)_integer(f->_parameters_type[i]));
  2003. }
  2004. */
  2005. _fs->AddInstruction(_OP_CALL, target, closure, stackbase, nargs);
  2006. }
  2007. void AddClassMemberExists(SQObjectPtr &member_names, SQObject &name)
  2008. {
  2009. SQObjectPtr oname = name, otrue = true;
  2010. _table(member_names)->NewSlot(oname, otrue);
  2011. }
  2012. void CheckClassMemberExists(SQObjectPtr &member_names, SQObject &name, bool addIfNotExists=true)
  2013. {
  2014. if(_table(member_names)->Exists(name))
  2015. {
  2016. Error(_SC("class already has a member named: %s"), _stringval(name));
  2017. }
  2018. if(addIfNotExists) AddClassMemberExists(member_names, name);
  2019. }
  2020. void CheckClassMemberExists(SQObjectPtr &member_names, const SQChar *name)
  2021. {
  2022. SQObject oname = _fs->CreateString(name);
  2023. CheckClassMemberExists(member_names, oname);
  2024. }
  2025. void ParseTableOrClass(SQInteger separator,SQInteger terminator, SQObjectPtr *class_name=NULL)
  2026. {
  2027. SQObjectPtr member_names;
  2028. SQInteger saved_tok, tpos = _fs->GetCurrentPos(),nkeys = 0;
  2029. SQObject type_name, obj_id;
  2030. bool isClass = separator == ';'; //hack recognizes a table/class from the separator
  2031. if(isClass)
  2032. {
  2033. member_names = SQTable::Create(_ss(_vm),0);
  2034. }
  2035. int addClassMember = 0;
  2036. while(_token != terminator)
  2037. {
  2038. bool hasattrs = false;
  2039. bool isstatic = false;
  2040. bool cppDestructor = false;
  2041. //bool isvirtual = false;
  2042. //bool isprivate = false;
  2043. bool compileTimeCheckedConstant = false;
  2044. const SQChar *membertypename = 0;
  2045. SQInteger member_type_token = 0;
  2046. //check if is an attribute
  2047. if(isClass)
  2048. {
  2049. if(_token == TK_ATTR_OPEN)
  2050. {
  2051. _fs->AddInstruction(_OP_NEWOBJ, _fs->PushTarget(),0,NOT_TABLE);
  2052. Lex();
  2053. ParseTableOrClass(',',TK_ATTR_CLOSE);
  2054. hasattrs = true;
  2055. }
  2056. if(_token == TK_STATIC)
  2057. {
  2058. isstatic = true;
  2059. Lex();
  2060. }
  2061. if(_token == TK_VIRTUAL)
  2062. {
  2063. //isvirtual = true;
  2064. Lex();
  2065. }
  2066. else if(_token == TK_PUBLIC)
  2067. {
  2068. Lex();
  2069. if(_token == _SC(':')) Lex();
  2070. }
  2071. else if(_token == TK_PRIVATE)
  2072. {
  2073. //isprivate = true;
  2074. Lex();
  2075. if(_token == _SC(':')) Lex();
  2076. }
  2077. else if(_token == TK_PROTECTED)
  2078. {
  2079. //isprivate = true;
  2080. Lex();
  2081. if(_token == _SC(':')) Lex();
  2082. }
  2083. else if(_token == TK_INLINE)
  2084. {
  2085. Lex();
  2086. }
  2087. else if(_token == TK_CONST)
  2088. {
  2089. Lex();
  2090. }
  2091. else if(_token == TK_TEMPLATE)
  2092. {
  2093. TemplateStatement();
  2094. }
  2095. }
  2096. member_has_type:
  2097. switch(_token)
  2098. {
  2099. case TK_FUNCTION:
  2100. case TK_CONSTRUCTOR:
  2101. case TK_DESTRUCTOR:
  2102. {
  2103. if(_token == TK_CONSTRUCTOR) _inside_constructor = true;
  2104. saved_tok = _token;
  2105. Lex();
  2106. obj_id = saved_tok == TK_FUNCTION ? Expect(TK_IDENTIFIER) :
  2107. _fs->CreateString(saved_tok == TK_CONSTRUCTOR ? _SC("constructor") : _SC("destructor"));
  2108. if(sq_type(member_names) == OT_TABLE) CheckClassMemberExists(member_names, obj_id);
  2109. else Error(_SC("unexpected error in class declaration"));
  2110. Expect(_SC('('));
  2111. function_params_decl:
  2112. _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(obj_id));
  2113. CreateFunction(obj_id, eFunctionType_member);
  2114. _fs->AddInstruction(_OP_CLOSURE, _fs->PushTarget(), _fs->_functions.size() - 1, 0);
  2115. _inside_constructor = false;
  2116. }
  2117. break;
  2118. case _SC('['):
  2119. Lex();
  2120. CommaExpr();
  2121. Expect(_SC(']'));
  2122. Expect(_SC('='));
  2123. Expression();
  2124. break;
  2125. case _SC('$'): //compile time checked constant
  2126. Lex();
  2127. ErrorIfNotToken(TK_IDENTIFIER);
  2128. compileTimeCheckedConstant = true;
  2129. //fallthrough to TK_IDENTIFIER
  2130. case TK_STRING_LITERAL: //JSON
  2131. if(isClass)
  2132. {
  2133. Error(_SC("unexpected string literal in class declaration"));
  2134. }
  2135. case TK_IDENTIFIER: //JSON
  2136. {
  2137. obj_id = GetTokenObject(_token);
  2138. if(compileTimeCheckedConstant)
  2139. {
  2140. SQObjectPtr val;
  2141. CompileTimeCheckConstsGet(obj_id, val);
  2142. obj_id = val;
  2143. }
  2144. SQInteger next_token = _SC('=');
  2145. if(isClass)
  2146. {
  2147. CheckClassMemberExists(member_names, obj_id, false);
  2148. addClassMember = 0;
  2149. EatTemplateInitialization();
  2150. switch(_token)
  2151. {
  2152. case _SC('('): //C/C++ style function declaration
  2153. Lex();
  2154. if(class_name)
  2155. {
  2156. //printf("ClassMember %d : %s : %s\n", (int)cppDestructor, _stringval(*class_name), _stringval(obj_id));
  2157. if(memcmp(_stringval(*class_name), _stringval(obj_id), _string(*class_name)->_len) == 0)
  2158. {
  2159. //C++ style constructor/destructor declaration
  2160. obj_id = _fs->CreateString(cppDestructor ? _SC("destructor") : _SC("constructor"));
  2161. cppDestructor = false;
  2162. }
  2163. }
  2164. AddClassMemberExists(member_names, obj_id);
  2165. goto function_params_decl;
  2166. case _SC(':'): //typescript field with type annotation
  2167. if(membertypename)
  2168. {
  2169. Error(_SC("member type already declared before %s"), _stringval(obj_id));
  2170. }
  2171. Lex();
  2172. type_name = ExpectTypeToken();
  2173. addClassMember = 1;
  2174. break;
  2175. case _SC(','):
  2176. ++addClassMember;
  2177. case _SC(';'): //member variable declaration without explicit initialization
  2178. Lex();
  2179. ++addClassMember;
  2180. break;
  2181. case TK_IDENTIFIER: //if 2 identifier found the first should be a type
  2182. if(CheckNameIsType(obj_id)) //Struct/Class/Typedef names
  2183. {
  2184. membertypename = _stringval(obj_id);
  2185. goto member_has_type;
  2186. }
  2187. }
  2188. if(addClassMember)
  2189. {
  2190. AddClassMemberExists(member_names, obj_id);
  2191. if(_token != _SC('='))
  2192. {
  2193. _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(obj_id));
  2194. if(member_type_token) GetVarTypeDeclaration(member_type_token, _fs->PushTarget());
  2195. else _fs->AddInstruction(_OP_LOADNULLS, _fs->PushTarget(), 1);
  2196. break;
  2197. }
  2198. }
  2199. }
  2200. if(_token == _SC(':'))
  2201. {
  2202. next_token = _token;
  2203. }
  2204. Expect(next_token);
  2205. _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(obj_id));
  2206. Expression();
  2207. break;
  2208. }
  2209. CASE_TK_LOCAL_TYPES: //class member variables;
  2210. case TK_LOCAL:
  2211. if(isClass)
  2212. {
  2213. member_type_token = _token;
  2214. membertypename = _lex.GetTokenName(_token);
  2215. Lex();
  2216. goto member_has_type;
  2217. }
  2218. case _SC('~'): //C++ style destructor declaration
  2219. if(isClass)
  2220. {
  2221. cppDestructor = true;
  2222. Lex();
  2223. goto member_has_type;
  2224. }
  2225. case TK_CONSTEXPR:
  2226. if(isClass)
  2227. {
  2228. Lex();
  2229. continue;
  2230. }
  2231. //else fallthrough
  2232. default :
  2233. ErrorIfNotToken(TK_IDENTIFIER);
  2234. }
  2235. if(_token == separator) Lex();//optional comma/semicolon
  2236. nkeys++;
  2237. SQInteger val = _fs->PopTarget();
  2238. SQInteger key = _fs->PopTarget();
  2239. SQInteger attrs = hasattrs ? _fs->PopTarget():-1;
  2240. assert((hasattrs && (attrs == key-1)) || !hasattrs);
  2241. (void)attrs; // UNUSED
  2242. unsigned char flags = (hasattrs?NEW_SLOT_ATTRIBUTES_FLAG:0)|(isstatic?NEW_SLOT_STATIC_FLAG:0);
  2243. SQInteger table = _fs->TopTarget(); //<<BECAUSE OF THIS NO COMMON EMIT FUNC IS POSSIBLE
  2244. if(isClass)
  2245. {
  2246. _fs->AddInstruction(_OP_NEWSLOTA, flags, table, key, val); //this for classes only as it invokes _newmember
  2247. if(addClassMember > 1)
  2248. {
  2249. addClassMember = 0;
  2250. goto member_has_type;
  2251. }
  2252. }
  2253. else
  2254. {
  2255. _fs->AddInstruction(_OP_NEWSLOT, 0xFF, table, key, val);
  2256. }
  2257. }
  2258. if(separator == _SC(',')) //hack recognizes a table from the separator
  2259. _fs->SetInstructionParam(tpos, 1, nkeys);
  2260. Lex();
  2261. }
  2262. void ExternDeclStatement()
  2263. {
  2264. _is_parsing_extern = true;
  2265. Lex();
  2266. LocalDeclStatement();
  2267. _is_parsing_extern = false;
  2268. }
  2269. void EatTemplateInitialization()
  2270. {
  2271. //C++/Java template instantiation
  2272. if(_token == _SC('<'))
  2273. {
  2274. int nested = 1;
  2275. Lex();
  2276. while(nested > 0)
  2277. {
  2278. ExpectTypeToken();
  2279. switch(_token)
  2280. {
  2281. case ',': break;
  2282. case '<': ++nested; break;
  2283. case '>': --nested; break;
  2284. default:
  2285. Error(_SC("template instantiation declaration unexpected token"));
  2286. }
  2287. Lex();
  2288. }
  2289. }
  2290. }
  2291. SQInteger GetVarTypeDeclaration(SQInteger declType, SQInteger dest=-1)
  2292. {
  2293. bool doAddInstruction = (dest >= 0);
  2294. switch(declType)
  2295. {
  2296. CASE_TK_LOCAL_CHAR_TYPES:
  2297. if(doAddInstruction) _fs->AddInstruction(_OP_LOADNULLS, dest,1);
  2298. declType = _VAR_STRING;
  2299. break;
  2300. case TK_LOCAL_BOOL_T:
  2301. //default value false
  2302. if(doAddInstruction) _fs->AddInstruction(_OP_LOADBOOL, dest,0);
  2303. declType = _VAR_BOOL;
  2304. break;
  2305. case TK_LOCAL_TABLE_T:
  2306. if(doAddInstruction) _fs->AddInstruction(_OP_LOADNULLS, dest,1);
  2307. declType = _VAR_TABLE;
  2308. break;
  2309. case TK_LOCAL_ARRAY_T:
  2310. if(doAddInstruction) _fs->AddInstruction(_OP_LOADNULLS, dest,1);
  2311. declType = _VAR_ARRAY;
  2312. break;
  2313. CASE_TK_LOCAL_INT_TYPES:
  2314. //default value 0
  2315. if(doAddInstruction) _fs->AddInstruction(_OP_LOADINT, dest,0);
  2316. declType = _VAR_INTEGER;
  2317. break;
  2318. CASE_TK_LOCAL_FLOAT_TYPES:
  2319. case TK_LOCAL_NUMBER_T: //start numbers as floats
  2320. //default value 0.0
  2321. if(doAddInstruction)
  2322. {
  2323. SQFloat value = 0.0;
  2324. EmitLoadConstFloat(value, dest);
  2325. }
  2326. declType = _VAR_FLOAT;
  2327. break;
  2328. //case TK_LOCAL:
  2329. default:
  2330. //default value null
  2331. if(doAddInstruction) _fs->AddInstruction(_OP_LOADNULLS, dest,1);
  2332. declType = _VAR_ANY;
  2333. }
  2334. return declType;
  2335. }
  2336. #define CHECK_REF_DECLARATION(tk) \
  2337. if(tk == _SC('&')){is_reference_declaration = true;Lex();}\
  2338. else if(tk == _SC('*')){is_pointer_declaration = true;Lex();}
  2339. void LocalDeclStatement()
  2340. {
  2341. SQObject varname;
  2342. SQObject type_name = GetTokenObject(_token, false);
  2343. //SQChar *param_type_name;
  2344. bool is_void_declaration = _token == TK_VOID;
  2345. bool is_const_declaration = _token == TK_CONST;
  2346. bool is_reference_declaration = false;
  2347. bool is_pointer_declaration = false;
  2348. //bool is_instance_declaration = _token == TK_IDENTIFIER;
  2349. SQInteger declType = _token;
  2350. Lex();
  2351. EatTemplateInitialization();
  2352. if( _token == TK_FUNCTION)
  2353. {
  2354. Lex();
  2355. varname = Expect(TK_IDENTIFIER);
  2356. CheckLocalNameScope(varname, _scope.nested);
  2357. Expect(_SC('('));
  2358. function_params_decl:
  2359. #if 1
  2360. //doing this way works but prevents garbage collection when doing multiple reloads on the same vm
  2361. //the following is an attempt to allow local declared functions be called recursivelly
  2362. SQInteger old_pos = _fs->GetCurrentPos(); //save current instructions position
  2363. if(_fs->PushLocalVariable(varname, _scope.nested, _VAR_CLOSURE) < 0) //add function name to find it as outer var if needed
  2364. ErrorNameAlreadyDeclared(varname);
  2365. if(sq_type(type_name) == OT_STRING) _fs->AddParameterTypeName(type_name);
  2366. CreateFunction(varname, eFunctionType_local, declType);
  2367. if(_is_parsing_extern)
  2368. {
  2369. if(_token == _SC(';')) //to parse thinscript
  2370. {
  2371. //Expect(_SC(';'));
  2372. CheckExternName(varname, true);
  2373. return;
  2374. }
  2375. else _is_parsing_extern = false;
  2376. }
  2377. _fs->AddInstruction(_OP_CLOSURE, _fs->PushTarget(), _fs->_functions.size() - 1, 0);
  2378. //rellocate any stack operation (default parameters & _OP_Closure)
  2379. for(int i=old_pos+1, curr_pos = _fs->GetCurrentPos(); i <= curr_pos; ++i)
  2380. {
  2381. SQInstruction & inst = _fs->GetInstruction(i);
  2382. //printf("%d: %s\n", inst.op, SQGetOpName(inst.op));
  2383. if(inst.op == _OP_CLOSURE) _fs->SetInstructionParam(i, 0, inst._arg0 -1);
  2384. }
  2385. _fs->PopTarget();
  2386. #else
  2387. CreateFunction(varname,eFunctionType::local);
  2388. _fs->AddInstruction(_OP_CLOSURE, _fs->PushTarget(), _fs->_functions.size() - 1, 0);
  2389. _fs->PopTarget();
  2390. if( _fs->PushLocalVariable(varname, _scope.nested, _VAR_CLOSURE) < 0)
  2391. ErrorNameAlreadyDeclared(varname);;
  2392. #endif
  2393. return;
  2394. }
  2395. do
  2396. {
  2397. //param_type_name = NULL;
  2398. CHECK_REF_DECLARATION(_token);
  2399. varname = Expect(TK_IDENTIFIER);
  2400. if(CheckNameIsType(varname))
  2401. {
  2402. //ignore custom types for now
  2403. //param_type_name = _stringval(varname);
  2404. declType = _VAR_ANY;
  2405. CHECK_REF_DECLARATION(_token);
  2406. varname = Expect(TK_IDENTIFIER);
  2407. }
  2408. CheckLocalNameScope(varname, _scope.nested);
  2409. EatTemplateInitialization();
  2410. if(_token == _SC('('))
  2411. {
  2412. declType = GetVarTypeDeclaration(declType);
  2413. //C/C++ style function declaration
  2414. Lex();
  2415. goto function_params_decl;
  2416. }
  2417. else if(_token == _SC(':'))
  2418. {
  2419. //type specifier like typescript
  2420. Lex();
  2421. declType = _token;
  2422. type_name = ExpectTypeToken(); //ignore for now
  2423. }
  2424. if(is_void_declaration && !is_pointer_declaration)
  2425. {
  2426. Error(_SC("void type is invalid here"));
  2427. }
  2428. else if(_token == _SC('='))
  2429. {
  2430. if(_is_parsing_extern)
  2431. {
  2432. Error(_SC("can not make assignment in external declarations"));
  2433. }
  2434. Lex();
  2435. Expression();
  2436. SQInteger src = _fs->PopTarget();
  2437. SQInteger dest = _fs->PushTarget();
  2438. if(dest != src) _fs->AddInstruction(_OP_MOVE, dest, src);
  2439. declType = GetVarTypeDeclaration(declType);
  2440. }
  2441. else if(is_const_declaration || is_reference_declaration)
  2442. Error(_SC("const/reference '%s' need an initializer"), _stringval(varname));
  2443. else
  2444. {
  2445. //check_param_type:
  2446. if(!_is_parsing_extern)
  2447. {
  2448. SQInteger dest = _fs->PushTarget();
  2449. declType = GetVarTypeDeclaration(declType, dest);
  2450. }
  2451. }
  2452. if(_is_parsing_extern)
  2453. {
  2454. if(CheckExternName(varname, true))
  2455. {
  2456. Error(_SC("extern %s already declared"), _stringval(varname));
  2457. }
  2458. }
  2459. else
  2460. {
  2461. _fs->PopTarget();
  2462. if(_fs->PushLocalVariable(varname, _scope.nested, (is_const_declaration ? _VAR_CONST : declType)
  2463. | (is_reference_declaration ? _VAR_REFERENCE : 0)
  2464. | (is_pointer_declaration ? _VAR_POINTER : 0)
  2465. ) < 0) ErrorNameAlreadyDeclared(varname);
  2466. if(sq_type(type_name) == OT_STRING) _fs->AddParameterTypeName(type_name);
  2467. }
  2468. if(_token == _SC(',')) Lex();
  2469. else break;
  2470. }
  2471. while(1);
  2472. }
  2473. void IfBlock()
  2474. {
  2475. if (_token == _SC('{'))
  2476. {
  2477. BEGIN_SCOPE();
  2478. Lex();
  2479. Statements();
  2480. Expect(_SC('}'));
  2481. if (true)
  2482. {
  2483. END_SCOPE();
  2484. }
  2485. else
  2486. {
  2487. END_SCOPE_NO_CLOSE();
  2488. }
  2489. }
  2490. else
  2491. {
  2492. //BEGIN_SCOPE();
  2493. Statement();
  2494. if (_lex.data->prevtoken != _SC('}') && _lex.data->prevtoken != _SC(';')) OptionalSemicolon();
  2495. //END_SCOPE();
  2496. }
  2497. }
  2498. void IfStatement()
  2499. {
  2500. SQInteger jmppos;
  2501. bool haselse = false;
  2502. Lex();
  2503. Expect(_SC('('));
  2504. CommaExpr(true);
  2505. Expect(_SC(')'));
  2506. _fs->AddInstruction(_OP_JZ, _fs->PopTarget());
  2507. SQInteger jnepos = _fs->GetCurrentPos();
  2508. IfBlock();
  2509. //there is a situation where the if statement has a statement enclosed by {}
  2510. //and after the closing "}" there is no newline or semicolom
  2511. //it's a valid construction but the compiler was complaining about it
  2512. //for now added "&& _token != TK_IDENTIFIER" to the check after "Statement()" call
  2513. /*
  2514. local color = "blue";
  2515. if(color == "yellow"){
  2516. print(color);
  2517. } print("Waht color is it ?");
  2518. */
  2519. SQInteger endifblock = _fs->GetCurrentPos();
  2520. if(_token == TK_ELSE)
  2521. {
  2522. haselse = true;
  2523. BEGIN_SCOPE();
  2524. _fs->AddInstruction(_OP_JMP);
  2525. jmppos = _fs->GetCurrentPos();
  2526. Lex();
  2527. Statement();
  2528. if(_token != TK_IDENTIFIER) OptionalSemicolon();
  2529. END_SCOPE();
  2530. _fs->SetInstructionParam(jmppos, 1, _fs->GetCurrentPos() - jmppos);
  2531. }
  2532. _fs->SetInstructionParam(jnepos, 1, endifblock - jnepos + (haselse?1:0));
  2533. }
  2534. void WhileStatement()
  2535. {
  2536. SQInteger jzpos, jmppos;
  2537. jmppos = _fs->GetCurrentPos();
  2538. Lex();
  2539. Expect(_SC('('));
  2540. CommaExpr(true);
  2541. Expect(_SC(')'));
  2542. BEGIN_BREAKBLE_BLOCK();
  2543. _fs->AddInstruction(_OP_JZ, _fs->PopTarget());
  2544. jzpos = _fs->GetCurrentPos();
  2545. BEGIN_SCOPE();
  2546. Statement();
  2547. END_SCOPE();
  2548. _fs->AddInstruction(_OP_JMP, 0, jmppos - _fs->GetCurrentPos() - 1);
  2549. _fs->SetInstructionParam(jzpos, 1, _fs->GetCurrentPos() - jzpos);
  2550. END_BREAKBLE_BLOCK(jmppos);
  2551. }
  2552. void DoWhileStatement()
  2553. {
  2554. Lex();
  2555. SQInteger jmptrg = _fs->GetCurrentPos();
  2556. BEGIN_BREAKBLE_BLOCK()
  2557. BEGIN_SCOPE();
  2558. Statement();
  2559. END_SCOPE();
  2560. //fix proposed by frosch to correct line number info in stack dumps
  2561. _fs->AddLineInfos(_lex.data->currentline, _lineinfo, true);
  2562. Expect(TK_WHILE);
  2563. SQInteger continuetrg = _fs->GetCurrentPos();
  2564. Expect(_SC('('));
  2565. CommaExpr(true);
  2566. Expect(_SC(')'));
  2567. _fs->AddInstruction(_OP_JZ, _fs->PopTarget(), 1);
  2568. _fs->AddInstruction(_OP_JMP, 0, jmptrg - _fs->GetCurrentPos() - 1);
  2569. END_BREAKBLE_BLOCK(continuetrg);
  2570. }
  2571. void ForStatement()
  2572. {
  2573. Lex();
  2574. BEGIN_SCOPE();
  2575. Expect(_SC('('));
  2576. switch(_token)
  2577. {
  2578. case TK_IDENTIFIER:
  2579. {
  2580. SQObject id = _fs->CreateString(_lex.data->svalue);
  2581. if(!CheckTypeName(id))
  2582. {
  2583. SQInteger lhtk = _lex.LookaheadLex();
  2584. if(lhtk == TK_IDENTIFIER) Error(_SC("unknown type specifier"));
  2585. goto lbl_commaexpr;//not a C/C++ type declaration;
  2586. }
  2587. }
  2588. //ignore custom types for now
  2589. //fallthrough as local declaration
  2590. CASE_TK_LOCAL_TYPES:
  2591. case TK_LOCAL:
  2592. LocalDeclStatement();
  2593. break;
  2594. default:
  2595. lbl_commaexpr:
  2596. if(_token != _SC(';'))
  2597. {
  2598. CommaExpr();
  2599. _fs->PopTarget();
  2600. }
  2601. }
  2602. Expect(_SC(';'));
  2603. _fs->SnoozeOpt();
  2604. SQInteger jmppos = _fs->GetCurrentPos();
  2605. SQInteger jzpos = -1;
  2606. if(_token != _SC(';'))
  2607. {
  2608. CommaExpr();
  2609. _fs->AddInstruction(_OP_JZ, _fs->PopTarget());
  2610. jzpos = _fs->GetCurrentPos();
  2611. }
  2612. Expect(_SC(';'));
  2613. _fs->SnoozeOpt();
  2614. SQInteger expstart = _fs->GetCurrentPos() + 1;
  2615. if(_token != _SC(')'))
  2616. {
  2617. CommaExpr();
  2618. _fs->PopTarget();
  2619. }
  2620. Expect(_SC(')'));
  2621. _fs->SnoozeOpt();
  2622. SQInteger expend = _fs->GetCurrentPos();
  2623. SQInteger expsize = (expend - expstart) + 1;
  2624. SQInstructionVec exp;
  2625. if(expsize > 0)
  2626. {
  2627. for(SQInteger i = 0; i < expsize; i++)
  2628. exp.push_back(_fs->GetInstruction(expstart + i));
  2629. _fs->PopInstructions(expsize);
  2630. }
  2631. BEGIN_BREAKBLE_BLOCK()
  2632. Statement();
  2633. SQInteger continuetrg = _fs->GetCurrentPos();
  2634. if(expsize > 0)
  2635. {
  2636. for(SQInteger i = 0; i < expsize; i++)
  2637. _fs->AddInstruction(exp[i]);
  2638. }
  2639. _fs->AddInstruction(_OP_JMP, 0, jmppos - _fs->GetCurrentPos() - 1, 0);
  2640. if(jzpos> 0) _fs->SetInstructionParam(jzpos, 1, _fs->GetCurrentPos() - jzpos);
  2641. END_BREAKBLE_BLOCK(continuetrg);
  2642. END_SCOPE();
  2643. }
  2644. void ForEachStatement()
  2645. {
  2646. SQObject idxname, valname;
  2647. Lex();
  2648. Expect(_SC('('));
  2649. valname = Expect(TK_IDENTIFIER);
  2650. if(_token == _SC(','))
  2651. {
  2652. idxname = valname;
  2653. Lex();
  2654. valname = Expect(TK_IDENTIFIER);
  2655. }
  2656. else
  2657. {
  2658. idxname = _fs->CreateString(_SC("@INDEX@"));
  2659. }
  2660. Expect(TK_IN);
  2661. //save the stack size
  2662. BEGIN_SCOPE();
  2663. //put the table in the stack(evaluate the table expression)
  2664. Expression();
  2665. Expect(_SC(')'));
  2666. SQInteger container = _fs->TopTarget();
  2667. //push the index local var
  2668. SQInteger indexpos = _fs->PushLocalVariable(idxname, _scope.nested);
  2669. if(indexpos < 0) ErrorNameAlreadyDeclared(idxname);
  2670. _fs->AddInstruction(_OP_LOADNULLS, indexpos,1);
  2671. //push the value local var
  2672. SQInteger valuepos = _fs->PushLocalVariable(valname, _scope.nested);
  2673. if(valuepos < 0) ErrorNameAlreadyDeclared(valname);
  2674. _fs->AddInstruction(_OP_LOADNULLS, valuepos,1);
  2675. //push reference index
  2676. SQObject strIter = _fs->CreateString(_SC("@ITERATOR@"));
  2677. SQInteger itrpos = _fs->PushLocalVariable(strIter, _scope.nested); //use invalid id to make it inaccessible
  2678. if(itrpos < 0) ErrorNameAlreadyDeclared(strIter);
  2679. _fs->AddInstruction(_OP_LOADNULLS, itrpos,1);
  2680. SQInteger jmppos = _fs->GetCurrentPos();
  2681. _fs->AddInstruction(_OP_FOREACH, container, 0, indexpos);
  2682. SQInteger foreachpos = _fs->GetCurrentPos();
  2683. _fs->AddInstruction(_OP_POSTFOREACH, container, 0, indexpos);
  2684. //generate the statement code
  2685. BEGIN_BREAKBLE_BLOCK()
  2686. Statement();
  2687. _fs->AddInstruction(_OP_JMP, 0, jmppos - _fs->GetCurrentPos() - 1);
  2688. _fs->SetInstructionParam(foreachpos, 1, _fs->GetCurrentPos() - foreachpos);
  2689. _fs->SetInstructionParam(foreachpos + 1, 1, _fs->GetCurrentPos() - foreachpos);
  2690. END_BREAKBLE_BLOCK(foreachpos - 1);
  2691. //restore the local variable stack(remove index,val and ref idx)
  2692. _fs->PopTarget();
  2693. END_SCOPE();
  2694. }
  2695. void SwitchStatement()
  2696. {
  2697. Lex();
  2698. Expect(_SC('('));
  2699. CommaExpr(true);
  2700. Expect(_SC(')'));
  2701. Expect(_SC('{'));
  2702. SQInteger expr = _fs->TopTarget();
  2703. bool bfirst = true;
  2704. SQInteger tonextcondjmp = -1;
  2705. SQInteger skipcondjmp = -1;
  2706. SQInteger __nbreaks__ = _fs->_unresolvedbreaks.size();
  2707. _fs->_breaktargets.push_back(0);
  2708. while(_token == TK_CASE)
  2709. {
  2710. if(!bfirst)
  2711. {
  2712. _fs->AddInstruction(_OP_JMP, 0, 0);
  2713. skipcondjmp = _fs->GetCurrentPos();
  2714. _fs->SetInstructionParam(tonextcondjmp, 1, _fs->GetCurrentPos() - tonextcondjmp);
  2715. }
  2716. //condition
  2717. Lex();
  2718. ExpressionScalar() /*Expression()*/;
  2719. Expect(_SC(':'));
  2720. SQInteger trg = _fs->PopTarget();
  2721. SQInteger eqtarget = trg;
  2722. bool local = _fs->IsLocal(trg);
  2723. if(local)
  2724. {
  2725. eqtarget = _fs->PushTarget(); //we need to allocate a extra reg
  2726. }
  2727. _fs->AddInstruction(_OP_EQ, eqtarget, trg, expr);
  2728. _fs->AddInstruction(_OP_JZ, eqtarget, 0);
  2729. if(local)
  2730. {
  2731. _fs->PopTarget();
  2732. }
  2733. //end condition
  2734. if(skipcondjmp != -1)
  2735. {
  2736. _fs->SetInstructionParam(skipcondjmp, 1, (_fs->GetCurrentPos() - skipcondjmp));
  2737. }
  2738. tonextcondjmp = _fs->GetCurrentPos();
  2739. BEGIN_SCOPE();
  2740. Statements();
  2741. END_SCOPE();
  2742. bfirst = false;
  2743. }
  2744. if(tonextcondjmp != -1)
  2745. _fs->SetInstructionParam(tonextcondjmp, 1, _fs->GetCurrentPos() - tonextcondjmp);
  2746. if(_token == TK_DEFAULT)
  2747. {
  2748. Lex();
  2749. Expect(_SC(':'));
  2750. BEGIN_SCOPE();
  2751. Statements();
  2752. END_SCOPE();
  2753. }
  2754. Expect(_SC('}'));
  2755. _fs->PopTarget();
  2756. __nbreaks__ = _fs->_unresolvedbreaks.size() - __nbreaks__;
  2757. if(__nbreaks__ > 0)ResolveBreaks(_fs, __nbreaks__);
  2758. _fs->_breaktargets.pop_back();
  2759. }
  2760. void TemplateStatement()
  2761. {
  2762. Lex(); //ignore for now
  2763. Expect(_SC('<'));
  2764. int nest_count = 1;
  2765. while(_token > 0 && nest_count > 0)
  2766. {
  2767. Lex();
  2768. switch(_token)
  2769. {
  2770. case _SC('>'):
  2771. --nest_count;
  2772. break;
  2773. case _SC('<'):
  2774. nest_count++;
  2775. }
  2776. }
  2777. if(nest_count == 0) Lex(); //last '>' ignore for now
  2778. }
  2779. void FunctionStatement()
  2780. {
  2781. SQObject id;
  2782. Lex();
  2783. id = Expect(TK_IDENTIFIER);
  2784. _fs->PushTarget(0);
  2785. _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(id));
  2786. if(_token == TK_DOUBLE_COLON) Emit2ArgsOP(_OP_GET);
  2787. else CheckGlobalName(id, true);
  2788. while(_token == TK_DOUBLE_COLON)
  2789. {
  2790. Lex();
  2791. id = Expect(TK_IDENTIFIER);
  2792. //todo check if class function already exists
  2793. _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(id));
  2794. if(_token == TK_DOUBLE_COLON) Emit2ArgsOP(_OP_GET);
  2795. }
  2796. Expect(_SC('('));
  2797. CreateFunction(id, eFunctionType_global);
  2798. _fs->AddInstruction(_OP_CLOSURE, _fs->PushTarget(), _fs->_functions.size() - 1, 0);
  2799. EmitDerefOp(_OP_NEWSLOT);
  2800. _fs->PopTarget();
  2801. }
  2802. void ClassStatement()
  2803. {
  2804. SQExpState es;
  2805. SQObjectPtr class_name;
  2806. Lex();
  2807. if(_token == TK_IDENTIFIER)
  2808. {
  2809. SQInteger lhtk = _lex.LookaheadLex();
  2810. switch(lhtk)
  2811. {
  2812. case TK_DOUBLE_COLON: //C++ style namespace
  2813. case _SC('.'):
  2814. break;
  2815. default:
  2816. class_name = SQString::Create(_ss(_vm), _lex.data->svalue);
  2817. CheckGlobalName(class_name, true);
  2818. CheckTypeName(class_name, true); //to allow C/C++ style instance declarations
  2819. }
  2820. }
  2821. es = _es;
  2822. _es.donot_get = true;
  2823. PrefixedExpr();
  2824. if(_token == _SC(';')) //class forward declaration
  2825. {
  2826. //return;
  2827. }
  2828. if(_es.etype == EXPR)
  2829. {
  2830. Error(_SC("invalid class name"));
  2831. }
  2832. else if(_es.etype == OBJECT || _es.etype == BASE)
  2833. {
  2834. ClassExp(&class_name);
  2835. EmitDerefOp(_OP_NEWSLOT);
  2836. _fs->PopTarget();
  2837. }
  2838. else
  2839. {
  2840. Error(_SC("cannot create a class in a local with the syntax(class <local>)"));
  2841. }
  2842. _es = es;
  2843. }
  2844. SQObject ExpectScalar()
  2845. {
  2846. SQObject val;
  2847. val._type = OT_NULL;
  2848. val._unVal.nInteger = 0; //shut up GCC 4.x
  2849. switch(_token)
  2850. {
  2851. CASE_TK_INTEGER:
  2852. val._type = OT_INTEGER;
  2853. val._unVal.nInteger = _lex.data->nvalue;
  2854. break;
  2855. case TK_FLOAT:
  2856. val._type = OT_FLOAT;
  2857. val._unVal.fFloat = _lex.data->fvalue;
  2858. break;
  2859. case TK_STRING_LITERAL:
  2860. val = _fs->CreateString(_lex.data->svalue,_lex.data->longstr.size()-1);
  2861. break;
  2862. case TK_TRUE:
  2863. case TK_FALSE:
  2864. val._type = OT_BOOL;
  2865. val._unVal.nInteger = _token == TK_TRUE ? 1 : 0;
  2866. break;
  2867. case '-':
  2868. Lex();
  2869. switch(_token)
  2870. {
  2871. CASE_TK_INTEGER:
  2872. val._type = OT_INTEGER;
  2873. val._unVal.nInteger = -_lex.data->nvalue;
  2874. break;
  2875. case TK_FLOAT:
  2876. val._type = OT_FLOAT;
  2877. val._unVal.fFloat = -_lex.data->fvalue;
  2878. break;
  2879. default:
  2880. Error(_SC("scalar expected : integer, float"));
  2881. }
  2882. break;
  2883. default:
  2884. Error(_SC("scalar expected : integer, float or string"));
  2885. }
  2886. Lex();
  2887. return val;
  2888. }
  2889. SQInteger ExpressionScalar()
  2890. {
  2891. SQInteger tk_type = _token;
  2892. switch(_token)
  2893. {
  2894. CASE_TK_INTEGER:
  2895. EmitLoadConstInt(_lex.data->nvalue,-1);
  2896. break;
  2897. case TK_FLOAT:
  2898. EmitLoadConstFloat(_lex.data->fvalue,-1);
  2899. break;
  2900. case TK_STRING_LITERAL:
  2901. _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(_fs->CreateString(_lex.data->svalue,_lex.data->longstr.size()-1)));
  2902. break;
  2903. case TK_IDENTIFIER:
  2904. {
  2905. SQObject id = _fs->CreateString(_lex.data->svalue);
  2906. Lex();
  2907. ExpressionConstant(id);
  2908. return tk_type;
  2909. }
  2910. break;
  2911. case '-':
  2912. Lex();
  2913. tk_type = _token;
  2914. switch(_token)
  2915. {
  2916. CASE_TK_INTEGER:
  2917. EmitLoadConstInt(-_lex.data->nvalue,-1);
  2918. break;
  2919. case TK_FLOAT:
  2920. EmitLoadConstFloat(-_lex.data->fvalue,-1);
  2921. break;
  2922. default:
  2923. Error(_SC("scalar expected : integer, float"));
  2924. }
  2925. break;
  2926. default:
  2927. goto error;
  2928. }
  2929. Lex();
  2930. return tk_type;
  2931. error:
  2932. Error(_SC("constant or scalar expected : integer, float or string"));
  2933. return 0;
  2934. }
  2935. void EnumStatement()
  2936. {
  2937. Lex();
  2938. SQObject id = Expect(TK_IDENTIFIER);
  2939. Expect(_SC('{'));
  2940. //CheckLocalNameScope(id, _scope.nested);
  2941. SQObjectPtr strongid = id;
  2942. CheckLocalNameScope(id, _scope.nested);
  2943. TypesNewSlot(strongid,strongid);
  2944. SQObject table = _fs->CreateTable();
  2945. //_fs->AddInstruction(_OP_NEWOBJ, _fs->PushTarget(),0,NOT_TABLE);
  2946. SQInteger nval = 0;
  2947. while(_token != _SC('}'))
  2948. {
  2949. SQObject key = Expect(TK_IDENTIFIER);
  2950. SQObject val;
  2951. if(_token == _SC('='))
  2952. {
  2953. Lex();
  2954. val = ExpectScalar();
  2955. if(sq_type(val) == OT_INTEGER) nval = _integer(val)+1;
  2956. }
  2957. else
  2958. {
  2959. val._type = OT_INTEGER;
  2960. val._unVal.nInteger = nval++;
  2961. }
  2962. //SQInteger table = _fs->TopTarget(); //<<BECAUSE OF THIS NO COMMON EMIT FUNC IS POSSIBLE
  2963. //_fs->AddInstruction(_OP_NEWSLOT, 0xFF, table, key, val);
  2964. _table(table)->NewSlot(SQObjectPtr(key),SQObjectPtr(val));
  2965. if(_token == ',') Lex();
  2966. }
  2967. ConstsNewSlot(SQObjectPtr(strongid),SQObjectPtr(table));
  2968. strongid.Null();
  2969. Lex();
  2970. }
  2971. void TryCatchStatement()
  2972. {
  2973. SQObject exid;
  2974. Lex();
  2975. _fs->AddInstruction(_OP_PUSHTRAP,0,0);
  2976. _fs->_traps++;
  2977. if(_fs->_breaktargets.size()) _fs->_breaktargets.top()++;
  2978. if(_fs->_continuetargets.size()) _fs->_continuetargets.top()++;
  2979. SQInteger trappos = _fs->GetCurrentPos();
  2980. {
  2981. BEGIN_SCOPE();
  2982. Statement();
  2983. END_SCOPE();
  2984. }
  2985. _fs->_traps--;
  2986. _fs->AddInstruction(_OP_POPTRAP, 1, 0);
  2987. if(_fs->_breaktargets.size()) _fs->_breaktargets.top()--;
  2988. if(_fs->_continuetargets.size()) _fs->_continuetargets.top()--;
  2989. _fs->AddInstruction(_OP_JMP, 0, 0);
  2990. SQInteger jmppos = _fs->GetCurrentPos();
  2991. _fs->SetInstructionParam(trappos, 1, (_fs->GetCurrentPos() - trappos));
  2992. Expect(TK_CATCH);
  2993. Expect(_SC('('));
  2994. exid = Expect(TK_IDENTIFIER);
  2995. if(CheckTypeName(exid)) //C/C++ type declaration;
  2996. {
  2997. //for now just accept the type and do nothing with it
  2998. exid = Expect(TK_IDENTIFIER);
  2999. }
  3000. Expect(_SC(')'));
  3001. {
  3002. BEGIN_SCOPE();
  3003. SQInteger ex_target = _fs->PushLocalVariable(exid, _scope.nested);
  3004. if(ex_target < 0) ErrorNameAlreadyDeclared(exid);
  3005. _fs->SetInstructionParam(trappos, 0, ex_target);
  3006. Statement();
  3007. _fs->SetInstructionParams(jmppos, 0, (_fs->GetCurrentPos() - jmppos), 0);
  3008. END_SCOPE();
  3009. }
  3010. }
  3011. void FunctionExp(SQInteger ftype,bool lambda = false)
  3012. {
  3013. Lex();
  3014. Expect(_SC('('));
  3015. SQObjectPtr dummy;
  3016. CreateFunction(dummy, lambda ? eFunctionType_lambda : eFunctionType_anonymous);
  3017. _fs->AddInstruction(_OP_CLOSURE, _fs->PushTarget(), _fs->_functions.size() - 1, ftype == TK_FUNCTION?0:1);
  3018. }
  3019. void ClassExp(SQObjectPtr *class_name)
  3020. {
  3021. SQInteger base = -1;
  3022. SQInteger attrs = -1;
  3023. bool hasInheritance = false;
  3024. switch(_token)
  3025. {
  3026. case _SC(':'): //C++ style class derivation
  3027. case TK_EXTENDS: //squirrel style class derivation
  3028. Lex();
  3029. hasInheritance = true;
  3030. switch(_token)
  3031. {
  3032. case TK_PRIVATE:
  3033. case TK_PROTECTED:
  3034. case TK_PUBLIC:
  3035. Lex(); //ignore, accepted only to compile a subset of C++
  3036. }
  3037. }
  3038. if(hasInheritance)
  3039. {
  3040. Expression();
  3041. base = _fs->TopTarget();
  3042. }
  3043. if(_token == TK_ATTR_OPEN)
  3044. {
  3045. Lex();
  3046. _fs->AddInstruction(_OP_NEWOBJ, _fs->PushTarget(),0,NOT_TABLE);
  3047. ParseTableOrClass(_SC(','),TK_ATTR_CLOSE);
  3048. attrs = _fs->TopTarget();
  3049. }
  3050. Expect(_SC('{'));
  3051. if(attrs != -1) _fs->PopTarget();
  3052. if(base != -1) _fs->PopTarget();
  3053. _fs->AddInstruction(_OP_NEWOBJ, _fs->PushTarget(), base, attrs,NOT_CLASS);
  3054. ParseTableOrClass(_SC(';'),_SC('}'), class_name);
  3055. }
  3056. void DeleteExpr()
  3057. {
  3058. SQExpState es;
  3059. Lex();
  3060. es = _es;
  3061. _es.donot_get = true;
  3062. PrefixedExpr();
  3063. if(_es.etype==EXPR) Error(_SC("can't delete an expression"));
  3064. if(_es.etype==OBJECT || _es.etype==BASE)
  3065. {
  3066. Emit2ArgsOP(_OP_DELETE);
  3067. }
  3068. else
  3069. {
  3070. Error(_SC("cannot delete an (outer) local"));
  3071. }
  3072. _es = es;
  3073. }
  3074. void PrefixIncDec(SQInteger token)
  3075. {
  3076. SQExpState es;
  3077. SQInteger diff = (token==TK_MINUSMINUS) ? -1 : 1;
  3078. Lex();
  3079. es = _es;
  3080. _es.donot_get = true;
  3081. PrefixedExpr();
  3082. if(_es.etype==EXPR)
  3083. {
  3084. Error(_SC("can't '++' or '--' an expression"));
  3085. }
  3086. else if(_es.etype==OBJECT || _es.etype==BASE)
  3087. {
  3088. Emit2ArgsOP(_OP_INC, diff);
  3089. }
  3090. else if(_es.etype==LOCAL)
  3091. {
  3092. SQInteger src = _fs->TopTarget();
  3093. _fs->AddInstruction(_OP_INCL, src, src, 0, diff);
  3094. }
  3095. else if(_es.etype==OUTER)
  3096. {
  3097. SQInteger tmp = _fs->PushTarget();
  3098. _fs->AddInstruction(_OP_GETOUTER, tmp, _es.epos);
  3099. _fs->AddInstruction(_OP_INCL, tmp, tmp, 0, diff);
  3100. _fs->AddInstruction(_OP_SETOUTER, tmp, _es.epos, tmp);
  3101. }
  3102. _es = es;
  3103. }
  3104. void CreateFunction(SQObject &name, int ftype, int fdeclType=0)
  3105. {
  3106. SQFuncState *funcstate = _fs->PushChildState(_ss(_vm));
  3107. funcstate->_name = name;
  3108. SQObject paramname, type_name;
  3109. SQObject strThis = _fs->CreateString(_SC("this"));
  3110. if(funcstate->AddParameter(strThis, _scope.nested+1) < 0)
  3111. ErrorNameAlreadyDeclared(strThis);
  3112. funcstate->_sourcename = _sourcename;
  3113. SQInteger defparams = 0;
  3114. bool is_reference_declaration = 0;
  3115. bool is_pointer_declaration = false;
  3116. const SQChar *param_type_name = 0;
  3117. bool isVoid = false;
  3118. if(fdeclType)
  3119. {
  3120. funcstate->SetReturnTypeName(SQGetVarTypeName(fdeclType));
  3121. }
  3122. while(_token!=_SC(')'))
  3123. {
  3124. if(isVoid)
  3125. {
  3126. Error(_SC("void type is invalid here"));
  3127. }
  3128. is_reference_declaration = 0; //reset is_reference_declaration
  3129. param_type_name = 0; //rest for each parameter
  3130. if(_token == TK_VOID)
  3131. {
  3132. isVoid = true;
  3133. Lex();
  3134. continue;
  3135. }
  3136. if(_token == TK_VARPARAMS)
  3137. {
  3138. if(defparams > 0) Error(_SC("function with default parameters cannot have variable number of parameters"));
  3139. SQObject strVargv = _fs->CreateString(_SC("vargv"));
  3140. if(funcstate->AddParameter(strVargv, _scope.nested+1) < 0)
  3141. ErrorNameAlreadyDeclared(strVargv);
  3142. funcstate->_varparams = true;
  3143. Lex();
  3144. if(_token != _SC(')')) Error(_SC("expected ')'"));
  3145. break;
  3146. }
  3147. else
  3148. {
  3149. if(_token == TK_CONST) Lex(); //C/C++ const cualifiers for now
  3150. switch(_token)
  3151. {
  3152. CASE_TK_LOCAL_TYPES: //accept C/C++ type parameter declarations
  3153. param_type_name = _lex.GetTokenName(_token);
  3154. Lex();
  3155. }
  3156. CHECK_REF_DECLARATION(_token);
  3157. paramname = Expect(TK_IDENTIFIER);
  3158. if(CheckNameIsType(paramname))
  3159. {
  3160. //ignore custom types for now
  3161. param_type_name = _stringval(paramname);
  3162. CHECK_REF_DECLARATION(_token);
  3163. EatTemplateInitialization();
  3164. paramname = Expect(TK_IDENTIFIER);
  3165. }
  3166. if(funcstate->AddParameter(paramname, _scope.nested+1, is_reference_declaration ? _VAR_REFERENCE :
  3167. (is_pointer_declaration ? _VAR_POINTER : _VAR_ANY)) < 0)
  3168. ErrorNameAlreadyDeclared(paramname);
  3169. if(param_type_name)
  3170. {
  3171. funcstate->AddParameterTypeName(param_type_name);
  3172. }
  3173. default_value:
  3174. if(_token == _SC('='))
  3175. {
  3176. if(is_reference_declaration) Error(_SC("parameter passed by reference can't have default value"));
  3177. Lex();
  3178. if(_token == _SC('[') || _token == _SC('{')) Error(_SC("default parameter with array/table values not supported"));
  3179. Expression();
  3180. //stack_offset to compensate for local functions been relocated to allow recursion
  3181. funcstate->AddDefaultParam(_fs->TopTarget());
  3182. defparams++;
  3183. }
  3184. else if(_token == _SC(':'))
  3185. {
  3186. //param type specifier like typescript
  3187. if(param_type_name)
  3188. {
  3189. Error(_SC("parameter type already declared before %s"), _stringval(paramname));
  3190. }
  3191. Lex();
  3192. type_name = ExpectTypeToken();
  3193. funcstate->AddParameterTypeName(type_name);
  3194. //printf("%d %s\n", __LINE__, _stringval(type_name));
  3195. if(_token == _SC('=')) goto default_value; //default value
  3196. }
  3197. else
  3198. {
  3199. if(defparams > 0) Error(_SC("expected '='"));
  3200. }
  3201. if(_token == _SC(',')) Lex();
  3202. else if(_token != _SC(')')) Error(_SC("expected ')' or ','"));
  3203. }
  3204. }
  3205. Expect(_SC(')'));
  3206. switch(_token)
  3207. {
  3208. case _SC(':'):
  3209. {
  3210. //return type specifier like typescript
  3211. Lex();
  3212. type_name = ExpectTypeToken();
  3213. funcstate->_return_type = type_name;
  3214. //printf("%d %s\n", __LINE__, _stringval(type_name));
  3215. }
  3216. break;
  3217. case TK_CONST:
  3218. case TK_NOEXCEPT:
  3219. case TK_VOLATILE:
  3220. Lex(); //accept and ignore
  3221. break;
  3222. case TK_IDENTIFIER:
  3223. if(ftype == eFunctionType_member)
  3224. {
  3225. if( (scstrcmp(_lex.data->svalue, _SC("final")) == 0) ||
  3226. (scstrcmp(_lex.data->svalue, _SC("override")) == 0))
  3227. {
  3228. Lex(); //accept but ignore then
  3229. }
  3230. }
  3231. }
  3232. for(SQInteger n = 0; n < defparams; n++)
  3233. {
  3234. _fs->PopTarget();
  3235. }
  3236. if(_is_parsing_extern)
  3237. {
  3238. if(_token == _SC(';')) //to parse thinscript
  3239. {
  3240. _fs->PopChildState();
  3241. return;
  3242. }
  3243. else _is_parsing_extern = false;
  3244. }
  3245. SQFuncState *currchunk = _fs;
  3246. _fs = funcstate;
  3247. if(ftype == eFunctionType_lambda)
  3248. {
  3249. Expression();
  3250. _fs->AddInstruction(_OP_RETURN, 1, _fs->PopTarget());
  3251. }
  3252. else
  3253. {
  3254. Statement(false);
  3255. }
  3256. ResolveGotos();
  3257. funcstate->AddLineInfos(_lex.data->prevtoken == _SC('\n')?_lex.data->lasttokenline:_lex.data->currentline, _lineinfo, true);
  3258. funcstate->AddInstruction(_OP_RETURN, -1);
  3259. funcstate->SetStackSize(0);
  3260. SQFunctionProto *func = funcstate->BuildProto();
  3261. #ifdef _DEBUG_DUMP
  3262. funcstate->Dump(func);
  3263. #endif
  3264. _fs = currchunk;
  3265. _fs->_functions.push_back(func);
  3266. _fs->PopChildState();
  3267. }
  3268. void AdjustGotoInstruction(const SQGotoLabelsInfo &goto_info, const SQGotoLabelsInfo &label)
  3269. {
  3270. SQInteger target_pos = label.pos;
  3271. SQInteger jump_pos = target_pos - goto_info.pos;
  3272. //set the jmp instruction
  3273. SQInteger poptraps = goto_info.traps - label.traps;
  3274. SQInt32 ioffset = 1;
  3275. while(poptraps)
  3276. {
  3277. SQInstruction &i_op_poptrap = _fs->GetInstruction(goto_info.pos-ioffset);
  3278. if(i_op_poptrap.op != _OP_NOP)
  3279. {
  3280. if(ioffset < 2)
  3281. {
  3282. ++ioffset;
  3283. continue;
  3284. }
  3285. Error(_SC("Compiler: This should not happen i_op_poptrap.op != _OP_NOP"));
  3286. }
  3287. //printf("poptraps for %s = %d : %d\n", _stringval(label.name), (int)poptraps, (int)i_op_poptrap._arg0);
  3288. i_op_poptrap.op = _OP_POPTRAP;
  3289. i_op_poptrap._arg0 = poptraps;
  3290. break;
  3291. }
  3292. _fs->SetInstructionParams(goto_info.pos, 0, jump_pos, 0);
  3293. }
  3294. void ResolveGotos()
  3295. {
  3296. //first we walk through the labels and when we come back
  3297. //nested labels we remove then because we only allow
  3298. //goto/joump out of blocks/scopes
  3299. SQInt16 last_nested = -1;
  3300. for(SQUnsignedInteger idxlabel = 0; idxlabel < _fs->_gototargets.size(); ++idxlabel)
  3301. {
  3302. SQGotoLabelsInfo &label = _fs->_gototargets[idxlabel];
  3303. SQUnsignedInteger resolved = 0;
  3304. for(SQUnsignedInteger idxGoto = 0; idxGoto < _fs->_unresolvedgotos.size(); ++idxGoto)
  3305. {
  3306. SQGotoLabelsInfo &goto_info = _fs->_unresolvedgotos[idxGoto];
  3307. if(_string(label.name) == _string(goto_info.name))
  3308. {
  3309. if(label.nested > goto_info.nested)
  3310. {
  3311. _lex.data->currentline = goto_info.line;
  3312. Error(_SC("Goto can not jump inside nested block/scope '%s'"), _stringval(goto_info.name));
  3313. }
  3314. AdjustGotoInstruction(goto_info, label);
  3315. //we dcrease idxGoto here to compensate the removal inside the for loop
  3316. _fs->_unresolvedgotos.remove(idxGoto--);
  3317. ++resolved;
  3318. }
  3319. }
  3320. if(!resolved)
  3321. {
  3322. //if a label isn't resolved we have an unused label
  3323. _lex.data->currentline = label.line;
  3324. Error(_SC("Label not resolved at this point '%s'"), _stringval(label.name));
  3325. }
  3326. if(label.nested < last_nested)
  3327. {
  3328. //remove any previous nested label to prevent goto/jump inside blocks/scopes
  3329. SQUnsignedInteger i = idxlabel - 1;
  3330. for(; i > 0; --i)
  3331. {
  3332. if(_fs->_gototargets[i].nested <= label.nested) break;
  3333. _fs->_gototargets.remove(i);
  3334. //we decrease the index idxlabel here due to remove before
  3335. //the current idxlabel
  3336. --idxlabel;
  3337. }
  3338. }
  3339. last_nested = label.nested;
  3340. }
  3341. if(_fs->_unresolvedgotos.size())
  3342. {
  3343. //if we still have any unresolved goto it's an error, let's show the first
  3344. SQGotoLabelsInfo &goto_info = _fs->_unresolvedgotos[0];
  3345. _lex.data->currentline = goto_info.line;
  3346. Error(_SC("Goto can not jump to undeclared label '%s'"), _stringval(goto_info.name));
  3347. }
  3348. }
  3349. void ResolveBreaks(SQFuncState *funcstate, SQInteger ntoresolve)
  3350. {
  3351. while(ntoresolve > 0)
  3352. {
  3353. SQInteger pos = funcstate->_unresolvedbreaks.back();
  3354. funcstate->_unresolvedbreaks.pop_back();
  3355. //set the jmp instruction
  3356. funcstate->SetInstructionParams(pos, 0, funcstate->GetCurrentPos() - pos, 0);
  3357. ntoresolve--;
  3358. }
  3359. }
  3360. void ResolveContinues(SQFuncState *funcstate, SQInteger ntoresolve, SQInteger targetpos)
  3361. {
  3362. while(ntoresolve > 0)
  3363. {
  3364. SQInteger pos = funcstate->_unresolvedcontinues.back();
  3365. funcstate->_unresolvedcontinues.pop_back();
  3366. //set the jmp instruction
  3367. funcstate->SetInstructionParams(pos, 0, targetpos - pos, 0);
  3368. ntoresolve--;
  3369. }
  3370. }
  3371. private:
  3372. SQInteger _token;
  3373. SQFuncState *_fs;
  3374. SQObjectPtr _sourcename;
  3375. SQLexer _lex;
  3376. bool _lineinfo;
  3377. bool _raiseerror;
  3378. bool _show_warnings;
  3379. bool _is_parsing_extern;
  3380. SQInteger _debugline;
  3381. SQInteger _debugop;
  3382. SQExpState _es;
  3383. SQScope _scope;
  3384. SQChar *_compilererror;
  3385. jmp_buf _errorjmp;
  3386. SQVM *_vm;
  3387. SQObjectPtrVec _scope_consts;
  3388. SQObjectPtrVec _scope_types;
  3389. SQObjectPtr _globals;
  3390. SQObjectPtr _type_names; //to allow C/C++ style instance declarations
  3391. SQObjectPtr _extern_names; //to allow C/C++ style extern declarations
  3392. SQChar error_buf[MAX_COMPILER_ERROR_LEN];
  3393. SQInteger _max_nested_includes, _nested_includes_count;
  3394. SQInteger _ifdef_exclude, _ifdef_line, _inside_ifdef;
  3395. const SQChar *squilu_lib_path;
  3396. bool _inside_constructor;
  3397. };
  3398. bool Compile(SQVM *vm,SQLEXREADFUNC rg, SQUserPointer up, const SQChar *sourcename, SQObjectPtr &out,
  3399. bool raiseerror, bool lineinfo, bool show_warnings, SQInteger max_nested_includes)
  3400. {
  3401. SQCompiler p(vm, rg, up, sourcename, raiseerror, lineinfo, show_warnings, max_nested_includes);
  3402. return p.Compile(out);
  3403. }
  3404. #endif