diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/runMain/kotlin/Lexical.kt | 9 | ||||
-rw-r--r-- | src/runMain/kotlin/Loader.kt | 2 | ||||
-rw-r--r-- | src/runMain/kotlin/Syntax.kt | 12 | ||||
-rw-r--r-- | src/runMain/kotlin/exceptions/SyntaxError.kt | 3 |
4 files changed, 15 insertions, 11 deletions
diff --git a/src/runMain/kotlin/Lexical.kt b/src/runMain/kotlin/Lexical.kt index 0d37065..4452235 100644 --- a/src/runMain/kotlin/Lexical.kt +++ b/src/runMain/kotlin/Lexical.kt @@ -7,20 +7,23 @@ class Lexical { */ fun analyze(source: String): MutableList<MutableList<Token>> { var buffer = "" + var lineNumber = 1 var skipStatementEnd = false var statementEnd: Boolean val statements = mutableListOf<MutableList<Token>>() val currentStatement = mutableListOf<Token>() for (i in source.indices) { - buffer += source[i] if (source[i] == '"') skipStatementEnd = !skipStatementEnd + if (source[i] == '\n') lineNumber++ statementEnd = source[i] == ';' && !skipStatementEnd + buffer += source[i] + val tokenType = getTokenType(buffer, if (source.length > i + 1) source[i + 1] else ' ') if (tokenType != Skip && !statementEnd) { val newToken = Token() newToken.content = buffer newToken.type = tokenType - newToken.lineNumber = 1 + newToken.lineNumber = lineNumber currentStatement.add(newToken) buffer = "" } else if (statementEnd) { @@ -80,5 +83,5 @@ class Lexical { private val punctuation = listOf(",", ":", ".", ";") private val brackets = listOf("(", ")", "[", "]", "{", "}") // TODO: Use brackets for functions private val classifier = listOf("\"", "'") - private val emptiness = listOf(" ", "\t") + private val emptiness = listOf(" ", "\t", "\n") } diff --git a/src/runMain/kotlin/Loader.kt b/src/runMain/kotlin/Loader.kt index 13e1157..0ec1817 100644 --- a/src/runMain/kotlin/Loader.kt +++ b/src/runMain/kotlin/Loader.kt @@ -9,7 +9,7 @@ class Loader(path: String) { * TODO: Add preprocessor managing imports and comments */ fun preprocess(): String { - return inputString.replace("\n", "") + return inputString } /** diff --git a/src/runMain/kotlin/Syntax.kt b/src/runMain/kotlin/Syntax.kt index 938814d..8ba809c 100644 --- a/src/runMain/kotlin/Syntax.kt +++ b/src/runMain/kotlin/Syntax.kt @@ -13,16 +13,16 @@ class Syntax { statement.forEach { println("${it.content} ${it.type}") } statement.forEachIndexed { j, token -> if (statement.count { it.content == "(" } != statement.count { it.content == ")" }) - throw SyntaxError("Bracket", "Brackets are not even") + throw SyntaxError("Bracket", "Brackets are not even", token.lineNumber) when (token.type) { Keyword -> allowNext(statement, j, listOf(Bracket)) Function -> allowNext(statement, j, listOf(Bracket)) Constant -> allowNext(statement, j, listOf(Comparison, Arithmetic, Logical, Bracket)) - Assignment -> allowNext(statement, j, listOf(Constant, Function)) - Arithmetic -> allowNext(statement, j, listOf(Constant, Function)) - Comparison -> allowNext(statement, j, listOf(Constant, Function)) - Logical -> allowNext(statement, j, listOf(Constant, Function)) + Assignment -> allowNext(statement, j, listOf(Constant, Function, Variable)) + Arithmetic -> allowNext(statement, j, listOf(Constant, Function, Variable)) + Comparison -> allowNext(statement, j, listOf(Constant, Function, Variable)) + Logical -> allowNext(statement, j, listOf(Constant, Function, Variable)) Variable -> allowNext(statement, j, listOf(Assignment, Bracket, Arithmetic, Logical, Comparison)) Punctuation -> allowNext(statement, j, listOf(Constant)) Bracket -> allowNext(statement, j, listOf(Constant, Keyword, Logical, Assignment, Variable)) @@ -82,7 +82,7 @@ class Syntax { private fun allowNext(statement: MutableList<Token>, index: kotlin.Int, allowed: List<TokenType>) { if (statement.size > index + 1 && nextNonEmpty(statement, index).type !in allowed) { val token = nextNonEmpty(statement, index) - throw SyntaxError(token.type.toString(), token.content) + throw SyntaxError(token.type.toString(), token.content, token.lineNumber) } } diff --git a/src/runMain/kotlin/exceptions/SyntaxError.kt b/src/runMain/kotlin/exceptions/SyntaxError.kt index 8609d38..b5ad163 100644 --- a/src/runMain/kotlin/exceptions/SyntaxError.kt +++ b/src/runMain/kotlin/exceptions/SyntaxError.kt @@ -3,4 +3,5 @@ package exceptions /** * Gets thrown if the syntax is wrong */ -class SyntaxError(description: String, context: String) : Exception("Unexpected $description: $context")
\ No newline at end of file +class SyntaxError(description: String, context: String, lineNumber: Int) + : Exception("Unexpected $description at line $lineNumber: $context")
\ No newline at end of file |