diff --git a/compiler/src/prog8/CompilerMain.kt b/compiler/src/prog8/CompilerMain.kt index 0eb770d7d..7c77c238b 100644 --- a/compiler/src/prog8/CompilerMain.kt +++ b/compiler/src/prog8/CompilerMain.kt @@ -39,7 +39,7 @@ private fun compileMain(args: Array): Boolean { val watchMode by cli.option(ArgType.Boolean, fullName = "watch", description = "continuous compilation mode (watches for file changes), greatly increases compilation speed") val slowCodegenWarnings by cli.option(ArgType.Boolean, fullName = "slowwarn", description="show debug warnings about slow/problematic assembly code generation") val compilationTarget by cli.option(ArgType.String, fullName = "target", description = "target output of the compiler, currently '${C64Target.name}' and '${Cx16Target.name}' available").default(C64Target.name) - val libDirs by cli.option(ArgType.String, fullName="libdirs", description = "list of extra paths to search in for imported modules").multiple().delimiter(File.pathSeparator) + val sourceDirs by cli.option(ArgType.String, fullName="srcdirs", description = "list of extra paths to search in for imported modules").multiple().delimiter(File.pathSeparator) val moduleFiles by cli.argument(ArgType.String, fullName = "modules", description = "main module file(s) to compile").multiple(999) try { @@ -61,9 +61,9 @@ private fun compileMain(args: Array): Boolean { return false } - val libdirs = libDirs.toMutableList() - if(libdirs.firstOrNull()!=".") - libdirs.add(0, ".") + val srcdirs = sourceDirs.toMutableList() + if(srcdirs.firstOrNull()!=".") + srcdirs.add(0, ".") if(watchMode==true) { val watchservice = FileSystems.getDefault().newWatchService() @@ -74,7 +74,7 @@ private fun compileMain(args: Array): Boolean { val results = mutableListOf() for(filepathRaw in moduleFiles) { val filepath = pathFrom(filepathRaw).normalize() - val compilationResult = compileProgram(filepath, dontOptimize!=true, dontWriteAssembly!=true, slowCodegenWarnings==true, compilationTarget, libdirs, outputPath) + val compilationResult = compileProgram(filepath, dontOptimize!=true, dontWriteAssembly!=true, slowCodegenWarnings==true, compilationTarget, srcdirs, outputPath) results.add(compilationResult) } @@ -111,7 +111,7 @@ private fun compileMain(args: Array): Boolean { val filepath = pathFrom(filepathRaw).normalize() val compilationResult: CompilationResult try { - compilationResult = compileProgram(filepath, dontOptimize!=true, dontWriteAssembly!=true, slowCodegenWarnings==true, compilationTarget, libdirs, outputPath) + compilationResult = compileProgram(filepath, dontOptimize!=true, dontWriteAssembly!=true, slowCodegenWarnings==true, compilationTarget, srcdirs, outputPath) if(!compilationResult.success) return false } catch (x: ParsingFailedError) { diff --git a/compiler/src/prog8/compiler/Compiler.kt b/compiler/src/prog8/compiler/Compiler.kt index 6f67e2e87..27472c2cc 100644 --- a/compiler/src/prog8/compiler/Compiler.kt +++ b/compiler/src/prog8/compiler/Compiler.kt @@ -70,7 +70,7 @@ fun compileProgram(filepath: Path, writeAssembly: Boolean, slowCodegenWarnings: Boolean, compilationTarget: String, - libdirs: List, + sourceDirs: List, outputDir: Path): CompilationResult { var programName = "" lateinit var programAst: Program @@ -87,7 +87,7 @@ fun compileProgram(filepath: Path, try { val totalTime = measureTimeMillis { // import main module and everything it needs - val (ast, compilationOptions, imported) = parseImports(filepath, errors, compTarget, libdirs) + val (ast, compilationOptions, imported) = parseImports(filepath, errors, compTarget, sourceDirs) compilationOptions.slowCodegenWarnings = slowCodegenWarnings compilationOptions.optimize = optimize programAst = ast @@ -172,15 +172,15 @@ private class BuiltinFunctionsFacade(functions: Map): IBuilt } fun parseImports(filepath: Path, - errors: IErrorReporter, - compTarget: ICompilationTarget, - libdirs: List): Triple> { + errors: IErrorReporter, + compTarget: ICompilationTarget, + sourceDirs: List): Triple> { println("Compiler target: ${compTarget.name}. Parsing...") val bf = BuiltinFunctionsFacade(BuiltinFunctions) val programAst = Program(filepath.nameWithoutExtension, bf, compTarget) bf.program = programAst - val importer = ModuleImporter(programAst, compTarget.name, errors, libdirs) + val importer = ModuleImporter(programAst, compTarget.name, errors, sourceDirs) val importedModuleResult = importer.importModule(filepath) importedModuleResult.onFailure { throw it } errors.report() diff --git a/compiler/src/prog8/compiler/ModuleImporter.kt b/compiler/src/prog8/compiler/ModuleImporter.kt index a4f5344ee..2e5ec5fca 100644 --- a/compiler/src/prog8/compiler/ModuleImporter.kt +++ b/compiler/src/prog8/compiler/ModuleImporter.kt @@ -18,9 +18,9 @@ import kotlin.io.path.* class ModuleImporter(private val program: Program, private val compilationTargetName: String, val errors: IErrorReporter, - libdirs: List) { + sourceDirs: List) { - private val libpaths: List = libdirs.map { Path(it) } + private val libpaths: List = sourceDirs.map { Path(it) } fun importModule(filePath: Path): Result { val currentDir = Path("").absolute() diff --git a/compiler/test/TestCompilerOnExamples.kt b/compiler/test/TestCompilerOnExamples.kt index a5fa1854c..352fe3154 100644 --- a/compiler/test/TestCompilerOnExamples.kt +++ b/compiler/test/TestCompilerOnExamples.kt @@ -41,7 +41,7 @@ class TestCompilerOnExamples { writeAssembly = true, slowCodegenWarnings = false, compilationTarget = platform.name, - libdirs = listOf(), + sourceDirs = listOf(), outputDir ).assertSuccess("; $displayName") } diff --git a/compiler/test/TestCompilerOptionLibdirs.kt b/compiler/test/TestCompilerOptionLibdirs.kt index aeff77691..e81108dbd 100644 --- a/compiler/test/TestCompilerOptionLibdirs.kt +++ b/compiler/test/TestCompilerOptionLibdirs.kt @@ -19,7 +19,7 @@ import kotlin.io.path.writeText * from source file loading all the way through to running 64tass. */ @TestInstance(TestInstance.Lifecycle.PER_CLASS) -class TestCompilerOptionLibdirs { +class TestCompilerOptionSourcedirs { private lateinit var tempFileInWorkingDir: Path @@ -39,14 +39,14 @@ class TestCompilerOptionLibdirs { tempFileInWorkingDir.deleteExisting() } - private fun compileFile(filePath: Path, libdirs: List) = + private fun compileFile(filePath: Path, sourceDirs: List) = compileProgram( filepath = filePath, optimize = false, writeAssembly = true, slowCodegenWarnings = false, compilationTarget = Cx16Target.name, - libdirs, + sourceDirs, outputDir ) @@ -65,7 +65,7 @@ class TestCompilerOptionLibdirs { } @Test - fun testFilePathInWorkingDirRelativeTo1stInLibdirs() { + fun testFilePathInWorkingDirRelativeTo1stInSourcedirs() { val filepath = assumeReadableFile(tempFileInWorkingDir) compileFile(filepath.fileName, listOf(workingDir.toString())) .assertSuccess() @@ -86,10 +86,10 @@ class TestCompilerOptionLibdirs { } @Test - fun testFilePathOutsideWorkingDirRelativeTo1stInLibdirs() { + fun testFilePathOutsideWorkingDirRelativeTo1stInSourcedirs() { val filepath = assumeReadableFile(fixturesDir, "simple_main.p8") - val libdirs = listOf("$fixturesDir") - compileFile(filepath.fileName, libdirs) + val sourcedirs = listOf("$fixturesDir") + compileFile(filepath.fileName, sourcedirs) .assertSuccess() } diff --git a/compiler/test/helpers/compileXyz.kt b/compiler/test/helpers/compileXyz.kt index 20fe00e94..4e59e499c 100644 --- a/compiler/test/helpers/compileXyz.kt +++ b/compiler/test/helpers/compileXyz.kt @@ -38,7 +38,7 @@ internal fun compileFile( writeAssembly = true, slowCodegenWarnings = false, platform.name, - libdirs = listOf(), + sourceDirs = listOf(), outputDir ) } diff --git a/docs/source/building.rst b/docs/source/building.rst index 3352bb974..f1dc5df90 100644 --- a/docs/source/building.rst +++ b/docs/source/building.rst @@ -118,11 +118,13 @@ They are embedded into the packaged release version of the compiler so you don't where they are, but their names are still reserved. -User defined library files and -location -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -You can create library files yourself too that can be shared among programs. -You can tell the compiler where it should look for these files, by using -the libdirs command line option. +Importing other source files and specifying search location(s) +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +You can create multiple source files yourself to modularize your large programs into +multiple module files. You can also create "library" modules this way with handy routines, +that can be shared among programs. By importing those module files, you can use them in other modules. +It is possible to tell the compiler where it should look for these files, by using +the ``srcdirs`` command line option. .. _debugging: diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 76ccad171..edb4135fc 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -3,7 +3,6 @@ TODO For next compiler release ^^^^^^^^^^^^^^^^^^^^^^^^^ -- rename libdirs option to srcdirs? - can we derive module.name from module.source (taking just the filename base)? - can Position.file be a Path- making the source variable for nodes unnecessary? - address more questions/issues from the testability discussions. diff --git a/examples/test.p8 b/examples/test.p8 index b1146ae87..665e629a0 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,14 +1,7 @@ -%import textio - +%option enable_floats main { - str myBar = "main.bar" - -foo_bar: - %asminclude "compiler/test/fixtures/foo_bar.asm" ; FIXME: should be accessible from inside start() but give assembler error - - sub start() { - txt.print(myBar) - txt.print(&foo_bar) - return - } + sub start() { + float[] cs = 1 to 42 ; values are computed at compile time + cs[0] = 23 ; keep optimizer from removing it + } } diff --git a/httpCompilerService/src/prog8/http/TestHttp.kt b/httpCompilerService/src/prog8/http/TestHttp.kt index 3fdcf4137..2cae665c7 100644 --- a/httpCompilerService/src/prog8/http/TestHttp.kt +++ b/httpCompilerService/src/prog8/http/TestHttp.kt @@ -34,7 +34,7 @@ class RequestParser : Take { writeAssembly = true, slowCodegenWarnings = true, compilationTarget = "c64", - libdirs = emptyList(), + sourceDirs = emptyList(), outputDir = Path.of(".") ) return RsJson(Jsonding())