diff --git a/src/main/java/edu/kit/typicalc/model/parser/LambdaParser.java b/src/main/java/edu/kit/typicalc/model/parser/LambdaParser.java index 2aab56f..dad8d52 100644 --- a/src/main/java/edu/kit/typicalc/model/parser/LambdaParser.java +++ b/src/main/java/edu/kit/typicalc/model/parser/LambdaParser.java @@ -107,6 +107,8 @@ public class LambdaParser { return new Result<>(let); } return new Result<>(let.unwrap()); + case VARIABLE: + return new Result<>(parseApplication()); case EOF: return new Result<>(null, ParseError.TOO_FEW_TOKENS); default: @@ -140,8 +142,13 @@ public class LambdaParser { if (left.isError()) { return left; } - while (ATOM_START_TOKENS.contains(token.getType())) { - Result atom = parseAtom(); + while (ATOM_START_TOKENS.contains(token.getType()) || token.getType() == TokenType.LAMBDA) { + Result atom; + if (token.getType() == TokenType.LAMBDA) { + atom = new Result<>(parseAbstraction()); + } else { + atom = parseAtom(); + } if (atom.isError()) { return atom; } diff --git a/src/main/java/edu/kit/typicalc/util/Result.java b/src/main/java/edu/kit/typicalc/util/Result.java index 639eff9..5b8e254 100644 --- a/src/main/java/edu/kit/typicalc/util/Result.java +++ b/src/main/java/edu/kit/typicalc/util/Result.java @@ -71,7 +71,7 @@ public class Result { */ public T unwrap() throws IllegalStateException { if (value == null) { - throw new IllegalStateException("tried to unwrap an error"); + throw new IllegalStateException("tried to unwrap a result, but error = " + error); } return value; } @@ -85,7 +85,7 @@ public class Result { */ public E unwrapError() { if (error == null) { - throw new IllegalStateException("tried to unwrap a value"); + throw new IllegalStateException("tried to unwrap the error, but value = " + value); } return error; } diff --git a/src/test/java/edu/kit/typicalc/model/parser/LambdaParserTest.java b/src/test/java/edu/kit/typicalc/model/parser/LambdaParserTest.java index aee613e..3897422 100644 --- a/src/test/java/edu/kit/typicalc/model/parser/LambdaParserTest.java +++ b/src/test/java/edu/kit/typicalc/model/parser/LambdaParserTest.java @@ -15,6 +15,8 @@ import nl.jqno.equalsverifier.EqualsVerifier; import org.junit.jupiter.api.Test; class LambdaParserTest { + private static final VarTerm X = new VarTerm("x"); + @Test void varTerm() { LambdaParser parser = new LambdaParser("x"); @@ -63,6 +65,11 @@ class LambdaParserTest { @Test void complicatedTerm() { LambdaParser parser = new LambdaParser("(λx.λy.x y 5)(λz.z)(true)"); + Result term = parser.parse(); + if (term.isError()) { + System.err.println(term.unwrapError()); + System.err.println(term.unwrapError().getCause()); + } assertEquals( new AppTerm( new AppTerm( @@ -86,7 +93,7 @@ class LambdaParserTest { ), new BooleanTerm(true) ), - parser.parse().unwrap() + term.unwrap() ); } @Test @@ -139,6 +146,17 @@ class LambdaParserTest { assertEquals(new Token(TokenType.RIGHT_PARENTHESIS, ")", 13), error.getCause()); } + @Test + void bugFoundByThomas() { + LambdaParser parser = new LambdaParser("(λx. x) λx. x"); + Result term = parser.parse(); + if (term.isError()) { + System.err.println(term.unwrapError()); + System.err.println(term.unwrapError().getCause()); + } + assertEquals(new AppTerm(new AbsTerm(X, X), new AbsTerm(X, X)), term.unwrap()); + } + @Test void equality() { EqualsVerifier.forClass(Token.class).usingGetClass().verify();