|
| 1 | +package xyz.leutgeb.lorenz.atlas.ast.expressions; |
| 2 | + |
| 3 | +import lombok.EqualsAndHashCode; |
| 4 | +import lombok.NonNull; |
| 5 | +import lombok.Value; |
| 6 | +import xyz.leutgeb.lorenz.atlas.ast.ArithmeticOperator; |
| 7 | +import xyz.leutgeb.lorenz.atlas.ast.Normalization; |
| 8 | +import xyz.leutgeb.lorenz.atlas.ast.sources.Derived; |
| 9 | +import xyz.leutgeb.lorenz.atlas.ast.sources.Source; |
| 10 | +import xyz.leutgeb.lorenz.atlas.typing.simple.TypeClass; |
| 11 | +import xyz.leutgeb.lorenz.atlas.typing.simple.TypeConstraint; |
| 12 | +import xyz.leutgeb.lorenz.atlas.typing.simple.TypeError; |
| 13 | +import xyz.leutgeb.lorenz.atlas.typing.simple.types.BoolType; |
| 14 | +import xyz.leutgeb.lorenz.atlas.typing.simple.types.Type; |
| 15 | +import xyz.leutgeb.lorenz.atlas.unification.UnificationContext; |
| 16 | +import xyz.leutgeb.lorenz.atlas.util.IntIdGenerator; |
| 17 | + |
| 18 | +import java.io.PrintStream; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.Stack; |
| 21 | +import java.util.stream.Stream; |
| 22 | + |
| 23 | +@Value |
| 24 | +@EqualsAndHashCode(callSuper = true) |
| 25 | +public class ArithmeticExpression extends Expression { |
| 26 | + |
| 27 | + @NonNull Expression left; |
| 28 | + @NonNull ArithmeticOperator operator; |
| 29 | + @NonNull Expression right; |
| 30 | + |
| 31 | + public ArithmeticExpression( |
| 32 | + Source source, |
| 33 | + @NonNull Expression left, |
| 34 | + @NonNull ArithmeticOperator operator, |
| 35 | + @NonNull Expression right) { |
| 36 | + super(source); |
| 37 | + this.left = left; |
| 38 | + this.operator = operator; |
| 39 | + this.right = right; |
| 40 | + } |
| 41 | + |
| 42 | + private ArithmeticExpression( |
| 43 | + Source source, Expression left, ArithmeticOperator operator, Expression right, Type type) { |
| 44 | + super(source); |
| 45 | + this.left = left; |
| 46 | + this.operator = operator; |
| 47 | + this.right = right; |
| 48 | + this.type = type; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public Stream<? extends Expression> getChildren() { |
| 53 | + return Stream.of(left, right); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public Type inferInternal(UnificationContext context) throws TypeError { |
| 58 | + var ty = context.fresh(); |
| 59 | + context.addEquivalenceIfNotEqual(right.infer(context), ty, source); |
| 60 | + context.addEquivalenceIfNotEqual(left.infer(context), ty, source); |
| 61 | + context |
| 62 | + .getSignature(context.getFunctionInScope(), source) |
| 63 | + .addConstraint(new TypeConstraint(TypeClass.NUM, ty)); |
| 64 | + return ty; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public Expression normalize(Stack<Normalization> context, IntIdGenerator idGenerator) { |
| 69 | + // TODO(lorenzleutgeb): Only create new expression if necessary! |
| 70 | + return new ArithmeticExpression( |
| 71 | + Derived.anf(this), |
| 72 | + left.normalize(context, idGenerator), |
| 73 | + operator, |
| 74 | + right.normalize(context, idGenerator)); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public void printTo(PrintStream out, int indentation) { |
| 79 | + left.printTo(out, indentation); |
| 80 | + out.print(" "); |
| 81 | + operator.printTo(out); |
| 82 | + out.print(" "); |
| 83 | + right.printTo(out, indentation); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void printHaskellTo(PrintStream out, int indentation, String currentFunction) { |
| 88 | + left.printHaskellTo(out, indentation, currentFunction); |
| 89 | + out.print(" "); |
| 90 | + operator.printHaskellTo(out); |
| 91 | + out.print(" "); |
| 92 | + right.printHaskellTo(out, indentation, currentFunction); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void printJavaTo(PrintStream out, int indentation, String currentFunction) { |
| 97 | + operator.printJavaTo( |
| 98 | + ((IdentifierExpression) left).getName(), ((IdentifierExpression) right).getName(), out); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public Expression unshare(IntIdGenerator idGenerator, boolean lazy) { |
| 103 | + return this; |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public boolean isImmediate() { |
| 108 | + return false; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public Expression rename(Map<String, String> renaming) { |
| 113 | + if (freeVariables().stream() |
| 114 | + .map(IdentifierExpression::getName) |
| 115 | + .anyMatch(renaming::containsKey)) { |
| 116 | + return new ArithmeticExpression( |
| 117 | + Derived.rename(this), left.rename(renaming), operator, right.rename(renaming), type); |
| 118 | + } |
| 119 | + return this; |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public String toString() { |
| 124 | + return left + " " + operator + " " + right; |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public boolean isTerminal() { |
| 129 | + return true; |
| 130 | + } |
| 131 | +} |
0 commit comments