コード例 #1
0
ファイル: OperationNode.cs プロジェクト: tedneward/katahdin
 public static object Multiply(object a, object b)
 {
     if (a is int)
     {
         return((int)a * ConvertNode.ToInt(b));
     }
     else
     {
         throw new NotImplementedException(
                   "No multiply operator for "
                   + TypeNames.GetName(a.GetType()) + " and "
                   + (b == null ? "null" : TypeNames.GetName(b.GetType())));
     }
 }
コード例 #2
0
ファイル: OperationNode.cs プロジェクト: tedneward/katahdin
 public static object Add(object a, object b)
 {
     if (a is int)
     {
         return((int)a + ConvertNode.ToInt(b));
     }
     else if (a is string)
     {
         return((string)a + ConvertNode.ToString(b));
     }
     else
     {
         throw new NotImplementedException(
                   "No add operator for "
                   + TypeNames.GetName(a.GetType()) + " and "
                   + (b == null ? "null" : TypeNames.GetName(b.GetType())));
     }
 }
コード例 #3
0
ファイル: OperationNode.cs プロジェクト: tedneward/katahdin
        public static bool CompareEquality(object a, object b)
        {
            if (a is double || b is double)
            {
                return(ConvertNode.ToDouble(a) == ConvertNode.ToDouble(b));
            }

            if (a == null)
            {
                return(b == null);
            }
            else if (b == null)
            {
                return(false);
            }
            else if (a is bool)
            {
                return((bool)a == ConvertNode.ToBool(b));
            }
            else if (a is int)
            {
                return((int)a == ConvertNode.ToInt(b));
            }
            else if (a is double)
            {
                return((double)a == ConvertNode.ToDouble(b));
            }
            else if (a is char)
            {
                return(a.ToString() == ConvertNode.ToString(b));
            }
            else if (a is string)
            {
                return((string)a == ConvertNode.ToString(b));
            }
            else if (a is char)
            {
                return(a.ToString() == ConvertNode.ToString(b));
            }
            else
            {
                return(a == b);
            }
        }
コード例 #4
0
ファイル: OperationNode.cs プロジェクト: tedneward/katahdin
        public static bool CompareLessOrEqual(object a, object b)
        {
            if (a is double || b is double)
            {
                return(ConvertNode.ToDouble(a) <= ConvertNode.ToDouble(b));
            }

            if (a is int)
            {
                return((int)a <= ConvertNode.ToInt(b));
            }
            else
            {
                throw new NotImplementedException(
                          "No less or equal comparision for "
                          + TypeNames.GetName(a.GetType()) + " and "
                          + (b == null ? "null" : TypeNames.GetName(b.GetType())));
            }
        }
コード例 #5
0
ファイル: OperationNode.cs プロジェクト: tedneward/katahdin
        public static bool CompareInequality(object a, object b)
        {
            if (a is double || b is double)
            {
                return(ConvertNode.ToDouble(a) != ConvertNode.ToDouble(b));
            }

            if (a == null)
            {
                return(b != null);
            }
            else if (b == null)
            {
                return(true);
            }
            else if (a is int)
            {
                return((int)a != ConvertNode.ToInt(b));
            }
            else if (a is double)
            {
                return((double)a != ConvertNode.ToDouble(b));
            }
            else if (a is string)
            {
                return((string)a != ConvertNode.ToString(b));
            }
            else if (a is char)
            {
                return(a.ToString() != ConvertNode.ToString(b));
            }
            else
            {
                throw new NotImplementedException(
                          "No inequality comparision for "
                          + TypeNames.GetName(a.GetType()) + " and "
                          + (b == null ? "null" : TypeNames.GetName(b.GetType())));
            }
        }
コード例 #6
0
 public override CodeTreeNode BuildCodeTree(RuntimeState state)
 {
     Expression firstExpression = (Expression) expressions[0];
     
     CodeTreeNode printed = firstExpression.BuildCodeTree(state);
     
     printed = new ConvertNode(
         Source,
         printed,
         new ValueNode(
             Source,
             typeof(string)));
     
     for (int n = 1; n < expressions.Count; n++)
     {
         Expression expression = (Expression) expressions[n];
         CodeTreeNode append = expression.BuildCodeTree(state);
         
         CodeTreeNode sep = new ValueNode(
             Source,
             " ");
         
         append = new OperationNode(
             Source,
             Operation.Add,
             sep,
             append);
         
         printed = new OperationNode(
             Source,
             Operation.Add,
             printed,
             append);
     }
     
     return new PrintNode(
         Source,
         printed);
 }
コード例 #7
0
ファイル: OperationNode.cs プロジェクト: tedneward/katahdin
        public static bool CompareGreater(object a, object b)
        {
            if (a is double || b is double)
            {
                return(ConvertNode.ToDouble(a) > ConvertNode.ToDouble(b));
            }

            if (a is int)
            {
                return((int)a > ConvertNode.ToInt(b));
            }
            else if (a is double)
            {
                return((double)a > ConvertNode.ToDouble(b));
            }
            else
            {
                throw new NotImplementedException(
                          "No greater comparision for "
                          + TypeNames.GetName(a.GetType()) + " and "
                          + (b == null ? "null" : TypeNames.GetName(b.GetType())));
            }
        }
コード例 #8
0
ファイル: OperationNode.cs プロジェクト: tedneward/katahdin
        public static object Divide(object a, object b)
        {
            if (a is double || b is double)
            {
                return(ConvertNode.ToDouble(a) / ConvertNode.ToDouble(b));
            }

            if (a is int)
            {
                return((int)a / ConvertNode.ToInt(b));
            }
            else if (a is double)
            {
                return((double)a / ConvertNode.ToDouble(b));
            }
            else
            {
                throw new NotImplementedException(
                          "No divide operator for "
                          + TypeNames.GetName(a.GetType()) + " and "
                          + (b == null ? "null" : TypeNames.GetName(b.GetType())));
            }
        }
コード例 #9
0
ファイル: OperationNode.cs プロジェクト: tedneward/katahdin
        public override object Get(RuntimeState state, object[] parametersHint)
        {
            // Short-circuit operators

            switch (operation)
            {
            case Operation.And:
            {
                if (!ConvertNode.ToBool(operands[0].Get(state)))
                {
                    return(false);
                }

                return(ConvertNode.ToBool(operands[1].Get(state)));
            }
            }

            // Normal operators

            object[] operandValues = new object[operands.Length];

            for (int n = 0; n < operands.Length; n++)
            {
                operandValues[n] = operands[n].Get(state);
            }

            state.RunningSource = Source;

            switch (operation)
            {
            case Operation.Is:
                return(Is(
                           operandValues[0],
                           (Type)operandValues[1]));

            case Operation.Not:
                return(Not(operandValues[0]));

            case Operation.Add:
                return(Add(
                           operandValues[0],
                           operandValues[1]));

            case Operation.Subtract:
                return(Subtract(
                           operandValues[0],
                           operandValues[1]));

            case Operation.Multiply:
                return(Multiply(
                           operandValues[0],
                           operandValues[1]));

            case Operation.Divide:
                return(Divide(
                           operandValues[0],
                           operandValues[1]));

            case Operation.CompareLess:
                return(CompareLess(
                           operandValues[0],
                           operandValues[1]));

            case Operation.CompareLessOrEqual:
                return(CompareLessOrEqual(
                           operandValues[0],
                           operandValues[1]));

            case Operation.CompareEquality:
                return(CompareEquality(
                           operandValues[0],
                           operandValues[1]));

            case Operation.CompareInequality:
                return(CompareInequality(
                           operandValues[0],
                           operandValues[1]));

            case Operation.CompareGreater:
                return(CompareGreater(
                           operandValues[0],
                           operandValues[1]));

            case Operation.CompareGreaterOrEqual:
                return(CompareGreaterOrEqual(
                           operandValues[0],
                           operandValues[1]));

            default:
                throw new NotImplementedException();
            }
        }
コード例 #10
0
ファイル: OperationNode.cs プロジェクト: tedneward/katahdin
 public static bool And(object a, object b)
 {
     return(ConvertNode.ToBool(a) && ConvertNode.ToBool(b));
 }
コード例 #11
0
        public static object Call(RuntimeState state, object callable,
                                  object[] parameters, bool wantRefParams, out bool[] refParams)
        {
            if (parameters == null)
            {
                parameters = new object[] {}
            }
            ;

            if (callable == null)
            {
                throw new NotImplementedException(
                          "No call operation for null");
            }
            else if (callable is ICallable)
            {
                return(((ICallable)callable).Call(state, parameters,
                                                  wantRefParams, out refParams));
            }
            else if (callable is MethodInfo)
            {
                ClrObjectMethodBinding binding
                    = new ClrObjectMethodBinding(null, (MethodInfo)callable);

                return(binding.Call(state, parameters, wantRefParams,
                                    out refParams));
            }
            else
            {
                object callMethod = MemberNode.GetMember(callable, "Call",
                                                         false);

                if (callMethod == null)
                {
                    throw new NotImplementedException(
                              "No call operation for "
                              + TypeNames.GetName(callable.GetType()));
                }

                if (wantRefParams)
                {
                    object getRefParamsMethod = MemberNode.GetMember(callable,
                                                                     "GetRefParams", false);

                    if (getRefParamsMethod != null)
                    {
                        object refParamsObject = CallNode.Call(state,
                                                               getRefParamsMethod, null);

                        refParams = (bool[])ConvertNode.Convert(
                            refParamsObject, typeof(bool[]));
                    }
                    else
                    {
                        refParams = null;
                    }
                }
                else
                {
                    refParams = null;
                }

                return(Call(state, callMethod, new object[] { parameters }));
            }
        }
    }