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
1 change: 1 addition & 0 deletions .idea/.name

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

22 changes: 22 additions & 0 deletions .idea/compiler.xml

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

3 changes: 3 additions & 0 deletions .idea/copyright/profiles_settings.xml

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

6 changes: 6 additions & 0 deletions .idea/encodings.xml

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

23 changes: 23 additions & 0 deletions .idea/misc.xml

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

2 changes: 1 addition & 1 deletion java-type-checker/.idea/misc.xml

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

2 changes: 1 addition & 1 deletion java-type-checker/.idea/type-checker.iml

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

104 changes: 103 additions & 1 deletion java-type-checker/java_type_checker/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def check_types(self):
Validates the structure of this expression, checking for any logical inconsistencies in the
child nodes and the operation this expression applies to them.
"""

raise NotImplementedError(type(self).__name__ + " must implement check_types()")


Expand All @@ -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`.
Expand All @@ -39,22 +46,76 @@ 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):
"""
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):
objType = self.receiver.static_type()
method = objType.method_named(self.method_name)
return method.return_type

def check_types(self):
primitives = [Type.int, Type.boolean, Type.double]
objType = self.receiver.static_type()
passedArgTypes = []

if objType in primitives: #cant call methods on primitives
raise JavaTypeError("Type {0} does not have methods".format(
objType.name
))

if objType.method_named(self.method_name): #if the object has the correct method
method = objType.method_named(self.method_name)
pass
else:
raise JavaTypeError("{0} has no method named {1}".format(
objType.name,
self.method_name
))

if len(self.args) == len(method.argument_types): #if you passed enough arguments for the method
pass
else:
raise JavaTypeError("Wrong number of arguments for {0}: expected {1}, got {2}".format(
objType.name + "." + method.name+ "()",
len(method.argument_types),
len(self.args)))

for e in self.args:
passedArgTypes.append(e.static_type())
for i in range(0,len(self.args)): # if you passed args of the correct type for the method
self.args[i].check_types()
if passedArgTypes[i] == method.argument_types[i]:
pass
elif method.argument_types[i] in passedArgTypes[i].direct_supertypes or passedArgTypes[i] == Type.null:
pass
else:
raise JavaTypeError("{0} expects arguments of type {1}, but got {2}".format(
objType.name + "." + method.name + "()",
names(method.argument_types),
names(passedArgTypes)))


class ConstructorCall(Expression):
"""
Expand All @@ -64,6 +125,47 @@ 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):
# can't instantiate primitives
primitives = [Type.int, Type.boolean, Type.double]
if self.instantiated_type in primitives:
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")

passedArgTypes = [] #setup a list for the types of passed arguments
expectedArgTypes = self.instantiated_type.constructor.argument_types
# make sure you passed the expected number of arguments
if len(self.args) == len(expectedArgTypes):
pass
else:
raise JavaTypeError("Wrong number of arguments for {0}: expected {1}, got {2}".format(
self.instantiated_type.name + " constructor",
len(expectedArgTypes),
len(self.args)))

# make sure you passed arguments with the expected types (or supertypes)
for e in self.args:
passedArgTypes.append(e.static_type())
for i in range(0,len(self.args)):
self.args[i].check_types()
if passedArgTypes[i] == expectedArgTypes[i]:
pass
elif expectedArgTypes[i] in passedArgTypes[i].direct_supertypes:
pass
elif passedArgTypes[i] == Type.null and not expectedArgTypes[i] in primitives:
pass
else:
raise JavaTypeError("{0} expects arguments of type {1}, but got {2}".format(
self.instantiated_type.name + " constructor",
names(expectedArgTypes),
names(passedArgTypes)))


class JavaTypeError(Exception):
""" Indicates a compile-time type error in an expression.
Expand Down
18 changes: 17 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,16 @@ 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 x in self.direct_supertypes:
for y in x.direct_supertypes:
if y.name == other.name:
return True
if x.name == other.name:
return True

return False

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

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
Expand Down
2 changes: 1 addition & 1 deletion java-type-checker/tests/test_null.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def test_catch_wrong_type_in_deep_expression(self):
"""
Equivalent Java:

GraphicsGroup group;
GraphicsGroup group;
Window window;

group.add(
Expand Down
6 changes: 6 additions & 0 deletions python-attr-lookup/.idea/compiler.xml

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

3 changes: 3 additions & 0 deletions python-attr-lookup/.idea/copyright/profiles_settings.xml

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

2 changes: 1 addition & 1 deletion python-attr-lookup/.idea/misc.xml

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

22 changes: 22 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,27 @@
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library" scope="TEST">
<library name="JUnit4">
<CLASSES>
<root url="jar://$APPLICATION_HOME_DIR$/lib/junit-4.12.jar!/" />
<root url="jar://$APPLICATION_HOME_DIR$/lib/hamcrest-core-1.3.jar!/" />
</CLASSES>
<JAVADOC />
<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>
24 changes: 16 additions & 8 deletions python-attr-lookup/src/plang/PythonObject.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package plang;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

/**
* The runtime state of an object in Python.
Expand Down Expand Up @@ -51,7 +47,10 @@ 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");
mro = new ArrayList<>();
mro.add(this);
mro.addAll(type.getMRO());
return mro;
}

/**
Expand All @@ -62,7 +61,13 @@ 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 currObj:getMRO()){
if(currObj.attrs.containsKey(attrName)){
return currObj.attrs.get(attrName);
}
}
throw new PythonAttributeException(this,attrName);
//throw new UnsupportedOperationException("not implemented yet");
}

/**
Expand All @@ -74,7 +79,10 @@ 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);

//throw new UnsupportedOperationException("not implemented yet");
}

@Override
Expand Down
Loading