Skip to content

Commit 85b7766

Browse files
committed
Add Unit Lexer
1 parent d13b6d3 commit 85b7766

4 files changed

Lines changed: 83 additions & 62 deletions

File tree

‎extlibs/PATCH.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Patches
2+
3+
Modifications (marked with `AM`) have been made to:
4+
5+
- Squirrel
6+
- [sqstdio.cpp](.\squirrel\sqstdlib\sqstdio.cpp)
7+
- [sqcompiler.cpp](.\squirrel\squirrel\sqcompiler.cpp)
8+
- [sqlexer.cpp](.\squirrel\squirrel\sqlexer.cpp)
9+
- [sqlexer.h](.\squirrel\squirrel\sqlexer.h)

‎extlibs/squirrel/squirrel/sqcompiler.cpp‎

Lines changed: 56 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class SQCompiler
9797
void Lex(){ _token = _lex.Lex();}
9898
SQObject Expect(SQInteger tok)
9999
{
100-
100+
101101
if(_token != tok) {
102102
if(_token == TK_CONSTRUCTOR && tok == TK_IDENTIFIER) {
103103
//do nothing
@@ -237,11 +237,11 @@ class SQCompiler
237237
_fs->_returnexp = retexp;
238238
_fs->AddInstruction(op, 1, _fs->PopTarget(),_fs->GetStackSize());
239239
}
240-
else{
240+
else{
241241
if(op == _OP_RETURN && _fs->_traps > 0)
242242
_fs->AddInstruction(_OP_POPTRAP, _fs->_traps ,0);
243243
_fs->_returnexp = -1;
244-
_fs->AddInstruction(op, 0xFF,0,_fs->GetStackSize());
244+
_fs->AddInstruction(op, 0xFF,0,_fs->GetStackSize());
245245
}
246246
break;}
247247
case TK_BREAK:
@@ -302,7 +302,7 @@ class SQCompiler
302302
SQObject val = ExpectScalar();
303303
OptionalSemicolon();
304304
SQTable *enums = _table(_ss(_vm)->_consts);
305-
SQObjectPtr strongid = id;
305+
SQObjectPtr strongid = id;
306306
enums->NewSlot(strongid,SQObjectPtr(val));
307307
strongid.Null();
308308
}
@@ -461,7 +461,7 @@ class SQCompiler
461461
}
462462
template<typename T> void BIN_EXP(SQOpcode op, T f,SQInteger op3 = 0)
463463
{
464-
Lex();
464+
Lex();
465465
INVOKE_EXP(f);
466466
SQInteger op1 = _fs->PopTarget();SQInteger op2 = _fs->PopTarget();
467467
_fs->AddInstruction(op, _fs->PushTarget(), op1, op2, op3);
@@ -502,7 +502,7 @@ class SQCompiler
502502
_fs->SetIntructionParam(jpos, 1, (_fs->GetCurrentPos() - jpos));
503503
break;
504504
}
505-
505+
506506
default:
507507
return;
508508
}
@@ -535,7 +535,7 @@ class SQCompiler
535535
case TK_EQ: BIN_EXP(_OP_EQ, &SQCompiler::CompExp); break;
536536
case TK_NE: BIN_EXP(_OP_NE, &SQCompiler::CompExp); break;
537537
case TK_3WAYSCMP: BIN_EXP(_OP_CMP, &SQCompiler::CompExp,CMP_3W); break;
538-
default: return;
538+
default: return;
539539
}
540540
}
541541
void CompExp()
@@ -548,7 +548,7 @@ class SQCompiler
548548
case TK_LE: BIN_EXP(_OP_CMP, &SQCompiler::ShiftExp,CMP_LE); break;
549549
case TK_IN: BIN_EXP(_OP_EXISTS, &SQCompiler::ShiftExp); break;
550550
case TK_INSTANCEOF: BIN_EXP(_OP_INSTANCEOF, &SQCompiler::ShiftExp); break;
551-
default: return;
551+
default: return;
552552
}
553553
}
554554
void ShiftExp()
@@ -558,7 +558,7 @@ class SQCompiler
558558
case TK_USHIFTR: BIN_EXP(_OP_BITW, &SQCompiler::PlusExp,BW_USHIFTR); break;
559559
case TK_SHIFTL: BIN_EXP(_OP_BITW, &SQCompiler::PlusExp,BW_SHIFTL); break;
560560
case TK_SHIFTR: BIN_EXP(_OP_BITW, &SQCompiler::PlusExp,BW_SHIFTR); break;
561-
default: return;
561+
default: return;
562562
}
563563
}
564564
SQOpcode ChooseArithOpByToken(SQInteger tok)
@@ -596,7 +596,7 @@ class SQCompiler
596596
default: return;
597597
}
598598
}
599-
599+
600600
void MultExp()
601601
{
602602
PrefixedExpr();
@@ -612,9 +612,18 @@ class SQCompiler
612612
SQInteger pos = Factor();
613613
for(;;) {
614614
switch(_token) {
615+
case TK_IDENTIFIER: // AM+ start
616+
if(_lex._unit) {
617+
pos = Factor();
618+
SQInteger unit = _fs->PopTarget();
619+
SQInteger num = _fs->PopTarget();
620+
_fs->AddInstruction(_OP_CALL, _fs->PushTarget(), unit, num - 1, 2);
621+
break;
622+
}
623+
return; // AM+ end
615624
case _SC('.'):
616625
pos = -1;
617-
Lex();
626+
Lex();
618627

619628
_fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(Expect(TK_IDENTIFIER)));
620629
if(_es.etype==BASE) {
@@ -632,7 +641,7 @@ class SQCompiler
632641
break;
633642
case _SC('['):
634643
if(_lex._prevtoken == _SC('\n')) Error(_SC("cannot brake deref/or comma needed after [exp]=exp slot declaration"));
635-
Lex(); Expression(); Expect(_SC(']'));
644+
Lex(); Expression(); Expect(_SC(']'));
636645
pos = -1;
637646
if(_es.etype==BASE) {
638647
Emit2ArgsOP(_OP_GET);
@@ -676,8 +685,8 @@ class SQCompiler
676685
}
677686
}
678687
return;
679-
break;
680-
case _SC('('):
688+
break;
689+
case _SC('('):
681690
switch(_es.etype) {
682691
case OBJECT: {
683692
SQInteger key = _fs->PopTarget(); /* location of the key */
@@ -713,7 +722,7 @@ class SQCompiler
713722
{
714723
case TK_STRING_LITERAL:
715724
_fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(_fs->CreateString(_lex._svalue,_lex._longstr.size()-1)));
716-
Lex();
725+
Lex();
717726
break;
718727
case TK_BASE:
719728
Lex();
@@ -747,7 +756,7 @@ class SQCompiler
747756
/* Handle a free var */
748757
if(NeedGet()) {
749758
_es.epos = _fs->PushTarget();
750-
_fs->AddInstruction(_OP_GETOUTER, _es.epos, pos);
759+
_fs->AddInstruction(_OP_GETOUTER, _es.epos, pos);
751760
/* _es.etype = EXPR; already default value */
752761
}
753762
else {
@@ -806,7 +815,7 @@ class SQCompiler
806815
_es.epos = -1;
807816
return _es.epos;
808817
break;
809-
case TK_NULL:
818+
case TK_NULL:
810819
_fs->AddInstruction(_OP_LOADNULLS, _fs->PushTarget(),1);
811820
Lex();
812821
break;
@@ -821,7 +830,7 @@ class SQCompiler
821830
SQInteger apos = _fs->GetCurrentPos(),key = 0;
822831
Lex();
823832
while(_token != _SC(']')) {
824-
Expression();
833+
Expression();
825834
if(_token == _SC(',')) Lex();
826835
SQInteger val = _fs->PopTarget();
827836
SQInteger array = _fs->TopTarget();
@@ -839,24 +848,24 @@ class SQCompiler
839848
case TK_FUNCTION: FunctionExp(_token);break;
840849
case _SC('@'): FunctionExp(_token,true);break;
841850
case TK_CLASS: Lex(); ClassExp();break;
842-
case _SC('-'):
843-
Lex();
851+
case _SC('-'):
852+
Lex();
844853
switch(_token) {
845854
case TK_INTEGER: EmitLoadConstInt(-_lex._nvalue,-1); Lex(); break;
846855
case TK_FLOAT: EmitLoadConstFloat(-_lex._fvalue,-1); Lex(); break;
847856
default: UnaryOP(_OP_NEG);
848857
}
849858
break;
850859
case _SC('!'): Lex(); UnaryOP(_OP_NOT); break;
851-
case _SC('~'):
852-
Lex();
860+
case _SC('~'):
861+
Lex();
853862
if(_token == TK_INTEGER) { EmitLoadConstInt(~_lex._nvalue,-1); Lex(); break; }
854-
UnaryOP(_OP_BWNOT);
863+
UnaryOP(_OP_BWNOT);
855864
break;
856865
case TK_TYPEOF : Lex() ;UnaryOP(_OP_TYPEOF); break;
857866
case TK_RESUME : Lex(); UnaryOP(_OP_RESUME); break;
858867
case TK_CLONE : Lex(); UnaryOP(_OP_CLONE); break;
859-
case TK_MINUSMINUS :
868+
case TK_MINUSMINUS :
860869
case TK_PLUSPLUS :PrefixIncDec(_token); break;
861870
case TK_DELETE : DeleteExpr(); break;
862871
case _SC('('): Lex(); CommaExpr(); Expect(_SC(')'));
@@ -910,9 +919,9 @@ class SQCompiler
910919
while(_token != _SC(')')) {
911920
Expression();
912921
MoveIfCurrentTargetIsLocal();
913-
nargs++;
914-
if(_token == _SC(',')){
915-
Lex();
922+
nargs++;
923+
if(_token == _SC(',')){
924+
Lex();
916925
if(_token == ')') Error(_SC("expression expected, found ')'"));
917926
}
918927
}
@@ -1024,11 +1033,11 @@ class SQCompiler
10241033
_fs->AddInstruction(_OP_JZ, _fs->PopTarget());
10251034
SQInteger jnepos = _fs->GetCurrentPos();
10261035
BEGIN_SCOPE();
1027-
1036+
10281037
Statement();
10291038
//
10301039
if(_token != _SC('}') && _token != TK_ELSE) OptionalSemicolon();
1031-
1040+
10321041
END_SCOPE();
10331042
SQInteger endifblock = _fs->GetCurrentPos();
10341043
if(_token == TK_ELSE){
@@ -1048,18 +1057,18 @@ class SQCompiler
10481057
SQInteger jzpos, jmppos;
10491058
jmppos = _fs->GetCurrentPos();
10501059
Lex(); Expect(_SC('(')); CommaExpr(); Expect(_SC(')'));
1051-
1060+
10521061
BEGIN_BREAKBLE_BLOCK();
10531062
_fs->AddInstruction(_OP_JZ, _fs->PopTarget());
10541063
jzpos = _fs->GetCurrentPos();
10551064
BEGIN_SCOPE();
1056-
1065+
10571066
Statement();
1058-
1067+
10591068
END_SCOPE();
10601069
_fs->AddInstruction(_OP_JMP, 0, jmppos - _fs->GetCurrentPos() - 1);
10611070
_fs->SetIntructionParam(jzpos, 1, _fs->GetCurrentPos() - jzpos);
1062-
1071+
10631072
END_BREAKBLE_BLOCK(jmppos);
10641073
}
10651074
void DoWhileStatement()
@@ -1119,7 +1128,7 @@ class SQCompiler
11191128
_fs->AddInstruction(_OP_JMP, 0, jmppos - _fs->GetCurrentPos() - 1, 0);
11201129
if(jzpos> 0) _fs->SetIntructionParam(jzpos, 1, _fs->GetCurrentPos() - jzpos);
11211130
END_SCOPE();
1122-
1131+
11231132
END_BREAKBLE_BLOCK(continuetrg);
11241133
}
11251134
void ForEachStatement()
@@ -1134,7 +1143,7 @@ class SQCompiler
11341143
idxname = _fs->CreateString(_SC("@INDEX@"));
11351144
}
11361145
Expect(TK_IN);
1137-
1146+
11381147
//save the stack size
11391148
BEGIN_SCOPE();
11401149
//put the table in the stack(evaluate the table expression)
@@ -1193,7 +1202,7 @@ class SQCompiler
11931202
if(local) {
11941203
_fs->PopTarget();
11951204
}
1196-
1205+
11971206
//end condition
11981207
if(skipcondjmp != -1) {
11991208
_fs->SetIntructionParam(skipcondjmp, 1, (_fs->GetCurrentPos() - skipcondjmp));
@@ -1225,7 +1234,7 @@ class SQCompiler
12251234
_fs->PushTarget(0);
12261235
_fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(id));
12271236
if(_token == TK_DOUBLE_COLON) Emit2ArgsOP(_OP_GET);
1228-
1237+
12291238
while(_token == TK_DOUBLE_COLON) {
12301239
Lex();
12311240
id = Expect(TK_IDENTIFIER);
@@ -1303,10 +1312,10 @@ class SQCompiler
13031312
}
13041313
void EnumStatement()
13051314
{
1306-
Lex();
1315+
Lex();
13071316
SQObject id = Expect(TK_IDENTIFIER);
13081317
Expect(_SC('{'));
1309-
1318+
13101319
SQObject table = _fs->CreateTable();
13111320
SQInteger nval = 0;
13121321
while(_token != _SC('}')) {
@@ -1324,7 +1333,7 @@ class SQCompiler
13241333
if(_token == ',') Lex();
13251334
}
13261335
SQTable *enums = _table(_ss(_vm)->_consts);
1327-
SQObjectPtr strongid = id;
1336+
SQObjectPtr strongid = id;
13281337
enums->NewSlot(SQObjectPtr(strongid),SQObjectPtr(table));
13291338
strongid.Null();
13301339
Lex();
@@ -1420,7 +1429,7 @@ class SQCompiler
14201429
else if(_es.etype==LOCAL) {
14211430
SQInteger src = _fs->TopTarget();
14221431
_fs->AddInstruction(_OP_INCL, src, src, 0, diff);
1423-
1432+
14241433
}
14251434
else if(_es.etype==OUTER) {
14261435
SQInteger tmp = _fs->PushTarget();
@@ -1450,7 +1459,7 @@ class SQCompiler
14501459
else {
14511460
paramname = Expect(TK_IDENTIFIER);
14521461
funcstate->AddParameter(paramname);
1453-
if(_token == _SC('=')) {
1462+
if(_token == _SC('=')) {
14541463
Lex();
14551464
Expression();
14561465
funcstate->AddDefaultParam(_fs->TopTarget());
@@ -1467,14 +1476,14 @@ class SQCompiler
14671476
for(SQInteger n = 0; n < defparams; n++) {
14681477
_fs->PopTarget();
14691478
}
1470-
1479+
14711480
SQFuncState *currchunk = _fs;
14721481
_fs = funcstate;
1473-
if(lambda) {
1474-
Expression();
1482+
if(lambda) {
1483+
Expression();
14751484
_fs->AddInstruction(_OP_RETURN, 1, _fs->PopTarget());}
1476-
else {
1477-
Statement(false);
1485+
else {
1486+
Statement(false);
14781487
}
14791488
funcstate->AddLineInfos(_lex._prevtoken == _SC('\n')?_lex._lasttokenline:_lex._currentline, _lineinfo, true);
14801489
funcstate->AddInstruction(_OP_RETURN, -1);

0 commit comments

Comments
 (0)