Ejemplo n.º 1
0
        // Generic visitor for comparison expressions
        private TypeNode VisitComparisonNode(InfixExpressionNode node)
        {
            TypeNode leftType  = Visit(node.Left);
            TypeNode rightType = Visit(node.Left);

            // If left type is Bool or String, log error
            if (leftType == StandardTypes.Bool || leftType == StandardTypes.String)
            {
                ErrorLogger.LogError(new CannotUseWithOperatorError(leftType, node, node.Left.SourcePosition));
                return(new ErrorTypeNode(node.Left.SourcePosition));
            }

            // If right type is Bool or String, log error
            if (rightType == StandardTypes.Bool || rightType == StandardTypes.String)
            {
                ErrorLogger.LogError(new CannotUseWithOperatorError(rightType, node, node.Left.SourcePosition));
                return(new ErrorTypeNode(node.Left.SourcePosition));
            }

            return(new BoolTypeNode(node.SourcePosition));
        }
Ejemplo n.º 2
0
        // HELPER METHODS

        // Generic visitor for infix boolean expressions
        private TypeNode VisitInfixBoolExpression(InfixExpressionNode node)
        {
            TypeNode leftType  = Visit(node.Left);
            TypeNode rightType = Visit(node.Right);

            // If left type is not a Bool, log error
            if (leftType != StandardTypes.Bool)
            {
                ErrorLogger.LogError(new CannotUseWithOperatorError(leftType, node, node.Left.SourcePosition));
                return(new ErrorTypeNode(node.SourcePosition));
            }

            // If right type is not a Bool, log error
            if (rightType != StandardTypes.Bool)
            {
                ErrorLogger.LogError(new CannotUseWithOperatorError(rightType, node, node.Left.SourcePosition));
                return(new ErrorTypeNode(node.SourcePosition));
            }

            return(new BoolTypeNode(node.SourcePosition));
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CannotUseWithOperatorError"/> class.
 /// </summary>
 /// <param name="type">The type that was attempted to be used with the operator.</param>
 /// <param name="op">The operator used.</param>
 /// <param name="sourcePosition">The source position of the node in the program.</param>
 public CannotUseWithOperatorError(TypeNode type, InfixExpressionNode op, SourcePosition sourcePosition) : base(sourcePosition)
 {
     Message = $"Type '{type}' cannot be used with '{op}' operator.";
 }