Jelajahi Sumber

Moved code up that checks if "const" id already exists to make error messages show correct line number.
As a sub product of trying fix the const/enums problem I found a way to make the compiler recognize the '&' on function arguments to indicate pass by ref.

mingodad 13 tahun lalu
induk
melakukan
1956ee8486
3 mengubah file dengan 13 tambahan dan 7 penghapusan
  1. 10 4
      squirrel/sqcompiler.cpp
  2. 2 2
      squirrel/sqfuncstate.cpp
  3. 1 1
      squirrel/sqfuncstate.h

+ 10 - 4
squirrel/sqcompiler.cpp

@@ -335,14 +335,14 @@ public:
 			Lex();
 			SQObject id = Expect(TK_IDENTIFIER);
 			Expect('=');
-			SQObject val = ExpectScalar();
-			OptionalSemicolon();
 			SQTable *enums = _table(_ss(_vm)->_consts);
 			SQObjectPtr strongid = id;
 			if(enums->Exists(strongid)) {
 			    strongid.Null();
 			    Error(_SC("constant '%s' already exists"), _stringval(id));
 			}
+			SQObject val = ExpectScalar();
+			OptionalSemicolon();
 			enums->NewSlot(strongid,SQObjectPtr(val));
 			strongid.Null();
 
@@ -1605,7 +1605,8 @@ if(color == "yellow"){
 		SQObject paramname;
 		funcstate->AddParameter(_fs->CreateString(_SC("this")), _scope.nested+1);
 		funcstate->_sourcename = _sourcename;
-		SQInteger defparams = 0;
+		SQInteger defparams = 0;
+		SQInteger is_reference = 0;
 		while(_token!=_SC(')')) {
 			if(_token == TK_VARPARAMS) {
 				if(defparams > 0) Error(_SC("function with default parameters cannot have variable number of parameters"));
@@ -1614,10 +1615,15 @@ if(color == "yellow"){
 				Lex();
 				if(_token != _SC(')')) Error(_SC("expected ')'"));
 				break;
+			}
+			else if(_token == _SC('&')){
+			    is_reference = 1;
+			    Lex();
 			}
 			else {
 				paramname = Expect(TK_IDENTIFIER);
-				funcstate->AddParameter(paramname, _scope.nested+1);
+				funcstate->AddParameter(paramname, _scope.nested+1, is_reference ? _VAR_REFERENCE : _VAR_ANY);
+				is_reference = 0; //reset is_reference
 				if(_token == _SC('=')) {
 					Lex();
 					Expression();

+ 2 - 2
squirrel/sqfuncstate.cpp

@@ -426,9 +426,9 @@ SQInteger SQFuncState::GetOuterVariable(const SQObject &name)
 	return -1;
 }
 
-void SQFuncState::AddParameter(const SQObject &name, SQInteger scope)
+void SQFuncState::AddParameter(const SQObject &name, SQInteger scope, SQInteger type)
 {
-	PushLocalVariable(name, scope);
+	PushLocalVariable(name, scope, type);
 	_parameters.push_back(name);
 }
 

+ 1 - 1
squirrel/sqfuncstate.h

@@ -29,7 +29,7 @@ struct SQFuncState
 	SQInteger GetNumericConstant(const SQInteger cons);
 	SQInteger GetNumericConstant(const SQFloat cons);
 	SQInteger PushLocalVariable(const SQObject &name, SQInteger scope, SQInteger type=_VAR_ANY);
-	void AddParameter(const SQObject &name, SQInteger scope);
+	void AddParameter(const SQObject &name, SQInteger scope, SQInteger type=_VAR_ANY);
 	//void AddOuterValue(const SQObject &name);
 	SQInteger GetLocalVariable(const SQObject &name);
 	void MarkLocalAsOuter(SQInteger pos);