Browse Source

Added field var usage from constructor.

woollybah 7 years ago
parent
commit
ea30f1faf0
2 changed files with 21 additions and 9 deletions
  1. 9 1
      expr.bmx
  2. 12 8
      stmt.bmx

+ 9 - 1
expr.bmx

@@ -164,6 +164,7 @@ Type TExpr
 						Local stmt:TExpr = New TStmtExpr.Create( New TDeclStmt.Create( tmp ), v ).Semant()
 						stmt.exprType = TNewObjectExpr(argExpr).ty
 						args[i] = stmt
+						argExpr = args[i]
 					End If
 					
 					If TVarExpr(argExpr) Or TMemberVarExpr(argExpr) Then
@@ -174,7 +175,14 @@ Type TExpr
 							decl = TMemberVarExpr(argExpr).decl
 						End If
 						If decl.IsReadOnly() Then
-							Err "Expression for 'Var' parameter cannot be a ReadOnly variable"
+							If TFieldDecl(decl) Then
+								Local scope:TFuncDecl = _env.FuncScope()
+								If Not scope Or Not scope.IsCtor() Or (decl.ClassScope() <> scope.ClassScope()) Then
+									Err "Expression for 'Var' parameter cannot be a ReadOnly variable"
+								End If
+							Else
+								Err "Expression for 'Var' parameter cannot be a ReadOnly variable"
+							End If
 						End If
 					End If
 

+ 12 - 8
stmt.bmx

@@ -129,19 +129,23 @@ Type TAssignStmt Extends TStmt
 			rhs=Null
 		Else
 		
-			' can't assign to readonly field outside of its class constructor
+			' can't assign to readonly field outside of its class constructor, or anytime for readonly variable
 			If TVarExpr(lhs) Or TMemberVarExpr(lhs) Then
-				Local decl:TFieldDecl
+				Local decl:TDecl
 				If TVarExpr(lhs) Then
-					decl = TFieldDecl(TVarExpr(lhs).decl)
+					decl = TVarExpr(lhs).decl
 				Else
-					decl = TFieldDecl(TMemberVarExpr(lhs).decl)
+					decl = TMemberVarExpr(lhs).decl
 				End If
 				If decl And decl.IsReadOnly() Then
-					' check scope for ctor
-					Local scope:TFuncDecl = _env.FuncScope()
-					If Not scope Or Not scope.IsCtor() Or (decl.ClassScope() <> scope.ClassScope()) Then
-						Err "Cannot modify ReadOnly field " + decl.ident
+					If TFieldDecl(decl) Then
+						' check scope for ctor
+						Local scope:TFuncDecl = _env.FuncScope()
+						If Not scope Or Not scope.IsCtor() Or (decl.ClassScope() <> scope.ClassScope()) Then
+							Err "Cannot modify ReadOnly field " + decl.ident
+						End If
+					Else
+						Err "Cannot modify ReadOnly variable " + decl.ident
 					End If
 				End If
 			End If