예제 #1
0
        private void InterpretExtendExpression(ExtendExpression expression)
        {
            var obj = InterpretObjectExpression(expression.Object);

            TypeExtender.Extend(this, expression.Type, obj);
        }
예제 #2
0
        private object InterpretMemberExpression(BinaryOperatorExpression expression, bool returnRef = false)
        {
            var obj = InterpretExpression(expression.LeftOperand) as AphidObject;

            string key;

            if (expression.RightOperand is IdentifierExpression)
            {
                key = (expression.RightOperand as IdentifierExpression).Identifier;
            }
            else if (expression.RightOperand is StringExpression)
            {
                key = (string)ValueHelper.Unwrap(InterpretStringExpression(expression.RightOperand as StringExpression));
            }
            else if (expression.RightOperand is DynamicMemberExpression)
            {
                var memberExp = expression.RightOperand as DynamicMemberExpression;
                key = ValueHelper.Unwrap(InterpretExpression(memberExp.MemberExpression)).ToString();
            }
            else
            {
                throw new AphidRuntimeException("Unexpected expression {0}", expression.RightOperand);
            }

            if (returnRef)
            {
                return(new AphidRef()
                {
                    Name = key, Object = obj
                });
            }
            else
            {
                AphidObject val;

                if (obj == null)
                {
                    throw new AphidRuntimeException("Undefined member {0} in expression {1}",
                                                    expression.LeftOperand,
                                                    expression);
                }
                else if (!obj.TryResolve(key, out val))
                {
                    var t      = obj.GetValueType();
                    var extKey = TypeExtender.GetName(t, key);

                    if (!_currentScope.TryResolve(extKey, out val))
                    {
                        throw new AphidRuntimeException("Undefined member {0} in expression {1}", key, expression);
                    }

                    var function = ((AphidFunction)val.Value).Clone();
                    function.ParentScope = new AphidObject {
                        Parent = _currentScope
                    };
                    function.ParentScope.Add(function.Args[0], obj);
                    function.Args = function.Args.Skip(1).ToArray();
                    val           = new AphidObject(function);
                }

                return(val);
            }
        }