Variables work now as intended

This commit is contained in:
2018-11-25 16:27:00 +01:00
parent 8c2db44e4c
commit 97d7beefcf
2 changed files with 34 additions and 33 deletions

View File

@@ -1,3 +1,5 @@
import kotlin.math.roundToInt
class Interpreter(val code: String) { class Interpreter(val code: String) {
class BslFunction(val name: String, val pos: Int) class BslFunction(val name: String, val pos: Int)
class BslVariable(val name: String, var value: String) class BslVariable(val name: String, var value: String)
@@ -8,7 +10,7 @@ class Interpreter(val code: String) {
val variables = ArrayList<BslVariable>() val variables = ArrayList<BslVariable>()
val key_fun = '#' val key_fun = '#'
val key_comment = '?' val key_comment = '§'
val key_pattern = '!' val key_pattern = '!'
val key_pattern_seperator = ':' val key_pattern_seperator = ':'
val key_opening_bracket = '{' val key_opening_bracket = '{'
@@ -17,6 +19,8 @@ class Interpreter(val code: String) {
val key_math_closing = ']' val key_math_closing = ']'
val key_section_marker = '"' val key_section_marker = '"'
val key_variable = '_' val key_variable = '_'
val key_assignment = '='
val key_ask_return = '?'
val key_return_one = '<' val key_return_one = '<'
val key_return_two = '-' val key_return_two = '-'
@@ -54,7 +58,7 @@ class Interpreter(val code: String) {
/* /*
Returns the current word at the cursor or the Value of the variable Returns the current word at the cursor or the Value of the variable
*/ */
fun popNextWord(escapeChar: Char): String { fun popNext(escapeChar: Char): String {
val builder = StringBuilder() val builder = StringBuilder()
var variable = false var variable = false
@@ -65,6 +69,12 @@ class Interpreter(val code: String) {
} else if (code[pos] == key_math_opening) { } else if (code[pos] == key_math_opening) {
pos++ pos++
return evalMathExpression() return evalMathExpression()
} else if (code[pos] == key_ask_return) {
//Function call with return value
pos++
val fun_call = popNext(' ')
gotoNext(fun_opening_bracket)
return callFunction(fun_call, popArgs())
} }
while (code[pos] != escapeChar) { while (code[pos] != escapeChar) {
@@ -107,7 +117,7 @@ class Interpreter(val code: String) {
val argsList = ArrayList<String>() val argsList = ArrayList<String>()
while (code[pos] != fun_closing_bracket) { while (code[pos] != fun_closing_bracket) {
jumpSpaces() jumpSpaces()
argsList.add(popNextWord(args_seperator)) argsList.add(popNext(args_seperator))
} }
pos++ pos++
return argsList.toTypedArray() return argsList.toTypedArray()
@@ -134,9 +144,11 @@ class Interpreter(val code: String) {
fun evalMathExpression(): String { fun evalMathExpression(): String {
jumpSpaces() jumpSpaces()
val varOne = popNextWord(' ') val varOne = popNext(' ')
val operation = popNextWord(' ') jumpSpaces()
val varTwo = popNextWord(' ') val operation = popNext(' ')
jumpSpaces()
val varTwo = popNext(' ')
gotoNext(key_math_closing) gotoNext(key_math_closing)
@@ -164,9 +176,9 @@ class Interpreter(val code: String) {
gotoNext(key_comment) gotoNext(key_comment)
} else if (code[pos] == key_pattern) { } else if (code[pos] == key_pattern) {
pos++ pos++
val timestamp = popNextWord(key_pattern_seperator) val timestamp = popNext(key_pattern_seperator)
val t_inverted = popNextWord(key_pattern_seperator) val t_inverted = popNext(key_pattern_seperator)
val runs = popNextWord(' ').toInt() - 1 val runs = popNext(' ').toDouble().roundToInt() - 1
val oldOffset = currentOffset val oldOffset = currentOffset
currentOffset += timestamp.toDouble() currentOffset += timestamp.toDouble()
@@ -187,32 +199,22 @@ class Interpreter(val code: String) {
} else if (code[pos] == key_return_one && code[pos + 1] == key_return_two) { } else if (code[pos] == key_return_one && code[pos + 1] == key_return_two) {
pos += 2 pos += 2
jumpSpaces() jumpSpaces()
val out = popNextWord(' ') val out = popNext(' ')
pos = old_pos pos = old_pos
return out return out
} else if (code[pos] == key_variable) { } else if (code[pos] == key_variable) {
pos++ pos++
val varName = popNextWord(' ') val varName = popNext(' ')
} else if (code[pos] == key_math_opening) {
pos++
val result = evalMathExpression()
jumpSpaces() jumpSpaces()
if (code[pos] == key_assign_from_fun_one && code[pos + 1] == key_assign_from_fun_two) { if (code[pos] == key_assignment) {
jumpSpaces()
pos++ pos++
addOrAssignVar(popNextWord(' '), result) jumpSpaces()
addOrAssignVar(varName, popNext(' '))
} }
} else { } else {
val fun_call = popNextWord(' ') val fun_call = popNext(' ')
gotoNext(fun_opening_bracket) gotoNext(fun_opening_bracket)
val returnVal = callFunction(fun_call, popArgs()) 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() jumpSpaces()
@@ -250,11 +252,11 @@ class Interpreter(val code: String) {
while (pos < code.length) { while (pos < code.length) {
if (code[pos] == key_fun) { if (code[pos] == key_fun) {
pos++ pos++
val fun_typ = popNextWord(' ') val fun_typ = popNext(' ')
jumpSpaces() jumpSpaces()
if (fun_typ.equals(fun_typ_pattern)) { if (fun_typ.equals(fun_typ_pattern)) {
val functionName = popNextWord(' ') val functionName = popNext(' ')
functions.add(BslFunction(functionName, pos)) functions.add(BslFunction(functionName, pos))
gotoNext(key_closing_bracket) gotoNext(key_closing_bracket)
} else if (fun_typ == fun_typ_main) { } else if (fun_typ == fun_typ_main) {

View File

@@ -14,12 +14,11 @@
test_two () test_two ()
Cube (1.2, 3, 6,) Cube (1.2, 3, 6,)
_x = ?test ()
!24.6:false:4 { !24.6:false:_x {
test () to _e _e = [ ?test () + 1.0 ]
[ _e + 1.0 ] to _e <- _e
<- [ _e + 2.0 ]
} }
} }