示例#1
0
        public override Type Typecheck(TypecheckingState state)
        {
            var type1 = Expression1.Typecheck(state);
            var type2 = Expression2.Typecheck(state);

            OperandType = type1;

            switch (Operator)
            {
            case BinaryOperator.Add:
                if (type1 != Type.NumType && type2 != Type.NumType)
                {
                    TypeError("+ expects number operands");
                }
                return(Type.NumType);

            case BinaryOperator.Sub:
                if (type1 != Type.NumType && type2 != Type.NumType)
                {
                    TypeError("- expects number operands");
                }
                return(Type.NumType);

            case BinaryOperator.Eq:
                if (type1 != type2)
                {
                    TypeError("== expects operands of the same type");
                }
                return(Type.BoolType);

            case BinaryOperator.Lt:
                if (type1 != Type.NumType && type2 != Type.NumType)
                {
                    TypeError("- expects number operands");
                }
                return(Type.BoolType);
            }

            throw new ImpossibleException();
        }
        // public Expression Expression1;
        // public Expression Expression2;

        public override Type Typecheck(TypecheckingState state)
        {
            Expression1.Typecheck(state);
            return(Expression2.Typecheck(state));
        }