From bad1dcb372a73f54c3943d04406acad3fe21ddda Mon Sep 17 00:00:00 2001 From: Bram Dingelstad Date: Sun, 21 Nov 2021 16:45:09 +0100 Subject: [PATCH] removed variable storage and converted it to dict --- addons/Wol/Wol.gd | 21 +++----- addons/Wol/core/VirtualMachine.gd | 13 ++--- addons/Wol/core/compiler/parser.gd | 32 ++++++----- addons/Wol/core/value.gd | 84 +++++++++++++---------------- addons/Wol/core/variable_storage.gd | 23 -------- 5 files changed, 66 insertions(+), 107 deletions(-) delete mode 100644 addons/Wol/core/variable_storage.gd diff --git a/addons/Wol/Wol.gd b/addons/Wol/Wol.gd index e398d48..f049974 100644 --- a/addons/Wol/Wol.gd +++ b/addons/Wol/Wol.gd @@ -16,20 +16,19 @@ signal command(command) signal started signal finished +export(String, FILE, '*.wol,*.yarn') var path setget set_path +export(String) var start_node = 'Start' +export(bool) var auto_start = false +export var auto_show_options = true + +export(Dictionary) var variable_storage = {} + const Constants = preload('res://addons/Wol/core/Constants.gd') const WolCompiler = preload('res://addons/Wol/core/compiler/Compiler.gd') const WolLibrary = preload('res://addons/Wol/core/Library.gd') const VirtualMachine = preload('res://addons/Wol/core/VirtualMachine.gd') const StandardLibrary = preload('res://addons/Wol/core/StandardLibrary.gd') -export(String, FILE, '*.wol,*.yarn') var path setget set_path -export(String) var start_node = 'Start' -export(bool) var auto_start = false -export(NodePath) var variable_storage_path -export var auto_show_options = true - -onready var variable_storage = get_node(variable_storage_path) - var virtual_machine func _ready(): @@ -42,12 +41,6 @@ func _ready(): set_path(path) - if not variable_storage: - variable_storage = Node.new() - variable_storage.name = 'VariableStorage' - variable_storage.set_script(load('res://addons/Wol/core/variable_storage.gd')) - add_child(variable_storage) - if auto_start: start() diff --git a/addons/Wol/core/VirtualMachine.gd b/addons/Wol/core/VirtualMachine.gd index 36817cc..96a5e94 100644 --- a/addons/Wol/core/VirtualMachine.gd +++ b/addons/Wol/core/VirtualMachine.gd @@ -215,14 +215,15 @@ func run_instruction(instruction): Constants.ByteCode.PushVariable: var name = instruction.operands[0].value - var loaded = dialogue.variable_storage.get_value(name) - state.push_value(loaded) + var godot_value = dialogue.variable_storage[name] + var value = Value.new(godot_value) + state.push_value(value) Constants.ByteCode.StoreVariable: - var top = state.peek_value() - var destination = instruction.operands[0].value - dialogue.variable_storage.set_value(destination, top) - + var value = state.peek_value() + var name = instruction.operands[0].value.replace('$', '') + dialogue.variable_storage[name] = value.value() + Constants.ByteCode.Stop: node_finished_handler.call_func(current_node.name) dialogue_finished_handler.call_func() diff --git a/addons/Wol/core/compiler/parser.gd b/addons/Wol/core/compiler/parser.gd index f27dfb9..5575cf8 100644 --- a/addons/Wol/core/compiler/parser.gd +++ b/addons/Wol/core/compiler/parser.gd @@ -246,9 +246,9 @@ class CustomCommand extends ParseNode: func tree_string(indent_level): match type: Type.Expression: - return tab(indent_level,'Expression: %s'% expression.tree_string(indent_level+1)) + return tab(indent_level, 'Expression: %s' % expression.tree_string(indent_level+1)) Type.ClientCommand: - return tab(indent_level,'Command: %s' % client_command) + return tab(indent_level, 'Command: %s' % client_command) return '' static func can_parse(parser): @@ -273,12 +273,12 @@ class ShortcutOptionGroup extends ParseNode: func tree_string(indent_level): var info = [] - info.append(tab(indent_level,'Shortcut Option Group{')) + info.append(tab(indent_level, 'Shortcut Option Group{')) for option in options: info.append(option.tree_string(indent_level+1)) - info.append(tab(indent_level,'}')) + info.append(tab(indent_level, '}')) return PoolStringArray(info).join('') @@ -295,7 +295,6 @@ class ShortCutOption extends ParseNode: label = parser.expect_symbol([Constants.TokenType.Text]).value # NOTE: Parse the conditional << if $x >> when it exists - var tags = [] while parser.next_symbols_are([Constants.TokenType.BeginCommand, Constants.TokenType.IfToken]) \ or parser.next_symbol_is([Constants.TokenType.TagMarker]): @@ -397,7 +396,7 @@ class OptionStatement extends ParseNode: func tree_string(indent_level): if label != null: - return tab(indent_level,'Option: %s -> %s' % [label, destination]) + return tab(indent_level, 'Option: %s -> %s' % [label, destination]) else: return tab(indent_level, 'Option: -> %s' % destination) @@ -491,11 +490,11 @@ class IfStatement extends ParseNode: for clause in clauses: if first: - info.append(tab(indent_level,'if:')) + info.append(tab(indent_level, 'if:')) elif clause.expression!=null: - info.append(tab(indent_level,'Else If')) + info.append(tab(indent_level, 'Else If')) else: - info.append(tab(indent_level,'Else:')) + info.append(tab(indent_level, 'Else:')) info.append(clause.tree_string(indent_level +1)) @@ -562,10 +561,10 @@ class ExpressionNode extends ParseNode: return value.tree_string(indent_level) Constants.ExpressionType.FunctionCall: - info.append(tab(indent_level,'Func[%s - parameters(%s)]:{'%[function, parameters.size()])) + info.append(tab(indent_level, 'Func[%s - parameters(%s)]:{'%[function, parameters.size()])) for param in parameters: info.append(param.tree_string(indent_level+1)) - info.append(tab(indent_level,'}')) + info.append(tab(indent_level, '}')) return info.join('') @@ -799,10 +798,10 @@ class Assignment extends ParseNode: func tree_string(indent_level): var info = [] - info.append(tab(indent_level,'set:')) - info.append(tab(indent_level+1, destination)) - info.append(tab(indent_level+1, Constants.token_type_name(operation))) - info.append(value.tree_string(indent_level+1)) + info.append(tab(indent_level, 'set:')) + info.append(tab(indent_level + 1, destination)) + info.append(tab(indent_level + 1, Constants.token_type_name(operation))) + info.append(value.tree_string(indent_level + 1)) return info.join('') @@ -892,7 +891,6 @@ class Operator extends ParseNode: Constants.TokenType.Xor ] - class OperatorInfo: var associativity var precedence = -1 @@ -921,7 +919,7 @@ class Clause: for statement in statements: info.append(statement.tree_string(indent_level + 1)) - info.append(tab(indent_level,'}')) + info.append(tab(indent_level, '}')) return PoolStringArray(info).join('') func tab(indent_level, input, newline = true): diff --git a/addons/Wol/core/value.gd b/addons/Wol/core/value.gd index 417c4e5..8da0295 100644 --- a/addons/Wol/core/value.gd +++ b/addons/Wol/core/value.gd @@ -14,10 +14,10 @@ var variable = '' var boolean = false func _init(value = NANI): - if typeof(value) == TYPE_OBJECT && value.get_script() == self.get_script(): + if typeof(value) == TYPE_OBJECT and value.get_script() == get_script(): if value.type == Constants.ValueType.Variable: - self.type = value.type - self.variable = value.variable + type = value.type + variable = value.variable else: set_value(value) @@ -57,12 +57,12 @@ func as_number(): return .0 func set_value(value): - if value == null || (typeof(value) == TYPE_STRING && value == NANI): + if value == null or (typeof(value) == TYPE_STRING and value == NANI): type = Constants.ValueType.Nullean return match typeof(value): - TYPE_INT,TYPE_REAL: + TYPE_INT, TYPE_REAL: type = Constants.ValueType.Number number = value TYPE_STRING: @@ -72,76 +72,66 @@ func set_value(value): type = Constants.ValueType.Boolean boolean = value -#operations >> - -#addition func add(other): - if self.type == Constants.ValueType.Str || other.type == Constants.ValueType.Str: - return get_script().new('%s%s'%[self.value(),other.value()]) - if self.type == Constants.ValueType.Number && other.type == Constants.ValueType.Number: - return get_script().new(self.number + other.number) + if type == Constants.ValueType.Str or other.type == Constants.ValueType.Str: + return get_script().new('%s%s'%[value(),other.value()]) + if type == Constants.ValueType.Number and other.type == Constants.ValueType.Number: + return get_script().new(number + other.number) return null -func equals(other)->bool: - if other.get_script() != self.get_script(): +func equals(other): + if other.get_script() != get_script(): return false - if other.value() != self.value(): + if other.value() != value(): return false - return true #refine this + # TODO: Add more equality cases + return true -#subtract func sub(other): - if self.type == Constants.ValueType.Str || other.type == Constants.ValueType.Str: + if type == Constants.ValueType.Str or other.type == Constants.ValueType.Str: return get_script().new(str(value()).replace(str(other.value()),'')) - if self.type == Constants.ValueType.Number && other.type == Constants.ValueType.Number: - return get_script().new(self.number - other.number) + if type == Constants.ValueType.Number and other.type == Constants.ValueType.Number: + return get_script().new(number - other.number) return null -#multiply func mult(other): - if self.type == Constants.ValueType.Number && other.type == Constants.ValueType.Number: - return get_script().new(self.number * other.number) + if type == Constants.ValueType.Number and other.type == Constants.ValueType.Number: + return get_script().new(number * other.number) return null -#division func div(other): - if self.type == Constants.ValueType.Number && other.type == Constants.ValueType.Number: - return get_script().new(self.number / other.number) + if type == Constants.ValueType.Number and other.type == Constants.ValueType.Number: + return get_script().new(number / other.number) return null -#modulus func mod(other): - if self.type == Constants.ValueType.Number && other.type == Constants.ValueType.Number: - return get_script().new(self.number % other.number) + if type == Constants.ValueType.Number and other.type == Constants.ValueType.Number: + return get_script().new(number % other.number) return null func negative(): - if self.type == Constants.ValueType.Number: - return get_script().new(-self.number) + if type == Constants.ValueType.Number: + return get_script().new(-number) return null -#greater than other -func greater(other)->bool: - if self.type == Constants.ValueType.Number && other.type == Constants.ValueType.Number: - return self.number > other.number +func greater(other): + if type == Constants.ValueType.Number and other.type == Constants.ValueType.Number: + return number > other.number return false -#less than other -func less(other)->bool: - if self.type == Constants.ValueType.Number && other.type == Constants.ValueType.Number: - return self.number < other.number +func less(other): + if type == Constants.ValueType.Number and other.type == Constants.ValueType.Number: + return number < other.number return false -#greater than or equal to other -func geq(other)->bool: - if self.type == Constants.ValueType.Number && other.type == Constants.ValueType.Number: - return self.number > other.number || self.equals(other) +func geq(other): + if type == Constants.ValueType.Number and other.type == Constants.ValueType.Number: + return number > other.number or equals(other) return false -#lesser than or equal to other -func leq(other)->bool: - if self.type == Constants.ValueType.Number && other.type == Constants.ValueType.Number: - return self.number < other.number || self.equals(other) +func leq(other): + if type == Constants.ValueType.Number and other.type == Constants.ValueType.Number: + return number < other.number or equals(other) return false func _to_string(): diff --git a/addons/Wol/core/variable_storage.gd b/addons/Wol/core/variable_storage.gd deleted file mode 100644 index 776588c..0000000 --- a/addons/Wol/core/variable_storage.gd +++ /dev/null @@ -1,23 +0,0 @@ -extends Node - -signal values_changed - -const Value = preload("res://addons/Wol/core/Value.gd") - -var variables = {} - -func set_value(name, value): - print('SETTING VALUES %s: %s' % [name, value]) - if !(value is Value): - variables[name] = Value.new(value) - else: - variables[name] = value - - emit_signal('values_changed') - -func get_value(name): - return variables.get(name) - -func clear_values(): - variables.clear() - emit_signal('values_changed')