From f02797230448683dc1fb9c9288f6e840c403e642 Mon Sep 17 00:00:00 2001 From: Cody Molho Date: Fri, 9 Feb 2018 02:11:35 -0600 Subject: [PATCH 1/4] 11/12 java tests pass --- python-attr-lookup/.idea/misc.xml | 2 +- python-attr-lookup/python-attr-lookup.iml | 12 ++++++++++++ .../src/plang/PythonObject.java | 19 ++++++++++++++++--- python-attr-lookup/src/plang/PythonType.java | 11 +++++++++-- 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/python-attr-lookup/.idea/misc.xml b/python-attr-lookup/.idea/misc.xml index 0548357..8e8843c 100644 --- a/python-attr-lookup/.idea/misc.xml +++ b/python-attr-lookup/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/python-attr-lookup/python-attr-lookup.iml b/python-attr-lookup/python-attr-lookup.iml index 01e8e1b..a2b4cff 100644 --- a/python-attr-lookup/python-attr-lookup.iml +++ b/python-attr-lookup/python-attr-lookup.iml @@ -20,5 +20,17 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/python-attr-lookup/src/plang/PythonObject.java b/python-attr-lookup/src/plang/PythonObject.java index a8e311f..3069c7a 100644 --- a/python-attr-lookup/src/plang/PythonObject.java +++ b/python-attr-lookup/src/plang/PythonObject.java @@ -51,7 +51,13 @@ public List getMRO() { * result (i.e. it remembers the list buildMRO() returned and keeps returning it). */ protected List buildMRO() { - throw new UnsupportedOperationException("not implemented yet"); +// throw new UnsupportedOperationException("not implemented yet"); + List mroBuild = new ArrayList<>(); + mroBuild.add(this); + if (type != null) { + mroBuild.addAll(type.getMRO()); + } + return mroBuild; } /** @@ -62,7 +68,13 @@ protected List buildMRO() { * @throws PythonAttributeException When there is no attribute on this object with that name. */ public final PythonObject get(String attrName) throws PythonAttributeException { - throw new UnsupportedOperationException("not implemented yet"); + if (attrs.containsKey(attrName)) { + return attrs.get(attrName); + } else if (getMRO().size() >= 2) { + return getMRO().get(1).get(attrName); + } else { + throw new PythonAttributeException(this, attrName); + } } /** @@ -74,7 +86,8 @@ public final PythonObject get(String attrName) throws PythonAttributeException { * @param value Its new value */ public final void set(String attrName, PythonObject value) { - throw new UnsupportedOperationException("not implemented yet"); +// throw new UnsupportedOperationException("not implemented yet"); + attrs.put(attrName, value); } @Override diff --git a/python-attr-lookup/src/plang/PythonType.java b/python-attr-lookup/src/plang/PythonType.java index 4e2be4a..b689550 100644 --- a/python-attr-lookup/src/plang/PythonType.java +++ b/python-attr-lookup/src/plang/PythonType.java @@ -41,7 +41,14 @@ public PythonObject getBase() { @Override protected List buildMRO() { - throw new UnsupportedOperationException("not implemented yet"); + List mroBuild = new ArrayList<>(); + mroBuild.add(this); + if (base != null) { + mroBuild.addAll(base.getMRO()); + } +// mroBuild.addAll(base.getMRO()); + return mroBuild; +// throw new UnsupportedOperationException("not implemented yet"); } /** @@ -49,7 +56,7 @@ protected List buildMRO() { * this PythonType. */ public PythonObject instantiate() { - throw new UnsupportedOperationException("not implemented yet"); + return new PythonObject(this); } @Override From 099d221fb6e6407dd4958b4559da971ce83e309b Mon Sep 17 00:00:00 2001 From: Cody Molho Date: Fri, 9 Feb 2018 17:05:58 -0600 Subject: [PATCH 2/4] finished python-attr-lookup --- .../src/plang/PythonObject.java | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/python-attr-lookup/src/plang/PythonObject.java b/python-attr-lookup/src/plang/PythonObject.java index 3069c7a..f18ec92 100644 --- a/python-attr-lookup/src/plang/PythonObject.java +++ b/python-attr-lookup/src/plang/PythonObject.java @@ -68,13 +68,22 @@ protected List buildMRO() { * @throws PythonAttributeException When there is no attribute on this object with that name. */ public final PythonObject get(String attrName) throws PythonAttributeException { - if (attrs.containsKey(attrName)) { - return attrs.get(attrName); - } else if (getMRO().size() >= 2) { - return getMRO().get(1).get(attrName); - } else { - throw new PythonAttributeException(this, attrName); + +// for(Utensil utensil : getKitchenUtensils()) { +// } +// +// if (attrs.containsKey(attrName)) { +// return attrs.get(attrName); +// } else if (getMRO().size() >= 2) { +// return getMRO().get(1).get(attrName); +// } + + for (PythonObject pythonObject : getMRO()) { + if (pythonObject.attrs.containsKey(attrName)) { + return pythonObject.attrs.get(attrName); + } } + throw new PythonAttributeException(this, attrName); } /** From dd604f81dbb5988c82c20cf1cc202b0e4fc5584a Mon Sep 17 00:00:00 2001 From: Cody Molho Date: Fri, 9 Feb 2018 21:00:16 -0600 Subject: [PATCH 3/4] finished up to null support --- .../java_type_checker/expressions.py | 69 +++++++++++++++++++ java-type-checker/java_type_checker/types.py | 14 +++- 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/java-type-checker/java_type_checker/expressions.py b/java-type-checker/java_type_checker/expressions.py index 27ed57e..b59c1cd 100644 --- a/java-type-checker/java_type_checker/expressions.py +++ b/java-type-checker/java_type_checker/expressions.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- from .types import Type +import re class Expression(object): @@ -31,6 +32,12 @@ def __init__(self, name, declared_type): self.name = name #: The name of the variable self.declared_type = declared_type #: The declared type of the variable (Type) + def static_type(self): + return self.declared_type + + def check_types(self): + pass + class Literal(Expression): """ A literal value entered in the code, e.g. `5` in the expression `x + 5`. @@ -39,11 +46,20 @@ def __init__(self, value, type): self.value = value #: The literal value, as a string self.type = type #: The type of the literal (Type) + def static_type(self): + return self.type + + def check_types(self): + pass + class NullLiteral(Literal): def __init__(self): super().__init__("null", Type.null) + def static_type(self): + return Type.null + class MethodCall(Expression): """ @@ -55,6 +71,37 @@ def __init__(self, receiver, method_name, *args): self.method_name = method_name #: The name of the method to call (String) self.args = args #: The method arguments (list of Expressions) + def static_type(self): + return self.receiver.static_type().method_named(self.method_name).return_type + + def check_types(self): + for arg in self.args: + arg.check_types() + + # Check not literal type + if not self.receiver.static_type().is_subtype_of(Type.object): + raise JavaTypeError("Type {0} does not have methods".format( + self.receiver.static_type().name)) + + expected_types = self.receiver.static_type().method_named(self.method_name).argument_types + + # Check number of args + if len(self.args) != len(expected_types): + raise JavaTypeError("Wrong number of arguments for {0}.{1}(): expected {2}, got {3}".format( + self.receiver.static_type().name, + self.method_name, + len(expected_types), + len(self.args))) + + # Check type of args + for i in range(len(self.args)): + if not self.args[i].static_type().is_subtype_of(expected_types[i]): + raise JavaTypeError("{0}.{1}() expects arguments of type {2}, but got {3}".format( + self.receiver.static_type().name, + self.method_name, + names(expected_types), + names([a.static_type() for a in self.args]))) + class ConstructorCall(Expression): """ @@ -64,6 +111,28 @@ def __init__(self, instantiated_type, *args): self.instantiated_type = instantiated_type #: The type to instantiate (Type) self.args = args #: Constructor arguments (list of Expressions) + def static_type(self): + return self.instantiated_type + + def check_types(self): + if not self.instantiated_type.is_subtype_of(Type.object): + error = "Type {0} is not instantiable".format( + self.instantiated_type.name) + raise JavaTypeError(error) + expected_types = self.instantiated_type.constructor.argument_types + if len(self.args) != len(expected_types): + error = "Wrong number of arguments for {0} constructor: expected {1}, got {2}".format( + self.instantiated_type.name, + len(expected_types), + len(self.args)) + raise JavaTypeError(error) + for i in range(len(self.args)): + if not self.args[i].static_type().is_subtype_of(expected_types[i]): + error = "{0} constructor expects arguments of type {1}, but got {2}".format( + self.instantiated_type.name, + names(expected_types), + names([a.static_type() for a in self.args])) + raise JavaTypeError(error) class JavaTypeError(Exception): """ Indicates a compile-time type error in an expression. diff --git a/java-type-checker/java_type_checker/types.py b/java-type-checker/java_type_checker/types.py index 465f7f4..5292f6d 100644 --- a/java-type-checker/java_type_checker/types.py +++ b/java-type-checker/java_type_checker/types.py @@ -12,7 +12,13 @@ def __init__(self, name, direct_supertypes=[]): def is_subtype_of(self, other): """ True if this type can be used where the other type is expected. """ - return True # TODO: implement + if other == self or other in self.direct_supertypes: + return True + else: + for st in self.direct_supertypes: + if st.is_subtype_of(other): + return True + return False def is_supertype_of(self, other): """ Convenience counterpart to is_subtype_of(). @@ -72,6 +78,12 @@ class NullType(Type): def __init__(self): super().__init__("null") + def is_supertype_of(self, other): + return False + + def is_subtype_of(self, other): + return True + class NoSuchMethod(Exception): pass From 5b8424c1a282d0c7c93a70f24d434ba5141de8ea Mon Sep 17 00:00:00 2001 From: Cody Molho Date: Fri, 9 Feb 2018 21:34:56 -0600 Subject: [PATCH 4/4] All tests pass except two from 1.4 --- .../java_type_checker/expressions.py | 28 ++++++++++--------- java-type-checker/java_type_checker/types.py | 3 ++ 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/java-type-checker/java_type_checker/expressions.py b/java-type-checker/java_type_checker/expressions.py index b59c1cd..caa3ba5 100644 --- a/java-type-checker/java_type_checker/expressions.py +++ b/java-type-checker/java_type_checker/expressions.py @@ -119,20 +119,22 @@ def check_types(self): error = "Type {0} is not instantiable".format( self.instantiated_type.name) raise JavaTypeError(error) - expected_types = self.instantiated_type.constructor.argument_types - if len(self.args) != len(expected_types): - error = "Wrong number of arguments for {0} constructor: expected {1}, got {2}".format( - self.instantiated_type.name, - len(expected_types), - len(self.args)) - raise JavaTypeError(error) - for i in range(len(self.args)): - if not self.args[i].static_type().is_subtype_of(expected_types[i]): - error = "{0} constructor expects arguments of type {1}, but got {2}".format( + try: + expected_types = self.instantiated_type.constructor.argument_types + if len(self.args) != len(expected_types): + raise JavaTypeError("Wrong number of arguments for {0} constructor: expected {1}, got {2}".format( self.instantiated_type.name, - names(expected_types), - names([a.static_type() for a in self.args])) - raise JavaTypeError(error) + len(expected_types), + len(self.args))) + for i in range(len(self.args)): + if not self.args[i].static_type().is_subtype_of(expected_types[i]): + raise JavaTypeError("{0} constructor expects arguments of type {1}, but got {2}".format( + self.instantiated_type.name, + names(expected_types), + names([a.static_type() for a in self.args]))) + except AttributeError: + raise JavaTypeError("Type {0} is not instantiable".format( + self.instantiated_type.name)) class JavaTypeError(Exception): """ Indicates a compile-time type error in an expression. diff --git a/java-type-checker/java_type_checker/types.py b/java-type-checker/java_type_checker/types.py index 5292f6d..0a2fe17 100644 --- a/java-type-checker/java_type_checker/types.py +++ b/java-type-checker/java_type_checker/types.py @@ -84,6 +84,9 @@ def is_supertype_of(self, other): def is_subtype_of(self, other): return True + def method_named(self, name): + raise NoSuchMethod("Cannot invoke method {0}() on null".format(name)) + class NoSuchMethod(Exception): pass