Inheritance: MethodExpression
Exemplo n.º 1
0
        protected override TargetObject DoEvaluateObject(ScriptingContext context)
        {
            Thread target = context.CurrentThread;
            TargetObject obj = expr.EvaluateObject (context);

            // array[int]
            TargetArrayObject aobj = obj as TargetArrayObject;
            if (aobj != null) {
                int[] int_indices = GetIntIndices (target, context);
                try {
                    return aobj.GetElement (target, int_indices);
                } catch (ArgumentException) {
                    throw new ScriptingException (
                        "Index of array expression `{0}' out of bounds.",
                        expr.Name);
                }
            }

            // pointer[int]
            TargetPointerObject pobj = obj as TargetPointerObject;
            if (pobj != null) {
                // single dimensional array only at present
                int[] int_indices = GetIntIndices (target, context);
                if (int_indices.Length != 1)
                    throw new ScriptingException (
                        "Multi-dimensial arrays of type {0} are not yet supported",
                        expr.Name);

                if (pobj.Type.IsArray)
                    return pobj.GetArrayElement (target, int_indices [0]);

                throw new ScriptingException (
                               "Variable {0} is not an array type.", expr.Name);
            }

            // indexers
            TargetClassObject sobj = Convert.ToClassObject (target, obj);
            if (sobj != null) {
                ArrayList props = new ArrayList ();
                foreach (TargetPropertyInfo prop in sobj.Type.Properties) {
                    if (!prop.CanRead)
                        continue;

                    props.Add (prop.Getter);
                }

                if (props.Count == 0)
                    throw new ScriptingException (
                        "Indexer `{0}' doesn't have a getter.", expr.Name);

                TargetFunctionType[] funcs = new TargetFunctionType [props.Count];
                props.CopyTo (funcs, 0);

                MethodGroupExpression mg = new MethodGroupExpression (
                    sobj.Type, sobj, expr.Name + ".this", funcs, true, false);

                InvocationExpression invocation = new InvocationExpression (
                    mg, indices);
                invocation.Resolve (context);

                return invocation.EvaluateObject (context);
            }

            throw new ScriptingException (
                "{0} is neither an array/pointer type, nor is it " +
                "an object with a valid indexer.", expr);
        }
Exemplo n.º 2
0
        protected override bool DoAssign(ScriptingContext context, TargetObject right)
        {
            Thread target = context.CurrentThread;
            TargetObject obj = expr.EvaluateObject (context);

            // array[int]
            TargetArrayObject aobj = obj as TargetArrayObject;
            if (aobj != null) {
                int[] int_indices = GetIntIndices (target, context);
                try {
                    aobj.SetElement (target, int_indices, right);
                } catch (ArgumentException) {
                    throw new ScriptingException (
                        "Index of array expression `{0}' out of bounds.",
                        expr.Name);
                }

                return true;
            }

            // indexers
            TargetClassObject sobj = Convert.ToClassObject (target, obj);
            if (sobj != null) {
                ArrayList props = new ArrayList ();
                foreach (TargetPropertyInfo prop in sobj.Type.Properties) {
                    if (!prop.CanWrite)
                        continue;

                    props.Add (prop.Setter);
                }

                if (props.Count == 0)
                    throw new ScriptingException (
                        "Indexer `{0}' doesn't have a setter.", expr.Name);

                TargetFunctionType[] funcs = new TargetFunctionType [props.Count];
                props.CopyTo (funcs, 0);

                MethodGroupExpression mg = new MethodGroupExpression (
                    sobj.Type, sobj, expr.Name + "[]", funcs, true, false);

                Expression[] indexargs = new Expression [indices.Length + 1];
                indices.CopyTo (indexargs, 0);
                indexargs [indices.Length] = new ArgumentExpression (right);

                InvocationExpression invocation = new InvocationExpression (
                    mg, indexargs);
                invocation.Resolve (context);

                invocation.Invoke (context, false);
                return true;
            }

            throw new ScriptingException (
                "{0} is neither an array/pointer type, nor is it " +
                "an object with a valid indexer.", expr);
        }
Exemplo n.º 3
0
        public TargetObject Invoke(ScriptingContext context)
        {
            TargetClassType stype = Convert.ToClassType (
                type_expr.EvaluateType (context));

            TargetMethodInfo[] ctors = stype.Constructors;
            TargetFunctionType[] funcs = new TargetFunctionType [ctors.Length];
            for (int i = 0; i < ctors.Length; i++)
                funcs [i] = ctors [i].Type;

            MethodGroupExpression mg = new MethodGroupExpression (
                stype, null, ".ctor", funcs, false, true);

            InvocationExpression invocation = new InvocationExpression (mg, arguments);
            invocation.Resolve (context);

            return invocation.EvaluateObject (context);
        }
Exemplo n.º 4
0
        protected override bool DoResolve(ScriptingContext context)
        {
            Expression expr = ParseExpression (context);
            if (expr == null)
                return false;

            expr = expr.Resolve (context);
            if (expr == null)
                return false;

            invocation = expr as InvocationExpression;
            if (invocation == null)
                throw new ScriptingException (
                    "Expression `{0}' is not a method.", expr.Name);

            return true;
        }