aboutsummaryrefslogtreecommitdiff
path: root/src/runMain
diff options
context:
space:
mode:
authorMarvin Borner2019-08-14 22:45:30 +0200
committerMarvin Borner2019-08-14 22:45:30 +0200
commitab91d3b7e92ab42c1b4c449797fee672bd5e2859 (patch)
tree461d0482c6b70cb425edc629797244d57ab8012f /src/runMain
parent938f655b120efd01a0fbdd5c3fde7dbc1b53d267 (diff)
Re-added files
Diffstat (limited to 'src/runMain')
-rw-r--r--src/runMain/kotlin/DataTypes.kt1
-rw-r--r--src/runMain/kotlin/Lexical.kt67
-rw-r--r--src/runMain/kotlin/Loader.kt9
-rw-r--r--src/runMain/kotlin/Main.kt3
-rw-r--r--src/runMain/kotlin/Syntax.kt8
-rw-r--r--src/runMain/kotlin/Testing.kt7
-rw-r--r--src/runMain/kotlin/TokenTypes.kt14
-rw-r--r--src/runMain/kotlin/exceptions/UnknownType.kt3
8 files changed, 112 insertions, 0 deletions
diff --git a/src/runMain/kotlin/DataTypes.kt b/src/runMain/kotlin/DataTypes.kt
new file mode 100644
index 0000000..4c6abb0
--- /dev/null
+++ b/src/runMain/kotlin/DataTypes.kt
@@ -0,0 +1 @@
+class Int \ No newline at end of file
diff --git a/src/runMain/kotlin/Lexical.kt b/src/runMain/kotlin/Lexical.kt
new file mode 100644
index 0000000..8529036
--- /dev/null
+++ b/src/runMain/kotlin/Lexical.kt
@@ -0,0 +1,67 @@
+import exceptions.UnknownType
+
+class Lexical {
+ fun analyze(source: String): MutableList<Pair<String, TokenType>> {
+ var buffer = ""
+ var statementEnd = false
+ val tokens = 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
+ buffer = ""
+ }
+ }
+ return tokens
+ }
+
+ /**
+ * Matches the tokens to a [TokenType]
+ */
+ private fun getTokenType(token: String, next: Char): 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.contains(" ") && token.length > 1 -> throw UnknownType(token)
+ token == " " -> TokenType.Empty
+
+ token in punctuation -> TokenType.Punctuation
+
+ token in brackets -> TokenType.Bracket
+
+ token in classifier -> TokenType.Classifier
+
+ 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("\"", "'")
+}
diff --git a/src/runMain/kotlin/Loader.kt b/src/runMain/kotlin/Loader.kt
new file mode 100644
index 0000000..19d695b
--- /dev/null
+++ b/src/runMain/kotlin/Loader.kt
@@ -0,0 +1,9 @@
+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 "Hallo"
+ }
+} \ No newline at end of file
diff --git a/src/runMain/kotlin/Main.kt b/src/runMain/kotlin/Main.kt
new file mode 100644
index 0000000..7b3e88b
--- /dev/null
+++ b/src/runMain/kotlin/Main.kt
@@ -0,0 +1,3 @@
+fun main() {
+ Testing()
+}
diff --git a/src/runMain/kotlin/Syntax.kt b/src/runMain/kotlin/Syntax.kt
new file mode 100644
index 0000000..3b8cfc8
--- /dev/null
+++ b/src/runMain/kotlin/Syntax.kt
@@ -0,0 +1,8 @@
+class Syntax {
+ fun check(tokens: MutableList<Pair<String, TokenType>>): Boolean {
+ for (token in tokens) {
+ print(token)
+ }
+ return true
+ }
+} \ No newline at end of file
diff --git a/src/runMain/kotlin/Testing.kt b/src/runMain/kotlin/Testing.kt
new file mode 100644
index 0000000..5ee9560
--- /dev/null
+++ b/src/runMain/kotlin/Testing.kt
@@ -0,0 +1,7 @@
+class Testing {
+ init {
+ val source = Loader("/home/melvin/coding/run/example.run").load()
+ val tokens = Lexical().analyze(source)
+ Syntax().check(tokens)
+ }
+} \ No newline at end of file
diff --git a/src/runMain/kotlin/TokenTypes.kt b/src/runMain/kotlin/TokenTypes.kt
new file mode 100644
index 0000000..9e15b5a
--- /dev/null
+++ b/src/runMain/kotlin/TokenTypes.kt
@@ -0,0 +1,14 @@
+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/runMain/kotlin/exceptions/UnknownType.kt b/src/runMain/kotlin/exceptions/UnknownType.kt
new file mode 100644
index 0000000..4c348a5
--- /dev/null
+++ b/src/runMain/kotlin/exceptions/UnknownType.kt
@@ -0,0 +1,3 @@
+package exceptions
+
+class UnknownType(token: String = "") : Exception("Token type not known: $token") \ No newline at end of file