diff options
author | Marvin Borner | 2019-08-14 23:58:10 +0200 |
---|---|---|
committer | Marvin Borner | 2019-08-14 23:58:10 +0200 |
commit | 8039d51f78f7a1cb0acc74a3e8f09d4a522cf6f2 (patch) | |
tree | f046eba3415f8385b0d20f54131c7ddf549959d5 | |
parent | 46f00834b7c11c35bda6857aeaefefe5c81bb5cc (diff) |
Added ending of statements via ;
-rw-r--r-- | src/runMain/kotlin/Lexical.kt | 19 | ||||
-rw-r--r-- | src/runMain/kotlin/Loader.kt | 11 | ||||
-rw-r--r-- | src/runMain/kotlin/Main.kt | 2 | ||||
-rw-r--r-- | src/runMain/kotlin/Syntax.kt | 6 | ||||
-rw-r--r-- | src/runMain/kotlin/Testing.kt | 6 |
5 files changed, 22 insertions, 22 deletions
diff --git a/src/runMain/kotlin/Lexical.kt b/src/runMain/kotlin/Lexical.kt index 8529036..7cd66af 100644 --- a/src/runMain/kotlin/Lexical.kt +++ b/src/runMain/kotlin/Lexical.kt @@ -1,20 +1,25 @@ -import exceptions.UnknownType +import exceptions.* class Lexical { - fun analyze(source: String): MutableList<Pair<String, TokenType>> { + fun analyze(source: String): MutableList<MutableList<Pair<String, TokenType>>> { var buffer = "" - var statementEnd = false - val tokens = mutableListOf<Pair<String, TokenType>>() + var statementEnd: Boolean + val statements = mutableListOf<MutableList<Pair<String, TokenType>>>() + val currentStatement = mutableListOf<Pair<String, TokenType>>() for (i in source.indices) { buffer += source[i] statementEnd = source[i] == ';' val tokenType = getTokenType(buffer, if (source.length > i + 1) source[i + 1] else ' ') - if (tokenType != TokenType.Skip) { - tokens += buffer to tokenType + if (tokenType != TokenType.Skip && !statementEnd) { + currentStatement.add(buffer to tokenType) + buffer = "" + } else if (statementEnd) { + statements.add(currentStatement.toMutableList()) + currentStatement.clear() buffer = "" } } - return tokens + return statements } /** diff --git a/src/runMain/kotlin/Loader.kt b/src/runMain/kotlin/Loader.kt index 7cec0a5..3aef50a 100644 --- a/src/runMain/kotlin/Loader.kt +++ b/src/runMain/kotlin/Loader.kt @@ -1,15 +1,12 @@ -import kotlinx.cinterop.ByteVar -import kotlinx.cinterop.allocArray -import kotlinx.cinterop.memScoped -import kotlinx.cinterop.toKString +import kotlinx.cinterop.* import platform.posix.* class Loader(path: String) { private val inputString = read(path) // TODO: Add preprocessor managing imports and comments - fun load(): String { - return inputString + fun preprocess(): String { + return inputString.replace("\n", "") } private fun read(path: String): String { @@ -17,7 +14,7 @@ class Loader(path: String) { val file = fopen(path, "r") if (file == null) { - perror("cannot open input file $path") + perror("Couldn't open file: $path") } memScoped { diff --git a/src/runMain/kotlin/Main.kt b/src/runMain/kotlin/Main.kt index 7b3e88b..8fe04be 100644 --- a/src/runMain/kotlin/Main.kt +++ b/src/runMain/kotlin/Main.kt @@ -1,3 +1,3 @@ fun main() { Testing() -} +}
\ No newline at end of file diff --git a/src/runMain/kotlin/Syntax.kt b/src/runMain/kotlin/Syntax.kt index 3b8cfc8..901b8d8 100644 --- a/src/runMain/kotlin/Syntax.kt +++ b/src/runMain/kotlin/Syntax.kt @@ -1,8 +1,6 @@ class Syntax { - fun check(tokens: MutableList<Pair<String, TokenType>>): Boolean { - for (token in tokens) { - print(token) - } + fun check(statements: MutableList<MutableList<Pair<String, TokenType>>>): Boolean { + print(statements) return true } }
\ No newline at end of file diff --git a/src/runMain/kotlin/Testing.kt b/src/runMain/kotlin/Testing.kt index 5ee9560..a8aa288 100644 --- a/src/runMain/kotlin/Testing.kt +++ b/src/runMain/kotlin/Testing.kt @@ -1,7 +1,7 @@ class Testing { init { - val source = Loader("/home/melvin/coding/run/example.run").load() - val tokens = Lexical().analyze(source) - Syntax().check(tokens) + val source = Loader("/home/melvin/coding/runc/example.run").preprocess() + val statements = Lexical().analyze(source) + Syntax().check(statements) } }
\ No newline at end of file |