Пример #1
0
        public Statement Visit(BinOperatorStatement binaryOperatorStatement)
        {
            binaryOperatorStatement.left.Accept(this);
            binaryOperatorStatement.right.Accept(this);

            return(null);
        }
Пример #2
0
        public Void Visit(BinOperatorStatement binaryOperatorStatement, SortedSet <string> free)
        {
            binaryOperatorStatement.left.Accept(this, free);
            binaryOperatorStatement.right.Accept(this, free);

            return(null);
        }
Пример #3
0
        // ---

        public IValue Visit(BinOperatorStatement binaryOperatorExpression)
        {
            var left  = binaryOperatorExpression.left.Accept(this);
            var right = binaryOperatorExpression.right.Accept(this);

            var opType = MapOpExpr[binaryOperatorExpression.type];


            if (opType == OperatorExpression.INTEGER)
            {
                if (left.Type == ValueType.Number && right.Type == ValueType.Number)
                {
                    var x = ((NumberValue)left).Number;
                    var y = ((NumberValue)right).Number;
                    switch (binaryOperatorExpression.type)
                    {
                    case BinOperatorStatement.Type.ADD:
                        return(new NumberValue(x + y));

                    case BinOperatorStatement.Type.SUB:
                        return(new NumberValue(x - y));

                    case BinOperatorStatement.Type.MUL:
                        return(new NumberValue(x * y));

                    case BinOperatorStatement.Type.DIV:
                        return(new NumberValue(x / y));
                    }
                }
                else
                {
                    var err = new ErrorMessage();
                    throw new EvaluationError(err.ErrorOutput(ErrorMessage.ErrorCode.BOP_1, binaryOperatorExpression));
                }
            }
            else if (opType == OperatorExpression.LOGICAL)
            {
                if (left.Type == ValueType.Bool && right.Type == ValueType.Bool)
                {
                    var x = ((BoolValue)left).Bool;
                    var y = ((BoolValue)right).Bool;
                    switch (binaryOperatorExpression.type)
                    {
                    case BinOperatorStatement.Type.AND:
                        return(new BoolValue(x && y));

                    case BinOperatorStatement.Type.OR:
                        return(new BoolValue(x || y));
                    }
                }
                else
                {
                    var err = new ErrorMessage();
                    ErrorMessage.ErrorCode code = (binaryOperatorExpression.type == BinOperatorStatement.Type.OR) ? ErrorMessage.ErrorCode.BOP_2 : ErrorMessage.ErrorCode.BOP_3;
                    throw new EvaluationError(err.ErrorOutput(code, binaryOperatorExpression));
                }
            }
            else if (opType == OperatorExpression.EQUALITY)
            {
                if (left.Type == ValueType.Number && right.Type == ValueType.Number)
                {
                    var x = ((NumberValue)left).Number;
                    var y = ((NumberValue)right).Number;
                    switch (binaryOperatorExpression.type)
                    {
                    case BinOperatorStatement.Type.EQ:
                        return(new BoolValue(x == y));

                    case BinOperatorStatement.Type.NEQ:
                        return(new BoolValue(x != y));
                    }
                }
                else if (left.Type == ValueType.Bool && right.Type == ValueType.Bool)
                {
                    var x = ((BoolValue)left).Bool;
                    var y = ((BoolValue)right).Bool;
                    switch (binaryOperatorExpression.type)
                    {
                    case BinOperatorStatement.Type.EQ:
                        return(new BoolValue(x == y));

                    case BinOperatorStatement.Type.NEQ:
                        return(new BoolValue(x != y));
                    }
                }
                else
                {
                    var err = new ErrorMessage();
                    throw new EvaluationError(err.ErrorOutput(ErrorMessage.ErrorCode.BOP_4, binaryOperatorExpression));
                }
            }
            else if (opType == OperatorExpression.INEQUALITY)
            {
                if (left.Type == ValueType.Number && right.Type == ValueType.Number)
                {
                    var x = ((NumberValue)left).Number;
                    var y = ((NumberValue)right).Number;

                    switch (binaryOperatorExpression.type)
                    {
                    case BinOperatorStatement.Type.GR:
                        return(new BoolValue(x > y));

                    case BinOperatorStatement.Type.LE:
                        return(new BoolValue(x < y));

                    case BinOperatorStatement.Type.GEQ:
                        return(new BoolValue(x >= y));

                    case BinOperatorStatement.Type.LEQ:
                        return(new BoolValue(x <= y));
                    }
                }
                else
                {
                    var err = new ErrorMessage();
                    throw new EvaluationError(err.ErrorOutput(ErrorMessage.ErrorCode.BOP_5, binaryOperatorExpression));
                }
            }
            else
            {
                return(null);
                //error
            }

            return(null);
        }
Пример #4
0
        public Type Visit(BinOperatorStatement binOperatorStatement, FunctionGeneratorEnvironment arg)
        {
            var left   = binOperatorStatement.left;
            var right  = binOperatorStatement.right;
            var type   = binOperatorStatement.type;
            var opType = MapOpExpr[binOperatorStatement.type];

            switch (opType)
            {
            case OperatorExpression.INTEGER:
                left.Accept(this, arg);
                right.Accept(this, arg);
                switch (type)
                {
                case BinOperatorStatement.Type.ADD:
                    Program.Emit(T42Instruction.ADD);
                    break;

                case BinOperatorStatement.Type.SUB:
                    Program.Emit(T42Instruction.SUB);
                    break;

                case BinOperatorStatement.Type.MUL:
                    Program.Emit(T42Instruction.MUL);
                    break;

                case BinOperatorStatement.Type.DIV:
                    Program.Emit(T42Instruction.DIV);
                    break;
                }
                break;

            case OperatorExpression.LOGICAL:
                var labelno_next = arg.GetNextLabelNumber();
                var labelno_end  = arg.GetNextLabelNumber();
                var end          = $"{arg.FunctionName}_LABEL{labelno_end}";
                var next         = $"{arg.FunctionName}_LABEL{labelno_next}";
                switch (binOperatorStatement.type)
                {
                case BinOperatorStatement.Type.OR:

                    left.Accept(this, arg);
                    Program.Emit(T42Instruction.BRF(next));
                    Program.Emit(T42Instruction.PUSHBOOL(1));
                    Program.Emit(T42Instruction.BRA(end));
                    Program.Emit(T42Instruction.LABEL(next));
                    right.Accept(this, arg);
                    Program.Emit(T42Instruction.LABEL(end));

                    break;

                case BinOperatorStatement.Type.AND:

                    left.Accept(this, arg);
                    Program.Emit(T42Instruction.BRF(next));
                    right.Accept(this, arg);
                    Program.Emit(T42Instruction.BRA(end));
                    Program.Emit(T42Instruction.LABEL(next));
                    Program.Emit(T42Instruction.PUSHBOOL(0));
                    Program.Emit(T42Instruction.LABEL(end));

                    break;
                }
                break;

            case OperatorExpression.INEQUALITY:
                left.Accept(this, arg);
                right.Accept(this, arg);
                switch (type)
                {
                case BinOperatorStatement.Type.LE:
                case BinOperatorStatement.Type.GEQ:
                    Program.Emit(T42Instruction.LTINT);
                    if (type == BinOperatorStatement.Type.GEQ)
                    {
                        Program.Emit(T42Instruction.NOT);
                    }
                    break;

                case BinOperatorStatement.Type.GR:
                case BinOperatorStatement.Type.LEQ:
                    Program.Emit(T42Instruction.GTINT);
                    if (type == BinOperatorStatement.Type.LEQ)
                    {
                        Program.Emit(T42Instruction.NOT);
                    }
                    break;
                }

                break;

            case OperatorExpression.EQUALITY:
                var t = left.Accept(this, arg);
                right.Accept(this, arg);

                if (t.GetType() == typeof(IntType))
                {
                    Program.Emit(T42Instruction.EQINT);
                }
                else
                {
                    Program.Emit(T42Instruction.EQBOOL);
                }

                if (type == BinOperatorStatement.Type.NEQ)
                {
                    Program.Emit(T42Instruction.NOT);
                }

                break;

            default:
                throw new Exception();
            }


            return(new BoolType());
        }
Пример #5
0
        public Type Visit(BinOperatorStatement binaryOperatorExpression)
        {
            var left  = binaryOperatorExpression.left.Accept(this);
            var right = binaryOperatorExpression.right.Accept(this);

            var opType = MapOpExpr[binaryOperatorExpression.type];


            if (opType == OperatorExpression.INTEGER)
            {
                if (left.GetType().Name == "IntType" && right.GetType().Name == "IntType")
                {
                    return(new IntType());
                }
                else
                {
                    var err = new TypeErrorMessage();
                    throw new TypeException(err.ErrorOutput(TypeErrorMessage.ErrorCode.BOP_1, binaryOperatorExpression));
                }
            }

            else if (opType == OperatorExpression.LOGICAL)
            {
                if (left.GetType().Name == "BoolType" && right.GetType().Name == "BoolType")
                {
                    return(new BoolType());
                }
                else
                {
                    var err = new TypeErrorMessage();
                    TypeErrorMessage.ErrorCode code = (binaryOperatorExpression.type == BinOperatorStatement.Type.OR) ? TypeErrorMessage.ErrorCode.BOP_2 : TypeErrorMessage.ErrorCode.BOP_3;
                    throw new TypeException(err.ErrorOutput(code, binaryOperatorExpression));
                }
            }
            else if (opType == OperatorExpression.EQUALITY)
            {
                if (left.GetType().Name == "BoolType" && right.GetType().Name == "BoolType")
                {
                    return(new BoolType());
                }
                else if (left.GetType().Name == "IntType" && right.GetType().Name == "IntType")
                {
                    return(new BoolType());
                }
                else
                {
                    var err = new TypeErrorMessage();
                    throw new TypeException(err.ErrorOutput(TypeErrorMessage.ErrorCode.BOP_4, binaryOperatorExpression));
                }
            }
            else if (opType == OperatorExpression.INEQUALITY)
            {
                if (left.GetType().Name == "IntType" && right.GetType().Name == "IntType")
                {
                    return(new BoolType());
                }
                else
                {
                    var err = new TypeErrorMessage();
                    throw new TypeException(err.ErrorOutput(TypeErrorMessage.ErrorCode.BOP_5, binaryOperatorExpression));
                }
            }
            else
            {
                return(null);
            }
        }