diff --git a/src/Interpreter.kt b/src/Interpreter.kt index cf3730d..5bfadc2 100644 --- a/src/Interpreter.kt +++ b/src/Interpreter.kt @@ -13,6 +13,8 @@ class Interpreter(val code: String) { val key_pattern_seperator = ':' val key_opening_bracket = '{' val key_closing_bracket = '}' + val key_math_opening = '[' + val key_math_closing = ']' val key_section_marker = '"' val key_variable = '_' @@ -60,6 +62,9 @@ class Interpreter(val code: String) { if (code[pos] == key_variable) { pos++ variable = true + } else if (code[pos] == key_math_opening) { + pos++ + return evalMathExpression() } while (code[pos] != escapeChar) { @@ -127,6 +132,27 @@ class Interpreter(val code: String) { return "VARIABLE " + name + " NOT FOUND" } + fun evalMathExpression(): String { + jumpSpaces() + val varOne = popNextWord(' ') + val operation = popNextWord(' ') + val varTwo = popNextWord(' ') + + gotoNext(key_math_closing) + + if (operation.equals("+")) { + return (varOne.toDouble() + varTwo.toDouble()).toString() + } else if (operation.equals("-")) { + return (varOne.toDouble() - varTwo.toDouble()).toString() + } else if (operation.equals("*")) { + return (varOne.toDouble() * varTwo.toDouble()).toString() + } else if (operation.equals("/")) { + return (varOne.toDouble() / varTwo.toDouble()).toString() + } else { + return "INVALID_MATH_OPERATION" + } + } + fun executeFun(my_pos: Int, agrsNames: Array, args: Array): String { val old_pos = pos pos = my_pos @@ -167,6 +193,15 @@ class Interpreter(val code: String) { } else if (code[pos] == key_variable) { pos++ val varName = popNextWord(' ') + } else if (code[pos] == key_math_opening) { + pos++ + val result = evalMathExpression() + jumpSpaces() + if (code[pos] == key_assign_from_fun_one && code[pos + 1] == key_assign_from_fun_two) { + jumpSpaces() + pos++ + addOrAssignVar(popNextWord(' '), result) + } } else { val fun_call = popNextWord(' ') gotoNext(fun_opening_bracket) diff --git a/src/test.bml b/src/test.bml index 6209939..6e19ff3 100644 --- a/src/test.bml +++ b/src/test.bml @@ -20,6 +20,6 @@ [ _e + 1.0 ] to _e - <- _e + <- [ _e + 2.0 ] } } \ No newline at end of file