mirror of
https://gitlab.com/camelot/kickc.git
synced 2024-12-20 07:30:00 +00:00
Disallowing code outside methods. Closes #18.
This commit is contained in:
parent
3023540e15
commit
fed99da261
@ -52,7 +52,16 @@ public class Pass1GenerateStatementSequence extends KickCBaseVisitor<Object> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Void visitFile(KickCParser.FileContext ctx) {
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -241,6 +241,16 @@ public class TestPrograms extends TestCase {
|
|||||||
compileAndCompare("forrangemin");
|
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 {
|
public void testUseUndeclared() throws IOException, URISyntaxException {
|
||||||
try {
|
try {
|
||||||
compileAndCompare("useundeclared");
|
compileAndCompare("useundeclared");
|
||||||
|
@ -0,0 +1,8 @@
|
|||||||
|
|
||||||
|
byte b=12;
|
||||||
|
b=b+1;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
byte* screen = $0400;
|
||||||
|
*screen = b;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user