-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
421 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 0 additions & 58 deletions
58
src/main/java/fr/imt/inference/ast/BinaryArithmeticOperation.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package fr.imt.inference.ast; | ||
|
||
import fr.imt.inference.ConstraintCollection; | ||
import fr.imt.inference.Environment; | ||
import fr.imt.inference.ast.binaryexpression.Condition; | ||
import fr.imt.inference.type.BooleanType; | ||
import fr.imt.inference.type.Type; | ||
|
||
public class If implements Expression { | ||
|
||
private Condition condition; | ||
private Expression thenExpression; | ||
private Expression elseExpression; | ||
|
||
public If(Condition condition, Expression thenExpression, Expression elseExpression) { | ||
this.condition = condition; | ||
this.thenExpression = thenExpression; | ||
this.elseExpression = elseExpression; | ||
} | ||
|
||
@Override | ||
public Type infer(Environment env, ConstraintCollection constraintCollection) { | ||
|
||
Type conditionInferred = condition.infer(env, constraintCollection); | ||
|
||
Type thenExpressionInferred = thenExpression.infer(env, constraintCollection); | ||
|
||
Type elseExpressionInferred = elseExpression.infer(env, constraintCollection); | ||
|
||
constraintCollection.add(conditionInferred, new BooleanType()); | ||
constraintCollection.add(thenExpressionInferred, elseExpressionInferred); | ||
|
||
// does not matter which type among (thenExpressionInferred, elseExpressionInferred) because a constraint is added | ||
return thenExpressionInferred; | ||
} | ||
|
||
|
||
@Override | ||
public String toString() { | ||
return "(if " + condition + " then " + thenExpression + " else " + elseExpression + ")"; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
If anIf = (If) o; | ||
|
||
if (condition != null ? !condition.equals(anIf.condition) : anIf.condition != null) return false; | ||
if (thenExpression != null ? !thenExpression.equals(anIf.thenExpression) : anIf.thenExpression != null) | ||
return false; | ||
return elseExpression != null ? elseExpression.equals(anIf.elseExpression) : anIf.elseExpression == null; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = condition != null ? condition.hashCode() : 0; | ||
result = 31 * result + (thenExpression != null ? thenExpression.hashCode() : 0); | ||
result = 31 * result + (elseExpression != null ? elseExpression.hashCode() : 0); | ||
return result; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/fr/imt/inference/ast/binaryexpression/BinaryArithmeticOperation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package fr.imt.inference.ast.binaryexpression; | ||
|
||
import fr.imt.inference.ConstraintCollection; | ||
import fr.imt.inference.Environment; | ||
import fr.imt.inference.FreshVariable; | ||
import fr.imt.inference.ast.Expression; | ||
import fr.imt.inference.ast.binaryexpression.operators.ArithmeticOperator; | ||
import fr.imt.inference.type.ArrowType; | ||
import fr.imt.inference.type.IntegerType; | ||
import fr.imt.inference.type.Type; | ||
import fr.imt.inference.type.TypeVariable; | ||
|
||
public class BinaryArithmeticOperation extends BinaryExpression { | ||
|
||
private final Expression left; | ||
private final Expression right; | ||
public final ArithmeticOperator arithmeticOperator; | ||
|
||
public BinaryArithmeticOperation(Expression left, Expression right, ArithmeticOperator operator) { | ||
this.left = left; | ||
this.right = right; | ||
this.arithmeticOperator = operator; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
return o instanceof BinaryArithmeticOperation | ||
&& ((BinaryArithmeticOperation) o).right.equals(this.right) | ||
&& ((BinaryArithmeticOperation) o).left.equals(this.left) | ||
&& ((BinaryArithmeticOperation) o).arithmeticOperator.equals(this.arithmeticOperator); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = left != null ? left.hashCode() : 0; | ||
result = 31 * result + (right != null ? right.hashCode() : 0); | ||
result = 31 * result + (arithmeticOperator != null ? arithmeticOperator.hashCode() : 0); | ||
return result; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "(" + left.toString() + ' ' + arithmeticOperator.toString() + ' ' + right.toString() + ')'; | ||
} | ||
|
||
@Override | ||
public Expression left() { | ||
return this.left; | ||
} | ||
|
||
@Override | ||
public Expression right() { | ||
return this.right; | ||
} | ||
|
||
@Override | ||
protected Type returnType() { | ||
return new IntegerType(); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/fr/imt/inference/ast/binaryexpression/BinaryExpression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package fr.imt.inference.ast.binaryexpression; | ||
|
||
import fr.imt.inference.ConstraintCollection; | ||
import fr.imt.inference.Environment; | ||
import fr.imt.inference.FreshVariable; | ||
import fr.imt.inference.ast.Expression; | ||
import fr.imt.inference.type.ArrowType; | ||
import fr.imt.inference.type.IntegerType; | ||
import fr.imt.inference.type.Type; | ||
import fr.imt.inference.type.TypeVariable; | ||
|
||
public abstract class BinaryExpression implements Expression { | ||
|
||
abstract public Expression left(); | ||
abstract public Expression right(); | ||
|
||
abstract protected Type returnType(); | ||
|
||
@Override | ||
public Type infer(Environment env, ConstraintCollection constraintCollection) { | ||
final Type leftType = left().infer(env, constraintCollection); | ||
final Type rightType = right().infer(env, constraintCollection); | ||
|
||
TypeVariable returnType = new FreshVariable(); | ||
|
||
ArrowType type1 = new ArrowType(leftType, new ArrowType(rightType, returnType)); | ||
ArrowType type2 = new ArrowType(new IntegerType(), new ArrowType(new IntegerType(), returnType())); | ||
|
||
constraintCollection.add(type1, type2); | ||
|
||
return returnType; | ||
} | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
src/main/java/fr/imt/inference/ast/binaryexpression/Condition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package fr.imt.inference.ast.binaryexpression; | ||
|
||
import fr.imt.inference.ConstraintCollection; | ||
import fr.imt.inference.Environment; | ||
import fr.imt.inference.ast.Expression; | ||
import fr.imt.inference.ast.binaryexpression.operators.EqualityOperator; | ||
import fr.imt.inference.type.BooleanType; | ||
import fr.imt.inference.type.Type; | ||
|
||
public class Condition extends BinaryExpression { | ||
|
||
private Expression left; | ||
private Expression right; | ||
|
||
@Override | ||
protected Type returnType() { | ||
return new BooleanType(); | ||
} | ||
|
||
private EqualityOperator operator; | ||
|
||
public Condition(Expression left, Expression right, EqualityOperator operator) { | ||
this.left = left; | ||
this.right = right; | ||
this.operator = operator; | ||
} | ||
|
||
@Override | ||
public Expression left() { | ||
return this.left; | ||
} | ||
|
||
@Override | ||
public Expression right() { | ||
return this.right; | ||
} | ||
|
||
|
||
@Override | ||
public String toString() { | ||
return "(" + this.left + " " + this.operator + " " + this.right + ")"; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
Condition condition = (Condition) o; | ||
|
||
if (left != null ? !left.equals(condition.left) : condition.left != null) return false; | ||
if (right != null ? !right.equals(condition.right) : condition.right != null) return false; | ||
return operator == condition.operator; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = left != null ? left.hashCode() : 0; | ||
result = 31 * result + (right != null ? right.hashCode() : 0); | ||
result = 31 * result + (operator != null ? operator.hashCode() : 0); | ||
return result; | ||
} | ||
} |
8 changes: 4 additions & 4 deletions
8
...n/java/fr/imt/inference/ast/Operator.java → ...ression/operators/ArithmeticOperator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/main/java/fr/imt/inference/ast/binaryexpression/operators/EqualityOperator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package fr.imt.inference.ast.binaryexpression.operators; | ||
|
||
import io.vavr.collection.List; | ||
|
||
public enum EqualityOperator { | ||
EQUALS("=="); | ||
|
||
public final String operator; | ||
|
||
EqualityOperator(String operator) { | ||
this.operator = operator; | ||
} | ||
|
||
public static List<EqualityOperator> all() { | ||
return List.of(EQUALS); | ||
} | ||
|
||
public Character toChar() { | ||
return operator.charAt(0); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return operator; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.