Browse Source

Refactor: Put responsibility where it's due.

Herby Vojčík 4 years ago
parent
commit
10e50375bf

+ 3 - 0
lang/API-CHANGES.txt

@@ -14,6 +14,7 @@
 + ExternallyKnownVar >>
   + isExternallyKnownVar
 + SemanticAnalyzer >>
+  + errorInvalidAssignment:
   + isVariableKnown:inPackage:
 + ScopeVar >>
   + isExternallyKnownVar
@@ -46,6 +47,7 @@
 - ScopeVar >>
   - isArgVar
   - isUnknownVar
+  - validateAssignment
 - SemanticAnalyzer >>
   - isVariableUndefined:inPackage:
 - SendNode >>
@@ -54,6 +56,7 @@
 - UnknownVar >>
   - isUnknownVar
 - VariableNode >>
+  - beAssigned
   - isArgument
 - amber/boot api >>
   - addElement(arraySet, el)

+ 0 - 25
lang/src/Compiler-AST.js

@@ -2425,31 +2425,6 @@ return self;
 }; }),
 $globals.VariableNode);
 
-$core.addMethod(
-$core.method({
-selector: "beAssigned",
-protocol: "accessing",
-//>>excludeStart("ide", pragmas.excludeIdeData);
-args: [],
-source: "beAssigned\x0a\x09self binding validateAssignment.\x0a\x09assigned := true",
-referencedClasses: [],
-//>>excludeEnd("ide");
-pragmas: [],
-messageSends: ["validateAssignment", "binding"]
-}, function ($methodClass){ return function (){
-var self=this,$self=this;
-//>>excludeStart("ctx", pragmas.excludeDebugContexts);
-return $core.withContext(function($ctx1) {
-//>>excludeEnd("ctx");
-$recv($self._binding())._validateAssignment();
-$self.assigned=true;
-return self;
-//>>excludeStart("ctx", pragmas.excludeDebugContexts);
-}, function($ctx1) {$ctx1.fill(self,"beAssigned",{})});
-//>>excludeEnd("ctx");
-}; }),
-$globals.VariableNode);
-
 $core.addMethod(
 $core.method({
 selector: "binding",

+ 0 - 5
lang/src/Compiler-AST.st

@@ -611,11 +611,6 @@ assigned: aBoolean
 	assigned := aBoolean
 !
 
-beAssigned
-	self binding validateAssignment.
-	assigned := true
-!
-
 binding
 	^ binding
 !

+ 38 - 34
lang/src/Compiler-Semantic.js

@@ -1295,36 +1295,6 @@ return self;
 }; }),
 $globals.ScopeVar);
 
-$core.addMethod(
-$core.method({
-selector: "validateAssignment",
-protocol: "testing",
-//>>excludeStart("ide", pragmas.excludeIdeData);
-args: [],
-source: "validateAssignment\x0a\x09self isImmutable ifTrue: [\x0a\x09\x09InvalidAssignmentError new\x0a\x09\x09\x09variableName: self name;\x0a\x09\x09\x09signal]",
-referencedClasses: ["InvalidAssignmentError"],
-//>>excludeEnd("ide");
-pragmas: [],
-messageSends: ["ifTrue:", "isImmutable", "variableName:", "new", "name", "signal"]
-}, function ($methodClass){ return function (){
-var self=this,$self=this;
-//>>excludeStart("ctx", pragmas.excludeDebugContexts);
-return $core.withContext(function($ctx1) {
-//>>excludeEnd("ctx");
-var $1,$2;
-$1=$self._isImmutable();
-if($core.assert($1)){
-$2=$recv($globals.InvalidAssignmentError)._new();
-$recv($2)._variableName_($self._name());
-$recv($2)._signal();
-}
-return self;
-//>>excludeStart("ctx", pragmas.excludeDebugContexts);
-}, function($ctx1) {$ctx1.fill(self,"validateAssignment",{})});
-//>>excludeEnd("ctx");
-}; }),
-$globals.ScopeVar);
-
 
 $core.addMethod(
 $core.method({
@@ -1876,6 +1846,33 @@ return $1;
 }; }),
 $globals.SemanticAnalyzer);
 
+$core.addMethod(
+$core.method({
+selector: "errorInvalidAssignment:",
+protocol: "error handling",
+//>>excludeStart("ide", pragmas.excludeIdeData);
+args: ["aString"],
+source: "errorInvalidAssignment: aString\x0a\x09InvalidAssignmentError new\x0a\x09\x09variableName: aString;\x0a\x09\x09signal",
+referencedClasses: ["InvalidAssignmentError"],
+//>>excludeEnd("ide");
+pragmas: [],
+messageSends: ["variableName:", "new", "signal"]
+}, function ($methodClass){ return function (aString){
+var self=this,$self=this;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx1) {
+//>>excludeEnd("ctx");
+var $1;
+$1=$recv($globals.InvalidAssignmentError)._new();
+$recv($1)._variableName_(aString);
+$recv($1)._signal();
+return self;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx1) {$ctx1.fill(self,"errorInvalidAssignment:",{aString:aString})});
+//>>excludeEnd("ctx");
+}; }),
+$globals.SemanticAnalyzer);
+
 $core.addMethod(
 $core.method({
 selector: "errorShadowingVariable:",
@@ -2267,16 +2264,18 @@ selector: "visitAssignmentNode:",
 protocol: "visiting",
 //>>excludeStart("ide", pragmas.excludeIdeData);
 args: ["aNode"],
-source: "visitAssignmentNode: aNode\x0a\x09super visitAssignmentNode: aNode.\x0a\x09aNode left beAssigned",
+source: "visitAssignmentNode: aNode\x0a\x09| lhs |\x0a\x09super visitAssignmentNode: aNode.\x0a\x09lhs := aNode left.\x0a\x09lhs isImmutable ifTrue: [ self errorInvalidAssignment: lhs value ].\x0a\x09lhs assigned: true",
 referencedClasses: [],
 //>>excludeEnd("ide");
 pragmas: [],
-messageSends: ["visitAssignmentNode:", "beAssigned", "left"]
+messageSends: ["visitAssignmentNode:", "left", "ifTrue:", "isImmutable", "errorInvalidAssignment:", "value", "assigned:"]
 }, function ($methodClass){ return function (aNode){
 var self=this,$self=this;
+var lhs;
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 return $core.withContext(function($ctx1) {
 //>>excludeEnd("ctx");
+var $1;
 (
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 $ctx1.supercall = true,
@@ -2285,10 +2284,15 @@ $ctx1.supercall = true,
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 $ctx1.supercall = false;
 //>>excludeEnd("ctx");;
-$recv($recv(aNode)._left())._beAssigned();
+lhs=$recv(aNode)._left();
+$1=$recv(lhs)._isImmutable();
+if($core.assert($1)){
+$self._errorInvalidAssignment_($recv(lhs)._value());
+}
+$recv(lhs)._assigned_(true);
 return self;
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
-}, function($ctx1) {$ctx1.fill(self,"visitAssignmentNode:",{aNode:aNode})});
+}, function($ctx1) {$ctx1.fill(self,"visitAssignmentNode:",{aNode:aNode,lhs:lhs})});
 //>>excludeEnd("ctx");
 }; }),
 $globals.SemanticAnalyzer);

+ 10 - 8
lang/src/Compiler-Semantic.st

@@ -266,13 +266,6 @@ isSuper
 
 isTempVar
 	^ false
-!
-
-validateAssignment
-	self isImmutable ifTrue: [
-		InvalidAssignmentError new
-			variableName: self name;
-			signal]
 ! !
 
 !ScopeVar class methodsFor: 'instance creation'!
@@ -480,6 +473,12 @@ thePackage: aPackage
 
 !SemanticAnalyzer methodsFor: 'error handling'!
 
+errorInvalidAssignment: aString
+	InvalidAssignmentError new
+		variableName: aString;
+		signal
+!
+
 errorShadowingVariable: aString
 	ShadowingVariableError new
 		variableName: aString;
@@ -567,8 +566,11 @@ isVariableKnown: aString inPackage: aPackage
 !SemanticAnalyzer methodsFor: 'visiting'!
 
 visitAssignmentNode: aNode
+	| lhs |
 	super visitAssignmentNode: aNode.
-	aNode left beAssigned
+	lhs := aNode left.
+	lhs isImmutable ifTrue: [ self errorInvalidAssignment: lhs value ].
+	lhs assigned: true
 !
 
 visitBlockNode: aNode