소스 검색

Changed the lexer/parser to accept and discard C/C++ template/goto/label

mingodad 9 년 전
부모
커밋
dc94a15a4e
5개의 변경된 파일60개의 추가작업 그리고 0개의 파일을 삭제
  1. 25 0
      SquiLu/samples/test-cpp-template.nut
  2. 14 0
      SquiLu/samples/test-goto.nut
  3. 19 0
      SquiLu/squirrel/sqcompiler.cpp
  4. 1 0
      SquiLu/squirrel/sqcompiler.h
  5. 1 0
      SquiLu/squirrel/sqlexer.cpp

+ 25 - 0
SquiLu/samples/test-cpp-template.nut

@@ -0,0 +1,25 @@
+typedef any T;
+typedef int32 int;
+typedef array charAry;
+typedef double_t double;
+typedef string_t string;
+
+template<typename T>
+T add2(T first, T second)
+{
+	return first + second;
+}
+
+function main(int argc, array_t argv)
+{
+	int ia = 4, ib = 6;
+	print(add2(ia, ib));
+
+	double da = 4.0, db = 6.0;
+	print(add2(da, db));
+	
+	string sa = "4", sb = "6";
+	print(add2(sa, sb));
+	
+	return 0;
+}

+ 14 - 0
SquiLu/samples/test-goto.nut

@@ -0,0 +1,14 @@
+local function test10(x)
+{
+dente:
+	if(x > 10)
+	{
+		goto dente;
+		goto done;
+		return true;
+	}
+done:
+	return false;
+}
+
+print(test10(23));

+ 19 - 0
SquiLu/squirrel/sqcompiler.cpp

@@ -753,6 +753,25 @@ public:
 		    Expect(_SC(';'));
 		    Expect(_SC(';'));
 		    break;
 		    break;
 
 
+        case TK_TEMPLATE: {
+            Lex(); //ignore for now
+            Expect(_SC('<'));
+            int nest_count = 1;
+            while(_token > 0 && nest_count > 0){
+                Lex();
+                switch(_token)
+                {
+                    case _SC('>'):
+                        --nest_count;
+                        break;
+                    case _SC('<'):
+                        nest_count++;
+                }
+            }
+            if(nest_count == 0) Lex(); //last '>' ignore for now
+            break;
+        }
+
         case TK_IDENTIFIER:{
         case TK_IDENTIFIER:{
             SQInteger lhtk = _lex.LookaheadLex();
             SQInteger lhtk = _lex.LookaheadLex();
             if(lhtk == _SC(':'))
             if(lhtk == _SC(':'))

+ 1 - 0
SquiLu/squirrel/sqcompiler.h

@@ -110,6 +110,7 @@ struct SQVM;
     ENUM_TK(STRING_LITERAL)\
     ENUM_TK(STRING_LITERAL)\
     ENUM_TK(STRUCT) \
     ENUM_TK(STRUCT) \
     ENUM_TK(SWITCH)\
     ENUM_TK(SWITCH)\
+    ENUM_TK(TEMPLATE)\
     ENUM_TK(THIS)\
     ENUM_TK(THIS)\
     ENUM_TK(THROW)\
     ENUM_TK(THROW)\
     ENUM_TK(TYPEDEF)\
     ENUM_TK(TYPEDEF)\

+ 1 - 0
SquiLu/squirrel/sqlexer.cpp

@@ -119,6 +119,7 @@ SQTable * SQLexer::GetKeywords()
 	ADD_KEYWORD(struct,TK_STRUCT);
 	ADD_KEYWORD(struct,TK_STRUCT);
 	ADD_KEYWORD(switch, TK_SWITCH);
 	ADD_KEYWORD(switch, TK_SWITCH);
 	ADD_KEYWORD(table_t, TK_LOCAL_TABLE_T);
 	ADD_KEYWORD(table_t, TK_LOCAL_TABLE_T);
+	ADD_KEYWORD(template, TK_TEMPLATE);
 	ADD_KEYWORD(this, TK_THIS);
 	ADD_KEYWORD(this, TK_THIS);
 	ADD_KEYWORD(throw, TK_THROW);
 	ADD_KEYWORD(throw, TK_THROW);
 	ADD_KEYWORD(typedef, TK_TYPEDEF);
 	ADD_KEYWORD(typedef, TK_TYPEDEF);