%asminclude now first looks in the same folder as the module it is in

This commit is contained in:
Irmen de Jong 2019-01-24 00:35:30 +01:00
parent fab5e4b17f
commit a0f0e7a034
5 changed files with 20 additions and 11 deletions

View File

@ -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<IStatement>,
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 {

View File

@ -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<DirectiveArg>) {
private fun translateAsmInclude(args: List<DirectiveArg>, 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<DirectiveArg>) {
TODO("asmbinary not implemented $args")
TODO("asmbinary not implemented yet $args")
}
}

View File

@ -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,

View File

@ -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

View File

@ -30,6 +30,7 @@
str hoi = "hoi"
str hoi2 = "hoi2"
}