From f2cd4370cf44a6397acd161717ab74cdc628c721 Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Wed, 27 Jan 2021 21:04:40 +0100 Subject: [PATCH] AbsTerm, AppTerm, mehr Parser-Tests --- .../edu/kit/typicalc/model/term/AbsTerm.java | 33 +++++++++++++++++- .../edu/kit/typicalc/model/term/AppTerm.java | 34 ++++++++++++++++++- .../model/parser/LambdaParserTest.java | 14 ++++++++ 3 files changed, 79 insertions(+), 2 deletions(-) diff --git a/src/main/java/edu/kit/typicalc/model/term/AbsTerm.java b/src/main/java/edu/kit/typicalc/model/term/AbsTerm.java index d14aa44..461b366 100644 --- a/src/main/java/edu/kit/typicalc/model/term/AbsTerm.java +++ b/src/main/java/edu/kit/typicalc/model/term/AbsTerm.java @@ -1,7 +1,38 @@ package edu.kit.typicalc.model.term; +import java.util.Objects; + public class AbsTerm extends LambdaTerm { + private final VarTerm var; + private final LambdaTerm body; + public AbsTerm(VarTerm var, LambdaTerm body) { - // TODO + this.var = var; + this.body = body; + } + + public VarTerm getVariable() { + return var; + } + + public LambdaTerm getInner() { + return body; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + AbsTerm absTerm = (AbsTerm) o; + return Objects.equals(var, absTerm.var) && Objects.equals(body, absTerm.body); + } + + @Override + public int hashCode() { + return Objects.hash(var, body); } } diff --git a/src/main/java/edu/kit/typicalc/model/term/AppTerm.java b/src/main/java/edu/kit/typicalc/model/term/AppTerm.java index 62fe32c..7415f43 100644 --- a/src/main/java/edu/kit/typicalc/model/term/AppTerm.java +++ b/src/main/java/edu/kit/typicalc/model/term/AppTerm.java @@ -1,6 +1,38 @@ package edu.kit.typicalc.model.term; +import java.util.Objects; + public class AppTerm extends LambdaTerm { - public AppTerm(LambdaTerm left, LambdaTerm atom) { + private final LambdaTerm left; + private final LambdaTerm right; + + public AppTerm(LambdaTerm left, LambdaTerm right) { + this.left = left; + this.right = right; + } + + public LambdaTerm getLeft() { + return left; + } + + public LambdaTerm getRight() { + return right; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + AppTerm appTerm = (AppTerm) o; + return Objects.equals(left, appTerm.left) && Objects.equals(right, appTerm.right); + } + + @Override + public int hashCode() { + return Objects.hash(left, right); } } 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 e33b8b3..a3f22cf 100644 --- a/src/test/java/edu/kit/typicalc/model/parser/LambdaParserTest.java +++ b/src/test/java/edu/kit/typicalc/model/parser/LambdaParserTest.java @@ -2,6 +2,8 @@ package edu.kit.typicalc.model.parser; import static org.junit.jupiter.api.Assertions.assertEquals; +import edu.kit.typicalc.model.term.AbsTerm; +import edu.kit.typicalc.model.term.AppTerm; import edu.kit.typicalc.model.term.VarTerm; import org.junit.jupiter.api.Test; @@ -11,4 +13,16 @@ class LambdaParserTest { LambdaParser parser = new LambdaParser("x"); assertEquals(parser.parse().unwrap(), new VarTerm("x")); } + @Test + void absTerm() { + LambdaParser parser = new LambdaParser("λx.x"); + assertEquals(parser.parse().unwrap(), new AbsTerm(new VarTerm("x"), new VarTerm("x"))); + } + @Test + void appTerm() { + LambdaParser parser = new LambdaParser("(λx.x)(λx.x)"); + assertEquals(parser.parse().unwrap(), + new AppTerm(new AbsTerm(new VarTerm("x"), new VarTerm("x")), + new AbsTerm(new VarTerm("x"), new VarTerm("x")))); + } }