From 182ef2a14d947c0cff7d2312fcdc609b3662972b Mon Sep 17 00:00:00 2001 From: Seggan Date: Mon, 15 Jul 2024 14:42:31 -0400 Subject: [PATCH] Move stuff out fo `init` --- .../kotlin/io/github/seggan/metis/app/Main.kt | 2 + .../io/github/seggan/metis/runtime/State.kt | 98 +++++++++++-------- .../metis/runtime/intrinsics/ModuleLoader.kt | 4 +- 3 files changed, 63 insertions(+), 41 deletions(-) diff --git a/metis-app/src/main/kotlin/io/github/seggan/metis/app/Main.kt b/metis-app/src/main/kotlin/io/github/seggan/metis/app/Main.kt index e4dd8da..3f3d862 100644 --- a/metis-app/src/main/kotlin/io/github/seggan/metis/app/Main.kt +++ b/metis-app/src/main/kotlin/io/github/seggan/metis/app/Main.kt @@ -55,6 +55,8 @@ private object Main : CliktCommand(name = "metis") { println(chunk) } val state = State() + state.loadCoreGlobals() + state.loadStandardLibrary() state.loadChunk(chunk) state.call(0) if (debug) { diff --git a/metis-lang/src/main/kotlin/io/github/seggan/metis/runtime/State.kt b/metis-lang/src/main/kotlin/io/github/seggan/metis/runtime/State.kt index 14e084b..6110dc4 100644 --- a/metis-lang/src/main/kotlin/io/github/seggan/metis/runtime/State.kt +++ b/metis-lang/src/main/kotlin/io/github/seggan/metis/runtime/State.kt @@ -129,48 +129,59 @@ class State(val parentState: State? = null) { globals["false"] = Value.Boolean.FALSE globals["null"] = Value.Null - for ((name, value) in Intrinsics.intrinsics) { - globals[name] = value - } - - val io = Value.Table() - io["stdout"] = zeroArgFunction { Value.Native(stdout, NativeObjects.OUTPUT_STREAM) } - io["stderr"] = zeroArgFunction { Value.Native(stderr, NativeObjects.OUTPUT_STREAM) } - io["stdin"] = zeroArgFunction { Value.Native(stdin, NativeObjects.INPUT_STREAM) } - - io["inStream"] = NativeObjects.INPUT_STREAM - io["outStream"] = NativeObjects.OUTPUT_STREAM - globals["io"] = io - - globals["string"] = Value.String.metatable - globals["number"] = Value.Number.metatable - globals["table"] = Value.Table.metatable - globals["list"] = Value.List.metatable - globals["bytes"] = Value.Bytes.metatable - globals["coroutine"] = Coroutine.metatable - - val pkg = Value.Table() - pkg["path"] = Value.List(mutableListOf("./".metisValue(), "/usr/lib/metis/".metisValue())) - pkg["loaded"] = Value.Table() - globals["package"] = pkg - - runCode(CodeSource("core") { State::class.java.classLoader.getResource("core.metis")!!.readText() }) - for (script in coreScripts) { - runCode(CodeSource(script) { - State::class.java.getResource("/core/$it.metis")!!.readText() - }) - } + loaders.add(NativeLoader(nativeLibraries)) + } + } - addNativeLibrary(OsLib) - addNativeLibrary(RegexLib) - addNativeLibrary(PathLib) - addNativeLibrary(MathLib) - addNativeLibrary(RandomLib) + /** + * Loads all the core globals into the state, like `print`, `io`, etc. + */ + fun loadCoreGlobals() { + for ((name, value) in Intrinsics.intrinsics) { + globals[name] = value + } - loaders.add(ResourceLoader) - loaders.add(NativeLoader(nativeLibraries)) - loaders.add(FileLoader) + val io = Value.Table() + io["stdout"] = zeroArgFunction { Value.Native(stdout, NativeObjects.OUTPUT_STREAM) } + io["stderr"] = zeroArgFunction { Value.Native(stderr, NativeObjects.OUTPUT_STREAM) } + io["stdin"] = zeroArgFunction { Value.Native(stdin, NativeObjects.INPUT_STREAM) } + + io["inStream"] = NativeObjects.INPUT_STREAM + io["outStream"] = NativeObjects.OUTPUT_STREAM + globals["io"] = io + + globals["string"] = Value.String.metatable + globals["number"] = Value.Number.metatable + globals["table"] = Value.Table.metatable + globals["list"] = Value.List.metatable + globals["bytes"] = Value.Bytes.metatable + globals["coroutine"] = Coroutine.metatable + + val pkg = Value.Table() + pkg["path"] = Value.List(mutableListOf("./".metisValue(), "/usr/lib/metis/".metisValue())) + pkg["loaded"] = Value.Table() + globals["package"] = pkg + + runCode(CodeSource("core") { State::class.java.classLoader.getResource("core.metis")!!.readText() }) + for (script in coreScripts) { + runCode(CodeSource(script) { + State::class.java.getResource("/core/$it.metis")!!.readText() + }) } + + loaders.add(FileLoader) + } + + /** + * Loads the standard library into the state. + */ + fun loadStandardLibrary() { + addNativeLibrary(OsLib) + addNativeLibrary(RegexLib) + addNativeLibrary(PathLib) + addNativeLibrary(MathLib) + addNativeLibrary(RandomLib) + loaders.add(ResourcesLoader) } /** @@ -191,6 +202,15 @@ class State(val parentState: State? = null) { nativeLibraries.add(lib) } + /** + * Removes a [NativeLibrary] from the state. + * + * @param name The library to remove. + */ + fun removeNativeLibrary(name: String) { + nativeLibraries.removeIf { it.name == name } + } + /** * Steps the state. * diff --git a/metis-lang/src/main/kotlin/io/github/seggan/metis/runtime/intrinsics/ModuleLoader.kt b/metis-lang/src/main/kotlin/io/github/seggan/metis/runtime/intrinsics/ModuleLoader.kt index a7a5c4e..efa9811 100644 --- a/metis-lang/src/main/kotlin/io/github/seggan/metis/runtime/intrinsics/ModuleLoader.kt +++ b/metis-lang/src/main/kotlin/io/github/seggan/metis/runtime/intrinsics/ModuleLoader.kt @@ -23,9 +23,9 @@ fun interface ModuleLoader { /** * A loader that loads from the Metis JAR's resources */ -object ResourceLoader : ModuleLoader { +object ResourcesLoader : ModuleLoader { override fun load(state: State, module: String): CallableValue? { - return ResourceLoader::class.java.getResource("/std/$module.metis")?.let { resource -> + return ResourcesLoader::class.java.getResource("/std/$module.metis")?.let { resource -> val source = CodeSource(module) { resource.readText() } val chunk = Chunk.load(source) chunk.Instance(state)