diff --git a/src/main/java/complexityparser/types/env/TypingEnv.java b/src/main/java/complexityparser/types/env/TypingEnv.java index 93018fb5d0839e73910d4bbd8b2fdc60e2b8ec7e..87f7d77d2f8ba1c1f218ca0f3f4ab20f75cf554d 100644 --- a/src/main/java/complexityparser/types/env/TypingEnv.java +++ b/src/main/java/complexityparser/types/env/TypingEnv.java @@ -35,14 +35,14 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.Map; /** -* This class encodes typing environements, information about the operations and their (admissible) types. +* This class encodes typing environements: information about the operations (operator, method, ...) and their (admissible) types. */ public class TypingEnv { private static TypingEnv instance = null; - private HashMap<String, Operator> operators; + private HashMap<String, Operator> env; protected TypingEnv() { - operators = new HashMap<>(); + env = new HashMap<>(); } /** @@ -62,13 +62,13 @@ public class TypingEnv { */ public void clear() { ArrayList<String> keys = new ArrayList<>(); - for(Map.Entry<String, Operator> entry : operators.entrySet()) { + for(Map.Entry<String, Operator> entry : env.entrySet()) { if(entry.getKey().contains("<root>")) { keys.add(entry.getKey()); } } for(String k : keys) { - operators.remove(k); + env.remove(k); } } @@ -81,23 +81,24 @@ public class TypingEnv { } /** - * Adds an operator and a name to the hashmap operators. + * Adds an operator and a name to the typing environment. * @param k - a name * @param o - an operator */ public void add(String k, Operator o) { - operators.put(k, o); + env.put(k, o); } /** * * @param r - a name. * @param in - input tiers. - * @return an output tier matching an given operator namespace and input tiers given as parameters. A default output tier if no match is found. + * @return an output tier matching an given operator namespace and input tiers given as parameters. + * A default output tier (None) if no match is found. */ public Output findRes(String r, Tier... in) { - if(operators.containsKey(r)) { - return operators.get(r).findRes(in); + if(env.containsKey(r)) { + return env.get(r).findRes(in); } return new Output(Tier.None, Tier.None); } @@ -110,8 +111,8 @@ public class TypingEnv { * @return a compatible output tier matching a namespace, a mask, and input tiers. A default output tier if no match is found. */ public Output findCompatibleIn(String r, Mask m, Tier... in) { - if(operators.containsKey(r)) { - return operators.get(r).findCompatibleRes(m, in); + if(env.containsKey(r)) { + return env.get(r).findCompatibleRes(m, in); } return new Output(Tier.None, Tier.None); } @@ -125,12 +126,12 @@ public class TypingEnv { public Output findMethod(String r, Tier... in) { Output res = null; TOS tos = TOS.getInstance(); - while (!r.contains("<root>::Object::") && !operators.containsKey(r)) { + while (!r.contains("<root>::Object::") && !env.containsKey(r)) { r = tos.getParentNamespace(r); } //If the namespace is of the form <root>::Object:: then the method has not been found in the class hierarchy. - if(!r.contains("<root>::Object::") && operators.containsKey(r)) { - res = operators.get(r).findRes(in); + if(!r.contains("<root>::Object::") && env.containsKey(r)) { + res = env.get(r).findRes(in); res = checkOverrideMethod(r, res, in); } return res; @@ -144,11 +145,11 @@ public class TypingEnv { */ public Output findCompatibleMethod(String r, Mask m, Tier... in) { TOS tos = TOS.getInstance(); - while (!r.contains("<root>::Object::") && !operators.containsKey(r)) { + while (!r.contains("<root>::Object::") && !env.containsKey(r)) { r = tos.getParentNamespace(r); } - if(!r.contains("<root>::Object") && operators.containsKey(r)) { - Input[] res = operators.get(r).findAllCompatibles(m, in); + if(!r.contains("<root>::Object") && env.containsKey(r)) { + Input[] res = env.get(r).findAllCompatibles(m, in); for(Input i : res) { Output tmp = checkOverrideMethod(r, i.getOut(), i.getIn()); if( ! tmp.equals(new Output(Tier.None, Tier.None))) { @@ -184,8 +185,8 @@ public class TypingEnv { */ private Output checkOverrideMethod_aux(String r, Output out, Tier... in) { Output res = new Output(out.getOut(), out.getEnv()); - if(operators.containsKey(r)) { - Output tmp = operators.get(r).findRes(in); + if(env.containsKey(r)) { + Output tmp = env.get(r).findRes(in); /* the method return type has to be typed by considering possible overridings: --if the return type is a primitive type: the output tier is the minimum of input tiers; @@ -193,7 +194,7 @@ public class TypingEnv { The tier of the method body is the maximum of the input tiers. */ res.setEnv(Tier.max(tmp.getEnv(), out.getEnv())); - if(operators.get(r).isPrimitiveReturnType()) { + if(env.get(r).isPrimitiveReturnType()) { res.setOut(Tier.min(tmp.getOut(), out.getOut())); } else { @@ -208,7 +209,7 @@ public class TypingEnv { for (int i = 0; i < children.length; i++) { Output tmp = checkOverrideMethod_aux(children[i], res, in); res.setEnv(Tier.max(tmp.getEnv(), out.getEnv())); - if(operators.get(r).isPrimitiveReturnType()) { + if(env.get(r).isPrimitiveReturnType()) { res.setOut(Tier.min(tmp.getOut(), out.getOut())); } else { @@ -222,9 +223,9 @@ public class TypingEnv { } /** - * loads the operators from a file using the ANTLR grammar and the OperatorTypingEnvBuilder class. + * loads the operators FOTypes from a file using the ANTLR grammar and the OperatorTypingEnvBuilder class. * @param fileName - a file name. - * @return the singleton instance of Operators. + * @return the singleton instance of TypingEnv. */ private static TypingEnv loadFromFile(String fileName) { TypingEnv res = null; @@ -254,13 +255,13 @@ public class TypingEnv { * @return the operator associated with the given namespace */ public Operator get(String namespace) { - return operators.get(namespace); + return env.get(namespace); } @Override public String toString() { StringBuilder str = new StringBuilder(); - for(Map.Entry<String, Operator> e : operators.entrySet()) { + for(Map.Entry<String, Operator> e : env.entrySet()) { str.append("\""); str.append(e.getKey()); str.append("\" {\n");