diff --git a/src/Interpreter.kt b/src/Interpreter.kt index 2463777..cf3730d 100644 --- a/src/Interpreter.kt +++ b/src/Interpreter.kt @@ -19,6 +19,9 @@ class Interpreter(val code: String) { val key_return_one = '<' val key_return_two = '-' + val key_assign_from_fun_one = 't' + val key_assign_from_fun_two = 'o' + val fun_typ_pattern = "fun" val fun_typ_main = "main" @@ -47,11 +50,18 @@ class Interpreter(val code: String) { } /* - Returns the current word at the cursor. + Returns the current word at the cursor or the Value of the variable */ fun popNextWord(escapeChar: Char): String { val builder = StringBuilder() + var variable = false + + if (code[pos] == key_variable) { + pos++ + variable = true + } + while (code[pos] != escapeChar) { if (code[pos] == key_section_marker) { while (code[pos] != key_section_marker) { //We use that for Strings with spaces. @@ -59,14 +69,18 @@ class Interpreter(val code: String) { pos++ } pos++ - }else { + } else { builder.append(code[pos]) pos++ } } pos++ - return builder.toString() + if (variable) { + return getVarVal(builder.toString()) + } else { + return builder.toString() + } } /* @@ -94,6 +108,25 @@ class Interpreter(val code: String) { return argsList.toTypedArray() } + fun addOrAssignVar(name: String, value: String) { + for (variable in variables) { + if (name.equals(variable.name)) { + variable.value = value + } + } + + variables.add(BslVariable(name, value)) + } + + fun getVarVal(name: String): String { + for (variable in variables) { + if (name.equals(variable.name)) + return variable.value + } + + return "VARIABLE " + name + " NOT FOUND" + } + fun executeFun(my_pos: Int, agrsNames: Array, args: Array): String { val old_pos = pos pos = my_pos @@ -127,15 +160,24 @@ class Interpreter(val code: String) { inverted = oldInverted } else if (code[pos] == key_return_one && code[pos + 1] == key_return_two) { pos += 2 + jumpSpaces() val out = popNextWord(' ') pos = old_pos return out } else if (code[pos] == key_variable) { - TODO() + pos++ + val varName = popNextWord(' ') } else { val fun_call = popNextWord(' ') gotoNext(fun_opening_bracket) - callFunction(fun_call, popArgs()) + val returnVal = callFunction(fun_call, popArgs()) + jumpSpaces() + if (code[pos] == key_assign_from_fun_one && code[pos + 1] == key_assign_from_fun_two) { + pos += 2 + jumpSpaces() + pos++ + addOrAssignVar(popNextWord(' '), returnVal) + } } jumpSpaces() @@ -146,7 +188,7 @@ class Interpreter(val code: String) { return "NO_RETURN" } - fun callFunction(name: String, args: Array) { + fun callFunction(name: String, args: Array): String { if (name == fun_stat_place_cube) { placeCube(args[0].toDouble(), args[1].toInt(), args[2].toInt()) } else { @@ -161,11 +203,12 @@ class Interpreter(val code: String) { pos = old_pos - executeFun(this_fun.pos, argsNames, args) - break + return executeFun(this_fun.pos, argsNames, args) } } } + + return "NO_FUNCTION_FOUND" } fun interpret() { diff --git a/src/test.bml b/src/test.bml index f0fd2f8..6209939 100644 --- a/src/test.bml +++ b/src/test.bml @@ -3,7 +3,7 @@ Cube (1.5, 2, 3,) Cube (2.0, 5, 3,) - <-2.0 + <- 2.0 } #fun test_two () { @@ -16,7 +16,10 @@ !24.6:false:4 { - test () - <-2.0 + test () to _e + + [ _e + 1.0 ] to _e + + <- _e } } \ No newline at end of file