public void SetLocalVar(string name, int i1, int i2, Value val)
 {
     if (Context.Context.InstanceVariables.Contains(name, StringComparer.Ordinal))
         FieldAccessor<Value>.SetNamedValue(this, name, val);
     else if (instancevars.ContainsKey(name))
         instancevars[name][i1, i2] = val;
     else
         instancevars.Add(name, new Variable(i1, i2, val));
 }
Esempio n. 2
0
        public override void Set(int i1, int i2, Value val)
        {
            for (int i = Values.Count; i <= i1; i++)
                Values.Add(new List<Value>() { 0 });

            var list = Values[i1];

            for (int i = list.Count; i <= i2; i++)
                list.Add(Value.Zero);

            list[i2] = val;
        }
 public static double real(Value val)
 {
     if (val.IsReal) return val.Real;
     if (val.String.Trim().Length == 0) return 0.0;
     try
     {
         return double.Parse(val.String, System.Globalization.NumberStyles.Float);
     }
     catch
     {
         throw new ProgramError(Error.RuntimeReal);
     }
 }
 public static void SetVar(int instance, string name, Value val)
 {
     SetVar(instance, name, 0, 0, val);
 }
        public static void SetVar(int instance, string name, int i1, int i2, Value val)
        {
            switch ((InstanceTarget)instance)
            {
                case InstanceTarget.Self:
                    Current.SetLocalVar(name, i1, i2, val);
                    break;
                case InstanceTarget.Other:
                    if (Other == null) throw new ProgramError(Error.CannotAssign);
                    Other.SetLocalVar(name, i1, i2, val);
                    break;
                case InstanceTarget.All:
                    foreach (var inst in Instances)
                    {
                        inst.SetLocalVar(name, i1, i2, val);
                    }
                    break;
                case InstanceTarget.Noone:
                    throw new ProgramError(Error.CannotAssign);
                case InstanceTarget.Global:
                    if (GlobalVariableExists(name))
                        GetGlobalVariable(name).Set(i1, i2, val);
                    else
                        GlobalVariables.Add(name, new Variable(val, i1: i1, i2: i2));
                    break;
                default:
                    var e = GetInstance(instance);

                    if (e != null)
                        e.SetLocalVar(name, i1, i2, val);
                    else if (instance < (int)InstanceTarget.Global)
                        throw new ProgramError(Error.CannotAssign);
                    else
                    {
                        foreach (var i in Instances.Where(ii => ii.object_index == instance))
                        {
                            e.SetLocalVar(name, i1, i2, val);
                        }
                    }
                    break;
            }
        }
Esempio n. 6
0
        static Value Unary(Value v, Func<double, double> op)
        {
            if (!v.IsReal)
                throw new ProgramError(Error.WrongUnaryTypes);

            return op(v);
        }
Esempio n. 7
0
 public Variable(Value val, int i2 = 0, int i1 = 0)
 {
     Values = new List<List<Value>>();
     Set(i1, i2, val);
 }
 public static string string_format(Value val)
 {
     return new Value(val.ToString());
 }
Esempio n. 9
0
 public static Value ShiftLeft(Value a, Value b)
 { return Bitwise(a, b, "<<", (v1, v2) => v1 << (int)v2); }
 public void SetLocalVar(string name, int index, Value val)
 {
     SetLocalVar(name, 0, index, val);
 }
        public static IEnumerable<RuntimeInstance> WithInstance(Value v)
        {
            if (!v.IsReal)
                throw new ProgramError(Error.ExpectedObjectId);

            // Save the last instances being used
            var c = Current;
            var o = Other;
            Other = c;

            try
            {
                switch ((InstanceTarget)(int)v)
                {
                    case InstanceTarget.Self:
                        yield return c;
                        break;
                    case InstanceTarget.Other:
                        if (o != null)
                            yield return o;
                        break;
                    case InstanceTarget.All:
                        foreach (var i in ExecutionContext.Instances)
                            yield return i;
                        break;
                    case InstanceTarget.Global:
                        throw new ProgramError(Error.GlobalWith);
                    default:
                        if (ExecutionContext.Instances.Any(e => e.id == v))
                            yield return LibraryContext.Current.InstanceFactory.Instances[v] as RuntimeInstance;
                        else
                            foreach (var i in ExecutionContext.Instances.Where(ii => ii.object_index == v))
                                yield return i;
                        break;
                }
            }
            finally
            {
                // Restore current and other instances
                Current = c;
                Other = o;
            }
        }
Esempio n. 12
0
 public static Value LogicalXor(Value a, Value b)
 { return Logical(a, b, "^^", (v1, v2) => v1 ^ v2); }
 public void SetLocalVar(string name, Value val)
 {
     SetLocalVar(name, 0, 0, val);
 }
Esempio n. 14
0
 public static Value LogicalOr(Value a, Value b)
 { return Logical(a, b, "||", (v1, v2) => v1 || v2); }
Esempio n. 15
0
 public static Value LogicalAnd(Value a, Value b)
 { return Logical(a, b, "&&", (v1, v2) => v1 && v2); }
Esempio n. 16
0
 public static Value ShiftRight(Value a, Value b)
 { return Bitwise(a, b, ">>", (v1, v2) => v1 >> (int)v2); }
        /// <summary>
        /// Use this at runtime to dereference a variable using dynamically typed values.
        /// </summary>
        public static Value Dereference(Value instance, string name, Value v1, Value v2)
        {
            ValidateArray(v1, v2);

            if (!instance.IsNull)
            {
                if (!instance.IsReal)
                    throw new ProgramError(Error.WrongVariableIndexType);

                if (v1 != 0 || v2 != 0)
                {
                    if (!VariableExists(instance, name) || !Variable(instance, name).CheckIndex(v1, v2))
                        throw new ProgramError(Error.UnknownArray, name);
                }
                else
                {
                    if (!VariableExists(instance, name))
                        throw new ProgramError(Error.UnknownVariable, name);
                }

                return GetVar(instance, name, v1, v2);
            }
            else
            {
                if (v1 != 0 || v2 != 0)
                {
                    if (!VariableExists(name) || !Variable(name).CheckIndex(v1, v2))
                        throw new ProgramError(Error.UnknownArray, name);
                }
                else
                {
                    if (!VariableExists(name))
                        throw new ProgramError(Error.UnknownVariable, name);
                }

                return GetVar(name, v1, v2);
            }
        }
 public static void SetArguments(Value[] args)
 {
     for (int i = 0; i < 16 && i < args.Length; i++)
         Globals.argument[i] = args[i];
 }
        public static void ValidateArray(Value v1, Value v2)
        {
            if (!(v1.IsReal && v2.IsReal))
                throw new ProgramError(Error.WrongArrayIndexType);

            if (v1 < 0 || v2 < 0)
                throw new ProgramError(Error.NegativeArrayIndex);

            if (v1 >= 32000 || v2 >= 32000)
                throw new ProgramError(Error.ArrayBounds);
        }
Esempio n. 20
0
 public static Value IntegerDivision(Value a, Value b)
 { return Arithmetic(a, b, "div", (v1, v2) => (double)((long)v1 / (long)v2)); }
        public static IEnumerable<int> Repeat(Value v)
        {
            if (!v.IsReal)
                throw new ProgramError(Error.RepeatExpression);

            var times = (int)Math.Round(v.Real);

            while (times > 0)
                yield return --times;
        }
Esempio n. 22
0
 public static double is_real(Value val)
 {
     return val.IsReal ? 1.0 : 0.0;
 }
 public static void SetVar(string name, int i1, int i2, Value val)
 {
     if (DeclaredVariables.Contains(name))
     {
         if (ScopedVariables.ContainsKey(name))
         {
             if (ScopedVariables[name].IsReadOnly) throw new ProgramError(Error.CannotAssign);
             ScopedVariables[name][i1, i2] = val;
         }
         else
             ScopedVariables.Add(name, new Variable(i1, i2, val));
     }
     else if (GlobalVariableExists(name))
     {
         var globalvar = GetGlobalVariable(name);
         if (globalvar.IsReadOnly) throw new ProgramError(Error.CannotAssign);
         globalvar[i1, i2] = val;
     }
     else if (Current != null)
     {
         if (Current.VariableExists(name))
         {
             var instancevar = Current.Variable(name);
             if (instancevar.IsReadOnly) throw new ProgramError(Error.CannotAssign);
             instancevar[i1, i2] = val;
         }
         else
            Current.SetLocalVar(name, val);
     }
     
 }
Esempio n. 24
0
 public static double is_string(Value val)
 {
     return val.IsString ? 1.0 : 0.0;
 }
 public static string _string(Value val)
 {
     return val.ToString();
 }
 public void DefineConstant(string name, Value val)
 {
     if (Constants.ContainsKey(name))
         Constants.Remove(name);
     Constants.Add(name, val);
 }
 public override void Set(int i1, int i2, Value value)
 {
     FieldAccessor<Value>.SetNamedValue(m_obj, m_name, i1, i2, value);
 }
 public static void SetVar(string name, int index, Value val)
 {
     SetVar(name, 0, index, val);
 }
 public static void SetVar(string name, Value val)
 {
     SetVar(name, 0, 0, val);
 }
Esempio n. 30
0
 static Value Arithmetic(Value v1, Value v2, string op, Func<double, double, double> realOperator, bool v1String, bool v2String, Func<Value, Value, Value> stringOperator)
 {
     if (v1.IsReal && v2.IsReal)
         return realOperator(v1, v2);
     else if (v1.IsString == v1String && v2.IsString == v2String)
         return stringOperator(v1, v2);
     else
         throw new ProgramError(Error.WrongArgumentTypes, op);
 }