sqcompiler.cpp 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692
  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('('):
  1321. Lex();
  1322. if(_token == TK_IDENTIFIER)
  1323. {
  1324. //check C/C++ cast
  1325. SQObject id = _fs->CreateString(_lex.data->svalue);
  1326. if(CheckTypeName(id)) //C/C++ type declaration;
  1327. {
  1328. SQInteger lhtk = _lex.LookaheadLex();
  1329. if(lhtk == _SC(')'))
  1330. {
  1331. Lex(); //eat TK_IDENTIFIER
  1332. Lex(); //eat ')'
  1333. CommaExpr();
  1334. break;
  1335. }
  1336. }
  1337. }
  1338. CommaExpr();
  1339. Expect(_SC(')'));
  1340. break;
  1341. case TK___LINE__: EmitLoadConstInt(_lex.data->currentline,-1); Lex(); break;
  1342. case TK___FILE__:
  1343. _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(_sourcename));
  1344. Lex();
  1345. break;
  1346. case TK___FUNCTION__:
  1347. _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(_fs->_name));
  1348. Lex();
  1349. break;
  1350. case TK_IGNORE:
  1351. //Warning("Keyword ignored \"%s\" at line %d:%d\n", _lex.Tok2Str(_token),
  1352. // _lex.data->currentline, _lex.data->currentcolumn);
  1353. Lex(); Factor();
  1354. break;
  1355. default: Error(_SC("expression expected"));
  1356. }
  1357. return -1;
  1358. }
  1359. void EmitLoadConstInt(SQInteger value,SQInteger target)
  1360. {
  1361. if(target < 0) {
  1362. target = _fs->PushTarget();
  1363. }
  1364. //with the line bellow we get wrong result for -1
  1365. //if(value <= INT_MAX && value > INT_MIN) { //does it fit in 32 bits?
  1366. if((value & (~((SQInteger)0xFFFFFFFF))) == 0) { //does it fit in 32 bits?
  1367. _fs->AddInstruction(_OP_LOADINT, target,value);
  1368. }
  1369. else {
  1370. _fs->AddInstruction(_OP_LOAD, target, _fs->GetNumericConstant(value));
  1371. }
  1372. }
  1373. void EmitLoadConstFloat(SQFloat value,SQInteger target)
  1374. {
  1375. if(target < 0) {
  1376. target = _fs->PushTarget();
  1377. }
  1378. if(sizeof(SQFloat) == sizeof(SQInt32)) {
  1379. _fs->AddInstruction(_OP_LOADFLOAT, target,*((SQInt32 *)&value));
  1380. }
  1381. else {
  1382. _fs->AddInstruction(_OP_LOAD, target, _fs->GetNumericConstant(value));
  1383. }
  1384. }
  1385. void UnaryOP(SQOpcode op)
  1386. {
  1387. PrefixedExpr();
  1388. SQInteger src = _fs->PopTarget();
  1389. _fs->AddInstruction(op, _fs->PushTarget(), src);
  1390. }
  1391. bool NeedGet()
  1392. {
  1393. switch(_token) {
  1394. case _SC('='): case _SC('('): case TK_NEWSLOT: case TK_MODEQ: case TK_MULEQ:
  1395. case TK_DIVEQ: case TK_MINUSEQ: case TK_PLUSEQ:
  1396. return false;
  1397. case TK_PLUSPLUS: case TK_MINUSMINUS:
  1398. if (!IsEndOfStatement()) {
  1399. return false;
  1400. }
  1401. break;
  1402. }
  1403. return (!_es.donot_get || ( _es.donot_get && (_token == _SC('.') || _token == _SC('['))));
  1404. }
  1405. void FunctionCallArgs()
  1406. {
  1407. SQInteger nargs = 1;//this
  1408. while(_token != _SC(')')) {
  1409. Expression();
  1410. MoveIfCurrentTargetIsLocal();
  1411. nargs++;
  1412. if(_token == _SC(',')){
  1413. Lex();
  1414. if(_token == ')') Error(_SC("expression expected, found ')'"));
  1415. }
  1416. }
  1417. Lex();
  1418. for(SQInteger i = 0; i < (nargs - 1); i++) _fs->PopTarget();
  1419. SQInteger stackbase = _fs->PopTarget();
  1420. SQInteger closure = _fs->PopTarget();
  1421. _fs->AddInstruction(_OP_CALL, _fs->PushTarget(), closure, stackbase, nargs);
  1422. }
  1423. void AddClassMemberExists(SQObjectPtr &member_names, SQObject &name)
  1424. {
  1425. SQObjectPtr oname = name, otrue = true;
  1426. _table(member_names)->NewSlot(oname, otrue);
  1427. }
  1428. void CheckClassMemberExists(SQObjectPtr &member_names, SQObject &name, bool addIfNotExists=true)
  1429. {
  1430. if(_table(member_names)->Exists(name))
  1431. {
  1432. Error(_SC("class already has a member named: %s"), _stringval(name));
  1433. }
  1434. if(addIfNotExists) AddClassMemberExists(member_names, name);
  1435. }
  1436. void CheckClassMemberExists(SQObjectPtr &member_names, const SQChar *name)
  1437. {
  1438. SQObject oname = _fs->CreateString(name);
  1439. CheckClassMemberExists(member_names, oname);
  1440. }
  1441. void ParseTableOrClass(SQInteger separator,SQInteger terminator, SQObjectPtr *class_name=NULL)
  1442. {
  1443. SQObjectPtr member_names;
  1444. SQInteger saved_tok, tpos = _fs->GetCurrentPos(),nkeys = 0;
  1445. SQObject type_name, obj_id;
  1446. bool isClass = separator == ';'; //hack recognizes a table/class from the separator
  1447. if(isClass)
  1448. {
  1449. member_names = SQTable::Create(_ss(_vm),0);
  1450. }
  1451. int addClassMember = 0;
  1452. while(_token != terminator) {
  1453. bool hasattrs = false;
  1454. bool isstatic = false;
  1455. bool cppDestructor = false;
  1456. //bool isvirtual = false;
  1457. //bool isprivate = false;
  1458. const SQChar *membertypename = 0;
  1459. //check if is an attribute
  1460. if(isClass) {
  1461. if(_token == TK_ATTR_OPEN) {
  1462. _fs->AddInstruction(_OP_NEWOBJ, _fs->PushTarget(),0,NOT_TABLE); Lex();
  1463. ParseTableOrClass(',',TK_ATTR_CLOSE);
  1464. hasattrs = true;
  1465. }
  1466. if(_token == TK_STATIC) {
  1467. isstatic = true;
  1468. Lex();
  1469. }
  1470. if(_token == TK_VIRTUAL) {
  1471. //isvirtual = true;
  1472. Lex();
  1473. }
  1474. else if(_token == TK_PUBLIC) {
  1475. Lex();
  1476. }
  1477. else if(_token == TK_PRIVATE) {
  1478. //isprivate = true;
  1479. Lex();
  1480. }
  1481. else if(_token == TK_INLINE) {
  1482. Lex();
  1483. }
  1484. else if(_token == TK_CONST) {
  1485. Lex();
  1486. }
  1487. else if(_token == TK_TEMPLATE) {
  1488. TemplateStatement();
  1489. }
  1490. }
  1491. member_has_type:
  1492. switch(_token) {
  1493. case TK_FUNCTION:
  1494. case TK_CONSTRUCTOR:
  1495. case TK_DESTRUCTOR:{
  1496. saved_tok = _token;
  1497. Lex();
  1498. obj_id = saved_tok == TK_FUNCTION ? Expect(TK_IDENTIFIER) :
  1499. _fs->CreateString(saved_tok == TK_CONSTRUCTOR ? _SC("constructor") : _SC("destructor"));
  1500. CheckClassMemberExists(member_names, obj_id);
  1501. Expect(_SC('('));
  1502. function_params_decl:
  1503. _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(obj_id));
  1504. CreateFunction(obj_id, eFunctionType_member);
  1505. _fs->AddInstruction(_OP_CLOSURE, _fs->PushTarget(), _fs->_functions.size() - 1, 0);
  1506. }
  1507. break;
  1508. case _SC('['):
  1509. Lex(); CommaExpr(); Expect(_SC(']'));
  1510. Expect(_SC('=')); Expression();
  1511. break;
  1512. case TK_STRING_LITERAL: //JSON
  1513. if(isClass)
  1514. {
  1515. Error(_SC("unexpected string literal in class declaration"));
  1516. }
  1517. case TK_IDENTIFIER: {//JSON
  1518. obj_id = GetTokenObject(_token);
  1519. SQInteger next_token = _SC('=');
  1520. if(isClass)
  1521. {
  1522. CheckClassMemberExists(member_names, obj_id, false);
  1523. addClassMember = 0;
  1524. switch(_token)
  1525. {
  1526. case _SC('('): //C/C++ style function declaration
  1527. Lex();
  1528. if(class_name)
  1529. {
  1530. //printf("ClassMember %d : %s : %s\n", (int)cppDestructor, _stringval(*class_name), _stringval(obj_id));
  1531. if(memcmp(_stringval(*class_name), _stringval(obj_id), _string(*class_name)->_len) == 0)
  1532. {
  1533. //C++ style constructor/destructor declaration
  1534. obj_id = _fs->CreateString(cppDestructor ? _SC("destructor") : _SC("constructor"));
  1535. cppDestructor = false;
  1536. }
  1537. }
  1538. AddClassMemberExists(member_names, obj_id);
  1539. goto function_params_decl;
  1540. break;
  1541. case _SC(':'): //typescript field with type annotation
  1542. if(membertypename)
  1543. {
  1544. Error(_SC("member type already declared before %s"), _stringval(obj_id));
  1545. }
  1546. Lex();
  1547. type_name = ExpectTypeToken();
  1548. addClassMember = 1;
  1549. break;
  1550. case _SC(','):
  1551. ++addClassMember;
  1552. case _SC(';'): //member variable declaration without explicit initialization
  1553. Lex();
  1554. ++addClassMember;
  1555. break;
  1556. case TK_IDENTIFIER: //if 2 identifier found the first should be a type
  1557. if(CheckNameIsType(obj_id)) //Struct/Class/Typedef names
  1558. {
  1559. membertypename = _stringval(obj_id);
  1560. goto member_has_type;
  1561. }
  1562. }
  1563. if(addClassMember)
  1564. {
  1565. AddClassMemberExists(member_names, obj_id);
  1566. _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(obj_id));
  1567. _fs->AddInstruction(_OP_LOADNULLS, _fs->PushTarget(), 1);
  1568. break;
  1569. }
  1570. }
  1571. if(_token == _SC(':')){
  1572. next_token = _token;
  1573. }
  1574. Expect(next_token);
  1575. _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(obj_id));
  1576. Expression();
  1577. break;
  1578. }
  1579. CASE_TK_LOCAL_TYPES: //class member variables;
  1580. case TK_LOCAL:
  1581. if(isClass)
  1582. {
  1583. membertypename = _lex.GetTokenName(_token);
  1584. Lex();
  1585. goto member_has_type;
  1586. }
  1587. case _SC('~'): //C++ style destructor declaration
  1588. if(isClass)
  1589. {
  1590. cppDestructor = true;
  1591. Lex();
  1592. goto member_has_type;
  1593. continue;
  1594. }
  1595. case TK_CONSTEXPR:
  1596. if(isClass)
  1597. {
  1598. Lex();
  1599. continue;
  1600. }
  1601. //else fallthrough
  1602. default :
  1603. ErrorIfNotToken(TK_IDENTIFIER);
  1604. }
  1605. if(_token == separator) Lex();//optional comma/semicolon
  1606. nkeys++;
  1607. SQInteger val = _fs->PopTarget();
  1608. SQInteger key = _fs->PopTarget();
  1609. SQInteger attrs = hasattrs ? _fs->PopTarget():-1;
  1610. assert((hasattrs && (attrs == key-1)) || !hasattrs);
  1611. (void)attrs; // UNUSED
  1612. unsigned char flags = (hasattrs?NEW_SLOT_ATTRIBUTES_FLAG:0)|(isstatic?NEW_SLOT_STATIC_FLAG:0);
  1613. SQInteger table = _fs->TopTarget(); //<<BECAUSE OF THIS NO COMMON EMIT FUNC IS POSSIBLE
  1614. if(isClass) {
  1615. _fs->AddInstruction(_OP_NEWSLOTA, flags, table, key, val); //this for classes only as it invokes _newmember
  1616. if(addClassMember > 1)
  1617. {
  1618. addClassMember = 0;
  1619. goto member_has_type;
  1620. }
  1621. }
  1622. else {
  1623. _fs->AddInstruction(_OP_NEWSLOT, 0xFF, table, key, val);
  1624. }
  1625. }
  1626. if(separator == _SC(',')) //hack recognizes a table from the separator
  1627. _fs->SetIntructionParam(tpos, 1, nkeys);
  1628. Lex();
  1629. }
  1630. void ExternDeclStatement()
  1631. {
  1632. _is_parsing_extern = true;
  1633. Lex();
  1634. LocalDeclStatement();
  1635. _is_parsing_extern = false;
  1636. }
  1637. #define CHECK_REF_DECLARATION(tk) if(tk == _SC('&')){is_reference_declaration = true;Lex();}
  1638. void LocalDeclStatement()
  1639. {
  1640. SQObject varname;
  1641. //SQChar *param_type_name;
  1642. bool is_void_declaration = _token == TK_VOID;
  1643. bool is_const_declaration = _token == TK_CONST;
  1644. bool is_reference_declaration = false;
  1645. //bool is_instance_declaration = _token == TK_IDENTIFIER;
  1646. SQInteger declType = _token;
  1647. Lex();
  1648. if( _token == TK_FUNCTION) {
  1649. Lex();
  1650. varname = Expect(TK_IDENTIFIER);
  1651. CheckLocalNameScope(varname, _scope.nested);
  1652. Expect(_SC('('));
  1653. function_params_decl:
  1654. #if 1 //doing this way works but prevents garbage collection when doing multiple reloads on the same vm
  1655. //the following is an attempt to allow local declared functions be called recursivelly
  1656. SQInteger old_pos = _fs->GetCurrentPos(); //save current instructions position
  1657. _fs->PushLocalVariable(varname, _scope.nested, _VAR_CLOSURE); //add function name to find it as outer var if needed
  1658. //-1 to compensate default parameters when relocating
  1659. CreateFunction(varname, eFunctionType_local, -1);
  1660. if(_is_parsing_extern) {
  1661. if(_token == _SC(';')) //to parse thinscript
  1662. {
  1663. //Expect(_SC(';'));
  1664. CheckExternName(varname, true);
  1665. return;
  1666. }
  1667. else _is_parsing_extern = false;
  1668. }
  1669. _fs->AddInstruction(_OP_CLOSURE, _fs->PushTarget(), _fs->_functions.size() - 1, 0);
  1670. //rellocate any stack operation (default parameters & _OP_Closure)
  1671. for(int i=old_pos+1, curr_pos = _fs->GetCurrentPos(); i <= curr_pos; ++i){
  1672. SQInstruction & inst = _fs->GetInstruction(i);
  1673. _fs->SetIntructionParam(i, 0, inst._arg0 -1);
  1674. }
  1675. _fs->PopTarget();
  1676. #else
  1677. CreateFunction(varname,eFunctionType::local);
  1678. _fs->AddInstruction(_OP_CLOSURE, _fs->PushTarget(), _fs->_functions.size() - 1, 0);
  1679. _fs->PopTarget();
  1680. _fs->PushLocalVariable(varname, _scope.nested, _VAR_CLOSURE);
  1681. #endif
  1682. return;
  1683. }
  1684. do {
  1685. //param_type_name = NULL;
  1686. CHECK_REF_DECLARATION(_token);
  1687. varname = Expect(TK_IDENTIFIER);
  1688. if(CheckNameIsType(varname))
  1689. {
  1690. //ignore custom types for now
  1691. //param_type_name = _stringval(varname);
  1692. declType = _VAR_ANY;
  1693. CHECK_REF_DECLARATION(_token);
  1694. varname = Expect(TK_IDENTIFIER);
  1695. }
  1696. CheckLocalNameScope(varname, _scope.nested);
  1697. if(_token == _SC('(')) {
  1698. //C/C++ style function declaration
  1699. Lex();
  1700. goto function_params_decl;
  1701. }
  1702. else if(_token == _SC(':')) {
  1703. //type specifier like typescript
  1704. Lex();
  1705. ExpectTypeToken(); //ignore for now
  1706. }
  1707. if(is_void_declaration)
  1708. {
  1709. Error(_SC("void type is invalid here"));
  1710. }
  1711. else if(_token == _SC('=')) {
  1712. if(_is_parsing_extern)
  1713. {
  1714. Error(_SC("can not make assignment in external declarations"));
  1715. }
  1716. Lex(); Expression();
  1717. SQInteger src = _fs->PopTarget();
  1718. SQInteger dest = _fs->PushTarget();
  1719. if(dest != src) _fs->AddInstruction(_OP_MOVE, dest, src);
  1720. declType = _VAR_ANY;
  1721. }
  1722. else if(is_const_declaration || is_reference_declaration)
  1723. Error(_SC("const/reference '%s' need an initializer"), _stringval(varname));
  1724. else {
  1725. //check_param_type:
  1726. if(!_is_parsing_extern) {
  1727. SQInteger dest = _fs->PushTarget();
  1728. switch(declType){
  1729. CASE_TK_LOCAL_CHAR_TYPES:
  1730. _fs->AddInstruction(_OP_LOADNULLS, dest,1);
  1731. declType = _VAR_STRING;
  1732. break;
  1733. case TK_LOCAL_BOOL_T:
  1734. //default value false
  1735. _fs->AddInstruction(_OP_LOADBOOL, dest,0);
  1736. declType = _VAR_BOOL;
  1737. break;
  1738. case TK_LOCAL_TABLE_T:
  1739. _fs->AddInstruction(_OP_LOADNULLS, dest,1);
  1740. declType = _VAR_TABLE;
  1741. break;
  1742. case TK_LOCAL_ARRAY_T:
  1743. _fs->AddInstruction(_OP_LOADNULLS, dest,1);
  1744. declType = _VAR_ARRAY;
  1745. break;
  1746. CASE_TK_LOCAL_INT_TYPES:
  1747. //default value 0
  1748. _fs->AddInstruction(_OP_LOADINT, dest,0);
  1749. declType = _VAR_INTEGER;
  1750. break;
  1751. CASE_TK_LOCAL_FLOAT_TYPES:
  1752. case TK_LOCAL_NUMBER_T: //start numbers as floats
  1753. //default value 0.0
  1754. //_OP_LOADFLOAT is only valid when SQFloat size == SQInt32 size
  1755. _fs->AddInstruction(_OP_LOADINT, dest,0);
  1756. declType = _VAR_FLOAT;
  1757. break;
  1758. //case TK_LOCAL:
  1759. default:
  1760. //default value null
  1761. _fs->AddInstruction(_OP_LOADNULLS, dest,1);
  1762. declType = _VAR_ANY;
  1763. }
  1764. }
  1765. }
  1766. if(_is_parsing_extern) {
  1767. if(CheckExternName(varname, true))
  1768. {
  1769. Error(_SC("extern %s already declared"), varname);
  1770. }
  1771. }
  1772. else {
  1773. _fs->PopTarget();
  1774. _fs->PushLocalVariable(varname, _scope.nested, (is_const_declaration ? _VAR_CONST : declType)
  1775. | (is_reference_declaration ? _VAR_REFERENCE : 0));
  1776. }
  1777. if(_token == _SC(',')) Lex(); else break;
  1778. } while(1);
  1779. }
  1780. void IfBlock()
  1781. {
  1782. if (_token == _SC('{'))
  1783. {
  1784. BEGIN_SCOPE();
  1785. Lex();
  1786. Statements();
  1787. Expect(_SC('}'));
  1788. if (true) {
  1789. END_SCOPE();
  1790. }
  1791. else {
  1792. END_SCOPE_NO_CLOSE();
  1793. }
  1794. }
  1795. else {
  1796. //BEGIN_SCOPE();
  1797. Statement();
  1798. if (_lex.data->prevtoken != _SC('}') && _lex.data->prevtoken != _SC(';')) OptionalSemicolon();
  1799. //END_SCOPE();
  1800. }
  1801. }
  1802. void IfStatement()
  1803. {
  1804. SQInteger jmppos;
  1805. bool haselse = false;
  1806. Lex(); Expect(_SC('(')); CommaExpr(true); Expect(_SC(')'));
  1807. _fs->AddInstruction(_OP_JZ, _fs->PopTarget());
  1808. SQInteger jnepos = _fs->GetCurrentPos();
  1809. BEGIN_SCOPE();
  1810. //there is a situation where the if statement has a statement enclosed by {}
  1811. //and after the closing "}" there is no newline or semicolom
  1812. //it's a valid construction but the compiler was complaining about it
  1813. //for now added "&& _token != TK_IDENTIFIER" to the check after "Statement()" call
  1814. /*
  1815. local color = "blue";
  1816. if(color == "yellow"){
  1817. print(color);
  1818. } print("Waht color is it ?");
  1819. */
  1820. Statement();
  1821. //
  1822. if(_token != _SC('}') && _token != TK_ELSE && _token != TK_IDENTIFIER) OptionalSemicolon();
  1823. END_SCOPE();
  1824. SQInteger endifblock = _fs->GetCurrentPos();
  1825. if(_token == TK_ELSE){
  1826. haselse = true;
  1827. BEGIN_SCOPE();
  1828. _fs->AddInstruction(_OP_JMP);
  1829. jmppos = _fs->GetCurrentPos();
  1830. Lex();
  1831. Statement(); if(_token != TK_IDENTIFIER) OptionalSemicolon();
  1832. END_SCOPE();
  1833. _fs->SetIntructionParam(jmppos, 1, _fs->GetCurrentPos() - jmppos);
  1834. }
  1835. _fs->SetIntructionParam(jnepos, 1, endifblock - jnepos + (haselse?1:0));
  1836. }
  1837. void WhileStatement()
  1838. {
  1839. SQInteger jzpos, jmppos;
  1840. jmppos = _fs->GetCurrentPos();
  1841. Lex(); Expect(_SC('(')); CommaExpr(true); Expect(_SC(')'));
  1842. BEGIN_BREAKBLE_BLOCK();
  1843. _fs->AddInstruction(_OP_JZ, _fs->PopTarget());
  1844. jzpos = _fs->GetCurrentPos();
  1845. BEGIN_SCOPE();
  1846. Statement();
  1847. END_SCOPE();
  1848. _fs->AddInstruction(_OP_JMP, 0, jmppos - _fs->GetCurrentPos() - 1);
  1849. _fs->SetIntructionParam(jzpos, 1, _fs->GetCurrentPos() - jzpos);
  1850. END_BREAKBLE_BLOCK(jmppos);
  1851. }
  1852. void DoWhileStatement()
  1853. {
  1854. Lex();
  1855. SQInteger jmptrg = _fs->GetCurrentPos();
  1856. BEGIN_BREAKBLE_BLOCK()
  1857. BEGIN_SCOPE();
  1858. Statement();
  1859. END_SCOPE();
  1860. //fix proposed by frosch to correct line number info in stack dumps
  1861. _fs->AddLineInfos(_lex.data->currentline, _lineinfo, true);
  1862. Expect(TK_WHILE);
  1863. SQInteger continuetrg = _fs->GetCurrentPos();
  1864. Expect(_SC('(')); CommaExpr(true); Expect(_SC(')'));
  1865. _fs->AddInstruction(_OP_JZ, _fs->PopTarget(), 1);
  1866. _fs->AddInstruction(_OP_JMP, 0, jmptrg - _fs->GetCurrentPos() - 1);
  1867. END_BREAKBLE_BLOCK(continuetrg);
  1868. }
  1869. void ForStatement()
  1870. {
  1871. Lex();
  1872. BEGIN_SCOPE();
  1873. Expect(_SC('('));
  1874. switch(_token){
  1875. case TK_IDENTIFIER:{
  1876. SQObject id = _fs->CreateString(_lex.data->svalue);
  1877. if(!CheckTypeName(id)) goto lbl_commaexpr;//not a C/C++ type declaration;
  1878. }
  1879. //ignore custom types for now
  1880. //fallthrough as local declaration
  1881. CASE_TK_LOCAL_TYPES:
  1882. case TK_LOCAL:
  1883. LocalDeclStatement();
  1884. break;
  1885. default:
  1886. lbl_commaexpr:
  1887. if(_token != _SC(';')){
  1888. CommaExpr();
  1889. _fs->PopTarget();
  1890. }
  1891. }
  1892. Expect(_SC(';'));
  1893. _fs->SnoozeOpt();
  1894. SQInteger jmppos = _fs->GetCurrentPos();
  1895. SQInteger jzpos = -1;
  1896. if(_token != _SC(';')) { CommaExpr(); _fs->AddInstruction(_OP_JZ, _fs->PopTarget()); jzpos = _fs->GetCurrentPos(); }
  1897. Expect(_SC(';'));
  1898. _fs->SnoozeOpt();
  1899. SQInteger expstart = _fs->GetCurrentPos() + 1;
  1900. if(_token != _SC(')')) {
  1901. CommaExpr();
  1902. _fs->PopTarget();
  1903. }
  1904. Expect(_SC(')'));
  1905. _fs->SnoozeOpt();
  1906. SQInteger expend = _fs->GetCurrentPos();
  1907. SQInteger expsize = (expend - expstart) + 1;
  1908. SQInstructionVec exp;
  1909. if(expsize > 0) {
  1910. for(SQInteger i = 0; i < expsize; i++)
  1911. exp.push_back(_fs->GetInstruction(expstart + i));
  1912. _fs->PopInstructions(expsize);
  1913. }
  1914. BEGIN_BREAKBLE_BLOCK()
  1915. Statement();
  1916. SQInteger continuetrg = _fs->GetCurrentPos();
  1917. if(expsize > 0) {
  1918. for(SQInteger i = 0; i < expsize; i++)
  1919. _fs->AddInstruction(exp[i]);
  1920. }
  1921. _fs->AddInstruction(_OP_JMP, 0, jmppos - _fs->GetCurrentPos() - 1, 0);
  1922. if(jzpos> 0) _fs->SetIntructionParam(jzpos, 1, _fs->GetCurrentPos() - jzpos);
  1923. END_SCOPE();
  1924. END_BREAKBLE_BLOCK(continuetrg);
  1925. }
  1926. void ForEachStatement()
  1927. {
  1928. SQObject idxname, valname;
  1929. Lex(); Expect(_SC('(')); valname = Expect(TK_IDENTIFIER);
  1930. if(_token == _SC(',')) {
  1931. idxname = valname;
  1932. Lex(); valname = Expect(TK_IDENTIFIER);
  1933. }
  1934. else{
  1935. idxname = _fs->CreateString(_SC("@INDEX@"));
  1936. }
  1937. Expect(TK_IN);
  1938. //save the stack size
  1939. BEGIN_SCOPE();
  1940. //put the table in the stack(evaluate the table expression)
  1941. Expression(); Expect(_SC(')'));
  1942. SQInteger container = _fs->TopTarget();
  1943. //push the index local var
  1944. SQInteger indexpos = _fs->PushLocalVariable(idxname, _scope.nested);
  1945. _fs->AddInstruction(_OP_LOADNULLS, indexpos,1);
  1946. //push the value local var
  1947. SQInteger valuepos = _fs->PushLocalVariable(valname, _scope.nested);
  1948. _fs->AddInstruction(_OP_LOADNULLS, valuepos,1);
  1949. //push reference index
  1950. SQInteger itrpos = _fs->PushLocalVariable(_fs->CreateString(_SC("@ITERATOR@")), _scope.nested); //use invalid id to make it inaccessible
  1951. _fs->AddInstruction(_OP_LOADNULLS, itrpos,1);
  1952. SQInteger jmppos = _fs->GetCurrentPos();
  1953. _fs->AddInstruction(_OP_FOREACH, container, 0, indexpos);
  1954. SQInteger foreachpos = _fs->GetCurrentPos();
  1955. _fs->AddInstruction(_OP_POSTFOREACH, container, 0, indexpos);
  1956. //generate the statement code
  1957. BEGIN_BREAKBLE_BLOCK()
  1958. Statement();
  1959. _fs->AddInstruction(_OP_JMP, 0, jmppos - _fs->GetCurrentPos() - 1);
  1960. _fs->SetIntructionParam(foreachpos, 1, _fs->GetCurrentPos() - foreachpos);
  1961. _fs->SetIntructionParam(foreachpos + 1, 1, _fs->GetCurrentPos() - foreachpos);
  1962. END_BREAKBLE_BLOCK(foreachpos - 1);
  1963. //restore the local variable stack(remove index,val and ref idx)
  1964. _fs->PopTarget();
  1965. END_SCOPE();
  1966. }
  1967. void SwitchStatement()
  1968. {
  1969. Lex(); Expect(_SC('(')); CommaExpr(true); Expect(_SC(')'));
  1970. Expect(_SC('{'));
  1971. SQInteger expr = _fs->TopTarget();
  1972. bool bfirst = true;
  1973. SQInteger tonextcondjmp = -1;
  1974. SQInteger skipcondjmp = -1;
  1975. SQInteger __nbreaks__ = _fs->_unresolvedbreaks.size();
  1976. _fs->_breaktargets.push_back(0);
  1977. while(_token == TK_CASE) {
  1978. if(!bfirst) {
  1979. _fs->AddInstruction(_OP_JMP, 0, 0);
  1980. skipcondjmp = _fs->GetCurrentPos();
  1981. _fs->SetIntructionParam(tonextcondjmp, 1, _fs->GetCurrentPos() - tonextcondjmp);
  1982. }
  1983. //condition
  1984. Lex(); ExpressionScalar() /*Expression()*/; Expect(_SC(':'));
  1985. SQInteger trg = _fs->PopTarget();
  1986. SQInteger eqtarget = trg;
  1987. bool local = _fs->IsLocal(trg);
  1988. if(local) {
  1989. eqtarget = _fs->PushTarget(); //we need to allocate a extra reg
  1990. }
  1991. _fs->AddInstruction(_OP_EQ, eqtarget, trg, expr);
  1992. _fs->AddInstruction(_OP_JZ, eqtarget, 0);
  1993. if(local) {
  1994. _fs->PopTarget();
  1995. }
  1996. //end condition
  1997. if(skipcondjmp != -1) {
  1998. _fs->SetIntructionParam(skipcondjmp, 1, (_fs->GetCurrentPos() - skipcondjmp));
  1999. }
  2000. tonextcondjmp = _fs->GetCurrentPos();
  2001. BEGIN_SCOPE();
  2002. Statements();
  2003. END_SCOPE();
  2004. bfirst = false;
  2005. }
  2006. if(tonextcondjmp != -1)
  2007. _fs->SetIntructionParam(tonextcondjmp, 1, _fs->GetCurrentPos() - tonextcondjmp);
  2008. if(_token == TK_DEFAULT) {
  2009. Lex(); Expect(_SC(':'));
  2010. BEGIN_SCOPE();
  2011. Statements();
  2012. END_SCOPE();
  2013. }
  2014. Expect(_SC('}'));
  2015. _fs->PopTarget();
  2016. __nbreaks__ = _fs->_unresolvedbreaks.size() - __nbreaks__;
  2017. if(__nbreaks__ > 0)ResolveBreaks(_fs, __nbreaks__);
  2018. _fs->_breaktargets.pop_back();
  2019. }
  2020. void TemplateStatement()
  2021. {
  2022. Lex(); //ignore for now
  2023. Expect(_SC('<'));
  2024. int nest_count = 1;
  2025. while(_token > 0 && nest_count > 0){
  2026. Lex();
  2027. switch(_token)
  2028. {
  2029. case _SC('>'):
  2030. --nest_count;
  2031. break;
  2032. case _SC('<'):
  2033. nest_count++;
  2034. }
  2035. }
  2036. if(nest_count == 0) Lex(); //last '>' ignore for now
  2037. }
  2038. void FunctionStatement()
  2039. {
  2040. SQObject id;
  2041. Lex(); id = Expect(TK_IDENTIFIER);
  2042. _fs->PushTarget(0);
  2043. _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(id));
  2044. if(_token == TK_DOUBLE_COLON) Emit2ArgsOP(_OP_GET);
  2045. else CheckGlobalName(id, true);
  2046. while(_token == TK_DOUBLE_COLON) {
  2047. Lex();
  2048. id = Expect(TK_IDENTIFIER);
  2049. //todo check if class function already exists
  2050. _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(id));
  2051. if(_token == TK_DOUBLE_COLON) Emit2ArgsOP(_OP_GET);
  2052. }
  2053. Expect(_SC('('));
  2054. CreateFunction(id, eFunctionType_global);
  2055. _fs->AddInstruction(_OP_CLOSURE, _fs->PushTarget(), _fs->_functions.size() - 1, 0);
  2056. EmitDerefOp(_OP_NEWSLOT);
  2057. _fs->PopTarget();
  2058. }
  2059. void ClassStatement()
  2060. {
  2061. SQExpState es;
  2062. SQObjectPtr class_name;
  2063. Lex();
  2064. if(_token == TK_IDENTIFIER) {
  2065. class_name = SQString::Create(_ss(_vm), _lex.data->svalue);
  2066. CheckGlobalName(class_name, true);
  2067. CheckTypeName(class_name, true); //to allow C/C++ style instance declarations
  2068. }
  2069. es = _es;
  2070. _es.donot_get = true;
  2071. PrefixedExpr();
  2072. if(_token == _SC(';')) //class forward declaration
  2073. {
  2074. //return;
  2075. }
  2076. if(_es.etype == EXPR) {
  2077. Error(_SC("invalid class name"));
  2078. }
  2079. else if(_es.etype == OBJECT || _es.etype == BASE) {
  2080. ClassExp(&class_name);
  2081. EmitDerefOp(_OP_NEWSLOT);
  2082. _fs->PopTarget();
  2083. }
  2084. else {
  2085. Error(_SC("cannot create a class in a local with the syntax(class <local>)"));
  2086. }
  2087. _es = es;
  2088. }
  2089. SQObject ExpectScalar()
  2090. {
  2091. SQObject val;
  2092. val._type = OT_NULL; val._unVal.nInteger = 0; //shut up GCC 4.x
  2093. switch(_token) {
  2094. case TK_INTEGER:
  2095. val._type = OT_INTEGER;
  2096. val._unVal.nInteger = _lex.data->nvalue;
  2097. break;
  2098. case TK_FLOAT:
  2099. val._type = OT_FLOAT;
  2100. val._unVal.fFloat = _lex.data->fvalue;
  2101. break;
  2102. case TK_STRING_LITERAL:
  2103. val = _fs->CreateString(_lex.data->svalue,_lex.data->longstr.size()-1);
  2104. break;
  2105. case TK_TRUE:
  2106. case TK_FALSE:
  2107. val._type = OT_BOOL;
  2108. val._unVal.nInteger = _token == TK_TRUE ? 1 : 0;
  2109. break;
  2110. case '-':
  2111. Lex();
  2112. switch(_token)
  2113. {
  2114. case TK_INTEGER:
  2115. val._type = OT_INTEGER;
  2116. val._unVal.nInteger = -_lex.data->nvalue;
  2117. break;
  2118. case TK_FLOAT:
  2119. val._type = OT_FLOAT;
  2120. val._unVal.fFloat = -_lex.data->fvalue;
  2121. break;
  2122. default:
  2123. Error(_SC("scalar expected : integer, float"));
  2124. }
  2125. break;
  2126. default:
  2127. Error(_SC("scalar expected : integer, float or string"));
  2128. }
  2129. Lex();
  2130. return val;
  2131. }
  2132. SQInteger ExpressionScalar()
  2133. {
  2134. SQInteger tk_type = _token;
  2135. switch(_token) {
  2136. case TK_INTEGER:
  2137. EmitLoadConstInt(_lex.data->nvalue,-1);
  2138. break;
  2139. case TK_FLOAT:
  2140. EmitLoadConstFloat(_lex.data->fvalue,-1);
  2141. break;
  2142. case TK_STRING_LITERAL:
  2143. _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(_fs->CreateString(_lex.data->svalue,_lex.data->longstr.size()-1)));
  2144. break;
  2145. case TK_IDENTIFIER: {
  2146. SQObject id = _fs->CreateString(_lex.data->svalue);
  2147. Lex();
  2148. ExpressionConstant(id);
  2149. return tk_type;
  2150. }
  2151. break;
  2152. case '-':
  2153. Lex();
  2154. tk_type = _token;
  2155. switch(_token)
  2156. {
  2157. case TK_INTEGER:
  2158. EmitLoadConstInt(-_lex.data->nvalue,-1);
  2159. break;
  2160. case TK_FLOAT:
  2161. EmitLoadConstFloat(-_lex.data->fvalue,-1);
  2162. break;
  2163. default:
  2164. Error(_SC("scalar expected : integer, float"));
  2165. }
  2166. break;
  2167. default:
  2168. goto error;
  2169. }
  2170. Lex();
  2171. return tk_type;
  2172. error:
  2173. Error(_SC("constant or scalar expected : integer, float or string"));
  2174. return 0;
  2175. }
  2176. void EnumStatement()
  2177. {
  2178. Lex();
  2179. SQObject id = Expect(TK_IDENTIFIER);
  2180. Expect(_SC('{'));
  2181. //CheckLocalNameScope(id, _scope.nested);
  2182. SQObjectPtr strongid = id;
  2183. CheckLocalNameScope(id, _scope.nested);
  2184. SQObject table = _fs->CreateTable();
  2185. //_fs->AddInstruction(_OP_NEWOBJ, _fs->PushTarget(),0,NOT_TABLE);
  2186. SQInteger nval = 0;
  2187. while(_token != _SC('}')) {
  2188. SQObject key = Expect(TK_IDENTIFIER);
  2189. SQObject val;
  2190. if(_token == _SC('=')) {
  2191. Lex();
  2192. val = ExpectScalar();
  2193. }
  2194. else {
  2195. val._type = OT_INTEGER;
  2196. val._unVal.nInteger = nval++;
  2197. }
  2198. //SQInteger table = _fs->TopTarget(); //<<BECAUSE OF THIS NO COMMON EMIT FUNC IS POSSIBLE
  2199. //_fs->AddInstruction(_OP_NEWSLOT, 0xFF, table, key, val);
  2200. _table(table)->NewSlot(SQObjectPtr(key),SQObjectPtr(val));
  2201. if(_token == ',') Lex();
  2202. }
  2203. ConstsNewSlot(SQObjectPtr(strongid),SQObjectPtr(table));
  2204. strongid.Null();
  2205. Lex();
  2206. }
  2207. void TryCatchStatement()
  2208. {
  2209. SQObject exid;
  2210. Lex();
  2211. _fs->AddInstruction(_OP_PUSHTRAP,0,0);
  2212. _fs->_traps++;
  2213. if(_fs->_breaktargets.size()) _fs->_breaktargets.top()++;
  2214. if(_fs->_continuetargets.size()) _fs->_continuetargets.top()++;
  2215. SQInteger trappos = _fs->GetCurrentPos();
  2216. {
  2217. BEGIN_SCOPE();
  2218. Statement();
  2219. END_SCOPE();
  2220. }
  2221. _fs->_traps--;
  2222. _fs->AddInstruction(_OP_POPTRAP, 1, 0);
  2223. if(_fs->_breaktargets.size()) _fs->_breaktargets.top()--;
  2224. if(_fs->_continuetargets.size()) _fs->_continuetargets.top()--;
  2225. _fs->AddInstruction(_OP_JMP, 0, 0);
  2226. SQInteger jmppos = _fs->GetCurrentPos();
  2227. _fs->SetIntructionParam(trappos, 1, (_fs->GetCurrentPos() - trappos));
  2228. Expect(TK_CATCH); Expect(_SC('(')); exid = Expect(TK_IDENTIFIER); Expect(_SC(')'));
  2229. {
  2230. BEGIN_SCOPE();
  2231. SQInteger ex_target = _fs->PushLocalVariable(exid, _scope.nested);
  2232. _fs->SetIntructionParam(trappos, 0, ex_target);
  2233. Statement();
  2234. _fs->SetIntructionParams(jmppos, 0, (_fs->GetCurrentPos() - jmppos), 0);
  2235. END_SCOPE();
  2236. }
  2237. }
  2238. void FunctionExp(SQInteger ftype,bool lambda = false)
  2239. {
  2240. Lex(); Expect(_SC('('));
  2241. SQObjectPtr dummy;
  2242. CreateFunction(dummy, lambda ? eFunctionType_lambda : eFunctionType_anonymous);
  2243. _fs->AddInstruction(_OP_CLOSURE, _fs->PushTarget(), _fs->_functions.size() - 1, ftype == TK_FUNCTION?0:1);
  2244. }
  2245. void ClassExp(SQObjectPtr *class_name)
  2246. {
  2247. SQInteger base = -1;
  2248. SQInteger attrs = -1;
  2249. bool hasInheritance = false;
  2250. switch(_token)
  2251. {
  2252. case _SC(':'): //C++ style class derivation
  2253. case TK_EXTENDS: //squirrel style class derivation
  2254. Lex();
  2255. hasInheritance = true;
  2256. switch(_token)
  2257. {
  2258. case TK_PRIVATE:
  2259. case TK_PUBLIC:
  2260. Lex(); //ignore, accepted only to compile a subset of C++
  2261. }
  2262. }
  2263. if(hasInheritance) {
  2264. Expression();
  2265. base = _fs->TopTarget();
  2266. }
  2267. if(_token == TK_ATTR_OPEN) {
  2268. Lex();
  2269. _fs->AddInstruction(_OP_NEWOBJ, _fs->PushTarget(),0,NOT_TABLE);
  2270. ParseTableOrClass(_SC(','),TK_ATTR_CLOSE);
  2271. attrs = _fs->TopTarget();
  2272. }
  2273. Expect(_SC('{'));
  2274. if(attrs != -1) _fs->PopTarget();
  2275. if(base != -1) _fs->PopTarget();
  2276. _fs->AddInstruction(_OP_NEWOBJ, _fs->PushTarget(), base, attrs,NOT_CLASS);
  2277. ParseTableOrClass(_SC(';'),_SC('}'), class_name);
  2278. }
  2279. void DeleteExpr()
  2280. {
  2281. SQExpState es;
  2282. Lex();
  2283. es = _es;
  2284. _es.donot_get = true;
  2285. PrefixedExpr();
  2286. if(_es.etype==EXPR) Error(_SC("can't delete an expression"));
  2287. if(_es.etype==OBJECT || _es.etype==BASE) {
  2288. Emit2ArgsOP(_OP_DELETE);
  2289. }
  2290. else {
  2291. Error(_SC("cannot delete an (outer) local"));
  2292. }
  2293. _es = es;
  2294. }
  2295. void PrefixIncDec(SQInteger token)
  2296. {
  2297. SQExpState es;
  2298. SQInteger diff = (token==TK_MINUSMINUS) ? -1 : 1;
  2299. Lex();
  2300. es = _es;
  2301. _es.donot_get = true;
  2302. PrefixedExpr();
  2303. if(_es.etype==EXPR) {
  2304. Error(_SC("can't '++' or '--' an expression"));
  2305. }
  2306. else if(_es.etype==OBJECT || _es.etype==BASE) {
  2307. Emit2ArgsOP(_OP_INC, diff);
  2308. }
  2309. else if(_es.etype==LOCAL) {
  2310. SQInteger src = _fs->TopTarget();
  2311. _fs->AddInstruction(_OP_INCL, src, src, 0, diff);
  2312. }
  2313. else if(_es.etype==OUTER) {
  2314. SQInteger tmp = _fs->PushTarget();
  2315. _fs->AddInstruction(_OP_GETOUTER, tmp, _es.epos);
  2316. _fs->AddInstruction(_OP_INCL, tmp, tmp, 0, diff);
  2317. _fs->AddInstruction(_OP_SETOUTER, tmp, _es.epos, tmp);
  2318. }
  2319. _es = es;
  2320. }
  2321. void CreateFunction(SQObject &name, int ftype, int stack_offset=0)
  2322. {
  2323. SQFuncState *funcstate = _fs->PushChildState(_ss(_vm));
  2324. funcstate->_name = name;
  2325. SQObject paramname, type_name;
  2326. funcstate->AddParameter(_fs->CreateString(_SC("this")), _scope.nested+1);
  2327. funcstate->_sourcename = _sourcename;
  2328. SQInteger defparams = 0;
  2329. bool is_reference_declaration = 0;
  2330. const SQChar *param_type_name = 0;
  2331. bool isVoid = false;
  2332. while(_token!=_SC(')')) {
  2333. if(isVoid)
  2334. {
  2335. Error(_SC("void type is invalid here"));
  2336. }
  2337. is_reference_declaration = 0; //reset is_reference_declaration
  2338. param_type_name = 0; //rest for each parameter
  2339. if(_token == TK_VOID)
  2340. {
  2341. isVoid = true;
  2342. Lex();
  2343. continue;
  2344. }
  2345. if(_token == TK_VARPARAMS) {
  2346. if(defparams > 0) Error(_SC("function with default parameters cannot have variable number of parameters"));
  2347. funcstate->AddParameter(_fs->CreateString(_SC("vargv")), _scope.nested+1);
  2348. funcstate->_varparams = true;
  2349. Lex();
  2350. if(_token != _SC(')')) Error(_SC("expected ')'"));
  2351. break;
  2352. }
  2353. else {
  2354. if(_token == TK_CONST) Lex(); //C/C++ const cualifiers for now
  2355. switch(_token)
  2356. {
  2357. CASE_TK_LOCAL_TYPES: //accept C/C++ type parameter declarations
  2358. param_type_name = _lex.GetTokenName(_token);
  2359. Lex();
  2360. }
  2361. CHECK_REF_DECLARATION(_token);
  2362. paramname = Expect(TK_IDENTIFIER);
  2363. if(CheckNameIsType(paramname))
  2364. {
  2365. //ignore custom types for now
  2366. param_type_name = _stringval(paramname);
  2367. CHECK_REF_DECLARATION(_token);
  2368. paramname = Expect(TK_IDENTIFIER);
  2369. }
  2370. funcstate->AddParameter(paramname, _scope.nested+1, is_reference_declaration ? _VAR_REFERENCE : _VAR_ANY);
  2371. if(param_type_name)
  2372. {
  2373. funcstate->AddParameterTypeName(param_type_name);
  2374. }
  2375. if(_token == _SC('=')) {
  2376. if(is_reference_declaration) Error(_SC("parameter passed by reference can't have default value"));
  2377. Lex();
  2378. if(_token == _SC('[') || _token == _SC('{')) Error(_SC("default parameter with array/table values not supported"));
  2379. Expression();
  2380. //stack_offset to compensate for local functions been relocated to allow recursion
  2381. funcstate->AddDefaultParam(_fs->TopTarget()+stack_offset);
  2382. defparams++;
  2383. }
  2384. else if(_token == _SC(':')){
  2385. //param type specifier like typescript
  2386. if(param_type_name)
  2387. {
  2388. Error(_SC("parameter type already declared before %s"), _string(paramname));
  2389. }
  2390. Lex();
  2391. type_name = ExpectTypeToken();
  2392. funcstate->AddParameterTypeName(type_name);
  2393. //printf("%d %s\n", __LINE__, _stringval(type_name));
  2394. }
  2395. else {
  2396. if(defparams > 0) Error(_SC("expected '='"));
  2397. }
  2398. if(_token == _SC(',')) Lex();
  2399. else if(_token != _SC(')')) Error(_SC("expected ')' or ','"));
  2400. }
  2401. }
  2402. Expect(_SC(')'));
  2403. switch(_token)
  2404. {
  2405. case _SC(':'):{
  2406. //return type specifier like typescript
  2407. Lex();
  2408. type_name = ExpectTypeToken();
  2409. funcstate->_return_type = type_name;
  2410. //printf("%d %s\n", __LINE__, _stringval(type_name));
  2411. }
  2412. break;
  2413. case TK_CONST:
  2414. case TK_NOEXCEPT:
  2415. case TK_VOLATILE:
  2416. Lex(); //accept and ignore
  2417. break;
  2418. case TK_IDENTIFIER:
  2419. if(ftype == eFunctionType_member)
  2420. {
  2421. if( (scstrcmp(_lex.data->svalue, _SC("final")) == 0) ||
  2422. (scstrcmp(_lex.data->svalue, _SC("override")) == 0))
  2423. {
  2424. Lex(); //accept but ignore then
  2425. }
  2426. }
  2427. }
  2428. for(SQInteger n = 0; n < defparams; n++) {
  2429. _fs->PopTarget();
  2430. }
  2431. if(_is_parsing_extern) {
  2432. if(_token == _SC(';')) //to parse thinscript
  2433. {
  2434. _fs->PopChildState();
  2435. return;
  2436. }
  2437. else _is_parsing_extern = false;
  2438. }
  2439. SQFuncState *currchunk = _fs;
  2440. _fs = funcstate;
  2441. if(ftype == eFunctionType_lambda) {
  2442. Expression();
  2443. _fs->AddInstruction(_OP_RETURN, 1, _fs->PopTarget());}
  2444. else {
  2445. Statement(false);
  2446. }
  2447. ResolveGotos();
  2448. funcstate->AddLineInfos(_lex.data->prevtoken == _SC('\n')?_lex.data->lasttokenline:_lex.data->currentline, _lineinfo, true);
  2449. funcstate->AddInstruction(_OP_RETURN, -1);
  2450. funcstate->SetStackSize(0);
  2451. SQFunctionProto *func = funcstate->BuildProto();
  2452. #ifdef _DEBUG_DUMP
  2453. funcstate->Dump(func);
  2454. #endif
  2455. _fs = currchunk;
  2456. _fs->_functions.push_back(func);
  2457. _fs->PopChildState();
  2458. }
  2459. void AdjustGotoInstruction(const SQGotoLabelsInfo &goto_info, const SQGotoLabelsInfo &label)
  2460. {
  2461. SQInteger target_pos = label.pos;
  2462. SQInteger jump_pos = target_pos - goto_info.pos;
  2463. //set the jmp instruction
  2464. SQInteger poptraps = goto_info.traps - label.traps;
  2465. SQInt32 ioffset = 1;
  2466. while(poptraps)
  2467. {
  2468. SQInstruction &i_op_poptrap = _fs->GetInstruction(goto_info.pos-ioffset);
  2469. if(i_op_poptrap.op != _OP_NOP)
  2470. {
  2471. if(ioffset < 2)
  2472. {
  2473. ++ioffset;
  2474. continue;
  2475. }
  2476. Error(_SC("Compiler: This should not happen i_op_poptrap.op != _OP_NOP"));
  2477. }
  2478. //printf("poptraps for %s = %d : %d\n", _stringval(label.name), (int)poptraps, (int)i_op_poptrap._arg0);
  2479. i_op_poptrap.op = _OP_POPTRAP;
  2480. i_op_poptrap._arg0 = poptraps;
  2481. break;
  2482. }
  2483. _fs->SetIntructionParams(goto_info.pos, 0, jump_pos, 0);
  2484. }
  2485. void ResolveGotos()
  2486. {
  2487. //first we walk through the labels and when we come back
  2488. //nested labels we remove then because we only allow
  2489. //goto/joump out of blocks/scopes
  2490. SQInt16 last_nested = -1;
  2491. for(SQUnsignedInteger idxlabel = 0; idxlabel < _fs->_gototargets.size(); ++idxlabel) {
  2492. SQGotoLabelsInfo &label = _fs->_gototargets[idxlabel];
  2493. SQUnsignedInteger resolved = 0;
  2494. for(SQUnsignedInteger idxGoto = 0; idxGoto < _fs->_unresolvedgotos.size(); ++idxGoto) {
  2495. SQGotoLabelsInfo &goto_info = _fs->_unresolvedgotos[idxGoto];
  2496. if(_string(label.name) == _string(goto_info.name))
  2497. {
  2498. if(label.nested > goto_info.nested)
  2499. {
  2500. _lex.data->currentline = goto_info.line;
  2501. Error(_SC("Goto can not jump inside nested block/scope '%s'"), _stringval(goto_info.name));
  2502. }
  2503. AdjustGotoInstruction(goto_info, label);
  2504. //we dcrease idxGoto here to compensate the removal inside the for loop
  2505. _fs->_unresolvedgotos.remove(idxGoto--);
  2506. ++resolved;
  2507. }
  2508. }
  2509. if(!resolved)
  2510. {
  2511. //if a label isn't resolved we have an unused label
  2512. _lex.data->currentline = label.line;
  2513. Error(_SC("Label not resolved at this point '%s'"), _stringval(label.name));
  2514. }
  2515. if(label.nested < last_nested)
  2516. {
  2517. //remove any previous nested label to prevent goto/jump inside blocks/scopes
  2518. SQUnsignedInteger i = idxlabel - 1;
  2519. for(; i > 0; --i)
  2520. {
  2521. if(_fs->_gototargets[i].nested <= label.nested) break;
  2522. _fs->_gototargets.remove(i);
  2523. //we decrease the index idxlabel here due to remove before
  2524. //the current idxlabel
  2525. --idxlabel;
  2526. }
  2527. }
  2528. last_nested = label.nested;
  2529. }
  2530. if(_fs->_unresolvedgotos.size())
  2531. {
  2532. //if we still have any unresolved goto it's an error, let's show the first
  2533. SQGotoLabelsInfo &goto_info = _fs->_unresolvedgotos[0];
  2534. _lex.data->currentline = goto_info.line;
  2535. Error(_SC("Goto can not jump to undeclared label '%s'"), _stringval(goto_info.name));
  2536. }
  2537. }
  2538. void ResolveBreaks(SQFuncState *funcstate, SQInteger ntoresolve)
  2539. {
  2540. while(ntoresolve > 0) {
  2541. SQInteger pos = funcstate->_unresolvedbreaks.back();
  2542. funcstate->_unresolvedbreaks.pop_back();
  2543. //set the jmp instruction
  2544. funcstate->SetIntructionParams(pos, 0, funcstate->GetCurrentPos() - pos, 0);
  2545. ntoresolve--;
  2546. }
  2547. }
  2548. void ResolveContinues(SQFuncState *funcstate, SQInteger ntoresolve, SQInteger targetpos)
  2549. {
  2550. while(ntoresolve > 0) {
  2551. SQInteger pos = funcstate->_unresolvedcontinues.back();
  2552. funcstate->_unresolvedcontinues.pop_back();
  2553. //set the jmp instruction
  2554. funcstate->SetIntructionParams(pos, 0, targetpos - pos, 0);
  2555. ntoresolve--;
  2556. }
  2557. }
  2558. private:
  2559. SQInteger _token;
  2560. SQFuncState *_fs;
  2561. SQObjectPtr _sourcename;
  2562. SQLexer _lex;
  2563. bool _lineinfo;
  2564. bool _raiseerror;
  2565. bool _show_warnings;
  2566. bool _is_parsing_extern;
  2567. SQInteger _debugline;
  2568. SQInteger _debugop;
  2569. SQExpState _es;
  2570. SQScope _scope;
  2571. SQChar *_compilererror;
  2572. jmp_buf _errorjmp;
  2573. SQVM *_vm;
  2574. SQObjectPtrVec _scope_consts;
  2575. SQObjectPtr _globals;
  2576. SQObjectPtr _type_names; //to allow C/C++ style instance declarations
  2577. SQObjectPtr _extern_names; //to allow C/C++ style extern declarations
  2578. SQChar error_buf[MAX_COMPILER_ERROR_LEN];
  2579. SQInteger _max_nested_includes, _nested_includes_count;
  2580. };
  2581. bool Compile(SQVM *vm,SQLEXREADFUNC rg, SQUserPointer up, const SQChar *sourcename, SQObjectPtr &out,
  2582. bool raiseerror, bool lineinfo, bool show_warnings, SQInteger max_nested_includes)
  2583. {
  2584. SQCompiler p(vm, rg, up, sourcename, raiseerror, lineinfo, show_warnings, max_nested_includes);
  2585. return p.Compile(out);
  2586. }
  2587. #endif