public override ASTN VisitComparer([NotNull] ComparerContext context)
        {
            ExprNode     left  = VisitExpr(context.left) as ExprNode;
            ExprNode     right = VisitExpr(context.right) as ExprNode;
            ComparerNode node;

            switch (context.op.Type)
            {
            case EQU:
                node = new EqualNode(context);
                break;

            case LOW:
                node = new LowerNode(context);
                break;

            default:
                node = new LowerOrEqualNode(context);
                break;
            }
            node.Leftexpr  = left;
            node.Rigthexpr = right;
            return(node);
        }
コード例 #2
0
    bool Visit(ComparerContext context)
    {
        var type  = GetType(context);
        var left  = Visit(context.left);
        var right = Visit(context.right);

        switch (context.op.Type)
        {
        case LOW:     // Lower
        {
            if (left is int && right is int)
            {
                return((int)left < (int)right);
            }
            else
            {
                throw new Exception($"Operator '<' cannot be applied to operands of type {left.GetType()} and {right.GetType()}");
            }
        }

        case LOE:     // Lower or Equal
        {
            if (left is int && right is int)
            {
                return((int)left <= (int)right);
            }
            else
            {
                throw new Exception($"Operator '<=' cannot be applied to operands of type {left.GetType()} and {right.GetType()}");
            }
        }

        case EQU:     // Equal
        {
            switch (type)
            {
            case COOL_TYPE.INTEGER:
            {
                if (left is int && right is int)
                {
                    return((int)left == (int)right);
                }
            } break;

            case COOL_TYPE.STRING:
            {
                if (left is string && right is string)
                {
                    return((string)left == (string)right);
                }
            } break;

            case COOL_TYPE.BOOL:
            {
                if (left is bool && right is bool)
                {
                    return((bool)left == (bool)right);
                }
            } break;

            default:
                throw new Exception($"Operator '==' cannot be applied to operands of type {left.GetType()} and {right.GetType()}");
            }
        } break;
        }

        throw new InvalidOperationException();
    }