mirror of
https://gitlab.kit.edu/uskyk/typicalc.git
synced 2024-11-10 03:10:44 +00:00
user input types of form t[0-9]+ parse to TypeVariable, everything else to NamedType
This commit is contained in:
parent
2ccf09da5f
commit
742f934788
@ -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));
|
||||
|
@ -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();
|
||||
|
Loading…
Reference in New Issue
Block a user