Skip to content

Commit

Permalink
✨ Add if then else WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Dnomyar committed Nov 29, 2017
1 parent c7c1402 commit dc21ba9
Show file tree
Hide file tree
Showing 15 changed files with 421 additions and 80 deletions.
6 changes: 6 additions & 0 deletions src/main/java/fr/imt/inference/ConstraintCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import fr.imt.inference.type.Type;
import fr.imt.logger.Logger;
import io.vavr.collection.HashSet;
import io.vavr.collection.Set;

import java.util.Deque;
import java.util.LinkedList;
Expand Down Expand Up @@ -42,6 +44,10 @@ public Constraint head() {
return constraints.getFirst();
}

public Set<Constraint> all(){
return HashSet.ofAll(constraints);
}

public ConstraintCollection tail() {
Deque<Constraint> newConstraints = new LinkedList<>();
newConstraints.addAll(this.constraints);
Expand Down
58 changes: 0 additions & 58 deletions src/main/java/fr/imt/inference/ast/BinaryArithmeticOperation.java

This file was deleted.

63 changes: 63 additions & 0 deletions src/main/java/fr/imt/inference/ast/If.java
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;
}
}
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();
}
}
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 src/main/java/fr/imt/inference/ast/binaryexpression/Condition.java
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;
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package fr.imt.inference.ast;
package fr.imt.inference.ast.binaryexpression.operators;

import io.vavr.collection.List;

public enum Operator {
public enum ArithmeticOperator {
PLUS("+"), MINUS("-"), TIME("*"), DIVIDE("/");

public final String operator;

Operator(String operator) {
ArithmeticOperator(String operator) {
this.operator = operator;
}

public static List<Operator> all() {
public static List<ArithmeticOperator> all() {
return List.of(PLUS, MINUS, TIME, DIVIDE);
}

Expand Down
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;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package fr.imt.inference.ast.factory;

import fr.imt.inference.ast.*;
import fr.imt.inference.ast.binaryexpression.Condition;
import fr.imt.inference.ast.binaryexpression.operators.ArithmeticOperator;
import fr.imt.inference.ast.binaryexpression.BinaryArithmeticOperation;
import fr.imt.inference.ast.binaryexpression.operators.EqualityOperator;

public class ExpressionFactory {

Expand Down Expand Up @@ -28,10 +32,19 @@ public static Let Let(Variable identifier, Expression definition, Expression bod
return new Let(identifier, definition, body);
}

public static BinaryArithmeticOperation Ope(Expression left, Expression right, Operator operator) {
public static BinaryArithmeticOperation Ope(Expression left, Expression right, ArithmeticOperator operator) {
return new BinaryArithmeticOperation(left, right, operator);
}

public static Condition Con(Expression left, Expression right, EqualityOperator operator) {
return new Condition(left, right, operator);
}


public static If If(Condition condition, Expression thenExpression, Expression elseExpression) {
return new If(condition, thenExpression, elseExpression);
}

public static TBoolean Bool(Boolean value) {
return new TBoolean(value);
}
Expand Down
Loading

0 comments on commit dc21ba9

Please sign in to comment.