fix cr/lfs and remove trailing whitespaces...
[supertux.git] / src / squirrel / squirrel / sqcompiler.cpp
1 /*
2         see copyright notice in squirrel.h
3 */
4 #include "sqpcheader.h"
5 #include <stdarg.h>
6 #include <setjmp.h>
7 #include "sqopcodes.h"
8 #include "sqstring.h"
9 #include "sqfuncproto.h"
10 #include "sqcompiler.h"
11 #include "sqfuncstate.h"
12 #include "sqlexer.h"
13 #include "sqvm.h"
14
15 #define DEREF_NO_DEREF  -1
16 #define DEREF_FIELD             -2
17
18 struct ExpState
19 {
20         ExpState()
21         {
22                 _deref = DEREF_NO_DEREF;
23                 _freevar = false;
24                 _class_or_delete = false;
25                 _funcarg = false;
26         }
27         bool _class_or_delete;
28         bool _funcarg;
29         bool _freevar;
30         SQInteger _deref;
31 };
32
33 typedef sqvector<ExpState> ExpStateVec;
34
35 #define _exst (_expstates.top())
36
37 #define BEGIN_BREAKBLE_BLOCK()  SQInteger __nbreaks__=_fs->_unresolvedbreaks.size(); \
38                                                         SQInteger __ncontinues__=_fs->_unresolvedcontinues.size(); \
39                                                         _fs->_breaktargets.push_back(0);_fs->_continuetargets.push_back(0);
40
41 #define END_BREAKBLE_BLOCK(continue_target) {__nbreaks__=_fs->_unresolvedbreaks.size()-__nbreaks__; \
42                                         __ncontinues__=_fs->_unresolvedcontinues.size()-__ncontinues__; \
43                                         if(__ncontinues__>0)ResolveContinues(_fs,__ncontinues__,continue_target); \
44                                         if(__nbreaks__>0)ResolveBreaks(_fs,__nbreaks__); \
45                                         _fs->_breaktargets.pop_back();_fs->_continuetargets.pop_back();}
46
47 class SQCompiler
48 {
49 public:
50         SQCompiler(SQVM *v, SQLEXREADFUNC rg, SQUserPointer up, const SQChar* sourcename, bool raiseerror, bool lineinfo)
51         {
52                 _vm=v;
53                 _lex.Init(_ss(v), rg, up,ThrowError,this);
54                 _sourcename = SQString::Create(_ss(v), sourcename);
55                 _lineinfo = lineinfo;_raiseerror = raiseerror;
56                 compilererror = NULL;
57         }
58         static void ThrowError(void *ud, const SQChar *s) {
59                 SQCompiler *c = (SQCompiler *)ud;
60                 c->Error(s);
61         }
62         void Error(const SQChar *s, ...)
63         {
64                 static SQChar temp[256];
65                 va_list vl;
66                 va_start(vl, s);
67                 scvsprintf(temp, s, vl);
68                 va_end(vl);
69                 compilererror = temp;
70                 longjmp(_errorjmp,1);
71         }
72         void Lex(){     _token = _lex.Lex();}
73         void PushExpState(){ _expstates.push_back(ExpState()); }
74         bool IsDerefToken(SQInteger tok)
75         {
76                 switch(tok){
77                 case _SC('='): case _SC('('): case TK_NEWSLOT:
78                 case TK_MODEQ: case TK_MULEQ: case TK_DIVEQ: case TK_MINUSEQ: case TK_PLUSEQ: case TK_PLUSPLUS: case TK_MINUSMINUS: return true;
79                 }
80                 return false;
81         }
82         ExpState PopExpState()
83         {
84                 ExpState ret = _expstates.top();
85                 _expstates.pop_back();
86                 return ret;
87         }
88         SQObject Expect(SQInteger tok)
89         {
90
91                 if(_token != tok) {
92                         if(_token == TK_CONSTRUCTOR && tok == TK_IDENTIFIER) {
93                                 //ret = SQString::Create(_ss(_vm),_SC("constructor"));
94                                 //do nothing
95                         }
96                         else {
97                                 const SQChar *etypename;
98                                 if(tok > 255) {
99                                         switch(tok)
100                                         {
101                                         case TK_IDENTIFIER:
102                                                 etypename = _SC("IDENTIFIER");
103                                                 break;
104                                         case TK_STRING_LITERAL:
105                                                 etypename = _SC("STRING_LITERAL");
106                                                 break;
107                                         case TK_INTEGER:
108                                                 etypename = _SC("INTEGER");
109                                                 break;
110                                         case TK_FLOAT:
111                                                 etypename = _SC("FLOAT");
112                                                 break;
113                                         default:
114                                                 etypename = _lex.Tok2Str(tok);
115                                         }
116                                         Error(_SC("expected '%s'"), etypename);
117                                 }
118                                 Error(_SC("expected '%c'"), tok);
119                         }
120                 }
121                 SQObjectPtr ret;
122                 switch(tok)
123                 {
124                 case TK_IDENTIFIER:
125                         ret = _fs->CreateString(_lex._svalue);
126                         break;
127                 case TK_STRING_LITERAL:
128                         ret = _fs->CreateString(_lex._svalue,_lex._longstr.size()-1);
129                         break;
130                 case TK_INTEGER:
131                         ret = SQObjectPtr(_lex._nvalue);
132                         break;
133                 case TK_FLOAT:
134                         ret = SQObjectPtr(_lex._fvalue);
135                         break;
136                 }
137                 Lex();
138                 return ret;
139         }
140         bool IsEndOfStatement() { return ((_lex._prevtoken == _SC('\n')) || (_token == SQUIRREL_EOB) || (_token == _SC('}')) || (_token == _SC(';'))); }
141         void OptionalSemicolon()
142         {
143                 if(_token == _SC(';')) { Lex(); return; }
144                 if(!IsEndOfStatement()) {
145                         Error(_SC("end of statement expected (; or lf)"));
146                 }
147         }
148         void MoveIfCurrentTargetIsLocal() {
149                 SQInteger trg = _fs->TopTarget();
150                 if(_fs->IsLocal(trg)) {
151                         trg = _fs->PopTarget(); //no pops the target and move it
152                         _fs->AddInstruction(_OP_MOVE, _fs->PushTarget(), trg);
153                 }
154         }
155         bool Compile(SQObjectPtr &o)
156         {
157                 _debugline = 1;
158                 _debugop = 0;
159
160                 SQFuncState funcstate(_ss(_vm), NULL,ThrowError,this);
161                 funcstate._name = SQString::Create(_ss(_vm), _SC("main"));
162                 _fs = &funcstate;
163                 _fs->AddParameter(_fs->CreateString(_SC("this")));
164                 _fs->_sourcename = _sourcename;
165                 SQInteger stacksize = _fs->GetStackSize();
166                 if(setjmp(_errorjmp) == 0) {
167                         Lex();
168                         while(_token > 0){
169                                 Statement();
170                                 if(_lex._prevtoken != _SC('}')) OptionalSemicolon();
171                         }
172                         CleanStack(stacksize);
173                         _fs->AddLineInfos(_lex._currentline, _lineinfo, true);
174                         _fs->AddInstruction(_OP_RETURN, 0xFF);
175                         _fs->SetStackSize(0);
176                         o =_fs->BuildProto();
177 #ifdef _DEBUG_DUMP
178                         _fs->Dump(_funcproto(o));
179 #endif
180                 }
181                 else {
182                         if(_raiseerror && _ss(_vm)->_compilererrorhandler) {
183                                 _ss(_vm)->_compilererrorhandler(_vm, compilererror, type(_sourcename) == OT_STRING?_stringval(_sourcename):_SC("unknown"),
184                                         _lex._currentline, _lex._currentcolumn);
185                         }
186                         _vm->_lasterror = SQString::Create(_ss(_vm), compilererror, -1);
187                         return false;
188                 }
189                 return true;
190         }
191         void Statements()
192         {
193                 while(_token != _SC('}') && _token != TK_DEFAULT && _token != TK_CASE) {
194                         Statement();
195                         if(_lex._prevtoken != _SC('}') && _lex._prevtoken != _SC(';')) OptionalSemicolon();
196                 }
197         }
198         void Statement()
199         {
200                 _fs->AddLineInfos(_lex._currentline, _lineinfo);
201                 switch(_token){
202                 case _SC(';'):  Lex();                                  break;
203                 case TK_IF:             IfStatement();                  break;
204                 case TK_WHILE:          WhileStatement();               break;
205                 case TK_DO:             DoWhileStatement();             break;
206                 case TK_FOR:            ForStatement();                 break;
207                 case TK_FOREACH:        ForEachStatement();             break;
208                 case TK_SWITCH: SwitchStatement();              break;
209                 case TK_LOCAL:          LocalDeclStatement();   break;
210                 case TK_RETURN:
211                 case TK_YIELD: {
212                         SQOpcode op;
213                         if(_token == TK_RETURN) {
214                                 op = _OP_RETURN;
215
216                         }
217                         else {
218                                 op = _OP_YIELD;
219                                 _fs->_bgenerator = true;
220                         }
221                         Lex();
222                         if(!IsEndOfStatement()) {
223                                 SQInteger retexp = _fs->GetCurrentPos()+1;
224                                 CommaExpr();
225                                 if(op == _OP_RETURN && _fs->_traps > 0)
226                                         _fs->AddInstruction(_OP_POPTRAP, _fs->_traps, 0);
227                                 _fs->_returnexp = retexp;
228                                 _fs->AddInstruction(op, 1, _fs->PopTarget());
229                         }
230                         else{
231                                 if(op == _OP_RETURN && _fs->_traps > 0)
232                                         _fs->AddInstruction(_OP_POPTRAP, _fs->_traps ,0);
233                                 _fs->_returnexp = -1;
234                                 _fs->AddInstruction(op, 0xFF);
235                         }
236                         break;}
237                 case TK_BREAK:
238                         if(_fs->_breaktargets.size() <= 0)Error(_SC("'break' has to be in a loop block"));
239                         if(_fs->_breaktargets.top() > 0){
240                                 _fs->AddInstruction(_OP_POPTRAP, _fs->_breaktargets.top(), 0);
241                         }
242                         _fs->AddInstruction(_OP_JMP, 0, -1234);
243                         _fs->_unresolvedbreaks.push_back(_fs->GetCurrentPos());
244                         Lex();
245                         break;
246                 case TK_CONTINUE:
247                         if(_fs->_continuetargets.size() <= 0)Error(_SC("'continue' has to be in a loop block"));
248                         if(_fs->_continuetargets.top() > 0) {
249                                 _fs->AddInstruction(_OP_POPTRAP, _fs->_continuetargets.top(), 0);
250                         }
251                         _fs->AddInstruction(_OP_JMP, 0, -1234);
252                         _fs->_unresolvedcontinues.push_back(_fs->GetCurrentPos());
253                         Lex();
254                         break;
255                 case TK_FUNCTION:
256                         FunctionStatement();
257                         break;
258                 case TK_CLASS:
259                         ClassStatement();
260                         break;
261                 case _SC('{'):{
262                                 SQInteger stacksize = _fs->GetStackSize();
263                                 Lex();
264                                 Statements();
265                                 Expect(_SC('}'));
266                                 _fs->SetStackSize(stacksize);
267                         }
268                         break;
269                 case TK_TRY:
270                         TryCatchStatement();
271                         break;
272                 case TK_THROW:
273                         Lex();
274                         CommaExpr();
275                         _fs->AddInstruction(_OP_THROW, _fs->PopTarget());
276                         break;
277                 default:
278                         CommaExpr();
279                         _fs->PopTarget();
280                         break;
281                 }
282                 _fs->SnoozeOpt();
283         }
284         void EmitDerefOp(SQOpcode op)
285         {
286                 SQInteger val = _fs->PopTarget();
287                 SQInteger key = _fs->PopTarget();
288                 SQInteger src = _fs->PopTarget();
289         _fs->AddInstruction(op,_fs->PushTarget(),src,key,val);
290         }
291         void Emit2ArgsOP(SQOpcode op, SQInteger p3 = 0)
292         {
293                 SQInteger p2 = _fs->PopTarget(); //src in OP_GET
294                 SQInteger p1 = _fs->PopTarget(); //key in OP_GET
295                 _fs->AddInstruction(op,_fs->PushTarget(), p1, p2, p3);
296         }
297         void EmitCompoundArith(SQInteger tok,bool deref)
298         {
299                 SQInteger oper;
300                 switch(tok){
301                 case TK_MINUSEQ: oper = '-'; break;
302                 case TK_PLUSEQ: oper = '+'; break;
303                 case TK_MULEQ: oper = '*'; break;
304                 case TK_DIVEQ: oper = '/'; break;
305                 case TK_MODEQ: oper = '%'; break;
306                 default: oper = 0; //shut up compiler
307                         assert(0); break;
308                 };
309                 if(deref) {
310                         SQInteger val = _fs->PopTarget();
311                         SQInteger key = _fs->PopTarget();
312                         SQInteger src = _fs->PopTarget();
313                         //mixes dest obj and source val in the arg1(hack?)
314                         _fs->AddInstruction(_OP_COMPARITH,_fs->PushTarget(),(src<<16)|val,key,oper);
315                 }
316                 else {
317                         Emit2ArgsOP(_OP_COMPARITHL, oper);
318                 }
319         }
320         void CommaExpr()
321         {
322                 for(Expression();_token == ',';_fs->PopTarget(), Lex(), CommaExpr());
323         }
324         ExpState Expression(bool funcarg = false)
325         {
326                 PushExpState();
327                 _exst._class_or_delete = false;
328                 _exst._funcarg = funcarg;
329                 LogicalOrExp();
330                 switch(_token)  {
331                 case _SC('='):
332                 case TK_NEWSLOT:
333                 case TK_MINUSEQ:
334                 case TK_PLUSEQ:
335                 case TK_MULEQ:
336                 case TK_DIVEQ:
337                 case TK_MODEQ:
338                 {
339                                 SQInteger op = _token;
340                                 SQInteger ds = _exst._deref;
341                                 bool freevar = _exst._freevar;
342                                 if(ds == DEREF_NO_DEREF) Error(_SC("can't assign expression"));
343                                 Lex(); Expression();
344
345                                 switch(op){
346                                 case TK_NEWSLOT:
347                                         if(freevar) Error(_SC("free variables cannot be modified"));
348                                         if(ds == DEREF_FIELD)
349                                                 EmitDerefOp(_OP_NEWSLOT);
350                                         else //if _derefstate != DEREF_NO_DEREF && DEREF_FIELD so is the index of a local
351                                                 Error(_SC("can't 'create' a local slot"));
352                                         break;
353                                 case _SC('='): //ASSIGN
354                                         if(freevar) Error(_SC("free variables cannot be modified"));
355                                         if(ds == DEREF_FIELD)
356                                                 EmitDerefOp(_OP_SET);
357                                         else {//if _derefstate != DEREF_NO_DEREF && DEREF_FIELD so is the index of a local
358                                                 SQInteger p2 = _fs->PopTarget(); //src in OP_GET
359                                                 SQInteger p1 = _fs->TopTarget(); //key in OP_GET
360                                                 _fs->AddInstruction(_OP_MOVE, p1, p2);
361                                         }
362                                         break;
363                                 case TK_MINUSEQ:
364                                 case TK_PLUSEQ:
365                                 case TK_MULEQ:
366                                 case TK_DIVEQ:
367                                 case TK_MODEQ:
368                                         EmitCompoundArith(op,ds == DEREF_FIELD);
369                                         break;
370                                 }
371                         }
372                         break;
373                 case _SC('?'): {
374                         Lex();
375                         _fs->AddInstruction(_OP_JZ, _fs->PopTarget());
376                         SQInteger jzpos = _fs->GetCurrentPos();
377                         SQInteger trg = _fs->PushTarget();
378                         Expression();
379                         SQInteger first_exp = _fs->PopTarget();
380                         if(trg != first_exp) _fs->AddInstruction(_OP_MOVE, trg, first_exp);
381                         SQInteger endfirstexp = _fs->GetCurrentPos();
382                         _fs->AddInstruction(_OP_JMP, 0, 0);
383                         Expect(_SC(':'));
384                         SQInteger jmppos = _fs->GetCurrentPos();
385                         Expression();
386                         SQInteger second_exp = _fs->PopTarget();
387                         if(trg != second_exp) _fs->AddInstruction(_OP_MOVE, trg, second_exp);
388                         _fs->SetIntructionParam(jmppos, 1, _fs->GetCurrentPos() - jmppos);
389                         _fs->SetIntructionParam(jzpos, 1, endfirstexp - jzpos + 1);
390                         _fs->SnoozeOpt();
391                         }
392                         break;
393                 }
394                 return PopExpState();
395         }
396         void BIN_EXP(SQOpcode op, void (SQCompiler::*f)(void),SQInteger op3 = 0)
397         {
398                 Lex(); (this->*f)();
399                 SQInteger op1 = _fs->PopTarget();SQInteger op2 = _fs->PopTarget();
400                 _fs->AddInstruction(op, _fs->PushTarget(), op1, op2, op3);
401         }
402         void LogicalOrExp()
403         {
404                 LogicalAndExp();
405                 for(;;) if(_token == TK_OR) {
406                         SQInteger first_exp = _fs->PopTarget();
407                         SQInteger trg = _fs->PushTarget();
408                         _fs->AddInstruction(_OP_OR, trg, 0, first_exp, 0);
409                         SQInteger jpos = _fs->GetCurrentPos();
410                         if(trg != first_exp) _fs->AddInstruction(_OP_MOVE, trg, first_exp);
411                         Lex(); LogicalOrExp();
412                         _fs->SnoozeOpt();
413                         SQInteger second_exp = _fs->PopTarget();
414                         if(trg != second_exp) _fs->AddInstruction(_OP_MOVE, trg, second_exp);
415                         _fs->SnoozeOpt();
416                         _fs->SetIntructionParam(jpos, 1, (_fs->GetCurrentPos() - jpos));
417                         break;
418                 }else return;
419         }
420         void LogicalAndExp()
421         {
422                 BitwiseOrExp();
423                 for(;;) switch(_token) {
424                 case TK_AND: {
425                         SQInteger first_exp = _fs->PopTarget();
426                         SQInteger trg = _fs->PushTarget();
427                         _fs->AddInstruction(_OP_AND, trg, 0, first_exp, 0);
428                         SQInteger jpos = _fs->GetCurrentPos();
429                         if(trg != first_exp) _fs->AddInstruction(_OP_MOVE, trg, first_exp);
430                         Lex(); LogicalAndExp();
431                         _fs->SnoozeOpt();
432                         SQInteger second_exp = _fs->PopTarget();
433                         if(trg != second_exp) _fs->AddInstruction(_OP_MOVE, trg, second_exp);
434                         _fs->SnoozeOpt();
435                         _fs->SetIntructionParam(jpos, 1, (_fs->GetCurrentPos() - jpos));
436                         break;
437                         }
438                 case TK_IN: BIN_EXP(_OP_EXISTS, &SQCompiler::BitwiseOrExp); break;
439                 case TK_INSTANCEOF: BIN_EXP(_OP_INSTANCEOF, &SQCompiler::BitwiseOrExp); break;
440                 default:
441                         return;
442                 }
443         }
444         void BitwiseOrExp()
445         {
446                 BitwiseXorExp();
447                 for(;;) if(_token == _SC('|'))
448                 {BIN_EXP(_OP_BITW, &SQCompiler::BitwiseXorExp,BW_OR);
449                 }else return;
450         }
451         void BitwiseXorExp()
452         {
453                 BitwiseAndExp();
454                 for(;;) if(_token == _SC('^'))
455                 {BIN_EXP(_OP_BITW, &SQCompiler::BitwiseAndExp,BW_XOR);
456                 }else return;
457         }
458         void BitwiseAndExp()
459         {
460                 CompExp();
461                 for(;;) if(_token == _SC('&'))
462                 {BIN_EXP(_OP_BITW, &SQCompiler::CompExp,BW_AND);
463                 }else return;
464         }
465         void CompExp()
466         {
467                 ShiftExp();
468                 for(;;) switch(_token) {
469                 case TK_EQ: BIN_EXP(_OP_EQ, &SQCompiler::ShiftExp); break;
470                 case _SC('>'): BIN_EXP(_OP_CMP, &SQCompiler::ShiftExp,CMP_G); break;
471                 case _SC('<'): BIN_EXP(_OP_CMP, &SQCompiler::ShiftExp,CMP_L); break;
472                 case TK_GE: BIN_EXP(_OP_CMP, &SQCompiler::ShiftExp,CMP_GE); break;
473                 case TK_LE: BIN_EXP(_OP_CMP, &SQCompiler::ShiftExp,CMP_LE); break;
474                 case TK_NE: BIN_EXP(_OP_NE, &SQCompiler::ShiftExp); break;
475                 default: return;
476                 }
477         }
478         void ShiftExp()
479         {
480                 PlusExp();
481                 for(;;) switch(_token) {
482                 case TK_USHIFTR: BIN_EXP(_OP_BITW, &SQCompiler::PlusExp,BW_USHIFTR); break;
483                 case TK_SHIFTL: BIN_EXP(_OP_BITW, &SQCompiler::PlusExp,BW_SHIFTL); break;
484                 case TK_SHIFTR: BIN_EXP(_OP_BITW, &SQCompiler::PlusExp,BW_SHIFTR); break;
485                 default: return;
486                 }
487         }
488         void PlusExp()
489         {
490                 MultExp();
491                 for(;;) switch(_token) {
492                 case _SC('+'): case _SC('-'):
493                         BIN_EXP(_OP_ARITH, &SQCompiler::MultExp,_token); break;
494                 default: return;
495                 }
496         }
497
498         void MultExp()
499         {
500                 PrefixedExpr();
501                 for(;;) switch(_token) {
502                 case _SC('*'): case _SC('/'): case _SC('%'):
503                         BIN_EXP(_OP_ARITH, &SQCompiler::PrefixedExpr,_token); break;
504                 default: return;
505                 }
506         }
507         //if 'pos' != -1 the previous variable is a local variable
508         void PrefixedExpr()
509         {
510                 SQInteger pos = Factor();
511                 for(;;) {
512                         switch(_token) {
513                         case _SC('.'): {
514                                 pos = -1;
515                                 Lex();
516                                 if(_token == TK_PARENT) {
517                                         Lex();
518                                         if(!NeedGet())
519                                                 Error(_SC("parent cannot be set"));
520                                         SQInteger src = _fs->PopTarget();
521                                         _fs->AddInstruction(_OP_GETPARENT, _fs->PushTarget(), src);
522                                 }
523                                 else {
524                                         _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(Expect(TK_IDENTIFIER)));
525                                         if(NeedGet()) Emit2ArgsOP(_OP_GET);
526                                 }
527                                 _exst._deref = DEREF_FIELD;
528                                 _exst._freevar = false;
529                                 }
530                                 break;
531                         case _SC('['):
532                                 if(_lex._prevtoken == _SC('\n')) Error(_SC("cannot brake deref/or comma needed after [exp]=exp slot declaration"));
533                                 Lex(); Expression(); Expect(_SC(']'));
534                                 pos = -1;
535                                 if(NeedGet()) Emit2ArgsOP(_OP_GET);
536                                 _exst._deref = DEREF_FIELD;
537                                 _exst._freevar = false;
538                                 break;
539                         case TK_MINUSMINUS:
540                         case TK_PLUSPLUS:
541                         if(_exst._deref != DEREF_NO_DEREF && !IsEndOfStatement()) {
542                                 SQInteger tok = _token; Lex();
543                                 if(pos < 0)
544                                         Emit2ArgsOP(_OP_PINC,tok == TK_MINUSMINUS?-1:1);
545                                 else {//if _derefstate != DEREF_NO_DEREF && DEREF_FIELD so is the index of a local
546                                         SQInteger src = _fs->PopTarget();
547                                         _fs->AddInstruction(_OP_PINCL, _fs->PushTarget(), src, 0, tok == TK_MINUSMINUS?-1:1);
548                                 }
549
550                         }
551                         return;
552                         break;
553                         case _SC('('):
554                                 {
555                                 if(_exst._deref != DEREF_NO_DEREF) {
556                                         if(pos<0) {
557                                                 SQInteger key = _fs->PopTarget(); //key
558                                                 SQInteger table = _fs->PopTarget(); //table etc...
559                                                 SQInteger closure = _fs->PushTarget();
560                                                 SQInteger ttarget = _fs->PushTarget();
561                                                 _fs->AddInstruction(_OP_PREPCALL, closure, key, table, ttarget);
562                                         }
563                                         else{
564                                                 _fs->AddInstruction(_OP_MOVE, _fs->PushTarget(), 0);
565                                         }
566                                 }
567                                 else
568                                         _fs->AddInstruction(_OP_MOVE, _fs->PushTarget(), 0);
569                                 _exst._deref = DEREF_NO_DEREF;
570                                 Lex();
571                                 FunctionCallArgs();
572                                  }
573                                 break;
574                         default: return;
575                         }
576                 }
577         }
578         SQInteger Factor()
579         {
580                 switch(_token)
581                 {
582                 case TK_STRING_LITERAL: {
583                                 //SQObjectPtr id(SQString::Create(_ss(_vm), _lex._svalue,_lex._longstr.size()-1));
584                                 _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(_fs->CreateString(_lex._svalue,_lex._longstr.size()-1)));
585                                 Lex();
586                         }
587                         break;
588                 case TK_VARGC: Lex(); _fs->AddInstruction(_OP_VARGC, _fs->PushTarget()); break;
589                 case TK_VARGV: { Lex();
590                         Expect(_SC('['));
591                         Expression();
592                         Expect(_SC(']'));
593                         SQInteger src = _fs->PopTarget();
594                         _fs->AddInstruction(_OP_GETVARGV, _fs->PushTarget(), src);
595                                            }
596                         break;
597                 case TK_IDENTIFIER:
598                 case TK_CONSTRUCTOR:
599                 case TK_THIS:{
600                         _exst._freevar = false;
601                         SQObject id;
602                                 switch(_token) {
603                                         case TK_IDENTIFIER: id = _fs->CreateString(_lex._svalue); break;
604                                         case TK_THIS: id = _fs->CreateString(_SC("this")); break;
605                                         case TK_CONSTRUCTOR: id = _fs->CreateString(_SC("constructor")); break;
606                                 }
607                                 SQInteger pos = -1;
608                                 Lex();
609                                 if((pos = _fs->GetLocalVariable(id)) == -1) {
610                                         //checks if is a free variable
611                                         if((pos = _fs->GetOuterVariable(id)) != -1) {
612                                                 _exst._deref = _fs->PushTarget();
613                                                 _fs->AddInstruction(_OP_LOADFREEVAR, _exst._deref ,pos);
614                                                 _exst._freevar = true;
615                                         } else {
616                                                 _fs->PushTarget(0);
617                                                 _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(id));
618                                                 if(NeedGet()) Emit2ArgsOP(_OP_GET);
619                                                 _exst._deref = DEREF_FIELD;
620                                         }
621                                 }
622                                 else{
623                                         _fs->PushTarget(pos);
624                                         _exst._deref = pos;
625                                 }
626                                 return _exst._deref;
627                         }
628                         break;
629                 case TK_PARENT: Lex();_fs->AddInstruction(_OP_GETPARENT, _fs->PushTarget(), 0); break;
630                 case TK_DOUBLE_COLON:  // "::"
631                         _fs->AddInstruction(_OP_LOADROOTTABLE, _fs->PushTarget());
632                         _exst._deref = DEREF_FIELD;
633                         _token = _SC('.'); //hack
634                         return -1;
635                         break;
636                 case TK_NULL:
637                         _fs->AddInstruction(_OP_LOADNULLS, _fs->PushTarget(),1);
638                         Lex();
639                         break;
640                 case TK_INTEGER: {
641                         if((_lex._nvalue & (~0x7FFFFFFF)) == 0) { //does it fit in 32 bits?
642                                 _fs->AddInstruction(_OP_LOADINT, _fs->PushTarget(),_lex._nvalue);
643                         }
644                         else {
645                                 _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetNumericConstant(_lex._nvalue));
646                         }
647                         Lex();
648                                                  }
649                         break;
650                 case TK_FLOAT:
651                         _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetNumericConstant(_lex._fvalue));
652                         Lex();
653                         break;
654                 case TK_TRUE: case TK_FALSE:
655                         _fs->AddInstruction(_OP_LOADBOOL, _fs->PushTarget(),_token == TK_TRUE?1:0);
656                         Lex();
657                         break;
658                 case _SC('['): {
659                                 _fs->AddInstruction(_OP_NEWARRAY, _fs->PushTarget());
660                                 SQInteger apos = _fs->GetCurrentPos(),key = 0;
661                                 Lex();
662                                 while(_token != _SC(']')) {
663                     Expression();
664                                         if(_token == _SC(',')) Lex();
665                                         SQInteger val = _fs->PopTarget();
666                                         SQInteger array = _fs->TopTarget();
667                                         _fs->AddInstruction(_OP_APPENDARRAY, array, val);
668                                         key++;
669                                 }
670                                 _fs->SetIntructionParam(apos, 1, key);
671                                 Lex();
672                         }
673                         break;
674                 case _SC('{'):{
675                         _fs->AddInstruction(_OP_NEWTABLE, _fs->PushTarget());
676                         Lex();ParseTableOrClass(_SC(','));
677                                  }
678                         break;
679                 case TK_FUNCTION: FunctionExp(_token);break;
680                 case TK_CLASS: Lex(); ClassExp();break;
681                 case _SC('-'): UnaryOP(_OP_NEG); break;
682                 case _SC('!'): UnaryOP(_OP_NOT); break;
683                 case _SC('~'): UnaryOP(_OP_BWNOT); break;
684                 case TK_TYPEOF : UnaryOP(_OP_TYPEOF); break;
685                 case TK_RESUME : UnaryOP(_OP_RESUME); break;
686                 case TK_CLONE : UnaryOP(_OP_CLONE); break;
687                 case TK_MINUSMINUS :
688                 case TK_PLUSPLUS :PrefixIncDec(_token); break;
689                 case TK_DELETE : DeleteExpr(); break;
690                 case TK_DELEGATE : DelegateExpr(); break;
691                 case _SC('('): Lex(); CommaExpr(); Expect(_SC(')'));
692                         break;
693                 default: Error(_SC("expression expected"));
694                 }
695                 return -1;
696         }
697         void UnaryOP(SQOpcode op)
698         {
699                 Lex(); PrefixedExpr();
700                 SQInteger src = _fs->PopTarget();
701                 _fs->AddInstruction(op, _fs->PushTarget(), src);
702         }
703         bool NeedGet()
704         {
705                 switch(_token) {
706                 case _SC('='): case _SC('('): case TK_NEWSLOT: case TK_PLUSPLUS: case TK_MINUSMINUS:
707                 case TK_PLUSEQ: case TK_MINUSEQ: case TK_MULEQ: case TK_DIVEQ: case TK_MODEQ:
708                         return false;
709                 }
710                 return (!_exst._class_or_delete) || (_exst._class_or_delete && (_token == _SC('.') || _token == _SC('[')));
711         }
712
713         void FunctionCallArgs()
714         {
715                 SQInteger nargs = 1;//this
716                  while(_token != _SC(')')) {
717                          Expression(true);
718                          MoveIfCurrentTargetIsLocal();
719                          nargs++;
720                          if(_token == _SC(',')){
721                                  Lex();
722                                  if(_token == ')') Error(_SC("expression expected, found ')'"));
723                          }
724                  }
725                  Lex();
726                  for(SQInteger i = 0; i < (nargs - 1); i++) _fs->PopTarget();
727                  SQInteger stackbase = _fs->PopTarget();
728                  SQInteger closure = _fs->PopTarget();
729          _fs->AddInstruction(_OP_CALL, _fs->PushTarget(), closure, stackbase, nargs);
730         }
731         void ParseTableOrClass(SQInteger separator,SQInteger terminator = '}')
732         {
733                 SQInteger tpos = _fs->GetCurrentPos(),nkeys = 0;
734
735                 while(_token != terminator) {
736                         bool hasattrs = false;
737                         bool isstatic = false;
738                         //check if is an attribute
739                         if(separator == ';') {
740                                 if(_token == TK_ATTR_OPEN) {
741                                         _fs->AddInstruction(_OP_NEWTABLE, _fs->PushTarget()); Lex();
742                                         ParseTableOrClass(',',TK_ATTR_CLOSE);
743                                         hasattrs = true;
744                                 }
745                                 if(_token == TK_STATIC) {
746                                         isstatic = true;
747                                         Lex();
748                                 }
749                         }
750                         switch(_token) {
751                                 case TK_FUNCTION:
752                                 case TK_CONSTRUCTOR:{
753                                         SQInteger tk = _token;
754                                         Lex();
755                                         SQObject id = tk == TK_FUNCTION ? Expect(TK_IDENTIFIER) : _fs->CreateString(_SC("constructor"));
756                                         Expect(_SC('('));
757                                         _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(id));
758                                         CreateFunction(id);
759                                         _fs->AddInstruction(_OP_CLOSURE, _fs->PushTarget(), _fs->_functions.size() - 1, 0);
760                                                                   }
761                                                                   break;
762                                 case _SC('['):
763                                         Lex(); CommaExpr(); Expect(_SC(']'));
764                                         Expect(_SC('=')); Expression();
765                                         break;
766                                 default :
767                                         _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(Expect(TK_IDENTIFIER)));
768                                         Expect(_SC('=')); Expression();
769                         }
770
771                         if(_token == separator) Lex();//optional comma/semicolon
772                         nkeys++;
773                         SQInteger val = _fs->PopTarget();
774                         SQInteger key = _fs->PopTarget();
775                         SQInteger attrs = hasattrs ? _fs->PopTarget():-1;
776                         assert(hasattrs && attrs == key-1 || !hasattrs);
777                         unsigned char flags = (hasattrs?NEW_SLOT_ATTRIBUTES_FLAG:0)|(isstatic?NEW_SLOT_STATIC_FLAG:0);
778                         SQInteger table = _fs->TopTarget(); //<<BECAUSE OF THIS NO COMMON EMIT FUNC IS POSSIBLE
779                         _fs->AddInstruction(_OP_NEWSLOTA, flags, table, key, val);
780                         //_fs->PopTarget();
781                 }
782                 if(separator == _SC(',')) //hack recognizes a table from the separator
783                         _fs->SetIntructionParam(tpos, 1, nkeys);
784                 Lex();
785         }
786         void LocalDeclStatement()
787         {
788                 SQObject varname;
789                 do {
790                         Lex(); varname = Expect(TK_IDENTIFIER);
791                         if(_token == _SC('=')) {
792                                 Lex(); Expression();
793                                 SQInteger src = _fs->PopTarget();
794                                 SQInteger dest = _fs->PushTarget();
795                                 if(dest != src) _fs->AddInstruction(_OP_MOVE, dest, src);
796                         }
797                         else{
798                                 _fs->AddInstruction(_OP_LOADNULLS, _fs->PushTarget(),1);
799                         }
800                         _fs->PopTarget();
801                         _fs->PushLocalVariable(varname);
802
803                 } while(_token == _SC(','));
804         }
805         void IfStatement()
806         {
807                 SQInteger jmppos;
808                 bool haselse = false;
809                 Lex(); Expect(_SC('(')); CommaExpr(); Expect(_SC(')'));
810                 _fs->AddInstruction(_OP_JZ, _fs->PopTarget());
811                 SQInteger jnepos = _fs->GetCurrentPos();
812                 SQInteger stacksize = _fs->GetStackSize();
813
814                 Statement();
815                 //
816                 if(_token != _SC('}') && _token != TK_ELSE) OptionalSemicolon();
817
818                 CleanStack(stacksize);
819                 SQInteger endifblock = _fs->GetCurrentPos();
820                 if(_token == TK_ELSE){
821                         haselse = true;
822                         stacksize = _fs->GetStackSize();
823                         _fs->AddInstruction(_OP_JMP);
824                         jmppos = _fs->GetCurrentPos();
825                         Lex();
826                         Statement(); OptionalSemicolon();
827                         CleanStack(stacksize);
828                         _fs->SetIntructionParam(jmppos, 1, _fs->GetCurrentPos() - jmppos);
829                 }
830                 _fs->SetIntructionParam(jnepos, 1, endifblock - jnepos + (haselse?1:0));
831         }
832         void WhileStatement()
833         {
834                 SQInteger jzpos, jmppos;
835                 SQInteger stacksize = _fs->GetStackSize();
836                 jmppos = _fs->GetCurrentPos();
837                 Lex(); Expect(_SC('(')); CommaExpr(); Expect(_SC(')'));
838
839                 BEGIN_BREAKBLE_BLOCK();
840                 _fs->AddInstruction(_OP_JZ, _fs->PopTarget());
841                 jzpos = _fs->GetCurrentPos();
842                 stacksize = _fs->GetStackSize();
843
844                 Statement();
845
846                 CleanStack(stacksize);
847                 _fs->AddInstruction(_OP_JMP, 0, jmppos - _fs->GetCurrentPos() - 1);
848                 _fs->SetIntructionParam(jzpos, 1, _fs->GetCurrentPos() - jzpos);
849
850                 END_BREAKBLE_BLOCK(jmppos);
851         }
852         void DoWhileStatement()
853         {
854                 Lex();
855                 SQInteger jzpos = _fs->GetCurrentPos();
856                 SQInteger stacksize = _fs->GetStackSize();
857                 BEGIN_BREAKBLE_BLOCK()
858                 Statement();
859                 CleanStack(stacksize);
860                 Expect(TK_WHILE);
861                 SQInteger continuetrg = _fs->GetCurrentPos();
862                 Expect(_SC('(')); CommaExpr(); Expect(_SC(')'));
863                 _fs->AddInstruction(_OP_JNZ, _fs->PopTarget(), jzpos - _fs->GetCurrentPos() - 1);
864                 END_BREAKBLE_BLOCK(continuetrg);
865         }
866         void ForStatement()
867         {
868                 Lex();
869                 SQInteger stacksize = _fs->GetStackSize();
870                 Expect(_SC('('));
871                 if(_token == TK_LOCAL) LocalDeclStatement();
872                 else if(_token != _SC(';')){
873                         CommaExpr();
874                         _fs->PopTarget();
875                 }
876                 Expect(_SC(';'));
877                 _fs->SnoozeOpt();
878                 SQInteger jmppos = _fs->GetCurrentPos();
879                 SQInteger jzpos = -1;
880                 if(_token != _SC(';')) { CommaExpr(); _fs->AddInstruction(_OP_JZ, _fs->PopTarget()); jzpos = _fs->GetCurrentPos(); }
881                 Expect(_SC(';'));
882                 _fs->SnoozeOpt();
883                 SQInteger expstart = _fs->GetCurrentPos() + 1;
884                 if(_token != _SC(')')) {
885                         CommaExpr();
886                         _fs->PopTarget();
887                 }
888                 Expect(_SC(')'));
889                 _fs->SnoozeOpt();
890                 SQInteger expend = _fs->GetCurrentPos();
891                 SQInteger expsize = (expend - expstart) + 1;
892                 SQInstructionVec exp;
893                 if(expsize > 0) {
894                         for(SQInteger i = 0; i < expsize; i++)
895                                 exp.push_back(_fs->GetInstruction(expstart + i));
896                         _fs->PopInstructions(expsize);
897                 }
898                 BEGIN_BREAKBLE_BLOCK()
899                 Statement();
900                 SQInteger continuetrg = _fs->GetCurrentPos();
901                 if(expsize > 0) {
902                         for(SQInteger i = 0; i < expsize; i++)
903                                 _fs->AddInstruction(exp[i]);
904                 }
905                 _fs->AddInstruction(_OP_JMP, 0, jmppos - _fs->GetCurrentPos() - 1, 0);
906                 if(jzpos>  0) _fs->SetIntructionParam(jzpos, 1, _fs->GetCurrentPos() - jzpos);
907                 CleanStack(stacksize);
908
909                 END_BREAKBLE_BLOCK(continuetrg);
910         }
911         void ForEachStatement()
912         {
913                 SQObject idxname, valname;
914                 Lex(); Expect(_SC('(')); valname = Expect(TK_IDENTIFIER);
915                 if(_token == _SC(',')) {
916                         idxname = valname;
917                         Lex(); valname = Expect(TK_IDENTIFIER);
918                 }
919                 else{
920                         idxname = _fs->CreateString(_SC("@INDEX@"));
921                 }
922                 Expect(TK_IN);
923
924                 //save the stack size
925                 SQInteger stacksize = _fs->GetStackSize();
926                 //put the table in the stack(evaluate the table expression)
927                 Expression(); Expect(_SC(')'));
928                 SQInteger container = _fs->TopTarget();
929                 //push the index local var
930                 SQInteger indexpos = _fs->PushLocalVariable(idxname);
931                 _fs->AddInstruction(_OP_LOADNULLS, indexpos,1);
932                 //push the value local var
933                 SQInteger valuepos = _fs->PushLocalVariable(valname);
934                 _fs->AddInstruction(_OP_LOADNULLS, valuepos,1);
935                 //push reference index
936                 SQInteger itrpos = _fs->PushLocalVariable(_fs->CreateString(_SC("@ITERATOR@"))); //use invalid id to make it inaccessible
937                 _fs->AddInstruction(_OP_LOADNULLS, itrpos,1);
938                 SQInteger jmppos = _fs->GetCurrentPos();
939                 _fs->AddInstruction(_OP_FOREACH, container, 0, indexpos);
940                 SQInteger foreachpos = _fs->GetCurrentPos();
941                 //generate the statement code
942                 BEGIN_BREAKBLE_BLOCK()
943                 Statement();
944                 _fs->AddInstruction(_OP_JMP, 0, jmppos - _fs->GetCurrentPos() - 1);
945                 _fs->SetIntructionParam(foreachpos, 1, _fs->GetCurrentPos() - foreachpos);
946                 //restore the local variable stack(remove index,val and ref idx)
947                 CleanStack(stacksize);
948                 END_BREAKBLE_BLOCK(foreachpos - 1);
949         }
950         void SwitchStatement()
951         {
952                 Lex(); Expect(_SC('(')); CommaExpr(); Expect(_SC(')'));
953                 Expect(_SC('{'));
954                 SQInteger expr = _fs->TopTarget();
955                 bool bfirst = true;
956                 SQInteger tonextcondjmp = -1;
957                 SQInteger skipcondjmp = -1;
958                 SQInteger __nbreaks__ = _fs->_unresolvedbreaks.size();
959                 _fs->_breaktargets.push_back(0);
960                 while(_token == TK_CASE) {
961                         //_fs->AddLineInfos(_lex._currentline, _lineinfo); think about this one
962                         if(!bfirst) {
963                                 _fs->AddInstruction(_OP_JMP, 0, 0);
964                                 skipcondjmp = _fs->GetCurrentPos();
965                                 _fs->SetIntructionParam(tonextcondjmp, 1, _fs->GetCurrentPos() - tonextcondjmp);
966                         }
967                         //condition
968                         Lex(); Expression(); Expect(_SC(':'));
969                         SQInteger trg = _fs->PopTarget();
970                         _fs->AddInstruction(_OP_EQ, trg, trg, expr);
971                         _fs->AddInstruction(_OP_JZ, trg, 0);
972                         //end condition
973                         if(skipcondjmp != -1) {
974                                 _fs->SetIntructionParam(skipcondjmp, 1, (_fs->GetCurrentPos() - skipcondjmp));
975                         }
976                         tonextcondjmp = _fs->GetCurrentPos();
977                         SQInteger stacksize = _fs->GetStackSize();
978                         Statements();
979                         _fs->SetStackSize(stacksize);
980                         bfirst = false;
981                 }
982                 if(tonextcondjmp != -1)
983                         _fs->SetIntructionParam(tonextcondjmp, 1, _fs->GetCurrentPos() - tonextcondjmp);
984                 if(_token == TK_DEFAULT) {
985                 //      _fs->AddLineInfos(_lex._currentline, _lineinfo);
986                         Lex(); Expect(_SC(':'));
987                         SQInteger stacksize = _fs->GetStackSize();
988                         Statements();
989                         _fs->SetStackSize(stacksize);
990                 }
991                 Expect(_SC('}'));
992                 _fs->PopTarget();
993                 __nbreaks__ = _fs->_unresolvedbreaks.size() - __nbreaks__;
994                 if(__nbreaks__ > 0)ResolveBreaks(_fs, __nbreaks__);
995                 _fs->_breaktargets.pop_back();
996
997         }
998         void FunctionStatement()
999         {
1000                 SQObject id;
1001                 Lex(); id = Expect(TK_IDENTIFIER);
1002                 _fs->PushTarget(0);
1003                 _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(id));
1004                 if(_token == TK_DOUBLE_COLON) Emit2ArgsOP(_OP_GET);
1005
1006                 while(_token == TK_DOUBLE_COLON) {
1007                         Lex();
1008                         id = Expect(TK_IDENTIFIER);
1009                         _fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(id));
1010                         if(_token == TK_DOUBLE_COLON) Emit2ArgsOP(_OP_GET);
1011                 }
1012                 Expect(_SC('('));
1013                 CreateFunction(id);
1014                 _fs->AddInstruction(_OP_CLOSURE, _fs->PushTarget(), _fs->_functions.size() - 1, 0);
1015                 EmitDerefOp(_OP_NEWSLOT);
1016                 _fs->PopTarget();
1017         }
1018         void ClassStatement()
1019         {
1020                 ExpState es;
1021                 Lex(); PushExpState();
1022                 _exst._class_or_delete = true;
1023                 _exst._funcarg = false;
1024                 PrefixedExpr();
1025                 es = PopExpState();
1026                 if(es._deref == DEREF_NO_DEREF) Error(_SC("invalid class name"));
1027                 if(es._deref == DEREF_FIELD) {
1028                         ClassExp();
1029                         EmitDerefOp(_OP_NEWSLOT);
1030                         _fs->PopTarget();
1031                 }
1032                 else Error(_SC("cannot create a class in a local with the syntax(class <local>)"));
1033         }
1034         void TryCatchStatement()
1035         {
1036                 SQObject exid;
1037                 Lex();
1038                 _fs->AddInstruction(_OP_PUSHTRAP,0,0);
1039                 _fs->_traps++;
1040                 if(_fs->_breaktargets.size()) _fs->_breaktargets.top()++;
1041                 if(_fs->_continuetargets.size()) _fs->_continuetargets.top()++;
1042                 SQInteger trappos = _fs->GetCurrentPos();
1043                 Statement();
1044                 _fs->_traps--;
1045                 _fs->AddInstruction(_OP_POPTRAP, 1, 0);
1046                 if(_fs->_breaktargets.size()) _fs->_breaktargets.top()--;
1047                 if(_fs->_continuetargets.size()) _fs->_continuetargets.top()--;
1048                 _fs->AddInstruction(_OP_JMP, 0, 0);
1049                 SQInteger jmppos = _fs->GetCurrentPos();
1050                 _fs->SetIntructionParam(trappos, 1, (_fs->GetCurrentPos() - trappos));
1051                 Expect(TK_CATCH); Expect(_SC('(')); exid = Expect(TK_IDENTIFIER); Expect(_SC(')'));
1052                 SQInteger stacksize = _fs->GetStackSize();
1053                 SQInteger ex_target = _fs->PushLocalVariable(exid);
1054                 _fs->SetIntructionParam(trappos, 0, ex_target);
1055                 Statement();
1056                 _fs->SetIntructionParams(jmppos, 0, (_fs->GetCurrentPos() - jmppos), 0);
1057                 CleanStack(stacksize);
1058         }
1059         void FunctionExp(SQInteger ftype)
1060         {
1061                 Lex(); Expect(_SC('('));
1062                 CreateFunction(_null_);
1063                 _fs->AddInstruction(_OP_CLOSURE, _fs->PushTarget(), _fs->_functions.size() - 1, ftype == TK_FUNCTION?0:1);
1064         }
1065         void ClassExp()
1066         {
1067                 SQInteger base = -1;
1068                 SQInteger attrs = -1;
1069                 if(_token == TK_EXTENDS) {
1070                         Lex(); Expression();
1071                         base = _fs->TopTarget();
1072                 }
1073                 if(_token == TK_ATTR_OPEN) {
1074                         Lex();
1075                         _fs->AddInstruction(_OP_NEWTABLE, _fs->PushTarget());
1076                         ParseTableOrClass(_SC(','),TK_ATTR_CLOSE);
1077                         attrs = _fs->TopTarget();
1078                 }
1079                 Expect(_SC('{'));
1080                 if(attrs != -1) _fs->PopTarget();
1081                 if(base != -1) _fs->PopTarget();
1082                 _fs->AddInstruction(_OP_CLASS, _fs->PushTarget(), base, attrs);
1083                 ParseTableOrClass(_SC(';'));
1084         }
1085         void DelegateExpr()
1086         {
1087                 Lex(); CommaExpr();
1088                 Expect(_SC(':'));
1089                 CommaExpr();
1090                 SQInteger table = _fs->PopTarget(), delegate = _fs->PopTarget();
1091                 _fs->AddInstruction(_OP_DELEGATE, _fs->PushTarget(), table, delegate);
1092         }
1093         void DeleteExpr()
1094         {
1095                 ExpState es;
1096                 Lex(); PushExpState();
1097                 _exst._class_or_delete = true;
1098                 _exst._funcarg = false;
1099                 PrefixedExpr();
1100                 es = PopExpState();
1101                 if(es._deref == DEREF_NO_DEREF) Error(_SC("can't delete an expression"));
1102                 if(es._deref == DEREF_FIELD) Emit2ArgsOP(_OP_DELETE);
1103                 else Error(_SC("cannot delete a local"));
1104         }
1105         void PrefixIncDec(SQInteger token)
1106         {
1107                 ExpState es;
1108                 Lex(); PushExpState();
1109                 _exst._class_or_delete = true;
1110                 _exst._funcarg = false;
1111                 PrefixedExpr();
1112                 es = PopExpState();
1113                 if(es._deref == DEREF_FIELD) Emit2ArgsOP(_OP_INC,token == TK_MINUSMINUS?-1:1);
1114                 else {
1115                         SQInteger src = _fs->PopTarget();
1116                         _fs->AddInstruction(_OP_INCL, _fs->PushTarget(), src, 0, token == TK_MINUSMINUS?-1:1);
1117                 }
1118         }
1119         void CreateFunction(SQObject &name)
1120         {
1121
1122                 SQFuncState *funcstate = _fs->PushChildState(_ss(_vm));
1123                 funcstate->_name = name;
1124                 SQObject paramname;
1125                 funcstate->AddParameter(_fs->CreateString(_SC("this")));
1126                 funcstate->_sourcename = _sourcename;
1127                 while(_token!=_SC(')')) {
1128                         if(_token == TK_VARPARAMS) {
1129                                 funcstate->_varparams = true;
1130                                 Lex();
1131                                 if(_token != _SC(')')) Error(_SC("expected ')'"));
1132                                 break;
1133                         }
1134                         else {
1135                                 paramname = Expect(TK_IDENTIFIER);
1136                                 funcstate->AddParameter(paramname);
1137                                 if(_token == _SC(',')) Lex();
1138                                 else if(_token != _SC(')')) Error(_SC("expected ')' or ','"));
1139                         }
1140                 }
1141                 Expect(_SC(')'));
1142                 //outer values
1143                 if(_token == _SC(':')) {
1144                         Lex(); Expect(_SC('('));
1145                         while(_token != _SC(')')) {
1146                                 paramname = Expect(TK_IDENTIFIER);
1147                                 //outers are treated as implicit local variables
1148                                 funcstate->AddOuterValue(paramname);
1149                                 if(_token == _SC(',')) Lex();
1150                                 else if(_token != _SC(')')) Error(_SC("expected ')' or ','"));
1151                         }
1152                         Lex();
1153                 }
1154
1155                 SQFuncState *currchunk = _fs;
1156                 _fs = funcstate;
1157                 Statement();
1158                 funcstate->AddLineInfos(_lex._prevtoken == _SC('\n')?_lex._lasttokenline:_lex._currentline, _lineinfo, true);
1159         funcstate->AddInstruction(_OP_RETURN, -1);
1160                 funcstate->SetStackSize(0);
1161                 //_fs->->_stacksize = _fs->_stacksize;
1162                 SQFunctionProto *func = funcstate->BuildProto();
1163 #ifdef _DEBUG_DUMP
1164                 funcstate->Dump(func);
1165 #endif
1166                 _fs = currchunk;
1167                 _fs->_functions.push_back(func);
1168                 _fs->PopChildState();
1169         }
1170         void CleanStack(SQInteger stacksize)
1171         {
1172                 if(_fs->GetStackSize() != stacksize)
1173                         _fs->SetStackSize(stacksize);
1174         }
1175         void ResolveBreaks(SQFuncState *funcstate, SQInteger ntoresolve)
1176         {
1177                 while(ntoresolve > 0) {
1178                         SQInteger pos = funcstate->_unresolvedbreaks.back();
1179                         funcstate->_unresolvedbreaks.pop_back();
1180                         //set the jmp instruction
1181                         funcstate->SetIntructionParams(pos, 0, funcstate->GetCurrentPos() - pos, 0);
1182                         ntoresolve--;
1183                 }
1184         }
1185         void ResolveContinues(SQFuncState *funcstate, SQInteger ntoresolve, SQInteger targetpos)
1186         {
1187                 while(ntoresolve > 0) {
1188                         SQInteger pos = funcstate->_unresolvedcontinues.back();
1189                         funcstate->_unresolvedcontinues.pop_back();
1190                         //set the jmp instruction
1191                         funcstate->SetIntructionParams(pos, 0, targetpos - pos, 0);
1192                         ntoresolve--;
1193                 }
1194         }
1195 private:
1196         SQInteger _token;
1197         SQFuncState *_fs;
1198         SQObjectPtr _sourcename;
1199         SQLexer _lex;
1200         bool _lineinfo;
1201         bool _raiseerror;
1202         SQInteger _debugline;
1203         SQInteger _debugop;
1204         ExpStateVec _expstates;
1205         SQChar *compilererror;
1206         jmp_buf _errorjmp;
1207         SQVM *_vm;
1208 };
1209
1210 bool Compile(SQVM *vm,SQLEXREADFUNC rg, SQUserPointer up, const SQChar *sourcename, SQObjectPtr &out, bool raiseerror, bool lineinfo)
1211 {
1212         SQCompiler p(vm, rg, up, sourcename, raiseerror, lineinfo);
1213         return p.Compile(out);
1214 }