%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.functions.builtinFunctionReturnType
import prog8.parser.prog8Parser import prog8.parser.prog8Parser
import java.io.File import java.io.File
import java.nio.file.Path
import kotlin.math.abs import kotlin.math.abs
import kotlin.math.floor import kotlin.math.floor
@ -456,7 +457,8 @@ class BuiltinFunctionStatementPlaceholder(val name: String, override val positio
class Module(override val name: String, class Module(override val name: String,
override var statements: MutableList<IStatement>, override var statements: MutableList<IStatement>,
override val position: Position, override val position: Position,
val isLibraryModule: Boolean) : Node, INameScope { val isLibraryModule: Boolean,
val importedFrom: Path) : Node, INameScope {
override lateinit var parent: Node override lateinit var parent: Node
override fun linkParents(parent: Node) { override fun linkParents(parent: Node) {
@ -1746,8 +1748,8 @@ class RepeatLoop(var body: AnonymousScope,
/***************** Antlr Extension methods to create AST ****************/ /***************** Antlr Extension methods to create AST ****************/
fun prog8Parser.ModuleContext.toAst(name: String, isLibrary: Boolean) : Module = fun prog8Parser.ModuleContext.toAst(name: String, isLibrary: Boolean, importedFrom: Path) : Module =
Module(name, modulestatement().asSequence().map { it.toAst() }.toMutableList(), toPosition(), isLibrary) Module(name, modulestatement().asSequence().map { it.toAst() }.toMutableList(), toPosition(), isLibrary, importedFrom)
private fun ParserRuleContext.toPosition() : Position { private fun ParserRuleContext.toPosition() : Position {

View File

@ -11,6 +11,7 @@ import prog8.optimizing.same
import prog8.parser.tryGetEmbeddedResource import prog8.parser.tryGetEmbeddedResource
import prog8.stackvm.Syscall import prog8.stackvm.Syscall
import java.io.File import java.io.File
import java.nio.file.Path
import java.util.* import java.util.*
import kotlin.math.abs import kotlin.math.abs
@ -148,7 +149,7 @@ class Compiler(private val options: CompilationOptions) {
println("\nCreating stackVM code...") println("\nCreating stackVM code...")
val namespace = module.definingScope() 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) val translator = StatementTranslator(program, namespace, heap)
translator.process(module) translator.process(module)
@ -224,7 +225,7 @@ private class StatementTranslator(private val prog: IntermediateProgram,
is Return -> translate(stmt) is Return -> translate(stmt)
is Directive -> { is Directive -> {
when(stmt.directive) { when(stmt.directive) {
"%asminclude" -> translateAsmInclude(stmt.args) "%asminclude" -> translateAsmInclude(stmt.args, prog.importedFrom)
"%asmbinary" -> translateAsmBinary(stmt.args) "%asmbinary" -> translateAsmBinary(stmt.args)
"%breakpoint" -> { "%breakpoint" -> {
prog.line(stmt.position) 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 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 scopeprefixEnd = if(args[1].str!!.isNotBlank()) "\t.pend\n" else ""
val filename=args[0].str!! val filename=args[0].str!!
@ -2238,7 +2239,11 @@ private class StatementTranslator(private val prog: IntermediateProgram,
val resource = tryGetEmbeddedResource(filename.substring(8)) ?: throw IllegalArgumentException("library file '$filename' not found") val resource = tryGetEmbeddedResource(filename.substring(8)) ?: throw IllegalArgumentException("library file '$filename' not found")
resource.bufferedReader().use { it.readText() } resource.bufferedReader().use { it.readText() }
} else { } else {
// TODO: look in directory of parent source file first // 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() File(filename).readText()
} }
@ -2246,7 +2251,7 @@ private class StatementTranslator(private val prog: IntermediateProgram,
} }
private fun translateAsmBinary(args: List<DirectiveArg>) { 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.CompilerException
import prog8.compiler.HeapValues import prog8.compiler.HeapValues
import java.io.PrintStream 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, class ProgramBlock(val scopedname: String,
val shortname: String, val shortname: String,

View File

@ -42,7 +42,7 @@ fun importModule(stream: CharStream, moduleName: String, isLibrary: Boolean): Mo
// tokens.commentTokens().forEach { println(it) } // tokens.commentTokens().forEach { println(it) }
// convert to Ast // convert to Ast
val moduleAst = parseTree.toAst(moduleName, isLibrary) val moduleAst = parseTree.toAst(moduleName, isLibrary, Paths.get(stream.sourceName))
importedModules[moduleAst.name] = moduleAst importedModules[moduleAst.name] = moduleAst
// process imports // process imports

View File

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