commit 0b05242184d454e310ddee4ba9db3b954072f8fe Author: Thea Schöbl Date: Sun Nov 25 14:07:08 2018 +0100 Initial commit diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml new file mode 100644 index 0000000..3c50bd3 --- /dev/null +++ b/.idea/kotlinc.xml @@ -0,0 +1,10 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/KotlinJavaRuntime.xml b/.idea/libraries/KotlinJavaRuntime.xml new file mode 100644 index 0000000..1a7265d --- /dev/null +++ b/.idea/libraries/KotlinJavaRuntime.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..0548357 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..a12da2e --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Bsl.iml b/Bsl.iml new file mode 100644 index 0000000..245d342 --- /dev/null +++ b/Bsl.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/src/Interpreter.kt b/src/Interpreter.kt new file mode 100644 index 0000000..c2b2424 --- /dev/null +++ b/src/Interpreter.kt @@ -0,0 +1,146 @@ +class Interpreter { + class BslFunction(val name: String, val pos: Int) + + var error = false + + val functions = ArrayList() + + val key_fun = '#' + val key_comment = '?' + val key_opening_bracket = '{' + val key_closing_bracket = '}' + val key_section_marker = '"' + + val fun_typ_pattern = "pattern" + val fun_typ_main = "main" + + val fun_stat_place_cube = "Cube" + + val fun_opening_bracket = '(' + val fun_closing_bracket = ')' + + val args_seperator = ',' + + var code = "#pattern test (runs, arg,) {Cube (1.0, 3, 5,)} #main () {test (1, \"Hello there\",) Cube (1.2, 3, 6,)}" + var pos = 0 + + fun placeCube(timestamp: Double, type: Int, value: Int) { + System.out.println("Cube placed: " + timestamp + " " + type + " " + value) + } + + fun placeBomb(timestamp: Double, value: Int) { + + } + + /* + Returns the current word at the cursor. + */ + fun popNextWord(escapeChar: Char): String { + val builder = StringBuilder() + + while (code[pos] != escapeChar) { + if (code[pos] == key_section_marker) { + while (code[pos] != key_section_marker) { //We use that for Strings with spaces. + builder.append(code[pos]) + pos++ + } + pos++ + }else { + builder.append(code[pos]) + pos++ + } + } + pos++ + + return builder.toString() + } + + /* + Goes to the next specified char. + */ + fun gotoNext(key: Char) { + while (code[pos] != key) pos++ + pos++ + } + + /* + Jumps to the next non-Space char + */ + fun jumpSpaces() { + while (code[pos] == ' ') pos++ + } + + fun popArgs(): Array { + val argsList = ArrayList() + while (code[pos] != fun_closing_bracket) { + jumpSpaces() + argsList.add(popNextWord(args_seperator)) + } + pos++ + return argsList.toTypedArray() + } + + fun executeFun(my_pos: Int) { + val old_pos = pos + pos = my_pos + + gotoNext(fun_opening_bracket) + //This time we just need the names for interpretation purposes + val agrsNames = popArgs() + + gotoNext(key_opening_bracket) + + while (code[pos] != key_closing_bracket) { + jumpSpaces() + + if (code[pos] == key_comment) { + gotoNext(key_comment) + } else { + val fun_call = popNextWord(' ') + gotoNext(fun_opening_bracket) + callFunction(fun_call, popArgs()) + } + } + + pos = old_pos + } + + fun callFunction(name: String, args: Array) { + if (name == fun_stat_place_cube) { + placeCube(args[0].toDouble(), args[1].toInt(), args[2].toInt()) + } else { + //Now we need to check for custom functions + + for (this_fun in functions) { + if (this_fun.name == name) { + executeFun(this_fun.pos) + break + } + } + } + } + + fun interpret() { + while (pos < code.length) { + if (code[pos] == key_fun) { + pos++ + val fun_typ = popNextWord(' ') + jumpSpaces() + + if (fun_typ.equals(fun_typ_pattern)) { + val functionName = popNextWord(' ') + functions.add(BslFunction(functionName, pos)) + gotoNext(key_closing_bracket) + } else if (fun_typ == fun_typ_main) { + executeFun(pos) + return + } + } else if (code[pos] == key_comment) { + //We don't need comments + gotoNext(key_comment) + } else { + pos++ + } + } + } +} \ No newline at end of file diff --git a/src/Main.kt b/src/Main.kt new file mode 100644 index 0000000..4ca4e3c --- /dev/null +++ b/src/Main.kt @@ -0,0 +1,5 @@ +fun main(args: Array) { + val interpreter = Interpreter() + + interpreter.interpret() +} \ No newline at end of file