1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2024-12-18 08:30:18 +00:00

Intorduced better error when assigning a constant. Closes #19

This commit is contained in:
jespergravgaard 2017-12-03 14:26:28 +01:00
parent ee5b113b3a
commit defd957837
3 changed files with 26 additions and 3 deletions

View File

@ -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<VariableVersion> 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<VariableVersion> 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 {

View File

@ -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");

View File

@ -0,0 +1,7 @@
const byte prime = 7;
void main() {
prime = 13;
byte* screen = $0400;
*screen = prime;
}