diff --git a/compiler/src/prog8/ast/AST.kt b/compiler/src/prog8/ast/AST.kt index d7dd90832..f84a06e0d 100644 --- a/compiler/src/prog8/ast/AST.kt +++ b/compiler/src/prog8/ast/AST.kt @@ -11,6 +11,7 @@ import prog8.functions.NotConstArgumentException import prog8.functions.builtinFunctionReturnType import prog8.parser.prog8Parser import java.io.File +import java.nio.file.Path import kotlin.math.abs import kotlin.math.floor @@ -456,7 +457,8 @@ class BuiltinFunctionStatementPlaceholder(val name: String, override val positio class Module(override val name: String, override var statements: MutableList, override val position: Position, - val isLibraryModule: Boolean) : Node, INameScope { + val isLibraryModule: Boolean, + val importedFrom: Path) : Node, INameScope { override lateinit var parent: Node override fun linkParents(parent: Node) { @@ -1746,8 +1748,8 @@ class RepeatLoop(var body: AnonymousScope, /***************** Antlr Extension methods to create AST ****************/ -fun prog8Parser.ModuleContext.toAst(name: String, isLibrary: Boolean) : Module = - Module(name, modulestatement().asSequence().map { it.toAst() }.toMutableList(), toPosition(), isLibrary) +fun prog8Parser.ModuleContext.toAst(name: String, isLibrary: Boolean, importedFrom: Path) : Module = + Module(name, modulestatement().asSequence().map { it.toAst() }.toMutableList(), toPosition(), isLibrary, importedFrom) private fun ParserRuleContext.toPosition() : Position { diff --git a/compiler/src/prog8/compiler/Compiler.kt b/compiler/src/prog8/compiler/Compiler.kt index eba188862..a8da38fcb 100644 --- a/compiler/src/prog8/compiler/Compiler.kt +++ b/compiler/src/prog8/compiler/Compiler.kt @@ -11,6 +11,7 @@ import prog8.optimizing.same import prog8.parser.tryGetEmbeddedResource import prog8.stackvm.Syscall import java.io.File +import java.nio.file.Path import java.util.* import kotlin.math.abs @@ -148,7 +149,7 @@ class Compiler(private val options: CompilationOptions) { println("\nCreating stackVM code...") val namespace = module.definingScope() - val program = IntermediateProgram(module.name, module.loadAddress, heap) + val program = IntermediateProgram(module.name, module.loadAddress, heap, module.importedFrom) val translator = StatementTranslator(program, namespace, heap) translator.process(module) @@ -224,7 +225,7 @@ private class StatementTranslator(private val prog: IntermediateProgram, is Return -> translate(stmt) is Directive -> { when(stmt.directive) { - "%asminclude" -> translateAsmInclude(stmt.args) + "%asminclude" -> translateAsmInclude(stmt.args, prog.importedFrom) "%asmbinary" -> translateAsmBinary(stmt.args) "%breakpoint" -> { prog.line(stmt.position) @@ -2229,7 +2230,7 @@ private class StatementTranslator(private val prog: IntermediateProgram, } } - private fun translateAsmInclude(args: List) { + private fun translateAsmInclude(args: List, importedFrom: Path) { val scopeprefix = if(args[1].str!!.isNotBlank()) "${args[1].str}\t.proc\n" else "" val scopeprefixEnd = if(args[1].str!!.isNotBlank()) "\t.pend\n" else "" val filename=args[0].str!! @@ -2238,15 +2239,19 @@ private class StatementTranslator(private val prog: IntermediateProgram, val resource = tryGetEmbeddedResource(filename.substring(8)) ?: throw IllegalArgumentException("library file '$filename' not found") resource.bufferedReader().use { it.readText() } } else { - // TODO: look in directory of parent source file first - File(filename).readText() + // first try in the same folder as where the containing file was imported from + val sib = importedFrom.resolveSibling(filename) + if(sib.toFile().isFile) + sib.toFile().readText() + else + File(filename).readText() } prog.instr(Opcode.INLINE_ASSEMBLY, callLabel=scopeprefix+sourcecode+scopeprefixEnd) } private fun translateAsmBinary(args: List) { - TODO("asmbinary not implemented $args") + TODO("asmbinary not implemented yet $args") } } diff --git a/compiler/src/prog8/compiler/intermediate/IntermediateProgram.kt b/compiler/src/prog8/compiler/intermediate/IntermediateProgram.kt index fd08c5749..7acc5fee3 100644 --- a/compiler/src/prog8/compiler/intermediate/IntermediateProgram.kt +++ b/compiler/src/prog8/compiler/intermediate/IntermediateProgram.kt @@ -4,9 +4,10 @@ import prog8.ast.* import prog8.compiler.CompilerException import prog8.compiler.HeapValues import java.io.PrintStream +import java.nio.file.Path -class IntermediateProgram(val name: String, var loadAddress: Int, val heap: HeapValues) { +class IntermediateProgram(val name: String, var loadAddress: Int, val heap: HeapValues, val importedFrom: Path) { class ProgramBlock(val scopedname: String, val shortname: String, diff --git a/compiler/src/prog8/parser/ModuleParsing.kt b/compiler/src/prog8/parser/ModuleParsing.kt index 35b6aad8f..362c2ff90 100644 --- a/compiler/src/prog8/parser/ModuleParsing.kt +++ b/compiler/src/prog8/parser/ModuleParsing.kt @@ -42,7 +42,7 @@ fun importModule(stream: CharStream, moduleName: String, isLibrary: Boolean): Mo // tokens.commentTokens().forEach { println(it) } // convert to Ast - val moduleAst = parseTree.toAst(moduleName, isLibrary) + val moduleAst = parseTree.toAst(moduleName, isLibrary, Paths.get(stream.sourceName)) importedModules[moduleAst.name] = moduleAst // process imports diff --git a/examples/test.p8 b/examples/test.p8 index 5af36944e..cf3015cf3 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -30,6 +30,7 @@ str hoi = "hoi" str hoi2 = "hoi2" + }