AbsTerm, AppTerm, mehr Parser-Tests

This commit is contained in:
Arne Keller 2021-01-27 21:04:40 +01:00
parent 938cd7e698
commit f2cd4370cf
3 changed files with 79 additions and 2 deletions

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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"))));
}
}