diff --git a/compiler/src/prog8/ast/AstChecker.kt b/compiler/src/prog8/ast/AstChecker.kt index 3a97696b3..8423d7e95 100644 --- a/compiler/src/prog8/ast/AstChecker.kt +++ b/compiler/src/prog8/ast/AstChecker.kt @@ -7,6 +7,7 @@ import prog8.compiler.target.c64.FLOAT_MAX_POSITIVE import prog8.functions.BuiltinFunctions import prog8.optimizing.same import prog8.parser.ParsingFailedError +import java.io.File /** * General checks on the Ast @@ -632,6 +633,7 @@ private class AstChecker(private val namespace: INameScope, if(directive.parent !is INameScope || directive.parent is Module) err("this directive may only occur in a block") if(directive.args.size!=2 || directive.args[0].str==null || directive.args[1].str==null) err("invalid asminclude directive, expected arguments: \"filename\", \"scopelabel\"") + checkFileExists(directive, directive.args[0].str!!) } "%asmbinary" -> { if(directive.parent !is INameScope || directive.parent is Module) err("this directive may only occur in a block") @@ -641,6 +643,7 @@ private class AstChecker(private val namespace: INameScope, if(directive.args.size>=2 && directive.args[1].int==null) err(errormsg) if(directive.args.size==3 && directive.args[2].int==null) err(errormsg) if(directive.args.size>3) err(errormsg) + checkFileExists(directive, directive.args[0].str!!) } "%option" -> { if(directive.parent !is Block && directive.parent !is Module) err("this directive may only occur in a block or at module level") @@ -654,6 +657,14 @@ private class AstChecker(private val namespace: INameScope, return super.process(directive) } + private fun checkFileExists(directive: Directive, filename: String) { + var definingModule = directive.parent + while (definingModule !is Module) + definingModule = definingModule.parent + if (!(filename.startsWith("library:") || definingModule.importedFrom.resolveSibling(filename).toFile().isFile || File(filename).isFile)) + checkResult.add(NameError("included file not found: $filename", directive.position)) + } + override fun process(literalValue: LiteralValue): LiteralValue { if(!compilerOptions.floats && literalValue.type==DataType.FLOAT) { checkResult.add(SyntaxError("floating point used, but that is not enabled via options", literalValue.position)) diff --git a/docs/source/syntaxreference.rst b/docs/source/syntaxreference.rst index 35004b7ed..d6aca343a 100644 --- a/docs/source/syntaxreference.rst +++ b/docs/source/syntaxreference.rst @@ -123,6 +123,8 @@ Directives This directive can only be used inside a block. The assembler will include the file as binary bytes at this point, prog8 will not process this at all. The optional offset and length can be used to select a particular piece of the file. + The compiler first looks for the file relative to the same directory as the module containing this statement is in, + if the file can't be found there it is searched relative to the current directory. .. data:: %asminclude "", "scopelabel" @@ -133,6 +135,8 @@ Directives The scopelabel argument will be used as a prefix to access the labels from the included source code, otherwise you would risk symbol redefinitions or duplications. If you know what you are doing you can leave it as an empty string to not have a scope prefix. + The compiler first looks for the file relative to the same directory as the module containing this statement is in, + if the file can't be found there it is searched relative to the current directory. .. data:: %breakpoint diff --git a/examples/test.p8 b/examples/test.p8 index 2b3db4975..a4885af26 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -4,25 +4,7 @@ sub start() { - ubyte[81] array2 = 20 to 100 - word[] array3 = 3000 to 3100 - - c64scr.print_uw(len(array2)) - c64.CHROUT('\n') - c64scr.print_uw(len(array3)) - c64.CHROUT('\n') - c64.CHROUT('\n') - c64scr.print_ub(array2[0]) - c64.CHROUT(',') - c64scr.print_ub(array2[1]) - c64.CHROUT(',') - c64scr.print_ub(array2[2]) - c64.CHROUT('\n') - c64scr.print_w(array3[0]) - c64.CHROUT(',') - c64scr.print_w(array3[1]) - c64.CHROUT(',') - c64scr.print_w(array3[2]) - c64.CHROUT('\n') + %asminclude "primes.p8", "derp" + %asmbinary "primes.p8", 10, 20 } }