public static void SetVariable(List <Variable> vars, Node variable, string type, Parser.Variable value) { var normal = variable as Parser.Variable; if (normal != null) { SetVariable(vars, variable.value, type, value.Clone()); return; } var varop = variable as Operation; if (varop != null) { var left = GetVariableByName(vars, varop.left); if (left.constant) { ShowError("left isde of exppression is constant"); return; } if (left.type == "undefined") { ShowError("Can not read property of undefined"); } else { var right = CalculateTree(varop.right, vars); bool found = false; if (right.value == "prototype") { if (value.type == "object") { left.prototype = value; } else { ShowError($"Prototype need to be object, but {value.type} were given"); } return; } foreach (var key in left.fields) { if (key.Key.IsDeepEqualTo(right)) { (key.Value as Parser.Variable)?.Set(value.Clone()); found = true; } } if (!found) { left.fields.Add(right.Clone(), value.Clone()); } } } }
public static void SetVariable(List <Variable> vars, string name, string type, Parser.Variable value) { for (var now = vars.Count - 1; now != -1; --now) { if (vars[now].name == name) { if (vars[now].value.constant) { ShowError("left isde of exppression is constant"); return; } vars[now].value = value.Clone(); vars[now].value.type = type; return; } } vars.Add(new Variable(name, type, value)); }