From 777c20de7791fd254ce747f7d7ab4b681c5a6001 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Sat, 17 Aug 2019 18:46:00 +0200 Subject: Renaming and semantic testing --- example.run | 3 ++- src/runMain/kotlin/Lexical.kt | 2 +- src/runMain/kotlin/Semantic.kt | 11 +++++++++++ src/runMain/kotlin/Syntax.kt | 11 +---------- src/runMain/kotlin/Token.kt | 9 +++++++++ src/runMain/kotlin/exceptions/LexicalError.kt | 6 ++++++ src/runMain/kotlin/exceptions/UnknownType.kt | 6 ------ 7 files changed, 30 insertions(+), 18 deletions(-) create mode 100644 src/runMain/kotlin/exceptions/LexicalError.kt delete mode 100644 src/runMain/kotlin/exceptions/UnknownType.kt diff --git a/example.run b/example.run index b6fa3dc..762bf5d 100644 --- a/example.run +++ b/example.run @@ -1 +1,2 @@ -print "hallo"; print baum ; +test = 123; +print(test); \ No newline at end of file diff --git a/src/runMain/kotlin/Lexical.kt b/src/runMain/kotlin/Lexical.kt index 4f6c419..ff17d17 100644 --- a/src/runMain/kotlin/Lexical.kt +++ b/src/runMain/kotlin/Lexical.kt @@ -63,7 +63,7 @@ class Lexical { (token + next).matches(Regex("[0-9]*")) -> Skip token.matches(Regex("[0-9]*")) -> Constant - token in emptiness && token.length > 1 -> throw UnknownType(token) + token in emptiness && token.length > 1 -> throw LexicalError(token) token in emptiness -> Empty token in punctuation -> Punctuation diff --git a/src/runMain/kotlin/Semantic.kt b/src/runMain/kotlin/Semantic.kt index 1657eeb..38dbd38 100644 --- a/src/runMain/kotlin/Semantic.kt +++ b/src/runMain/kotlin/Semantic.kt @@ -1,8 +1,19 @@ +import TokenType.* + class Semantic { /** * Checks and validates whether the code complies with the semantic rules + * TODO: Handle scopes via { and } */ fun check(statements: MutableList>): Boolean { + val variables = mutableListOf>() + statements.forEach { statement -> + if (statement[0].type == Variable) { + if (nextNonEmpty(statement, 0).type == Assignment) { + variables.add(statement.take(5)) + } + } + } return true } } \ No newline at end of file diff --git a/src/runMain/kotlin/Syntax.kt b/src/runMain/kotlin/Syntax.kt index 54a0ebe..950cd4c 100644 --- a/src/runMain/kotlin/Syntax.kt +++ b/src/runMain/kotlin/Syntax.kt @@ -97,7 +97,7 @@ class Syntax { Classifier -> Unit Empty -> Unit StatementEnd -> Unit - Skip -> throw UnknownType(token.content) + Skip -> throw LexicalError(token.content) } } @@ -110,13 +110,4 @@ class Syntax { throw SyntaxError(token.type.toString().toLowerCase(), token.content, token.lineNumber) } } - - /** - * Finds the next non empty token by [index] - */ - private fun nextNonEmpty(statement: MutableList, index: Int): Token { - var i = index + 1 - while (statement[i].type == Empty) i++ - return statement[i] - } } \ No newline at end of file diff --git a/src/runMain/kotlin/Token.kt b/src/runMain/kotlin/Token.kt index d132968..df5d3d2 100644 --- a/src/runMain/kotlin/Token.kt +++ b/src/runMain/kotlin/Token.kt @@ -3,3 +3,12 @@ class Token { lateinit var type: TokenType var lineNumber: Int = 0 } + +/** + * Finds the next non empty token by [index] + */ +fun nextNonEmpty(statement: MutableList, index: Int): Token { + var i = index + 1 + while (statement[i].type == TokenType.Empty) i++ + return statement[i] +} \ No newline at end of file diff --git a/src/runMain/kotlin/exceptions/LexicalError.kt b/src/runMain/kotlin/exceptions/LexicalError.kt new file mode 100644 index 0000000..a891d3f --- /dev/null +++ b/src/runMain/kotlin/exceptions/LexicalError.kt @@ -0,0 +1,6 @@ +package exceptions + +/** + * Gets thrown if the entered token/code does not exist in this language + */ +class LexicalError(token: String = "") : Exception("Token type not known: $token") \ No newline at end of file diff --git a/src/runMain/kotlin/exceptions/UnknownType.kt b/src/runMain/kotlin/exceptions/UnknownType.kt deleted file mode 100644 index a696891..0000000 --- a/src/runMain/kotlin/exceptions/UnknownType.kt +++ /dev/null @@ -1,6 +0,0 @@ -package exceptions - -/** - * Gets thrown if the entered token/code does not exist in this language - */ -class UnknownType(token: String = "") : Exception("Token type not known: $token") \ No newline at end of file -- cgit v1.2.3