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); } } }
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); }
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)), });
public virtual TResult Visit(LogicalExpressionSyntax syntax) { return(DefaultResult); }
public override TypeSymbol Visit(LogicalExpressionSyntax syntax) { return(TypeSymbol.Bool); }
public void LeaveLogicalExpr(LogicalExpressionSyntax logicalExpr, WaddleContext ctx) { }