Skip to content

Commit

Permalink
🚨 Reformat code + Use pattern matching for REPL
Browse files Browse the repository at this point in the history
  • Loading branch information
anaelChardan committed Nov 29, 2017
1 parent 94457c3 commit c7b62c9
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 71 deletions.
11 changes: 5 additions & 6 deletions src/main/java/fr/imt/inference/ExpressionInferer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import fr.imt.inference.errors.UnificationFailureException;
import fr.imt.inference.errors.UnificationMismatchException;
import fr.imt.inference.type.Type;
import fr.imt.inference.type.TypeVariable;
import fr.imt.logger.Logger;
import io.vavr.control.Either;

Expand All @@ -29,11 +28,11 @@ public Either<String, Type> infer(Expression expression) {

return Either.right(rawReturnType.applySubstitution(result));
} catch (
UnificationMismatchException |
UnificationFailureException |
InfiniteTypeException |
NonexistentVariableException e
) {
UnificationMismatchException |
UnificationFailureException |
InfiniteTypeException |
NonexistentVariableException e
) {
return Either.left(e.getMessage());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public Type infer(Environment env, ConstraintCollection constraintCollection) {
@Override
public boolean equals(Object o) {
return o instanceof BinaryArithmeticOperation
&& ((BinaryArithmeticOperation) o).right.equals(this.right)
&& ((BinaryArithmeticOperation) o).left.equals(this.left)
&& ((BinaryArithmeticOperation) o).operator.equals(this.operator);
&& ((BinaryArithmeticOperation) o).right.equals(this.right)
&& ((BinaryArithmeticOperation) o).left.equals(this.left)
&& ((BinaryArithmeticOperation) o).operator.equals(this.operator);
}

@Override
Expand Down
1 change: 0 additions & 1 deletion src/main/java/fr/imt/inference/type/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public interface Type extends Substituable<Type>, FreeTypeVariableContainer {
* Convert a type T into a type S by closing overall free type variables in a type scheme.
*
* @param environment all the variables
*
* @return a Schema containing all the free variables
*/
Scheme generalize(Environment environment);
Expand Down
1 change: 0 additions & 1 deletion src/main/java/fr/imt/inference/type/TypeInstantiator.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public interface TypeInstantiator {
* that does not appear in the current typing environment.
*
* @param environment the typing environment
*
* @return the T type
*/
Type instantiate(Environment environment);
Expand Down
82 changes: 41 additions & 41 deletions src/main/java/fr/imt/parser/ExpressionParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public Either<String, Expression> parse(String str) {

try {
return parser.bind(p -> eof.then(retn(p)))
.parse(Input.of(str))
.match(
parsed -> Either.right(parsed.result),
error -> Either.left(error.getMsg())
);
.parse(Input.of(str))
.match(
parsed -> Either.right(parsed.result),
error -> Either.left(error.getMsg())
);
} catch (Throwable e) {
return Either.left("Cannot parse input");
}
Expand Down Expand Up @@ -72,32 +72,32 @@ private Parser<Character, Operator> arithmeticOperatorParser() {
*/
private Parser<Character, BinaryArithmeticOperation> arithmeticOperationParser(Parser<Character, Expression> expression) {
return
string("op").then(
space(
expression.bind(left ->
string("op").then(
space(
arithmeticOperatorParser().bind(operator ->
space(
expression.bind(right ->
retn(new BinaryArithmeticOperation(left, right, operator)))))))));
expression.bind(left ->
space(
arithmeticOperatorParser().bind(operator ->
space(
expression.bind(right ->
retn(new BinaryArithmeticOperation(left, right, operator)))))))));
}

/**
* \ <var+> -> <exp>
*/
private Parser<Character, Lambda> lambdaParser(Parser<Character, Expression> expression) {
return
chr('\\').then(
space(
variableParser().bind(id ->
space(retn(id))
).many().bind(ids ->
chr('\\').then(
space(
string("->").then(
space(
expression.bind(body ->
variableParser().bind(id ->
space(retn(id))
).many().bind(ids ->
space(
retn(toLambda(ids, body))))))))));
string("->").then(
space(
expression.bind(body ->
space(
retn(toLambda(ids, body))))))))));
}

/**
Expand All @@ -114,31 +114,31 @@ private Lambda toLambda(IList<Variable> ids, Expression body) {
*/
private Parser<Character, Let> letParser(Parser<Character, Expression> expression) {
return
string("let").then(
space(
variableParser().bind(id ->
string("let").then(
space(
chr('=').then(
space(
expression.bind(def ->
variableParser().bind(id ->
space(
string("in").then(
space(
expression.bind(body ->
retn(new Let(id, def, body)))))))))))));
chr('=').then(
space(
expression.bind(def ->
space(
string("in").then(
space(
expression.bind(body ->
retn(new Let(id, def, body)))))))))))));
}

/**
* app <exp> <exp>
*/
private Parser<Character, Application> appParser(Parser<Character, Expression> expression) {
return
string("app").then(
space(
expression.bind(body ->
string("app").then(
space(
expression.bind(arg ->
retn(new Application(body, arg)))))));
expression.bind(body ->
space(
expression.bind(arg ->
retn(new Application(body, arg)))))));
}

private Parser<Character, Expression> expressionParser() {
Expand All @@ -165,12 +165,12 @@ private Parser<Character, Expression> expressionParser() {

private <T> Parser<Character, T> addParentheses(Parser<Character, T> expression) {
return
chr('(').then(
space(
expression.bind(exp ->
chr('(').then(
space(
chr(')').then(
retn(exp))))));
expression.bind(exp ->
space(
chr(')').then(
retn(exp))))));
}

private <T> Parser<Character, T> space(Parser<Character, T> parser) {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/fr/imt/parser/InferenceProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public InferenceProcessor(Inferable<T, ?> inferer) {
@Override
public String process(T value) {
return inferer.infer(value)
.fold(
inferError -> Color.red("Inference error : " + inferError),
Object::toString
);
.fold(
inferError -> Color.red("Inference error : " + inferError),
Object::toString
);
}
}
37 changes: 22 additions & 15 deletions src/main/java/fr/imt/parser/Repl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import fr.imt.logger.Color;
import fr.imt.logger.Logger;
import io.vavr.API;

import java.util.Scanner;

import static io.vavr.API.*;

public class Repl<T> {

private final Parsable<T> parser;
Expand All @@ -27,28 +30,32 @@ public void run() {
System.out.print("> ");
String line = scanner.nextLine().trim();

// TODO: refactor by creating a command handler service
if (line.equals(":q")) {
break;
} else if (line.equals(":log")) {
this.showLog = !this.showLog;
System.out.println(Color.grey("Log " + (this.showLog ? "enabled" : "disabled")));
} else if (line.equals(":h")) {
showHelp();
} else {
process(line);
}
Match(line).of(
Case($(":q"), o -> API.run(this::endProgram)),
Case($(":log"), o -> API.run(this::toggleLog)),
Case($(":h"), o -> API.run(this::showHelp)),
Case($(), o -> API.run(() -> this.process(line)))
);
}

}

private void endProgram() {
System.out.println(Color.grey("Bye!"));
System.exit(0);
}

private void toggleLog() {
this.showLog = !this.showLog;
System.out.println(Color.grey("Log " + (this.showLog ? "enabled" : "disabled")));
}

private void process(String line) {
String result = parser.parse(line)
.fold(
parseError -> Color.red("Parsing error : " + parseError),
processor::process
);
.fold(
parseError -> Color.red("Parsing error : " + parseError),
processor::process
);

if (this.showLog) System.out.print(Logger.instance);

Expand Down

0 comments on commit c7b62c9

Please sign in to comment.