mirror of
https://gitlab.com/camelot/kickc.git
synced 2025-02-20 00:29:10 +00:00
Added a few enum error tests
This commit is contained in:
parent
291c87621e
commit
2acc1010ec
@ -1207,26 +1207,30 @@ public class Pass0GenerateStatementSequence extends KickCBaseVisitor<Object> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object visitEnumDef(KickCParser.EnumDefContext ctx) {
|
public Object visitEnumDef(KickCParser.EnumDefContext ctx) {
|
||||||
String enumDefName;
|
try {
|
||||||
if(ctx.NAME() != null) {
|
String enumDefName;
|
||||||
enumDefName = ctx.NAME().getText();
|
if(ctx.NAME() != null) {
|
||||||
} else {
|
enumDefName = ctx.NAME().getText();
|
||||||
enumDefName = getCurrentScope().allocateIntermediateVariableName();
|
} else {
|
||||||
|
enumDefName = getCurrentScope().allocateIntermediateVariableName();
|
||||||
|
}
|
||||||
|
EnumDefinition enumDefinition = new EnumDefinition(enumDefName, getCurrentScope());
|
||||||
|
getCurrentScope().add(enumDefinition);
|
||||||
|
this.currentEnum = enumDefinition;
|
||||||
|
scopeStack.push(currentEnum);
|
||||||
|
visit(ctx.enumMemberList());
|
||||||
|
scopeStack.pop();
|
||||||
|
this.currentEnum = null;
|
||||||
|
// Copy all members to upper-level scope
|
||||||
|
Scope parentScope = getCurrentScope();
|
||||||
|
while(parentScope instanceof StructDefinition) parentScope = parentScope.getScope();
|
||||||
|
for(ConstantVar member : enumDefinition.getAllConstants(false)) {
|
||||||
|
parentScope.add(new ConstantVar(member.getLocalName(), parentScope, SymbolType.BYTE, member.getValue()));
|
||||||
|
}
|
||||||
|
return SymbolType.BYTE;
|
||||||
|
} catch (CompileError e) {
|
||||||
|
throw new CompileError(e.getMessage(), new StatementSource(ctx));
|
||||||
}
|
}
|
||||||
EnumDefinition enumDefinition = new EnumDefinition(enumDefName, getCurrentScope());
|
|
||||||
getCurrentScope().add(enumDefinition);
|
|
||||||
this.currentEnum = enumDefinition;
|
|
||||||
scopeStack.push(currentEnum);
|
|
||||||
visit(ctx.enumMemberList());
|
|
||||||
scopeStack.pop();
|
|
||||||
this.currentEnum = null;
|
|
||||||
// Copy all members to upper-level scope
|
|
||||||
Scope parentScope = getCurrentScope();
|
|
||||||
while(parentScope instanceof StructDefinition) parentScope = parentScope.getScope();
|
|
||||||
for(ConstantVar member : enumDefinition.getAllConstants(false)) {
|
|
||||||
parentScope.add(new ConstantVar(member.getLocalName(), parentScope, SymbolType.BYTE, member.getValue()));
|
|
||||||
}
|
|
||||||
return SymbolType.BYTE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -35,6 +35,21 @@ public class TestPrograms {
|
|||||||
public TestPrograms() {
|
public TestPrograms() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEnumErr2() throws IOException, URISyntaxException {
|
||||||
|
assertError("enum-err-2", "Enum value not constant");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEnumErr1() throws IOException, URISyntaxException {
|
||||||
|
assertError("enum-err-1", "Symbol already declared");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEnumErr0() throws IOException, URISyntaxException {
|
||||||
|
assertError("enum-err-0", "Symbol already declared");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEnum8() throws IOException, URISyntaxException {
|
public void testEnum8() throws IOException, URISyntaxException {
|
||||||
compileAndCompare("enum-8");
|
compileAndCompare("enum-8");
|
||||||
|
15
src/test/kc/enum-err-0.kc
Normal file
15
src/test/kc/enum-err-0.kc
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
// Test of simple enum error - name already used
|
||||||
|
|
||||||
|
byte ON = 17;
|
||||||
|
|
||||||
|
enum State {
|
||||||
|
OFF,
|
||||||
|
ON
|
||||||
|
};
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
enum State state = ON;
|
||||||
|
const byte* SCREEN = 0x0400;
|
||||||
|
*SCREEN = state;
|
||||||
|
}
|
||||||
|
|
14
src/test/kc/enum-err-1.kc
Normal file
14
src/test/kc/enum-err-1.kc
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
// Test of simple enum error - name already used
|
||||||
|
|
||||||
|
enum State {
|
||||||
|
OFF,
|
||||||
|
ON,
|
||||||
|
OFF
|
||||||
|
};
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
enum State state = ON;
|
||||||
|
const byte* SCREEN = 0x0400;
|
||||||
|
*SCREEN = state;
|
||||||
|
}
|
||||||
|
|
15
src/test/kc/enum-err-2.kc
Normal file
15
src/test/kc/enum-err-2.kc
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
// Test of simple enum error - name already used
|
||||||
|
|
||||||
|
byte val;
|
||||||
|
|
||||||
|
enum State {
|
||||||
|
OFF,
|
||||||
|
ON = val
|
||||||
|
};
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
enum State state = ON;
|
||||||
|
const byte* SCREEN = 0x0400;
|
||||||
|
*SCREEN = state;
|
||||||
|
}
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user