Skip to content

Commit

Permalink
Fix lexer and separate stream wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
Seggan committed Dec 1, 2023
1 parent 9b04750 commit f58ed1c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ object Lexer {
matchers.add(TokenMatcher.StringyLiteral('\'', Token.Type.BYTES))
}

private var pos = 0

/**
* Lexes the source code.
*
Expand All @@ -99,6 +97,7 @@ object Lexer {
fun lex(source: CodeSource): List<Token> {
val tokens = mutableListOf<Token>()
val code = StringBuilder(source.text)
var pos = 0
while (code.isNotEmpty()) {
val matched = mutableListOf<Pair<TokenMatcher.Match, Token>>()
for (matcher in matchers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ data class Span(val start: Int, val end: Int, val source: CodeSource) {
val lines = source.text.split("\n")
var pos = start
var line = 0
while (pos > lines[line].length) {
while (line < lines.size && pos > lines[line].length) {
pos -= lines[line].length + 1
line++
}
Expand Down
18 changes: 14 additions & 4 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 @@ -63,6 +63,16 @@ class State(parentState: State? = null) {
*/
var currentDir = fileSystem.getPath(System.getProperty("user.dir")).toAbsolutePath()

/**
* The function used to wrap [InputStream]s into Metis objects.
*/
var inStreamWrapper: InputStreamWrapper = InputStreamWrapper { Value.Native(it, inStreamMetatable) }

/**
* The function used to wrap [OutputStream]s into Metis objects.
*/
var outStreamWrapper: OutputStreamWrapper = OutputStreamWrapper { Value.Native(it, outStreamMetatable) }

internal val openUpvalues = ArrayDeque<Upvalue.Instance>()

private var throwingException: MetisRuntimeException? = null
Expand Down Expand Up @@ -125,9 +135,9 @@ class State(parentState: State? = null) {
}

val io = Value.Table()
io["stdout"] = zeroArgFunction { wrapOutStream(stdout) }
io["stderr"] = zeroArgFunction { wrapOutStream(stderr) }
io["stdin"] = zeroArgFunction { wrapInStream(stdin) }
io["stdout"] = zeroArgFunction { outStreamWrapper.wrap(stdout) }
io["stderr"] = zeroArgFunction { outStreamWrapper.wrap(stderr) }
io["stdin"] = zeroArgFunction { inStreamWrapper.wrap(stdin) }

io["inStream"] = inStreamMetatable
io["outStream"] = outStreamMetatable
Expand All @@ -148,7 +158,7 @@ class State(parentState: State? = null) {
runCode(CodeSource("core") { State::class.java.classLoader.getResource("core.metis")!!.readText() })
for (script in coreScripts) {
runCode(CodeSource(script) {
State::class.java.classLoader.getResource("./core/$it.metis")!!.readText()
State::class.java.getResource("/core/$it.metis")!!.readText()
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ object OsLib : NativeLibrary("os") {
*/
object PathLib : NativeLibrary("__path") {

private inline fun pathFunction(crossinline fn: (Path) -> Value) = oneArgFunction { self ->
private inline fun pathFunction(crossinline fn: State.(Path) -> Value) = oneArgFunction { self ->
translateIoError { fn(toPath(self)) }
}

Expand Down Expand Up @@ -154,8 +154,8 @@ object PathLib : NativeLibrary("__path") {
lib["createDir"] = pathFunction { it.createDirectory().absolutePathString().metisValue() }
lib["createDirs"] = pathFunction { it.createDirectories().absolutePathString().metisValue() }
lib["deleteRecursive"] = pathFunction { it.deleteRecursively(); Value.Null }
lib["openWrite"] = pathFunction { wrapOutStream(it.outputStream()) }
lib["openRead"] = pathFunction { wrapInStream(it.inputStream()) }
lib["openWrite"] = pathFunction { outStreamWrapper.wrap(it.outputStream()) }
lib["openRead"] = pathFunction { inStreamWrapper.wrap(it.inputStream()) }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,9 @@ internal val outStreamMetatable = buildTable { table ->
}
}

/**
* Wraps an [OutputStream] in a [Value].
*/
fun wrapOutStream(stream: OutputStream): Value = Value.Native(stream, outStreamMetatable)
fun interface OutputStreamWrapper {
fun wrap(stream: OutputStream): Value
}

internal val inStreamMetatable = buildTable { table ->
table["read"] = twoArgFunction(true) { self, buffer ->
Expand All @@ -88,10 +87,9 @@ internal val inStreamMetatable = buildTable { table ->
}
}

/**
* Wraps an [InputStream] in a [Value].
*/
fun wrapInStream(stream: InputStream): Value = Value.Native(stream, inStreamMetatable)
fun interface InputStreamWrapper {
fun wrap(stream: InputStream): Value
}

private val sbMetatable = buildTable { table ->
table["__append"] = twoArgFunction(true) { self, value ->
Expand Down

0 comments on commit f58ed1c

Please sign in to comment.