Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Real set variable #240

Closed
wants to merge 8 commits into from
64 changes: 44 additions & 20 deletions addons/Rakugo/lib/systems/Executer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ var regex := {
}

var regex_cache := {}

var menu_jump_index:int
var menu_jump_index: int

func _init():
for key in regex:
Expand Down Expand Up @@ -88,7 +87,6 @@ func do_execute_script_end(parameters:Dictionary):
threads.erase(current_thread.get_id())

current_thread = null

current_semaphore = null

func do_execute_jump(jump_label:String, labels:Dictionary) -> int:
Expand Down Expand Up @@ -246,29 +244,55 @@ func do_execute_script(parameters:Dictionary):

"SET_VARIABLE":
var rvar_name = result.get_string("rvar_name")
var text = result.get_string("text")
var operator = result.get_string("operator")
var final_value = null

var values = []
for var_name in line[3]:
var var_ = Rakugo.get_variable(var_name)

if !var_:
parameters["error"] = "Executer::do_execute_script::SET_VARIABLE, can not get variable :" + var_name
parameters["stop"] = true
break

values.push_back(var_)

final_value = line[2].execute(values)

var value
if line[2].has_execute_failed():
parameters["error"] = "Executer::do_execute_script::SET_VARIABLE, failed to execute expression : " + result.get_string("expression")
parameters["stop"] = true
break

if !rvar_name.is_empty():
value = Rakugo.get_variable(rvar_name)
if operator != "=":
if Rakugo.has_variable(rvar_name):
var org_value = Rakugo.get_variable(rvar_name)

match operator:
"+=":
Rakugo.set_variable(rvar_name, org_value + final_value)

"-=":
Rakugo.set_variable(rvar_name, org_value - final_value)

"*=":
Rakugo.set_variable(rvar_name, org_value * final_value)

"/=":
Rakugo.set_variable(rvar_name, org_value / final_value)

"%=":
Rakugo.set_variable(rvar_name, org_value % final_value)

if !value:
parameters["error"] = "Executer::do_execute_script::SET_VARIABLE, can not get variable :" + rvar_name
else:
parameters["error"] = "Executer::do_execute_script::SET_VARIABLE, variable not found : " + rvar_name
parameters["stop"] = true
break

elif !text.is_empty():
value = remove_double_quotes(text)
else:
value = result.get_string("number")

if value.is_valid_int():
value = int(value)
else:
value = float(value)

Rakugo.set_variable(result.get_string("lvar_name"), value)
else:
Rakugo.set_variable(rvar_name, final_value)

Jeremi360 marked this conversation as resolved.
Show resolved Hide resolved
_:
Rakugo.sg_custom_regex.emit(line[0], result)

Expand Down
75 changes: 45 additions & 30 deletions addons/Rakugo/lib/systems/Parser.gd
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ 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}))$",
ASSIGNMENT = "^(=|\\+=|\\-=|\\*=|\\/=|\\%=)",
Jeremi360 marked this conversation as resolved.
Show resolved Hide resolved
SET_VARIABLE = "^(?<rvar_name>{VARIABLE})\\s*(?<operator>ASSIGNMENT)\\s*(?<expression>.+)$",
# $ some_gd_script_code
# IN_LINE_GDSCRIPT = "^\\$.*",
# gdscript:
Expand Down Expand Up @@ -124,19 +125,42 @@ func count_indent(s:String) -> int:

return ret

func get_vars_in_expression(str_expression:String):
var sub_results = other_cache["VARIABLE"].search_all(str_expression)
var vars = []

# Expression does not like '.'
var vars_expression = []

for sub_result in sub_results:
var sub_result_str = sub_result.strings[0]

if !vars.has(sub_result_str):
vars.push_back(sub_result_str)

var var_name_expr = sub_result.get_string("char_tag")

if !var_name_expr.is_empty():
var_name_expr += "_" + sub_result.get_string("var_name")

str_expression = str_expression.replace(sub_result_str, var_name_expr)
else:
var_name_expr = sub_result.get_string("var_name")

if !vars_expression.has(var_name_expr):
vars_expression.push_back(var_name_expr)

return vars_expression

func parse_script(lines:PackedStringArray) -> Dictionary:
if lines.is_empty():
push_error("Parser, parse_script : lines is empty !")
return {}

var parse_array:Array

var labels:Dictionary

var indent_count:int

var menu_choices

var current_menu_result

state = State.Normal
Expand Down Expand Up @@ -203,42 +227,33 @@ func parse_script(lines:PackedStringArray) -> Dictionary:
if str_expression.is_empty():
parse_array.push_back([key, result])
break

var vars = get_vars_in_expression(str_expression)
var expression = Expression.new()
if expression.parse(str_expression, vars) != OK:
push_error("Parser: Error on line: " + str(i+1) + ", " + expression.get_error_text())
return {}

var sub_results = other_cache["VARIABLE"].search_all(str_expression)

var vars = []

# Expression does not like '.'
var vars_expression = []

for sub_result in sub_results:
var sub_result_str = sub_result.strings[0]

if !vars.has(sub_result_str):
vars.push_back(sub_result_str)

var var_name_expr = sub_result.get_string("char_tag")

if !var_name_expr.is_empty():
var_name_expr += "_" + sub_result.get_string("var_name")
parse_array.push_back([key, result, expression, vars])

str_expression = str_expression.replace(sub_result_str, var_name_expr)
else:
var_name_expr = sub_result.get_string("var_name")

if !vars_expression.has(var_name_expr):
vars_expression.push_back(var_name_expr)
"SET_VARIABLE":
var str_expression:String = result.get_string("expression")

if str_expression.is_empty():
parse_array.push_back([key, result])
break
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If your str_expression is empty why you do not send an error ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure, I last touch this code over a month ago, so maybe I was to change into something and forgot.
I will test with out this if-block and only if find that is really need restore it in proper form.


var vars = get_vars_in_expression(str_expression)
var expression = Expression.new()

if expression.parse(str_expression, vars_expression) != OK:
if expression.parse(str_expression, vars) != OK:
push_error("Parser: Error on line: " + str(i+1) + ", " + expression.get_error_text())
return {}

parse_array.push_back([key, result, expression, vars])

_:
parse_array.push_back([key, result])

break

if (not have_find_key):
Expand Down