From 0b8e3183b7cbbf86553cdbad45eb386f75eb8b94 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Mon, 13 Aug 2018 12:07:16 +0200 Subject: [PATCH] import lib paths --- il65/src/il65/Main.kt | 26 +++++++++++++++++++------- il65/src/il65/ast/AST.kt | 10 +++++----- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/il65/src/il65/Main.kt b/il65/src/il65/Main.kt index 66ab4d8f2..b77f1c1d9 100644 --- a/il65/src/il65/Main.kt +++ b/il65/src/il65/Main.kt @@ -86,12 +86,24 @@ fun fileNameWithoutSuffix(filePath: Path) = filePath.fileName.toString().substringBeforeLast('.') -fun discoverImportedModule(name: String, importedFrom: Path): Path { - val tryName = Paths.get(importedFrom.parent.toString(), name + ".ill") - if(Files.exists(tryName)) - return tryName - else - throw ParsingFailedError("No such module source file: $tryName") +fun discoverImportedModule(name: String, importedFrom: Path, position: Position?): Path { + val fileName = name + ".ill" + val locations = mutableListOf(Paths.get(importedFrom.parent.toString())) + + val propPath = System.getProperty("il65.libpath") + if(propPath!=null) + locations.add(Paths.get(propPath)) + val envPath = System.getenv("IL65_LIBPATH") + if(envPath!=null) + locations.add(Paths.get(envPath)) + locations.add(Paths.get(Paths.get("").toAbsolutePath().toString(), "lib65")) + + locations.forEach { + val file = Paths.get(it.toString(), fileName) + if (Files.isReadable(file)) return file + } + + throw ParsingFailedError("$position Import: no module source file '$fileName' found (I've looked in: $locations)") } @@ -102,7 +114,7 @@ fun executeImportDirective(import: Directive, importedFrom: Path): Module? { if(importedModules.containsKey(moduleName)) return null - val modulePath = discoverImportedModule(moduleName, importedFrom) + val modulePath = discoverImportedModule(moduleName, importedFrom, import.position) val importedModule = loadModule(modulePath) importedModule.checkImportValid() diff --git a/il65/src/il65/ast/AST.kt b/il65/src/il65/ast/AST.kt index c053853b6..568a30d19 100644 --- a/il65/src/il65/ast/AST.kt +++ b/il65/src/il65/ast/AST.kt @@ -250,7 +250,7 @@ data class AssignTarget(val register: Register?, val identifier: Identifier?) : identifier?.linkParents(this) } - fun process(processor: IAstProcessor) = this // for now + fun process(processor: IAstProcessor) = this } @@ -649,8 +649,8 @@ private fun il65Parser.LabeldefContext.toAst(withPosition: Boolean): IStatement private fun il65Parser.SubroutineContext.toAst(withPosition: Boolean) : Subroutine { val sub = Subroutine(identifier().text, - if(sub_params()==null) emptyList() else sub_params().toAst(withPosition), - if(sub_returns()==null) emptyList() else sub_returns().toAst(withPosition), + if(sub_params()==null) emptyList() else sub_params().toAst(), + if(sub_returns()==null) emptyList() else sub_returns().toAst(), sub_address()?.integerliteral()?.toAst(), if(sub_body()==null) emptyList() else sub_body().statement().map {it.toAst(withPosition)}) sub.position = toPosition(withPosition) @@ -658,11 +658,11 @@ private fun il65Parser.SubroutineContext.toAst(withPosition: Boolean) : Subrouti } -private fun il65Parser.Sub_paramsContext.toAst(withPosition: Boolean): List = +private fun il65Parser.Sub_paramsContext.toAst(): List = sub_param().map { SubroutineParameter(it.identifier().text, it.register().toAst()) } -private fun il65Parser.Sub_returnsContext.toAst(withPosition: Boolean): List = +private fun il65Parser.Sub_returnsContext.toAst(): List = sub_return().map { val isClobber = it.childCount==2 && it.children[1].text == "?" SubroutineReturnvalue(it.register().toAst(), isClobber)