diff --git a/src/main/java/dk/camelot64/kickc/passes/Pass1GenerateSingleStaticAssignmentForm.java b/src/main/java/dk/camelot64/kickc/passes/Pass1GenerateSingleStaticAssignmentForm.java index f4b7bf362..193fa6f13 100644 --- a/src/main/java/dk/camelot64/kickc/passes/Pass1GenerateSingleStaticAssignmentForm.java +++ b/src/main/java/dk/camelot64/kickc/passes/Pass1GenerateSingleStaticAssignmentForm.java @@ -45,9 +45,16 @@ public class Pass1GenerateSingleStaticAssignmentForm extends Pass1Base { if (assignedVar instanceof VariableUnversioned) { // Assignment to a non-versioned non-intermediary variable VariableUnversioned assignedSymbol = (VariableUnversioned) assignedVar; - VariableVersion version = assignedSymbol.createVersion(); + VariableVersion version; if(assignedSymbol.isDeclaredConstant()) { + Collection versions = assignedVar.getScope().getVersions(assignedSymbol); + if(versions.size()!=0) { + throw new CompileError("Error! Constants can not be modified "+statement); + } + version = assignedSymbol.createVersion(); version.setDeclaredConstant(true); + } else { + version = assignedSymbol.createVersion(); } statementLValue.setlValue(version.getRef()); } @@ -173,7 +180,7 @@ public class Pass1GenerateSingleStaticAssignmentForm extends Pass1Base { Scope scope = rSymbol.getScope(); Collection versions = scope.getVersions(rSymbol); if(versions.size()!=1) { - throw new RuntimeException("Error! Constants always must exactly one version "+rSymbol); + throw new CompileError("Error! Constants must have exactly one version "+rSymbol); } return versions.iterator().next(); } else { diff --git a/src/main/java/dk/camelot64/kickc/test/TestPrograms.java b/src/main/java/dk/camelot64/kickc/test/TestPrograms.java index d7f14de9e..3d6f0c65e 100644 --- a/src/main/java/dk/camelot64/kickc/test/TestPrograms.java +++ b/src/main/java/dk/camelot64/kickc/test/TestPrograms.java @@ -28,7 +28,6 @@ public class TestPrograms extends TestCase { compileAndCompare("constabsmin"); } - public void testBasicFloats() throws IOException, URISyntaxException { compileAndCompare("basic-floats"); } @@ -258,6 +257,16 @@ public class TestPrograms extends TestCase { compileAndCompare("forrangemin"); } + public void testAssignConst() throws IOException, URISyntaxException { + try { + compileAndCompare("assign-const"); + } catch (CompileError e) { + // expecting error! + return; + } + fail("Expected compile error."); + } + public void testStmtOutsideMethod() throws IOException, URISyntaxException { try { compileAndCompare("stmt-outside-method"); diff --git a/src/main/java/dk/camelot64/kickc/test/assign-const.kc b/src/main/java/dk/camelot64/kickc/test/assign-const.kc new file mode 100644 index 000000000..7bd3167f5 --- /dev/null +++ b/src/main/java/dk/camelot64/kickc/test/assign-const.kc @@ -0,0 +1,7 @@ +const byte prime = 7; + +void main() { + prime = 13; + byte* screen = $0400; + *screen = prime; +} \ No newline at end of file