user input types of form t[0-9]+ parse to TypeVariable, everything else to NamedType

This commit is contained in:
Johanna Stuber 2021-02-04 20:54:00 +01:00
parent 2ccf09da5f
commit 742f934788
2 changed files with 84 additions and 10 deletions

View File

@ -2,18 +2,21 @@ package edu.kit.typicalc.model.parser;
import edu.kit.typicalc.model.parser.Token.TokenType;
import edu.kit.typicalc.model.term.VarTerm;
import edu.kit.typicalc.model.type.FunctionType;
import edu.kit.typicalc.model.type.NamedType;
import edu.kit.typicalc.model.type.Type;
import edu.kit.typicalc.model.type.TypeAbstraction;
import edu.kit.typicalc.model.type.*;
import edu.kit.typicalc.util.Result;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TypeAssumptionParser {
private static final String TYPE_VARIABLE_PATTERN = "t(\\d+)";
private static final int GRPUP_CONTAINING_INDEX = 1;
public Result<Map<VarTerm, TypeAbstraction>, ParseError> parse(Map<String, String> oldAssumptions) {
Map<VarTerm, TypeAbstraction> typeAssumptions = new HashMap<>();
for (Map.Entry<String, String> entry : oldAssumptions.entrySet()) {
@ -59,7 +62,14 @@ public class TypeAssumptionParser {
}
break;
case VARIABLE:
type = new NamedType(t.getText());
Pattern typeVariablePattern = Pattern.compile(TYPE_VARIABLE_PATTERN);
Matcher typeVariableMatcher = typeVariablePattern.matcher(t.getText());
if (typeVariableMatcher.matches()) {
int typeVariableIndex = Integer.parseInt(typeVariableMatcher.group(GRPUP_CONTAINING_INDEX));
type = new TypeVariable(TypeVariableKind.USER_INPUT, typeVariableIndex);
} else {
type = new NamedType(t.getText());
}
break;
default:
return new Result<>(null, ParseError.UNEXPECTED_TOKEN.withToken(t));

View File

@ -1,10 +1,7 @@
package edu.kit.typicalc.model.parser;
import edu.kit.typicalc.model.term.VarTerm;
import edu.kit.typicalc.model.type.FunctionType;
import edu.kit.typicalc.model.type.NamedType;
import edu.kit.typicalc.model.type.Type;
import edu.kit.typicalc.model.type.TypeAbstraction;
import edu.kit.typicalc.model.type.*;
import edu.kit.typicalc.util.Result;
import org.junit.jupiter.api.Test;
@ -16,7 +13,7 @@ import static edu.kit.typicalc.model.type.NamedType.INT;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
class TypeAssumptionParserTest {
class TypeAssumptionParserTest {
@Test
void simpleType() {
TypeAssumptionParser parser = new TypeAssumptionParser();
@ -31,6 +28,58 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
assertEquals(new TypeAbstraction(INT), assumption.getValue());
}
@Test
void typeVariablesOneDigitIndex() {
TypeAssumptionParser parser = new TypeAssumptionParser();
HashMap<String, String> assumptions = new HashMap<>();
assumptions.put("x", "t1");
Result<Map<VarTerm, TypeAbstraction>, ParseError> type = parser.parse(assumptions);
assertTrue(type.isOk());
Map<VarTerm, TypeAbstraction> types = type.unwrap();
assertEquals(1, types.size());
Map.Entry<VarTerm, TypeAbstraction> assumption = types.entrySet().stream().findFirst().get();
assertEquals(new VarTerm("x"), assumption.getKey());
assertEquals(new TypeAbstraction(new TypeVariable(TypeVariableKind.USER_INPUT, 1)), assumption.getValue());
HashMap<String, String> assumptions2 = new HashMap<>();
assumptions2.put("x", "t001");
Result<Map<VarTerm, TypeAbstraction>, ParseError> type2 = parser.parse(assumptions2);
assertTrue(type.isOk());
Map<VarTerm, TypeAbstraction> types2 = type2.unwrap();
assertEquals(1, types2.size());
Map.Entry<VarTerm, TypeAbstraction> assumption2 = types2.entrySet().stream().findFirst().get();
assertEquals(new VarTerm("x"), assumption2.getKey());
assertEquals(new TypeAbstraction(new TypeVariable(TypeVariableKind.USER_INPUT, 1)), assumption2.getValue());
}
@Test
void typeVariablesMultipleDigitIndex() {
TypeAssumptionParser parser = new TypeAssumptionParser();
HashMap<String, String> assumptions = new HashMap<>();
assumptions.put("x", "t123456");
Result<Map<VarTerm, TypeAbstraction>, ParseError> type = parser.parse(assumptions);
assertTrue(type.isOk());
Map<VarTerm, TypeAbstraction> types = type.unwrap();
assertEquals(1, types.size());
Map.Entry<VarTerm, TypeAbstraction> assumption = types.entrySet().stream().findFirst().get();
assertEquals(new VarTerm("x"), assumption.getKey());
assertEquals(new TypeAbstraction(new TypeVariable(TypeVariableKind.USER_INPUT, 123456)), assumption.getValue());
}
@Test
void namedTypeStartingWithT() {
TypeAssumptionParser parser = new TypeAssumptionParser();
HashMap<String, String> assumptions = new HashMap<>();
assumptions.put("x", "tau1");
Result<Map<VarTerm, TypeAbstraction>, ParseError> type = parser.parse(assumptions);
assertTrue(type.isOk());
Map<VarTerm, TypeAbstraction> types = type.unwrap();
assertEquals(1, types.size());
Map.Entry<VarTerm, TypeAbstraction> assumption = types.entrySet().stream().findFirst().get();
assertEquals(new VarTerm("x"), assumption.getKey());
assertEquals(new TypeAbstraction(new NamedType("tau1")), assumption.getValue());
}
@Test
void functionType() {
TypeAssumptionParser parser = new TypeAssumptionParser();
@ -63,6 +112,21 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
)), assumption.getValue());
}
@Test
void functionTypeWithVariables() {
TypeAssumptionParser parser = new TypeAssumptionParser();
HashMap<String, String> assumptions = new HashMap<>();
assumptions.put("fun", "t0 -> t0");
Result<Map<VarTerm, TypeAbstraction>, ParseError> type = parser.parse(assumptions);
assertTrue(type.isOk());
Map<VarTerm, TypeAbstraction> types = type.unwrap();
assertEquals(1, types.size());
Map.Entry<VarTerm, TypeAbstraction> assumption = types.entrySet().stream().findFirst().get();
assertEquals(new VarTerm("fun"), assumption.getKey());
TypeVariable expectedVar = new TypeVariable(TypeVariableKind.USER_INPUT, 0);
assertEquals(new TypeAbstraction(new FunctionType(expectedVar, expectedVar)), assumption.getValue());
}
@Test
void complicatedTypes() {
TypeAssumptionParser parser = new TypeAssumptionParser();