Variables are working

This commit is contained in:
2018-11-25 15:45:20 +01:00
parent c2dca8aafc
commit 1e313af80a
2 changed files with 57 additions and 11 deletions

View File

@@ -19,6 +19,9 @@ class Interpreter(val code: String) {
val key_return_one = '<' val key_return_one = '<'
val key_return_two = '-' 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_pattern = "fun"
val fun_typ_main = "main" 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 { fun popNextWord(escapeChar: Char): String {
val builder = StringBuilder() val builder = StringBuilder()
var variable = false
if (code[pos] == key_variable) {
pos++
variable = true
}
while (code[pos] != escapeChar) { while (code[pos] != escapeChar) {
if (code[pos] == key_section_marker) { if (code[pos] == key_section_marker) {
while (code[pos] != key_section_marker) { //We use that for Strings with spaces. while (code[pos] != key_section_marker) { //We use that for Strings with spaces.
@@ -59,14 +69,18 @@ class Interpreter(val code: String) {
pos++ pos++
} }
pos++ pos++
}else { } else {
builder.append(code[pos]) builder.append(code[pos])
pos++ 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() 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<String>, args: Array<String>): String { fun executeFun(my_pos: Int, agrsNames: Array<String>, args: Array<String>): String {
val old_pos = pos val old_pos = pos
pos = my_pos pos = my_pos
@@ -127,15 +160,24 @@ class Interpreter(val code: String) {
inverted = oldInverted inverted = oldInverted
} 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()
val out = popNextWord(' ') val out = popNextWord(' ')
pos = old_pos pos = old_pos
return out return out
} else if (code[pos] == key_variable) { } else if (code[pos] == key_variable) {
TODO() pos++
val varName = popNextWord(' ')
} else { } else {
val fun_call = popNextWord(' ') val fun_call = popNextWord(' ')
gotoNext(fun_opening_bracket) 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() jumpSpaces()
@@ -146,7 +188,7 @@ class Interpreter(val code: String) {
return "NO_RETURN" return "NO_RETURN"
} }
fun callFunction(name: String, args: Array<String>) { fun callFunction(name: String, args: Array<String>): String {
if (name == fun_stat_place_cube) { if (name == fun_stat_place_cube) {
placeCube(args[0].toDouble(), args[1].toInt(), args[2].toInt()) placeCube(args[0].toDouble(), args[1].toInt(), args[2].toInt())
} else { } else {
@@ -161,11 +203,12 @@ class Interpreter(val code: String) {
pos = old_pos pos = old_pos
executeFun(this_fun.pos, argsNames, args) return executeFun(this_fun.pos, argsNames, args)
break
} }
} }
} }
return "NO_FUNCTION_FOUND"
} }
fun interpret() { fun interpret() {

View File

@@ -3,7 +3,7 @@
Cube (1.5, 2, 3,) Cube (1.5, 2, 3,)
Cube (2.0, 5, 3,) Cube (2.0, 5, 3,)
<-2.0 <- 2.0
} }
#fun test_two () { #fun test_two () {
@@ -16,7 +16,10 @@
!24.6:false:4 { !24.6:false:4 {
test () test () to _e
<-2.0
[ _e + 1.0 ] to _e
<- _e
} }
} }