private BinaryExpression CreateBinaryExpression(DustParser.ExpressionContext context, DustParser.ExpressionContext leftContext, string @operator, DustParser.ExpressionContext rightContext) { Expression left = (Expression)Visit(leftContext); Expression right = (Expression)Visit(rightContext); BinaryOperatorType operatorType = BinaryOperatorTypeHelper.FromString(@operator); switch (operatorType) { case BinaryOperatorType.EQUAL: case BinaryOperatorType.NOT_EQUAL: case BinaryOperatorType.BIGGER: case BinaryOperatorType.BIGGER_EQUAL: case BinaryOperatorType.SMALLER: case BinaryOperatorType.SMALLER_EQUAL: return(new BinaryExpression(left, operatorType, right, context.GetRange())); } if (left.Type == DustType.Number && right.Type == DustType.Number) { switch (operatorType) { case BinaryOperatorType.PLUS: case BinaryOperatorType.MINUS: case BinaryOperatorType.TIMES: case BinaryOperatorType.DIVIDE: return(new BinaryExpression(left, operatorType, right, context.GetRange())); } } // TODO: Make this look better if ((left.Type == DustType.String || left.Type == DustType.Number) && (right.Type == DustType.String || right.Type == DustType.Number)) { switch (operatorType) { case BinaryOperatorType.PLUS: case BinaryOperatorType.TIMES: return(new BinaryExpression(left, operatorType, right, context.GetRange())); } } visitorContext.ErrorHandler.ThrowError(new SyntaxError($"Cannot apply operator '{BinaryOperatorTypeHelper.ToString(operatorType)}' to operands of type '{left.Type}' and '{right.Type}'", context.GetRange())); return(null); }
private UnaryExpression CreateUnaryExpression(DustParser.ExpressionContext context, DustParser.ExpressionContext expressionContext, string @operator) { Expression expression = (Expression)Visit(expressionContext); UnaryOperatorType operatorType = UnaryOperatorTypeHelper.FromString(@operator); if (expression is IdentifierExpression || expression is Function) { switch (operatorType) { case UnaryOperatorType.DELETE: return(new UnaryExpression(expression, operatorType, context.GetRange())); } } if (expression.Type == DustType.Number) { switch (operatorType) { case UnaryOperatorType.PLUS_PLUS: case UnaryOperatorType.MINUS_MINUS: case UnaryOperatorType.TIMES_TIMES: case UnaryOperatorType.DIVIDE_DIVIDE: return(new UnaryExpression(expression, operatorType, context.GetRange())); } } if (expression.Type == DustType.Bool) { switch (operatorType) { case UnaryOperatorType.BANG: return(new UnaryExpression(expression, operatorType, context.GetRange())); } } visitorContext.ErrorHandler.ThrowError(new SyntaxError($"Cannot apply operator '{UnaryOperatorTypeHelper.ToString(operatorType)}' to operand of type '{expression.Type}'", context.GetRange())); return(null); }