aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarvin Borner2019-08-12 16:01:51 +0200
committerMarvin Borner2019-08-12 16:01:51 +0200
commita382841dc7d554b2ba3920d52f73e4cec428743e (patch)
tree536035f23c3d0d1ced960372b297a4869621c595
Basic parser functions
-rw-r--r--.gitignore3
-rw-r--r--example.run1
-rw-r--r--src/DataTypes.kt3
-rw-r--r--src/Lexical.kt64
-rw-r--r--src/Loader.kt12
-rw-r--r--src/Main.kt5
-rw-r--r--src/Testing.kt13
7 files changed, 101 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..5f05927
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+.idea
+out
+*.iml \ No newline at end of file
diff --git a/example.run b/example.run
new file mode 100644
index 0000000..f7d418f
--- /dev/null
+++ b/example.run
@@ -0,0 +1 @@
+print string(2 + 2); \ No newline at end of file
diff --git a/src/DataTypes.kt b/src/DataTypes.kt
new file mode 100644
index 0000000..290481d
--- /dev/null
+++ b/src/DataTypes.kt
@@ -0,0 +1,3 @@
+class Int {
+
+} \ No newline at end of file
diff --git a/src/Lexical.kt b/src/Lexical.kt
new file mode 100644
index 0000000..bd8bc3a
--- /dev/null
+++ b/src/Lexical.kt
@@ -0,0 +1,64 @@
+class Lexical {
+ fun analyze(source: String): MutableList<Pair<String, TokenType>> {
+ var buffer = ""
+ val tokens = mutableListOf<Pair<String, TokenType>>()
+ for (i in source.indices) {
+ if (source.length > i + 1 && getType(source[i]) == getType(source[i + 1])) {
+ buffer += source[i]
+ } else {
+ buffer += source[i]
+ if (getType(source[i]) != Type.Empty)
+ tokens += buffer to getTokenType(buffer to getType(source[i]))
+ buffer = ""
+ }
+ }
+ return tokens
+ }
+
+ /**
+ * Matches the characters to groups ([Type])
+ */
+ private fun getType(char: Char): Type {
+ return when (char) {
+ in 'a'..'z', in 'A'..'Z' -> Type.Alphabetic
+ in '1'..'9' -> Type.Numeric
+ ' ' -> Type.Empty
+ else -> Type.Unknown
+ }
+ }
+
+ /**
+ * Matches the tokens to a [TokenType]
+ */
+ private fun getTokenType(token: Pair<String, Type>): TokenType {
+ var type = when (token.first) {
+ "print" -> TokenType.Keyword // TODO: DataType matching
+ "=", "+", "-", "*", "/", "==", ">", "<" -> TokenType.Operator
+ ";", ":" -> TokenType.Symbol
+ else -> TokenType.Unknown
+ }
+ if (type == TokenType.Unknown)
+ type = when (token.second) {
+ Type.Numeric -> TokenType.Constant
+ Type.Alphabetic -> TokenType.Identifier
+ else -> TokenType.Unknown
+ }
+ return type
+ }
+}
+
+private enum class Type {
+ Alphabetic,
+ Numeric,
+ Empty,
+ Unknown
+}
+
+enum class TokenType {
+ Keyword,
+ Identifier,
+ Operator,
+ Constant,
+ Symbol,
+ Unknown
+} \ No newline at end of file
diff --git a/src/Loader.kt b/src/Loader.kt
new file mode 100644
index 0000000..2161c51
--- /dev/null
+++ b/src/Loader.kt
@@ -0,0 +1,12 @@
+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
+ fun load(): String {
+ return inputString
+ }
+} \ No newline at end of file
diff --git a/src/Main.kt b/src/Main.kt
new file mode 100644
index 0000000..24a2e08
--- /dev/null
+++ b/src/Main.kt
@@ -0,0 +1,5 @@
+import Testing
+
+fun main() {
+ Testing()
+}
diff --git a/src/Testing.kt b/src/Testing.kt
new file mode 100644
index 0000000..0b16145
--- /dev/null
+++ b/src/Testing.kt
@@ -0,0 +1,13 @@
+class Testing {
+ init {
+ val source = Loader("/home/melvin/coding/run/example.run").load()
+ val tokens = Lexical().analyze(source)
+ for (token in tokens) {
+ print(token.first)
+ print("\n")
+ print(token.second)
+ print("\n")
+ }
+ print(source)
+ }
+} \ No newline at end of file