aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble/couscous/backends/python/ast/PythonBinaryOperation.java
blob: ecaf038742e47cbbc7c3fbd0a93c718c2f712b5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package org.zwobble.couscous.backends.python.ast;

import org.zwobble.couscous.backends.python.ast.visitors.PythonNodeVisitor;

public class PythonBinaryOperation implements PythonExpressionNode {
    public static PythonExpressionNode pythonIs(PythonExpressionNode left, PythonExpressionNode right) {
        return new PythonBinaryOperation("is", 50, left, right);
    }

    public static PythonExpressionNode pythonAnd(PythonExpressionNode left, PythonExpressionNode right) {
        return new PythonBinaryOperation("and", 20, left, right);
    }

    public static PythonExpressionNode pythonOr(PythonExpressionNode left, PythonExpressionNode right) {
        return new PythonBinaryOperation("or", 20, left, right);
    }

    public static PythonExpressionNode pythonAdd(PythonExpressionNode left, PythonExpressionNode right) {
        return new PythonBinaryOperation("+", 100, left, right);
    }

    public static PythonExpressionNode pythonSubtract(PythonExpressionNode left, PythonExpressionNode right) {
        return new PythonBinaryOperation("-", 100, left, right);
    }

    public static PythonExpressionNode pythonMultiply(PythonExpressionNode left, PythonExpressionNode right) {
        return new PythonBinaryOperation("*", 110, left, right);
    }

    public static PythonExpressionNode pythonEqual(PythonExpressionNode left, PythonExpressionNode right) {
        return new PythonBinaryOperation("==", 50, left, right);
    }

    public static PythonExpressionNode pythonNotEqual(PythonExpressionNode left, PythonExpressionNode right) {
        return new PythonBinaryOperation("!=", 50, left, right);
    }

    public static PythonExpressionNode pythonLessThan(PythonExpressionNode left, PythonExpressionNode right) {
        return new PythonBinaryOperation("<", 50, left, right);
    }

    public static PythonExpressionNode pythonLessThanOrEqual(PythonExpressionNode left, PythonExpressionNode right) {
        return new PythonBinaryOperation("<=", 50, left, right);
    }

    public static PythonExpressionNode pythonGreaterThan(PythonExpressionNode left, PythonExpressionNode right) {
        return new PythonBinaryOperation(">", 50, left, right);
    }

    public static PythonExpressionNode pythonGreatThanOrEqual(PythonExpressionNode left, PythonExpressionNode right) {
        return new PythonBinaryOperation(">=", 50, left, right);
    }
    
    private final String operator;
    private final int precedence;
    private final PythonExpressionNode left;
    private final PythonExpressionNode right;

    private PythonBinaryOperation(
        String operator,
        int precedence,
        PythonExpressionNode left,
        PythonExpressionNode right)
    {
        this.operator = operator;
        this.precedence = precedence;
        this.left = left;
        this.right = right;
    }
    
    public String getOperator() {
        return operator;
    }
    
    public PythonExpressionNode getLeft() {
        return left;
    }
    
    public PythonExpressionNode getRight() {
        return right;
    }

    @Override
    public void accept(PythonNodeVisitor visitor) {
        visitor.visit(this);
    }

    @Override
    public int precedence() {
        return precedence;
    }
}