TypeAssumptionParser und ModelImpl angepasst

This commit is contained in:
uogau 2021-02-03 13:40:56 +01:00
parent 874b648caf
commit 9e77c422f2
3 changed files with 29 additions and 27 deletions

View File

@ -8,7 +8,6 @@ import edu.kit.typicalc.model.term.VarTerm;
import edu.kit.typicalc.model.type.TypeAbstraction;
import edu.kit.typicalc.util.Result;
import java.util.HashMap;
import java.util.Map;
/**
@ -36,19 +35,12 @@ public class ModelImpl implements Model {
}
//Parse Type Assumptions
TypeAssumptionParser assumptionParser = new TypeAssumptionParser();
HashMap<VarTerm, TypeAbstraction> assumptionMap = new HashMap<>();
for (Map.Entry<String, String> entry : typeAssumptions.entrySet()) {
Result<Map<VarTerm, TypeAbstraction>, ParseError> newAssumption =
assumptionParser.parse(entry.getKey(), entry.getValue());
if (newAssumption.isError()) {
return new Result<>(null, newAssumption.unwrapError());
}
assumptionMap.putAll(newAssumption.unwrap());
Result<Map<VarTerm, TypeAbstraction>, ParseError> assumptionMap = assumptionParser.parse(typeAssumptions);
if (assumptionMap.isError()) {
return new Result<>(null, assumptionMap.unwrapError());
}
//Create and return TypeInferer
TypeInferer typeInferer = new TypeInferer(result.unwrap(), assumptionMap);
TypeInferer typeInferer = new TypeInferer(result.unwrap(), assumptionMap.unwrap());
return new Result<>(typeInferer, null);
}
}

View File

@ -15,15 +15,16 @@ import java.util.HashMap;
import java.util.Map;
public class TypeAssumptionParser {
// TODO: why is a Map returned?
public Result<Map<VarTerm, TypeAbstraction>, ParseError> parse(String varName, String type) {
public Result<Map<VarTerm, TypeAbstraction>, ParseError> parse(Map<String, String> oldAssumptions) {
Map<VarTerm, TypeAbstraction> typeAssumptions = new HashMap<>();
VarTerm var = new VarTerm(varName);
Result<TypeAbstraction, ParseError> typeAbs = parseType(type);
if (typeAbs.isError()) {
return new Result<>(typeAbs);
for (Map.Entry<String, String> entry : oldAssumptions.entrySet()) {
VarTerm var = new VarTerm(entry.getKey());
Result<TypeAbstraction, ParseError> typeAbs = parseType(entry.getValue());
if (typeAbs.isError()) {
return new Result<>(typeAbs);
}
typeAssumptions.put(var, typeAbs.unwrap());
}
typeAssumptions.put(var, typeAbs.unwrap());
return new Result<>(typeAssumptions);
}

View File

@ -9,6 +9,7 @@ import edu.kit.typicalc.util.Result;
import org.junit.jupiter.api.Test;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import static edu.kit.typicalc.model.type.NamedType.BOOLEAN;
@ -16,11 +17,13 @@ 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;
public class TypeAssumptionParserTest {
class TypeAssumptionParserTest {
@Test
void simpleType() {
TypeAssumptionParser parser = new TypeAssumptionParser();
Result<Map<VarTerm, TypeAbstraction>, ParseError> type = parser.parse("a", "int");
HashMap<String, String> assumptions = new HashMap<>();
assumptions.put("a", "int");
Result<Map<VarTerm, TypeAbstraction>, ParseError> type = parser.parse(assumptions);
assertTrue(type.isOk());
Map<VarTerm, TypeAbstraction> types = type.unwrap();
assertEquals(1, types.size());
@ -32,7 +35,9 @@ public class TypeAssumptionParserTest {
@Test
void functionType() {
TypeAssumptionParser parser = new TypeAssumptionParser();
Result<Map<VarTerm, TypeAbstraction>, ParseError> type = parser.parse("id", "int -> int");
HashMap<String, String> assumptions = new HashMap<>();
assumptions.put("id", "int -> int");
Result<Map<VarTerm, TypeAbstraction>, ParseError> type = parser.parse(assumptions);
assertTrue(type.isOk());
Map<VarTerm, TypeAbstraction> types = type.unwrap();
assertEquals(1, types.size());
@ -40,7 +45,9 @@ public class TypeAssumptionParserTest {
assertEquals(new VarTerm("id"), assumption.getKey());
assertEquals(new TypeAbstraction(new FunctionType(INT, INT), Collections.emptyList()), assumption.getValue());
type = parser.parse("f", "int -> int -> int");
HashMap<String, String> assumptions2 = new HashMap<>();
assumptions2.put("f", "int -> int -> int");
type = parser.parse(assumptions2);
if (type.isError()) {
System.err.println(type.unwrapError());
System.err.println(type.unwrapError().getCause());
@ -61,8 +68,9 @@ public class TypeAssumptionParserTest {
@Test
void complicatedTypes() {
TypeAssumptionParser parser = new TypeAssumptionParser();
Result<Map<VarTerm, TypeAbstraction>, ParseError> type = parser.parse(
"id", "(int -> int) -> (boolean -> boolean)");
HashMap<String, String> assumptions = new HashMap<>();
assumptions.put("id", "(int -> int) -> (boolean -> boolean)");
Result<Map<VarTerm, TypeAbstraction>, ParseError> type = parser.parse(assumptions);
if (type.isError()) {
System.err.println(type.unwrapError());
System.err.println(type.unwrapError().getCause());
@ -79,8 +87,9 @@ public class TypeAssumptionParserTest {
),
Collections.emptyList()), assumption.getValue());
parser = new TypeAssumptionParser();
type = parser.parse(
"id", "((int -> int) -> (boolean -> boolean)) -> ((int2 -> boolean2) -> (boolean2 -> int2))");
HashMap<String, String> assumptions2 = new HashMap<>();
assumptions2.put("id", "((int -> int) -> (boolean -> boolean)) -> ((int2 -> boolean2) -> (boolean2 -> int2))");
type = parser.parse(assumptions2);
if (type.isError()) {
System.err.println(type.unwrapError());
System.err.println(type.unwrapError().getCause());