示例#1
0
        private ExpressionSyntax ParseLogicalExpression()
        {
            var startToken        = CurrentToken;
            ExpressionSyntax left = ParseRelationalExpression();

            while (true)
            {
                switch (CurrentToken.Type)
                {
                case TokenType.Or:
                    Next();
                    left = new LogicalExpressionSyntax(startToken, left, ParseRelationalExpression(),
                                                       LogicalOperator.Or);
                    break;

                case TokenType.And:
                    Next();
                    left = new LogicalExpressionSyntax(startToken, left, ParseRelationalExpression(),
                                                       LogicalOperator.And);
                    break;

                default:
                    return(left);
                }
            }
        }
示例#2
0
        public bool EnterLogicalExpr(LogicalExpressionSyntax logicalExpr, WaddleContext ctx)
        {
            if (logicalExpr.Left.Accept(TypeVisitor) != TypeSymbol.Bool)
            {
                throw new SemanticErrorException($"Not an Bool @({logicalExpr.Left.StartToken.LineNumber}{logicalExpr.Left.StartToken.CharPosition}).");
            }
            if (logicalExpr.Right.Accept(TypeVisitor) != TypeSymbol.Bool)
            {
                throw new SemanticErrorException($"Not an Bool @({logicalExpr.Right.StartToken.LineNumber}{logicalExpr.Right.StartToken.CharPosition}).");
            }

            return(true);
        }
示例#3
0
 private TypeSymbol WaddleExpression(ExpressionSyntax exprStmt)
 {
     return(exprStmt switch
     {
         LogicalExpressionSyntax logicalExpr => WaddleLogicalExpr(logicalExpr),
         ProductExpressionSyntax productExpr => WaddleProductExpr(productExpr),
         RelationalExpressionSyntax relationalExpr => WaddleRelationalExpr(relationalExpr),
         TermExpressionSyntax termExpr => WaddleTermExpr(termExpr),
         InvocationExpressionSyntax invocationExpr => WaddleInvocationExpr(invocationExpr),
         BoolLiteralAtom _ => TypeSymbol.Bool,
         IntegerLiteralAtom _ => TypeSymbol.Integer,
         StringLiteralAtom _ => TypeSymbol.String,
         IdentifierAtom identifierAtom => _currentFunction?.Variables[identifierAtom.Identifier].Type
         ?? throw new SemanticErrorException($"{identifierAtom.Identifier} does not have a type"),
         _ => throw new ArgumentOutOfRangeException(nameof(exprStmt)),
     });
示例#4
0
 public virtual TResult Visit(LogicalExpressionSyntax syntax)
 {
     return(DefaultResult);
 }
示例#5
0
 public override TypeSymbol Visit(LogicalExpressionSyntax syntax)
 {
     return(TypeSymbol.Bool);
 }
示例#6
0
 public void LeaveLogicalExpr(LogicalExpressionSyntax logicalExpr, WaddleContext ctx)
 {
 }