sqcompiler.cpp 83 KB

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