diff options
author | Marvin Borner | 2019-08-14 22:31:46 +0200 |
---|---|---|
committer | Marvin Borner | 2019-08-14 22:31:46 +0200 |
commit | 938f655b120efd01a0fbdd5c3fde7dbc1b53d267 (patch) | |
tree | d7e7ff88280a110e1220573ff41e79cc98baa68b | |
parent | 38aad3dcda01fc5ed82556dbfdf4cfc30ff479d9 (diff) |
Using Kotlin/Native now
-rw-r--r-- | .gitignore | 9 | ||||
-rw-r--r-- | src/DataTypes.kt | 3 | ||||
-rw-r--r-- | src/Lexical.kt | 68 | ||||
-rw-r--r-- | src/Loader.kt | 12 | ||||
-rw-r--r-- | src/Main.kt | 5 | ||||
-rw-r--r-- | src/Syntax.kt | 7 | ||||
-rw-r--r-- | src/Testing.kt | 8 | ||||
-rw-r--r-- | src/TokenTypes.kt | 14 | ||||
-rw-r--r-- | src/exceptions/UnknownType.kt | 3 |
9 files changed, 6 insertions, 123 deletions
@@ -1,3 +1,6 @@ -.idea -out -*.iml
\ No newline at end of file +.gradle +.gitignore +build +gradle +gradlew +*.iml diff --git a/src/DataTypes.kt b/src/DataTypes.kt deleted file mode 100644 index 290481d..0000000 --- a/src/DataTypes.kt +++ /dev/null @@ -1,3 +0,0 @@ -class Int { - -}
\ No newline at end of file diff --git a/src/Lexical.kt b/src/Lexical.kt deleted file mode 100644 index df60a8e..0000000 --- a/src/Lexical.kt +++ /dev/null @@ -1,68 +0,0 @@ -import exceptions.UnknownType - -class Lexical { - fun analyze(source: String): MutableList<Pair<String, TokenType>> { - var buffer = "" - var stringMode = false - val tokens = mutableListOf<Pair<String, TokenType>>() - for (i in source.indices) { - buffer += source[i] - val tokenType = getTokenType(buffer, if (source.length > i + 1) source[i + 1] else ' ', stringMode) - if (tokenType != TokenType.Skip) { - tokens += buffer to tokenType - if (buffer == "\"") stringMode = true - buffer = "" - } - if (tokenType == TokenType.Empty) buffer = "" - } - return tokens - } - - /** - * Matches the tokens to a [TokenType] - */ - private fun getTokenType(token: String, next: Char, stringMode: Boolean): TokenType { - return when { - token + next in keyword -> TokenType.Skip - token in keyword -> TokenType.Keyword - - token + next in comparison -> TokenType.Skip - token in assignment -> TokenType.Assignment - - token + next in assignment -> TokenType.Skip - token in arithmetic -> TokenType.Arithmetic - - token + next in comparison -> TokenType.Skip - token in comparison -> TokenType.Comparison - - token + next in comparison -> TokenType.Skip - token in logical -> TokenType.Logical - - (token + next).matches(Regex("[a-zA-Z]*")) -> TokenType.Skip - token.matches(Regex("[a-zA-Z]*")) -> TokenType.Identifier - - (token + next).matches(Regex("[0-9]*")) -> TokenType.Skip - token.matches(Regex("[0-9]*")) -> TokenType.Constant - - token in punctuation -> TokenType.Punctuation - - token in brackets -> TokenType.Bracket - - token in classifier -> TokenType.Classifier - - token.contains(" ") && token.length > 1 && !stringMode -> throw UnknownType(token) - token == " " && !stringMode -> TokenType.Empty - - else -> TokenType.Skip - } - } - - private val keyword = listOf("print") // TODO: DataType matching - private val assignment = listOf("=", "+=", "-=", "*=", "/*") - private val arithmetic = listOf("+", "-", "*", "/", "%") - private val comparison = listOf("==", "!=", "<", "<=", ">", ">=") - private val logical = listOf("&&", "||", "!") - private val punctuation = listOf(",", ":", ".") - private val brackets = listOf("(", ")", "[", "]", "{", "}") // TODO: Use brackets for functions - private val classifier = listOf("\"") // TODO: Add char mode e.g 'a' -} diff --git a/src/Loader.kt b/src/Loader.kt deleted file mode 100644 index 1972939..0000000 --- a/src/Loader.kt +++ /dev/null @@ -1,12 +0,0 @@ -import java.io.File -import java.io.InputStream - -class Loader(path: String) { - private val inputStream: InputStream = File(path).inputStream() - private val inputString = inputStream.bufferedReader().use { it.readText() } - - // TODO: Add preprocessor managing imports and comments - fun load(): String { - return inputString - } -}
\ No newline at end of file diff --git a/src/Main.kt b/src/Main.kt deleted file mode 100644 index 24a2e08..0000000 --- a/src/Main.kt +++ /dev/null @@ -1,5 +0,0 @@ -import Testing - -fun main() { - Testing() -} diff --git a/src/Syntax.kt b/src/Syntax.kt deleted file mode 100644 index 7d7ab9d..0000000 --- a/src/Syntax.kt +++ /dev/null @@ -1,7 +0,0 @@ -class Syntax { - fun check(tokens: MutableList<Pair<String, TokenType>>) { - for (token in tokens) { - print(token) - } - } -}
\ No newline at end of file diff --git a/src/Testing.kt b/src/Testing.kt deleted file mode 100644 index d1e6b17..0000000 --- a/src/Testing.kt +++ /dev/null @@ -1,8 +0,0 @@ -class Testing { - init { - val source = Loader("/home/melvin/coding/run/example.run").load() - val tokens = Lexical().analyze(source) - Syntax().check(tokens) - print(source) - } -}
\ No newline at end of file diff --git a/src/TokenTypes.kt b/src/TokenTypes.kt deleted file mode 100644 index 9e15b5a..0000000 --- a/src/TokenTypes.kt +++ /dev/null @@ -1,14 +0,0 @@ -enum class TokenType { - Keyword, - Assignment, - Arithmetic, - Comparison, - Logical, - Identifier, - Constant, - Punctuation, - Bracket, - Classifier, - Empty, - Skip -}
\ No newline at end of file diff --git a/src/exceptions/UnknownType.kt b/src/exceptions/UnknownType.kt deleted file mode 100644 index 4c348a5..0000000 --- a/src/exceptions/UnknownType.kt +++ /dev/null @@ -1,3 +0,0 @@ -package exceptions - -class UnknownType(token: String = "") : Exception("Token type not known: $token")
\ No newline at end of file |