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
15 changes: 15 additions & 0 deletions java-type-checker/.idea/compiler.xml

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

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

12 changes: 0 additions & 12 deletions java-type-checker/.idea/misc.xml

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

79 changes: 78 additions & 1 deletion java-type-checker/java_type_checker/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@ def check_types(self):
"""
raise NotImplementedError(type(self).__name__ + " must implement check_types()")


class Variable(Expression):
""" An expression that reads the value of a variable, e.g. `x` in the expression `x + 5`.
"""
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,11 +43,18 @@ 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):
"""
Expand All @@ -55,6 +66,41 @@ 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()

objectTypeName = self.receiver.static_type().name

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

# test_flags_too_few/many_arguments
expected_arg_types = self.receiver.static_type().method_named(self.method_name).argument_types

if len(expected_arg_types) == len(self.args):
pass
else:
raise TypeError(
"Wrong number of arguments for {0}: expected {1}, got {2}".format(
objectTypeName + "." + self.method_name + "()",
len(expected_arg_types),
len(self.args)))

#
for i in range(len(self.args)):
if not self.args[i].static_type().is_subtype_of(expected_arg_types[i]):
raise JavaTypeError("{0}.{1}() expects arguments of type {2}, but got {3}".format(
objectTypeName,
self.method_name,
names(expected_arg_types),
names([arg.static_type() for arg in self.args])))

class ConstructorCall(Expression):
"""
Expand All @@ -64,6 +110,37 @@ 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 self.instantiated_type.is_subtype_of(Type.object):
pass
else:
raise JavaTypeError(
"Type {0} is not instantiable".format(self.instantiated_type.name)
)

if self.instantiated_type == Type.null:
raise JavaTypeError("Type null is not instantiable")

expected_arg_types = self.instantiated_type.constructor.argument_types
if len(expected_arg_types) == len(self.args):
pass
else:
raise JavaTypeError(
"Wrong number of arguments for {0} constructor: expected {1}, got {2}".format(
self.instantiated_type.name,
len(expected_arg_types),
len(self.args))
)

for i in range(len(self.args)):
if not self.args[i].static_type().is_subtype_of(expected_arg_types[i]):
raise JavaTypeError("{0} constructor expects arguments of type {1}, but got {2}".format(
self.instantiated_type.name,
names(expected_arg_types),
names([arg.static_type() for arg in self.args])))

class JavaTypeError(Exception):
""" Indicates a compile-time type error in an expression.
Expand Down
10 changes: 9 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 self.name == other.name:
return True
for supertype in self.direct_supertypes:
if 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,9 @@ class NullType(Type):
def __init__(self):
super().__init__("null")

def method_named(self, name):
raise NoSuchMethod("Cannot invoke method {0}() on null".format(name))


class NoSuchMethod(Exception):
pass
Expand Down
12 changes: 12 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,17 @@
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library" scope="TEST">
<library name="JUnit5.0">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.0.0/junit-jupiter-api-5.0.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.0.0/apiguardian-api-1.0.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.0.0/opentest4j-1.0.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.0.0/junit-platform-commons-1.0.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
15 changes: 11 additions & 4 deletions python-attr-lookup/src/plang/PythonObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ public List<PythonObject> getMRO() {
}

/**
* Constructs the MRO. Called only once, the first time we need the MRO; this class memoizes the
* Constructs the MRO. Called only once, the first time we need the MRO; this class memorizes the
* result (i.e. it remembers the list buildMRO() returned and keeps returning it).
*/
protected List<PythonObject> buildMRO() {
throw new UnsupportedOperationException("not implemented yet");
List<PythonObject> newMRO = type.buildMRO();
newMRO.add(0,this);
return newMRO;
}

/**
Expand All @@ -62,7 +64,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 pyObj : getMRO()){
if(pyObj.attrs.containsKey(attrName)){
return pyObj.attrs.get(attrName);
}
}
throw new PythonAttributeException(this,attrName);
}

/**
Expand All @@ -74,7 +81,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
12 changes: 9 additions & 3 deletions python-attr-lookup/src/plang/PythonType.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,21 @@ public PythonObject getBase() {

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

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

@Override
Expand Down