Example #1
0
        protected override Expression DoResolve(ScriptingContext context)
        {
            expr = expr.Resolve (context);
            if (expr == null)
                return null;

            resolved = true;
            return this;
        }
Example #2
0
        public TargetFunctionType EvaluateMethod(ScriptingContext context,
							  LocationType type, Expression [] types)
        {
            if (!resolved)
                throw new InvalidOperationException (
                    String.Format (
                        "Some clown tried to evaluate the " +
                        "unresolved expression `{0}'", Name));

            try {
                TargetFunctionType func = DoEvaluateMethod (context, type, types);
                if (func == null)
                    throw new ScriptingException (
                        "Expression `{0}' is not a method", Name);

                return func;
            } catch (LocationInvalidException ex) {
                throw new ScriptingException (
                    "Location of variable `{0}' is invalid: {1}",
                    Name, ex.Message);
            }
        }
Example #3
0
 public ConditionalExpression(Expression test, Expression true_expr, Expression false_expr)
 {
     this.test = test;
       this.true_expr = true_expr;
       this.false_expr = false_expr;
 }
Example #4
0
 public BinaryOperator(Kind kind, Expression left, Expression right)
 {
     this.kind = kind;
     this.left = left;
     this.right = right;
 }
Example #5
0
        private long GetValue(ScriptingContext context, Expression expr)
        {
            object val = expr.Evaluate (context);
            again:
            if (val is int)
                return (long) (int) val;
            else if (val is uint)
                return (long) (uint) val;
            else if (val is ulong)
                return (long) (ulong) val;
            else if (val is long)
                return (long) val;
            else if (val is TargetPointerObject) {
                TargetPointerObject pobj = (TargetPointerObject) val;
                return pobj.GetAddress (context.CurrentThread).Address;
            } else if (val is TargetFundamentalObject) {
                TargetFundamentalObject fobj = (TargetFundamentalObject) val;
                val = fobj.GetObject (context.CurrentThread);
                if (!(val is TargetFundamentalObject))
                    goto again;
            }

            throw new ScriptingException ("Cannot evaluate expression `{0}'", expr.Name);
        }
Example #6
0
        protected override TargetFunctionType DoEvaluateMethod(ScriptingContext context,
									LocationType type,
									Expression[] types)
        {
            switch (type) {
            case LocationType.PropertyGetter:
            case LocationType.PropertySetter:
                TargetPropertyInfo property = Member as TargetPropertyInfo;
                if (property == null)
                    return null;

                if (type == LocationType.PropertyGetter) {
                    if (!property.CanRead)
                        throw new ScriptingException (
                            "Property {0} doesn't have a getter.", Name);
                    return property.Getter;
                } else {
                    if (!property.CanWrite)
                        throw new ScriptingException (
                            "Property {0} doesn't have a setter.", Name);
                    return property.Setter;
                }

            case LocationType.EventAdd:
            case LocationType.EventRemove:
                TargetEventInfo ev = Member as TargetEventInfo;
                if (ev == null)
                    return null;

                if (type == LocationType.EventAdd)
                    return ev.Add;
                else
                    return ev.Remove;

            case LocationType.Method:
            case LocationType.DelegateInvoke:
                MethodGroupExpression mg = InvocationExpression.ResolveDelegate (
                    context, this);
                if (mg == null)
                    return null;

                return mg.EvaluateMethod (context, LocationType.Method, types);

            default:
                return null;
            }
        }
Example #7
0
 public TypeOfExpression(Expression expr)
 {
     this.expr = expr;
 }
Example #8
0
        protected override SourceLocation DoEvaluateSource(ScriptingContext context)
        {
            Expression[] types = new Expression [arguments.Length];
            TargetType[] argtypes = new TargetType [types.Length];

            for (int i = 0; i < arguments.Length; i++) {
                types [i] = arguments [i].ResolveType (context);
                argtypes [i] = types [i].EvaluateType (context);
            }

            TargetFunctionType func = method_expr.OverloadResolve (context, argtypes);
            if (func != null)
                return new SourceLocation (func);

            return null;
        }
Example #9
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);
        }
Example #10
0
        public static MethodGroupExpression ResolveDelegate(ScriptingContext context,
								     Expression expr)
        {
            TargetClassType ctype = Convert.ToClassType (expr.EvaluateType (context));
            if (ctype == null)
                return null;

            TargetClassObject cobj;
            try {
                cobj = Convert.ToClassObject (
                    context.CurrentThread, expr.EvaluateObject (context));
            } catch {
                cobj = null;
            }

            TargetClassType delegate_type = ctype.Language.DelegateType;
            if (!CastExpression.TryCast (context, ctype, delegate_type))
                return null;

            TargetFunctionType invoke = null;
            foreach (TargetMethodInfo method in ctype.Methods) {
                if (method.Name == "Invoke") {
                    invoke = method.Type;
                    break;
                }
            }

            if (invoke == null)
                return null;

            TargetFunctionType[] methods = new TargetFunctionType[] { invoke };

            MethodGroupExpression mg = new MethodGroupExpression (
                ctype, cobj, "Invoke", methods, true, false);
            return mg;
        }
Example #11
0
        protected override TargetFunctionType DoEvaluateMethod(ScriptingContext context,
									LocationType type,
									Expression[] types)
        {
            return method_expr.EvaluateMethod (context, type, types);
        }
Example #12
0
        public InvocationExpression(Expression expr, Expression[] arguments)
        {
            this.expr = expr;
            this.arguments = arguments;

            name = String.Format ("{0} ()", expr.Name);
        }
Example #13
0
        protected virtual TargetFunctionType DoEvaluateMethod(ScriptingContext context,
								       LocationType type,
								       Expression[] types)
        {
            return null;
        }
Example #14
0
        public ArrayAccessExpression(Expression expr, Expression[] indices)
        {
            this.expr = expr;
            this.indices = indices;

            StringBuilder sb = new StringBuilder("");
            bool comma = false;
            foreach (Expression index in indices) {
                if (comma) sb.Append(",");
                sb.Append (index.ToString());
                comma = true;
            }
            name = String.Format ("{0}[{1}]", expr.Name, sb.ToString());
        }
Example #15
0
 public PointerTypeExpression(Expression expr)
 {
     this.expr = expr;
 }
Example #16
0
 public MemberAccessExpression(Expression left, string name)
 {
     this.left = left;
     this.name = name;
 }
Example #17
0
        protected override Expression DoResolve(ScriptingContext context)
        {
            int i;
            expr = expr.Resolve (context);
            if (expr == null)
                return null;

            for (i = 0; i < indices.Length; i ++) {
                indices[i] = indices[i].Resolve (context);
                if (indices[i] == null)
                    return null;
            }

            resolved = true;
            return this;
        }
Example #18
0
        protected override TargetFunctionType DoEvaluateMethod(ScriptingContext context,
									LocationType type,
									Expression[] types)
        {
            if (type != LocationType.Method)
                return null;

            if (types == null) {
                if (methods.Length == 1)
                    return (TargetFunctionType) methods [0];

                                throw new ScriptingException (
                                        "Ambiguous method `{0}'; need to use full name", Name);
            }

            TargetType[] argtypes = new TargetType [types.Length];
            for (int i = 0; i < types.Length; i++)
                argtypes [i] = types [i].EvaluateType (context);

            TargetFunctionType func = OverloadResolve (context, argtypes);
            if (func != null)
                return func;

            return context.Interpreter.QueryMethod (methods);
        }
Example #19
0
        int GetIntIndex(Thread target, Expression index, ScriptingContext context)
        {
            try {
                object idx = index.Evaluate (context);

                if (idx is int)
                    return (int) idx;
                else if (idx is long)
                    return (int) (long) idx;

                TargetFundamentalObject obj = (TargetFundamentalObject) idx;
                return (int) obj.GetObject (target);
            } catch (Exception e) {
                throw new ScriptingException (
                    "Cannot convert {0} to an integer for indexing: {1}",
                    index, e);
            }
        }
Example #20
0
        public NewExpression(Expression type_expr, Expression[] arguments)
        {
            this.type_expr = type_expr;
            this.arguments = arguments;

            name = String.Format ("new {0} ()", type_expr.Name);
        }
Example #21
0
        public AssignmentExpression(Expression left, Expression right)
        {
            this.left = left;
            this.right = right;

            name = left.Name + "=" + right.Name;
        }
Example #22
0
        protected override Expression DoResolve(ScriptingContext context)
        {
            type_expr = type_expr.ResolveType (context);
            if (type_expr == null)
                return null;

            for (int i = 0; i < arguments.Length; i++) {
                arguments [i] = arguments [i].Resolve (context);
                if (arguments [i] == null)
                    return null;
            }

            resolved = true;
            return this;
        }
Example #23
0
        protected override Expression DoResolve(ScriptingContext context)
        {
            left = left.Resolve (context);
            if (left == null)
                return null;

            right = right.Resolve (context);
            if (right == null)
                return null;

            resolved = true;
            return this;
        }
Example #24
0
        public ParentExpression(Expression expr, int level)
        {
            this.expr = expr;
            this.level = level;

            if (level > 0)
                name = String.Format ("$parent+{0} ({1})", level, expr.Name);
            else
                name = String.Format ("$parent ({0})", expr.Name);
        }
Example #25
0
 public CastExpression(Expression target, Expression expr)
 {
     this.target = target;
     this.expr = expr;
     this.name = String.Format ("(({0}) {1})", target.Name, expr.Name);
 }
Example #26
0
 public PointerDereferenceExpression(Expression expr, bool current_ok)
 {
     this.expr = expr;
     this.current_ok = current_ok;
     name = '*' + expr.Name;
 }
Example #27
0
        TargetObject DoCast(ScriptingContext context, Expression expr,
				     TargetType target_type)
        {
            TargetObject source = expr.EvaluateObject (context);
            if (source == null)
                return null;

            if (target_type is TargetObjectType) {
                if (((source is TargetClassObject) && !source.Type.IsByRef) ||
                    (source is TargetFundamentalObject))
                    return target_type.Language.CreateBoxedObject (context.CurrentThread, source);

                if (source is TargetObjectObject)
                    return source;

                throw new ScriptingException (
                    "Cannot box object `{0}': not a value-type", expr.Name);
            }

            if (target_type is TargetPointerType) {
                TargetAddress address;

                PointerExpression pexpr = expr as PointerExpression;
                if (pexpr != null)
                    address = pexpr.EvaluateAddress (context);
                else {
                    TargetPointerType ptype = expr.EvaluateType (context)
                        as TargetPointerType;
                    if ((ptype == null) || ptype.IsTypesafe)
                        return null;

                    pexpr = new AddressOfExpression (expr);
                    pexpr.Resolve (context);

                    address = pexpr.EvaluateAddress (context);
                }

                return ((TargetPointerType) target_type).GetObject (address);
            }

            if (target_type is TargetFundamentalType) {
                TargetFundamentalObject fobj = expr.EvaluateObject (context)
                    as TargetFundamentalObject;
                if (fobj == null)
                    return null;

                TargetFundamentalType ftype = target_type as TargetFundamentalType;
                return Convert.ExplicitFundamentalConversion (context, fobj, ftype);
            }

            TargetClassType ctype = Convert.ToClassType (target_type);
            TargetClassObject source_cobj = Convert.ToClassObject (context.CurrentThread, source);

            if (source_cobj == null)
                throw new ScriptingException (
                    "Variable {0} is not a class type.", expr.Name);

            return TryCast (context, source_cobj, ctype);
        }
Example #28
0
 public AddressOfExpression(Expression expr)
 {
     this.expr = expr;
     name = '&' + expr.Name;
 }
Example #29
0
        protected override Expression DoResolve(ScriptingContext context)
        {
            this.test = this.test.Resolve (context);
              if (this.test == null)
            return null;

              this.true_expr = this.true_expr.Resolve (context);
              if (this.true_expr == null)
            return null;

              this.false_expr = this.false_expr.Resolve (context);
              if (this.false_expr == null)
            return null;

            resolved = true;
            return this;
        }
Example #30
0
        protected override bool DoResolve(ScriptingContext context)
        {
            if (Repeating)
                return true;

            expression = ParseExpression (context);
            if (expression == null)
                return false;

            expression = expression.Resolve (context);
            return expression != null;
        }