aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarvin Borner2019-08-26 00:06:12 +0200
committerMarvin Borner2019-08-26 00:06:12 +0200
commitd9f3c4329e8cf4de1435c6390a44f3a8399cfd3d (patch)
tree1ff65bf3abb1e0117311d965e5cf6e2ffa24dc12
parent777c20de7791fd254ce747f7d7ab4b681c5a6001 (diff)
Added undeclared variable exception
-rw-r--r--src/runMain/kotlin/Semantic.kt10
-rw-r--r--src/runMain/kotlin/Testing.kt3
2 files changed, 10 insertions, 3 deletions
diff --git a/src/runMain/kotlin/Semantic.kt b/src/runMain/kotlin/Semantic.kt
index 38dbd38..b903f84 100644
--- a/src/runMain/kotlin/Semantic.kt
+++ b/src/runMain/kotlin/Semantic.kt
@@ -6,11 +6,17 @@ class Semantic {
* TODO: Handle scopes via { and }
*/
fun check(statements: MutableList<MutableList<Token>>): Boolean {
- val variables = mutableListOf<List<Token>>()
+ val variables = mutableListOf<String>()
statements.forEach { statement ->
if (statement[0].type == Variable) {
if (nextNonEmpty(statement, 0).type == Assignment) {
- variables.add(statement.take(5))
+ variables.add(statement[0].content)
+ }
+ }
+
+ for (token in statement) {
+ if (token.type == Variable && token.content !in variables) {
+ throw Exception("Undeclared variable")
}
}
}
diff --git a/src/runMain/kotlin/Testing.kt b/src/runMain/kotlin/Testing.kt
index a8aa288..434fe08 100644
--- a/src/runMain/kotlin/Testing.kt
+++ b/src/runMain/kotlin/Testing.kt
@@ -1,7 +1,8 @@
class Testing {
init {
- val source = Loader("/home/melvin/coding/runc/example.run").preprocess()
+ val source = Loader("/home/melvin/coding/run/example.run").preprocess()
val statements = Lexical().analyze(source)
Syntax().check(statements)
+ Semantic().check(statements)
}
} \ No newline at end of file