Просмотр исходного кода

Modify lexeer and parser to accept class declaration like in typescript.
Types are not enforced and discarded right now.

class Person {
private name: string;
private age: number;
dad = null;

constructor(name: string, age: number) {
this.name = name;
this.age = age;
}

function asString(): string {
return this.name + " (" + this.age + ")";
}
}

mingodad 13 лет назад
Родитель
Сommit
406210b6bf
3 измененных файлов с 33 добавлено и 8 удалено
  1. 29 8
      SquiLu/squirrel/sqcompiler.cpp
  2. 2 0
      SquiLu/squirrel/sqcompiler.h
  3. 2 0
      SquiLu/squirrel/sqlexer.cpp

+ 29 - 8
SquiLu/squirrel/sqcompiler.cpp

@@ -1070,10 +1070,12 @@ public:
 	}
 	void ParseTableOrClass(SQInteger separator,SQInteger terminator)
 	{
-		SQInteger tpos = _fs->GetCurrentPos(),nkeys = 0;
+		SQInteger tpos = _fs->GetCurrentPos(),nkeys = 0;
+		SQObject type_name;
 		while(_token != terminator) {
 			bool hasattrs = false;
 			bool isstatic = false;
+			bool isprivate = false;
 			//check if is an attribute
 			if(separator == ';') {
 				if(_token == TK_ATTR_OPEN) {
@@ -1085,6 +1087,13 @@ public:
 					isstatic = true;
 					Lex();
 				}
+				else if(_token == TK_PUBLIC) {
+					Lex();
+				}
+				else if(_token == TK_PRIVATE) {
+					isprivate = true;
+					Lex();
+				}
 			}
 			switch(_token) {
 			case TK_FUNCTION:
@@ -1105,19 +1114,31 @@ public:
 
 			case TK_STRING_LITERAL: //JSON
 			case TK_IDENTIFIER: {//JSON
-                SQObjectPtr obj = GetTokenObject(_token);
-                SQInteger next_token = _SC('=');
-                if(separator == ',' && _token == _SC(':')){ //only works for tables
-                    next_token = _token;
-                }
-                Expect(next_token);
+				SQObjectPtr obj = GetTokenObject(_token);
+				SQInteger next_token = _SC('=');
+				if(_token == _SC(':')){
+					if(separator == _SC(',')){ //only works for tables
+						next_token = _token;
+					}
+					else
+					{
+						//class field with type annotation
+						Lex();
+						type_name = Expect(TK_IDENTIFIER);
+						_fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(obj));
+						_fs->AddInstruction(_OP_LOADNULLS, _fs->PushTarget(), 1);
+						break;
+					}
+				}
+				Expect(next_token);
 				_fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(obj));
 				Expression();
 				break;
 			}
 			default :
-                ErrorIfNotToken(TK_IDENTIFIER);
+				ErrorIfNotToken(TK_IDENTIFIER);
 			}
+
 			if(_token == separator) Lex();//optional comma/semicolon
 			nkeys++;
 			SQInteger val = _fs->PopTarget();

+ 2 - 0
SquiLu/squirrel/sqcompiler.h

@@ -79,6 +79,8 @@ enum SQKeywordsEnum {
     TK___LINE__,
     TK___FUNCTION__,
     TK___FILE__,
+    TK_PRIVATE,
+    TK_PUBLIC,
 
     TK_IGNORE,
 

+ 2 - 0
SquiLu/squirrel/sqlexer.cpp

@@ -108,6 +108,8 @@ SQTable * SQLexer::GetKeywords()
 	ADD_KEYWORD(__LINE__,TK___LINE__);
 	ADD_KEYWORD(__FUNCTION__,TK___FUNCTION__);
 	ADD_KEYWORD(__FILE__,TK___FILE__);
+	ADD_KEYWORD(private,TK_PRIVATE);
+	ADD_KEYWORD(public,TK_PUBLIC);
 	ADD_KEYWORD(new,TK_IGNORE);
 
 	return tbl;