Skip to content

Commit

Permalink
Merge pull request #263 from theludovyc/fix#262_all_assignment_operators
Browse files Browse the repository at this point in the history
fix #262 Handle common operators for variable assignment
  • Loading branch information
Jeremi360 authored Oct 27, 2023
2 parents b08c5dc + 2d37f96 commit 8ff1c14
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 10 deletions.
14 changes: 8 additions & 6 deletions Test/TestParser/TestVariables/TestVariables.gd
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@ func test_variables():

await wait_parse_and_execute_script(file_path)

await wait_execute_script_finished(file_base_name)
var error_str = "Executer::do_execute_script::SET_VARIABLE, Cannot resolve assignement: f of type(2) += with type(4)"

await wait_execute_script_finished(file_base_name, error_str)

assert_variable("a", TYPE_INT, 1)

assert_variable("b", TYPE_FLOAT, 2.5)
assert_variable("b", TYPE_FLOAT, 3.5)

assert_variable("c", TYPE_STRING, "Hello, world !")

assert_variable("d", TYPE_INT, Rakugo.get_variable("a"))
assert_variable("d", TYPE_INT, -1)

assert_variable("Sy.name", TYPE_STRING, "Sylvie")

assert_variable("Sy.life", TYPE_INT, 5)

assert_variable("e", TYPE_INT, Rakugo.get_variable("Sy.life"))

assert_variable("Sy.life", TYPE_INT, 10)

assert_eq(Rakugo.get_variable("f"), null)
assert_eq(Rakugo.get_variable("g"), null)
11 changes: 9 additions & 2 deletions Test/TestParser/TestVariables/TestVariables.rk
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
a = 1
b = 2.5
c = "Hello, world !"
c = "Hello,"
d = a
character Sy "Sylvie"
Sy.life = 5
e = Sy.life
a /= 1
b += 1
c += " world !"
d -= 2
Sy.life *= 2
e = Sy.life
f = 1
f += "1"
42 changes: 41 additions & 1 deletion addons/Rakugo/lib/systems/Executer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,47 @@ func do_execute_script(parameters:Dictionary):
else:
value = float(value)

Rakugo.set_variable(result["lvar_name"], value)
var assignment = result["assignment"]

var lvar_name = result["lvar_name"]

if assignment != "=":
var lvalue = Rakugo.get_variable(lvar_name)

if lvalue == null:
parameters["error"] = "Executer::do_execute_script::SET_VARIABLE, Rakugo does not knew a variable called: " + lvar_name
parameters["stop"] = true
break

var lvalue_type = typeof(lvalue)
var value_type = typeof(value)

# required because the thread crash (not godot) without error
# we only accept string and numbers when we parse
if value_type == TYPE_STRING and lvalue_type != TYPE_STRING:
parameters["error"] = "Executer::do_execute_script::SET_VARIABLE, Cannot resolve assignement: " + lvar_name + " of type(" + str(lvalue_type) + ") " + assignment + " with type(" + str(value_type) + ")"
parameters["stop"] = true
break

match(assignment):
"+=":
value = lvalue + value

"-=":
value = lvalue - value

"*=":
value = lvalue * value

"/=":
value = lvalue / value

_:
parameters["error"] = "Executer::do_execute_script::SET_VARIABLE, the assignment operator is not implemented :" + assignment
parameters["stop"] = true
break

Rakugo.set_variable(lvar_name, value)
_:
var foo = func():
Rakugo.sg_custom_regex.emit(line[0], result)
Expand Down
4 changes: 3 additions & 1 deletion addons/Rakugo/lib/systems/Parser.gd
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var Regex := {
NUMERIC = "-?[0-9]\\.?[0-9]*",
STRING = "\".+?\"",
VARIABLE = "((?<char_tag>{NAME})\\.)?(?<var_name>{NAME})",
ASSIGNMENT = "(?<assignment>=|\\+=|\\-=|\\*=|\\/=)"
# MULTILINE_STRING = "\"\"\"(?<string>.*)\"\"\"",
}

Expand All @@ -65,7 +66,7 @@ var parser_regex :={
# jump label
JUMP = "^jump (?<label>{NAME})( if (?<expression>.+))?$",
# for setting Rakugo variables
SET_VARIABLE = "^(?<lvar_name>{VARIABLE}) = ((?<text>{STRING})|(?<number>{NUMERIC})|(?<rvar_name>{VARIABLE}))$",
SET_VARIABLE = "^(?<lvar_name>{VARIABLE})\\s*{ASSIGNMENT}\\s*((?<text>{STRING})|(?<number>{NUMERIC})|(?<rvar_name>{VARIABLE}))$",
# $ some_gd_script_code
# IN_LINE_GDSCRIPT = "^\\$.*",
# gdscript:
Expand Down Expand Up @@ -267,6 +268,7 @@ func parse_script(lines:PackedStringArray) -> Dictionary:
"SET_VARIABLE":
var dic_result := {
"lvar_name":result.get_string("lvar_name"),
"assignment":result.get_string("assignment"),
"rvar_name":result.get_string("rvar_name"),
"number":result.get_string("number"),
"text":treat_string(result.get_string("text"))
Expand Down

0 comments on commit 8ff1c14

Please sign in to comment.