mirror of
https://gitlab.com/camelot/kickc.git
synced 2024-12-25 11:32:07 +00:00
Intorduced better error when assigning a constant. Closes #19
This commit is contained in:
parent
ee5b113b3a
commit
defd957837
@ -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 {
|
||||
|
@ -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");
|
||||
|
7
src/main/java/dk/camelot64/kickc/test/assign-const.kc
Normal file
7
src/main/java/dk/camelot64/kickc/test/assign-const.kc
Normal file
@ -0,0 +1,7 @@
|
||||
const byte prime = 7;
|
||||
|
||||
void main() {
|
||||
prime = 13;
|
||||
byte* screen = $0400;
|
||||
*screen = prime;
|
||||
}
|
Loading…
Reference in New Issue
Block a user