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

Add a table to store defined class/struct names to be used as types.

mingodad 9 лет назад
Родитель
Сommit
2d3885ea64
2 измененных файлов с 34 добавлено и 2 удалено
  1. 12 1
      SquiLu/samples/declarations.nut
  2. 22 1
      SquiLu/squirrel/sqcompiler.cpp

+ 12 - 1
SquiLu/samples/declarations.nut

@@ -25,6 +25,17 @@ struct Person //struct is handled internally as a class
 	int32_t weight;
 };
 
+Person person;
+
+Person checkCredit(int64_t id)
+{
+	Person p = Person();
+	p.name = "Bert";
+	return p;
+}
+
+print(checkCredit(12).name);
+
 class BaseVector {
 	constructor(...)
 	{
@@ -75,7 +86,7 @@ local v2 = v0 + v1;
 v2.Print();
 print(v2.isEmpty());
 
-local v4 = Vector4();
+Vector4 v4 = Vector4();
 print(v4.isFull());
 
 FakeNamespace <- {

+ 22 - 1
SquiLu/squirrel/sqcompiler.cpp

@@ -129,6 +129,7 @@ public:
 		_scope.nested = 0;
 		_compilererror = NULL;
         _globals = SQTable::Create(_ss(_vm),0);
+        _type_names = SQTable::Create(_ss(_vm),0);
         _max_nested_includes = max_nested_includes;
         _nested_includes_count = 0;
 	}
@@ -161,6 +162,15 @@ public:
 		va_end(vl);
 	}
 
+	bool CheckTypeName(const SQObject &name, bool addIfNotExists=false){
+	    bool found = _table(_type_names)->Exists(name);
+	    if(addIfNotExists && !found) {
+	        SQObjectPtr oname = name, otrue = true;
+	        _table(_type_names)->NewSlot(oname, otrue);
+	    }
+	    return found;
+	}
+
 	void CheckGlobalName(const SQObject &name, bool addIfNotExists=false, bool checkLocals=true){
 	    if(_table(_globals)->Exists(name)){
             if(checkLocals) Error(_SC("global '%s' already declared"), _stringval(name));
@@ -484,6 +494,7 @@ public:
 	}
 	void Statement(bool closeframe = true)
 	{
+	    SQObject id;
 		_fs->AddLineInfos(_lex._currentline, _lineinfo);
     start_again:
 		switch(_token){
@@ -586,7 +597,7 @@ public:
 		case TK_CONST:
 			{
 			Lex();
-			SQObject id = Expect(TK_IDENTIFIER);
+			id = Expect(TK_IDENTIFIER);
 			Expect('=');
 			SQObjectPtr strongid = id;
 			CheckLocalNameScope(id, _scope.nested);
@@ -601,6 +612,14 @@ public:
 		    Pragma();
 		    break;
 
+        case TK_IDENTIFIER:
+            id = _fs->CreateString(_lex._svalue);
+            if(CheckTypeName(id)) //C/C++ type declaration;
+            {
+                LocalDeclStatement();
+                break;
+            }
+
 		default:
 			CommaExpr();
 			_fs->DiscardTarget();
@@ -1783,6 +1802,7 @@ if(color == "yellow"){
 		if(_token == TK_IDENTIFIER) {
 		    SQObjectPtr str = SQString::Create(_ss(_vm), _lex._svalue);
 		    CheckGlobalName(str, true);
+            CheckTypeName(str, true); //to allow C/C++ style instance declarations
 		}
 		es = _es;
 		_es.donot_get = true;
@@ -2171,6 +2191,7 @@ private:
 	SQVM *_vm;
 	SQObjectPtrVec _scope_consts;
 	SQObjectPtr _globals;
+	SQObjectPtr _type_names; //to allow C/C++ style instance declarations
 	SQChar error_buf[MAX_COMPILER_ERROR_LEN];
 	SQInteger _max_nested_includes, _nested_includes_count;
 };