Skip to content

Commit

Permalink
Move stuff out fo init
Browse files Browse the repository at this point in the history
  • Loading branch information
Seggan committed Jul 15, 2024
1 parent 3da1e16 commit 182ef2a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 41 deletions.
2 changes: 2 additions & 0 deletions metis-app/src/main/kotlin/io/github/seggan/metis/app/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
98 changes: 59 additions & 39 deletions metis-lang/src/main/kotlin/io/github/seggan/metis/runtime/State.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

/**
Expand All @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 182ef2a

Please sign in to comment.