Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 73 additions & 1 deletion java-type-checker/java_type_checker/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,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`.
Expand All @@ -39,22 +45,52 @@ 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

def check_types(self):
pass


class MethodCall(Expression):
"""
A Java method invocation, i.e. `foo.bar(0, 1, 2)`.
"""
def __init__(self, receiver, method_name, *args):
self.receiver = receiver
self.receiver = receiver #: The object whose method we are calling (Expression)
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.declared_type.method_named(self.method_name).return_type

def check_types(self):

for arg in self.args:
arg.check_types()

receiver_type = self.receiver.static_type()
if not receiver_type.is_subtype_of(Type.object):
raise JavaTypeError("Type {0} does not have methods".format(receiver_type.name))

method = receiver_type.method_named(self.method_name) # throws NoMethodError
expected_types = method.argument_types
actual_types = [arg.static_type() for arg in self.args]
call_name = "{0}.{1}()".format(self.receiver.static_type().name, self.method_name)

check_arguments(expected_types, actual_types, call_name)


class ConstructorCall(Expression):
"""
Expand All @@ -64,13 +100,49 @@ 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):

for arg in self.args:
arg.check_types()

if not self.instantiated_type.is_instantiable:
raise JavaTypeError("Type {0} is not instantiable".format(self.instantiated_type.name))

expected_types = self.instantiated_type.constructor.argument_types
actual_types = [arg.static_type() for arg in self.args]
call_name = "{0} constructor".format(self.instantiated_type.name)

check_arguments(expected_types, actual_types, call_name)


class JavaTypeError(Exception):
""" Indicates a compile-time type error in an expression.
"""
pass


def check_arguments(expected_types, actual_types, call_name):
""""
Helper to check arguments. Raises JavaTypeError.
"""
if len(expected_types) != len(actual_types):
raise JavaTypeError(
"Wrong number of arguments for {0}: expected {1}, got {2}".format(
call_name,
len(expected_types),
len(actual_types)))

for expected_type, actual_type in zip(expected_types, actual_types):
if not expected_type.is_supertype_of(actual_type):
raise JavaTypeError(
"{0} expects arguments of type {1}, but got {2}".format(
call_name,
names(expected_types),
names(actual_types)))

def names(named_things):
""" Helper for formatting pretty error messages
"""
Expand Down
19 changes: 18 additions & 1 deletion java-type-checker/java_type_checker/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ 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 is self:
return True
for direct_supertype in self.direct_supertypes:
if other is direct_supertype or direct_supertype.is_subtype_of(other):
return True
return False

def is_supertype_of(self, other):
""" Convenience counterpart to is_subtype_of().
Expand Down Expand Up @@ -72,6 +77,18 @@ class NullType(Type):
def __init__(self):
super().__init__("null")

def method_named(self, name):
"""
Raises a NoSuchMethod error since null has no method.
"""
raise NoSuchMethod("Cannot invoke method {1}() on null".format(self.name, name))

def is_subtype_of(self, other):
return type(other) == ClassOrInterface

def is_supertype_of(self, other):
return other.is_subtype_of(self)


class NoSuchMethod(Exception):
pass
Expand Down
10 changes: 10 additions & 0 deletions python-attr-lookup/python-attr-lookup.iml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,15 @@
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library" scope="TEST">
<library name="JUnit5">
<CLASSES>
<root url="jar://$APPLICATION_HOME_DIR$/plugins/junit/lib/junit-jupiter-api-5.0.0-M5.jar!/" />
<root url="jar://$APPLICATION_HOME_DIR$/plugins/junit/lib/opentest4j-1.0.0-M2.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
16 changes: 13 additions & 3 deletions python-attr-lookup/src/plang/PythonObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ public List<PythonObject> getMRO() {
* result (i.e. it remembers the list buildMRO() returned and keeps returning it).
*/
protected List<PythonObject> buildMRO() {
throw new UnsupportedOperationException("not implemented yet");
if (mro == null) {
mro = new ArrayList<>();
mro.add(this);
mro.addAll(type.buildMRO());
}
return mro;
}

/**
Expand All @@ -62,7 +67,12 @@ protected List<PythonObject> 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");
for (PythonObject pythonObject : getMRO()) {
if (pythonObject.attrs.containsKey(attrName)) {
return pythonObject.attrs.get(attrName);
}
}
throw new PythonAttributeException(this, attrName);
}

/**
Expand All @@ -74,7 +84,7 @@ 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");
attrs.put(attrName, value);
}

@Override
Expand Down
9 changes: 7 additions & 2 deletions python-attr-lookup/src/plang/PythonType.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,20 @@ public PythonObject getBase() {

@Override
protected List<PythonObject> buildMRO() {
throw new UnsupportedOperationException("not implemented yet");
List<PythonObject> mroList = new ArrayList<>();
mroList.add(this);
if (base != null) {
mroList.addAll(base.buildMRO());
}
return mroList;
}

/**
* Creates and returns a new instance of this class, i.e. a PythonObject whose type is
* this PythonType.
*/
public PythonObject instantiate() {
throw new UnsupportedOperationException("not implemented yet");
return new PythonObject(this);
}

@Override
Expand Down