1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2024-12-19 15:29:48 +00:00

Disallowing code outside methods. Closes #18.

This commit is contained in:
jespergravgaard 2017-12-01 23:25:53 +01:00
parent 3023540e15
commit fed99da261
3 changed files with 28 additions and 1 deletions

View File

@ -52,7 +52,16 @@ public class Pass1GenerateStatementSequence extends KickCBaseVisitor<Object> {
@Override
public Void visitFile(KickCParser.FileContext ctx) {
this.visit(ctx.stmtSeq());
List<KickCParser.StmtContext> stmts = ctx.stmtSeq().stmt();
for (KickCParser.StmtContext stmt : stmts) {
if (stmt instanceof KickCParser.StmtDeclarationContext || stmt instanceof KickCParser.StmtFunctionContext) {
this.visit(stmt);
} else {
program.getLog().append("Statement not allowed outside method. " + stmt.getText());
throw new CompileError("Statement not allowed outside method. " + stmt.getText());
}
}
return null;
}

View File

@ -241,6 +241,16 @@ public class TestPrograms extends TestCase {
compileAndCompare("forrangemin");
}
public void testStmtOutsideMethod() throws IOException, URISyntaxException {
try {
compileAndCompare("stmt-outside-method");
} catch (CompileError e) {
// expecting error!
return;
}
fail("Expected compile error.");
}
public void testUseUndeclared() throws IOException, URISyntaxException {
try {
compileAndCompare("useundeclared");

View File

@ -0,0 +1,8 @@
byte b=12;
b=b+1;
void main() {
byte* screen = $0400;
*screen = b;
}