internal static void SetNear(VirtualMachine vm, Instruction i) { Value vName = vm.Stack.Shift(); Value vValue = vm.Stack.Shift(); if (vName.Type != ValueTypes.STRING || vName.String_Value == null || vName.String_Value == "") { vm.RaiseError("Tried to use a non-string value for a variable name"); return; } if (vm.Exists(vName.String_Value) == false) { if (vm.CurrentEnvironment.Parent == null || vm.CurrentEnvironment.Parent.Exists(vName.String_Value) == false) { vm.RaiseError("Tried to access non-existent value name " + vName.String_Value); return; } if (i.Type == OpCodeTypes.TYPESET) { if (vm.CurrentEnvironment.Parent.Get(vName.String_Value).Type != vValue.Type) { vm.RaiseError("Tried to redefine type for variable " + vName.String_Value + ": value is " + vm.Get(vName.String_Value).Type + ", new value is " + vValue.Type); return; } } vm.CurrentEnvironment.Parent.Set(vName.String_Value, vValue); vm.Stack.Push(vName); return; } else { if (i.Type == OpCodeTypes.TYPESET) { if (vm.Get(vName.String_Value).Type != vValue.Type) { vm.RaiseError("Tried to redefine type for variable " + vName.String_Value + ": value is " + vm.Get(vName.String_Value).Type + ", new value is " + vValue.Type); return; } } vm.Set(vName.String_Value, vValue); vm.Stack.Push(vName); } }
/// <summary> /// /// </summary> /// <param name="vm"></param> /// <param name="i"></param> internal static void SetVar(VirtualMachine vm, Instruction i) { if (i.Args.Count() < 1) { vm.RaiseError("Tried to set a variable argument without an argument."); return; } Value vName = i.Args[0]; Value vValue = Value.New(ValueTypes.NULL); if(i.Args.Count() > 1){ vValue = i.Args[1]; } else { vValue = vm.Stack.Shift(); } if (vName.Type != ValueTypes.STRING || vName.String_Value == null || vName.String_Value == "") { vm.RaiseError("Tried to use a non-string value for a variable argument name"); return; } Environment e = vm.CurrentEnvironment; while (e != null) { if (e.Exists(vName.String_Value)) { break; } e = e.Parent; } if (e == null) { e = vm.CurrentEnvironment; } else { if (i.Type == OpCodeTypes.TYPESET) { Value typeValue = e.Get(vName.String_Value); if (typeValue.Type != ValueTypes.ARRAY && typeValue.Type != vValue.Type) { vm.RaiseError("Tried to redefine type for variable " + vName.String_Value + ": value is " + vm.Get(vName.String_Value).Type + ", new value is " + vValue.Type); return; } } } e.Set(vName.String_Value, vValue); vm.Stack.Push(vName); }
/// <summary> /// value, name (end) | name (end) /// </summary> /// <param name="vm"></param> internal static void Set(VirtualMachine vm, Instruction i) { Value vName = vm.Stack.Shift(); Value vValue = vm.Stack.Shift(); if (vName.Type != ValueTypes.STRING || vName.String_Value == null || vName.String_Value == "") { vm.RaiseError("Tried to use a non-string value for a variable name"); return; } Environment e = vm.CurrentEnvironment; while (e != null) { if (e.Exists(vName.String_Value)) { break; } e = e.Parent; } if (e == null) { e = vm.CurrentEnvironment; } else { if (i.Type == OpCodeTypes.TYPESET) { if (e.Get(vName.String_Value).Type != vValue.Type) { vm.RaiseError("Tried to redefine type for variable " + vName.String_Value + ": value is " + vm.Get(vName.String_Value).Type + ", new value is " + vValue.Type); return; } } } e.Set(vName.String_Value, vValue); vm.Stack.Push(vName); }