Procházet zdrojové kódy

Refactored a bit Lexer with the idea to expose it for SquiLu scripts.
Add new keyword "__FUNCTION__" to be used in scripts.
Changed the keyword token definitions from "#defines" to an enum.

mingodad před 13 roky
rodič
revize
a2d1a995fa
4 změnil soubory, kde provedl 129 přidání a 113 odebrání
  1. 4 0
      squirrel/sqcompiler.cpp
  2. 101 98
      squirrel/sqcompiler.h
  3. 22 14
      squirrel/sqlexer.cpp
  4. 2 1
      squirrel/sqlexer.h

+ 4 - 0
squirrel/sqcompiler.cpp

@@ -912,6 +912,10 @@ public:
 			_fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(_sourcename));
 			Lex();
 			break;
+		case TK___FUNCTION__:
+			_fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(_fs->_name));
+			Lex();
+			break;
 		case TK_IGNORE:
             //Warning("Keyword ignored \"%s\" at line %d:%d\n", _lex.Tok2Str(_token),
             //        _lex._currentline, _lex._currentcolumn);

+ 101 - 98
squirrel/sqcompiler.h

@@ -3,105 +3,108 @@
 #define _SQCOMPILER_H_
 
 struct SQVM;
-
-#define	TK_IDENTIFIER	258
-#define	TK_STRING_LITERAL	259
-#define	TK_INTEGER	260
-#define	TK_FLOAT	261
-#define	TK_BASE	262
-#define	TK_DELETE	263
-#define	TK_EQ	264
-#define	TK_NE	265
-#define	TK_LE	266
-#define	TK_GE	267
-#define	TK_SWITCH	268
-#define	TK_ARROW	269
-#define	TK_AND	270
-#define	TK_OR	271
-#define	TK_IF	272
-#define	TK_ELSE	273
-#define	TK_WHILE	274
-#define	TK_BREAK	275
-#define	TK_FOR	276
-#define	TK_DO	277
-#define	TK_NULL	278
-#define	TK_FOREACH	279
-#define	TK_IN	280
-#define	TK_NEWSLOT	281
-#define	TK_MODULO	282
-#define	TK_LOCAL	283
-#define	TK_CLONE	284
-#define	TK_FUNCTION	285
-#define	TK_RETURN	286
-#define	TK_TYPEOF	287
-#define	TK_UMINUS	288
-#define	TK_PLUSEQ	289
-#define	TK_MINUSEQ	290
-#define	TK_CONTINUE	291
-#define TK_YIELD 292
-#define TK_TRY 293
-#define TK_CATCH 294
-#define TK_THROW 295
-#define TK_SHIFTL 296
-#define TK_SHIFTR 297
-#define TK_RESUME 298
-#define TK_DOUBLE_COLON 299
-#define TK_CASE 300
-#define TK_DEFAULT 301
-#define TK_THIS 302
-#define TK_PLUSPLUS 303
-#define TK_MINUSMINUS 304
-#define TK_3WAYSCMP 305
-#define TK_USHIFTR 306
-#define TK_CLASS 307
-#define TK_EXTENDS 308
-#define TK_CONSTRUCTOR 310
-#define TK_INSTANCEOF 311
-#define TK_VARPARAMS 312
-//#define TK_VARGC 313
-//#define TK_VARGV 314
-#define TK_TRUE 315
-#define TK_FALSE 316
-#define TK_MULEQ 317
-#define TK_DIVEQ 318
-#define TK_MODEQ 319
-#define TK_ATTR_OPEN 320
-#define TK_ATTR_CLOSE 321
-#define TK_STATIC 322
-#define TK_ENUM 323
-#define TK_CONST 324
-#define TK___LINE__ 325
-#define TK___FILE__ 326
-
-#define TK_IGNORE 327
 
-#define	TK_LOCAL_CHAR_T	328
-#define	TK_LOCAL_WCHAR_T	329
-#define	TK_LOCAL_BOOL_T	330
-
-#define	TK_LOCAL_TABLE_T	331
-#define	TK_LOCAL_ARRAY_T	332
-
-#define	TK_LOCAL_INT8_T	333
-#define	TK_LOCAL_INT16_T	334
-#define	TK_LOCAL_INT32_T	335
-#define	TK_LOCAL_INT64_T	336
-#define	TK_LOCAL_INT_T	337
-#define	TK_LOCAL_UINT8_T	338
-#define	TK_LOCAL_UINT16_T	339
-#define	TK_LOCAL_UINT32_T	340
-#define	TK_LOCAL_UINT64_T	341
-#define	TK_LOCAL_UINT_T	342
-
-#define	TK_LOCAL_FLOAT_T	343
-#define	TK_LOCAL_DOUBLE_T	344
-#define	TK_LOCAL_LONG_DOUBLE_T	345
-
-#define	TK_BIT_AND_EQ	346
-#define	TK_BIT_OR_EQ	347
-#define	TK_BIT_XOR_EQ	348
-#define	TK_BIT_SHIFT_LEFT_EQ	349
-#define	TK_BIT_SHIFT_RIGHT_EQ	350
+enum SQKeywordsEnum {
+    TK_IDENTIFIER = 258,
+    TK_STRING_LITERAL,
+    TK_INTEGER,
+    TK_FLOAT,
+    TK_BASE,
+    TK_DELETE,
+    TK_EQ,
+    TK_NE,
+    TK_LE,
+    TK_GE,
+    TK_SWITCH,
+    TK_ARROW,
+    TK_AND,
+    TK_OR,
+    TK_IF,
+    TK_ELSE,
+    TK_WHILE,
+    TK_BREAK,
+    TK_FOR,
+    TK_DO,
+    TK_NULL,
+    TK_FOREACH,
+    TK_IN,
+    TK_NEWSLOT,
+    TK_MODULO,
+    TK_LOCAL,
+    TK_CLONE,
+    TK_FUNCTION,
+    TK_RETURN,
+    TK_TYPEOF,
+    TK_UMINUS,
+    TK_PLUSEQ,
+    TK_MINUSEQ,
+    TK_CONTINUE,
+    TK_YIELD,
+    TK_TRY,
+    TK_CATCH,
+    TK_THROW,
+    TK_SHIFTL,
+    TK_SHIFTR,
+    TK_RESUME,
+    TK_DOUBLE_COLON,
+    TK_CASE,
+    TK_DEFAULT,
+    TK_THIS,
+    TK_PLUSPLUS,
+    TK_MINUSMINUS,
+    TK_3WAYSCMP,
+    TK_USHIFTR,
+    TK_CLASS,
+    TK_EXTENDS,
+    TK_CONSTRUCTOR,
+    TK_INSTANCEOF,
+    TK_VARPARAMS,
+    //TK_VARGC,
+    //TK_VARGV,
+    TK_TRUE,
+    TK_FALSE,
+    TK_MULEQ,
+    TK_DIVEQ,
+    TK_MODEQ,
+    TK_ATTR_OPEN,
+    TK_ATTR_CLOSE,
+    TK_STATIC,
+    TK_ENUM,
+    TK_CONST,
+    TK___LINE__,
+    TK___FUNCTION__,
+    TK___FILE__,
+
+    TK_IGNORE,
+
+    TK_LOCAL_CHAR_T,
+    TK_LOCAL_WCHAR_T,
+    TK_LOCAL_BOOL_T,
+
+    TK_LOCAL_TABLE_T,
+    TK_LOCAL_ARRAY_T,
+
+    TK_LOCAL_INT8_T,
+    TK_LOCAL_INT16_T,
+    TK_LOCAL_INT32_T,
+    TK_LOCAL_INT64_T,
+    TK_LOCAL_INT_T,
+    TK_LOCAL_UINT8_T,
+    TK_LOCAL_UINT16_T,
+    TK_LOCAL_UINT32_T,
+    TK_LOCAL_UINT64_T,
+    TK_LOCAL_UINT_T,
+
+    TK_LOCAL_FLOAT_T,
+    TK_LOCAL_DOUBLE_T,
+    TK_LOCAL_LONG_DOUBLE_T,
+
+    TK_BIT_AND_EQ,
+    TK_BIT_OR_EQ,
+    TK_BIT_XOR_EQ,
+    TK_BIT_SHIFT_LEFT_EQ,
+    TK_BIT_SHIFT_RIGHT_EQ,
+};
 
 typedef void(*CompilerErrorFunc)(void *ud, const SQChar *s);
 bool Compile(SQVM *vm, SQLEXREADFUNC rg, SQUserPointer up, const SQChar *sourcename, SQObjectPtr &out, bool raiseerror, bool lineinfo);

+ 22 - 14
squirrel/sqlexer.cpp

@@ -17,9 +17,9 @@
 #define INIT_TEMP_STRING() { _longstr.resize(0);}
 #define APPEND_CHAR(c) { _longstr.push_back(c);}
 #define TERMINATE_BUFFER() {_longstr.push_back(_SC('\0'));}
-#define ADD_KEYWORD(key,id) _keywords->NewSlot( SQString::Create(ss, _SC(#key)) ,SQInteger(id))
+#define ADD_KEYWORD(key,id) tbl->NewSlot( SQString::Create(_sharedstate, _SC(#key)) ,SQInteger(id))
 
-SQLexer::SQLexer(){}
+SQLexer::SQLexer(){_keywords=0;}
 SQLexer::~SQLexer()
 {
 	_keywords->Release();
@@ -29,8 +29,21 @@ void SQLexer::Init(SQSharedState *ss, SQLEXREADFUNC rg, SQUserPointer up,Compile
 {
 	_errfunc = efunc;
 	_errtarget = ed;
-	_sharedstate = ss;
-	_keywords = SQTable::Create(ss, 26);
+	_sharedstate = ss;
+	if(_keywords) _keywords->Release();
+    _keywords = GetKeywords();
+	_readf = rg;
+	_up = up;
+	_lasttokenline = _currentline = 1;
+	_currentcolumn = 0;
+	_prevtoken = -1;
+	_reached_eof = SQFalse;
+	Next();
+}
+
+SQTable * SQLexer::GetKeywords()
+{
+	SQTable *tbl = SQTable::Create(_sharedstate, 58 /*26*/);
 	ADD_KEYWORD(while, TK_WHILE);
 	ADD_KEYWORD(do, TK_DO);
 	ADD_KEYWORD(if, TK_IF);
@@ -93,17 +106,12 @@ void SQLexer::Init(SQSharedState *ss, SQLEXREADFUNC rg, SQUserPointer up,Compile
 	ADD_KEYWORD(enum,TK_ENUM);
 	ADD_KEYWORD(const,TK_CONST);
 	ADD_KEYWORD(__LINE__,TK___LINE__);
+	ADD_KEYWORD(__FUNCTION__,TK___FUNCTION__);
 	ADD_KEYWORD(__FILE__,TK___FILE__);
-	ADD_KEYWORD(new,TK_IGNORE);
-
-	_readf = rg;
-	_up = up;
-	_lasttokenline = _currentline = 1;
-	_currentcolumn = 0;
-	_prevtoken = -1;
-	_reached_eof = SQFalse;
-	Next();
-}
+	ADD_KEYWORD(new,TK_IGNORE);
+
+	return tbl;
+}
 
 void SQLexer::Error(const SQChar *err)
 {

+ 2 - 1
squirrel/sqlexer.h

@@ -12,7 +12,8 @@ struct SQLexer
 {
 	SQLexer();
 	~SQLexer();
-	void Init(SQSharedState *ss,SQLEXREADFUNC rg,SQUserPointer up,CompilerErrorFunc efunc,void *ed);
+	void Init(SQSharedState *ss,SQLEXREADFUNC rg,SQUserPointer up,CompilerErrorFunc efunc,void *ed);
+	SQTable * GetKeywords();
 	void Error(const SQChar *err);
 	SQInteger Lex();
 	const SQChar *Tok2Str(SQInteger tok);