From ec3fe1a632e237ae0e522e2d908a99e86353b01a Mon Sep 17 00:00:00 2001 From: Neil Brown Date: Thu, 6 Feb 2025 19:07:00 +0000 Subject: [PATCH 1/2] Fix problems with code completion and lists, and added some tests (one of which previously failed). There were two issues: - LIST_TYPE had some list methods which as far as I can tell don't exist in Python: first, head, last, tail. I might be wrong but Python 3 doesn't seem to have them. - Autocompletion on lists was blank rather than showing available methods. I think this was because it was not wrapping ListType in an Instance (the default Instance apply doesn't wrap lists). --- .../utilities/types/BuiltinTypes.scala | 6 ------ .../utilities/types/TypeAstWalker.scala | 2 +- .../programs/completer/completer_example_4.py | 4 ++++ .../programs/completer/completer_example_5.py | 16 ++++++++++++++++ 4 files changed, 21 insertions(+), 7 deletions(-) create mode 100644 tpParser/shared/src/test/programs/completer/completer_example_4.py create mode 100644 tpParser/shared/src/test/programs/completer/completer_example_5.py diff --git a/tpParser/shared/src/main/scala/tigerpython/utilities/types/BuiltinTypes.scala b/tpParser/shared/src/main/scala/tigerpython/utilities/types/BuiltinTypes.scala index 17b5df5..b3ee518 100644 --- a/tpParser/shared/src/main/scala/tigerpython/utilities/types/BuiltinTypes.scala +++ b/tpParser/shared/src/main/scala/tigerpython/utilities/types/BuiltinTypes.scala @@ -103,12 +103,6 @@ object BuiltinTypes { BuiltinFunction("sort", Array(), MUTABLE_SEQ, "sort the items of the sequence in place") ) val LIST_TYPE = PrimitiveType("list", MUTABLE_SEQ) - LIST_TYPE.addFields( - PrimitiveType("first"), - PrimitiveType("head"), - PrimitiveType("last"), - PrimitiveType("tail", LIST_TYPE) - ) val TUPLE_TYPE = PrimitiveType("tuple", SEQ_TYPE) val BYTEARRAY_TYPE = PrimitiveType("bytearray", SEQ_TYPE) val BUFFER_TYPE = PrimitiveType("buffer", SEQ_TYPE) diff --git a/tpParser/shared/src/main/scala/tigerpython/utilities/types/TypeAstWalker.scala b/tpParser/shared/src/main/scala/tigerpython/utilities/types/TypeAstWalker.scala index 35831a9..7071967 100644 --- a/tpParser/shared/src/main/scala/tigerpython/utilities/types/TypeAstWalker.scala +++ b/tpParser/shared/src/main/scala/tigerpython/utilities/types/TypeAstWalker.scala @@ -169,7 +169,7 @@ class TypeAstWalker { for (element <- list.elements) result = DataType.getCompatibleType(result, getType(element)) if (result != null && result != ANY_TYPE) - Instance(ListType(result)) + new Instance(ListType(result)) else BuiltinTypes.LIST } else diff --git a/tpParser/shared/src/test/programs/completer/completer_example_4.py b/tpParser/shared/src/test/programs/completer/completer_example_4.py new file mode 100644 index 0000000..a966957 --- /dev/null +++ b/tpParser/shared/src/test/programs/completer/completer_example_4.py @@ -0,0 +1,4 @@ +# 18 +# append;count;extend;index;insert;pop;remove;reverse;sort +li = [1,2,3,4] +li.append(5) \ No newline at end of file diff --git a/tpParser/shared/src/test/programs/completer/completer_example_5.py b/tpParser/shared/src/test/programs/completer/completer_example_5.py new file mode 100644 index 0000000..a8378b5 --- /dev/null +++ b/tpParser/shared/src/test/programs/completer/completer_example_5.py @@ -0,0 +1,16 @@ +# 243 +# draw;extent;pos_x;pos_y +class Shape: + + def __init__(self, x, y): + self.pos_x = x + self.pos_y = y + + def draw(self): + pass + + def extent(self): + return (0, 0) + +myshape = [Shape(123, 456), Shape(123, 456), Shape(123, 456)] +myshape[0].draw() \ No newline at end of file From 565d64d3cbe8dea41d71c5715848d5e9eee4ba1f Mon Sep 17 00:00:00 2001 From: Neil Brown Date: Thu, 6 Feb 2025 19:39:54 +0000 Subject: [PATCH 2/2] Fix problems with code completion and dicts, and added a test (which previously failed). There were two issues: - DICT_TYPE had some dict methods which were removed in Python 3: iteritems (et al) and viewitems (et al). I think TigerPython now only targets Python 3, so they should be removed? - Autocompletion on dicts was blank rather than showing available methods. I don't want to go to the effort of adding full dict type support like there is for lists, but this at least gets it showing the methods on a dict which are available on all dicts (regardless of the type contained within). --- .../tigerpython/parser/types/TypeAstWalker.scala | 4 ++++ .../tigerpython/utilities/types/BuiltinTypes.scala | 14 +------------- .../utilities/types/TypeAstWalker.scala | 4 ++++ .../test/programs/completer/completer_example_6.py | 4 ++++ 4 files changed, 13 insertions(+), 13 deletions(-) create mode 100644 tpParser/shared/src/test/programs/completer/completer_example_6.py diff --git a/tpParser/shared/src/main/scala/tigerpython/parser/types/TypeAstWalker.scala b/tpParser/shared/src/main/scala/tigerpython/parser/types/TypeAstWalker.scala index f97d50b..ea9c4c3 100755 --- a/tpParser/shared/src/main/scala/tigerpython/parser/types/TypeAstWalker.scala +++ b/tpParser/shared/src/main/scala/tigerpython/parser/types/TypeAstWalker.scala @@ -65,6 +65,10 @@ class TypeAstWalker { getTypeOfList(list) case list: AstNode.ListComp => getTypeOfListComp(list) + case _: AstNode.Dict => + BuiltinTypes.DICT + case _dict: AstNode.DictComp => + BuiltinTypes.DICT case name: AstNode.Name => validateDataType(findName(name.name)) case subscript: AstNode.Subscript => diff --git a/tpParser/shared/src/main/scala/tigerpython/utilities/types/BuiltinTypes.scala b/tpParser/shared/src/main/scala/tigerpython/utilities/types/BuiltinTypes.scala index b3ee518..080cfc8 100644 --- a/tpParser/shared/src/main/scala/tigerpython/utilities/types/BuiltinTypes.scala +++ b/tpParser/shared/src/main/scala/tigerpython/utilities/types/BuiltinTypes.scala @@ -221,12 +221,6 @@ object BuiltinTypes { "to None, so that this method never raises a KeyError."), BuiltinFunction("items", Array(), LIST_TYPE, "Return a copy of the dictionary’s list of (key, value) pairs."), - BuiltinFunction("iteritems", Array(), ITERATOR_TYPE, - "Return an iterator over the dictionary’s (key, value) pairs."), - BuiltinFunction("iterkeys", Array(), ITERATOR_TYPE, - "Return an iterator over the dictionary’s keys."), - BuiltinFunction("itervalues", Array(), ITERATOR_TYPE, - "Return an iterator over the dictionary’s values."), BuiltinFunction("keys", Array(), LIST_TYPE, "Return a copy of the dictionary’s list of keys."), BuiltinFunction("pop", Array("key", "default"), ANY_TYPE, @@ -240,13 +234,7 @@ object BuiltinTypes { BuiltinFunction("update", Array("other"), NONE_TYPE, "Update the dictionary with the key/value pairs from other, overwriting existing keys. Return None."), BuiltinFunction("values", Array(), LIST_TYPE, - "Return a copy of the dictionary’s list of values."), - BuiltinFunction("viewitems", Array(), ANY_TYPE, - "Return a new view of the dictionary’s items ((key, value) pairs)."), - BuiltinFunction("viewkeys", Array(), ANY_TYPE, - "Return a new view of the dictionary’s keys."), - BuiltinFunction("viewvalues", Array(), ANY_TYPE, - "Return a new view of the dictionary’s values.") + "Return a copy of the dictionary’s list of values.") ) val FILE_TYPE = PrimitiveType("file", Map( diff --git a/tpParser/shared/src/main/scala/tigerpython/utilities/types/TypeAstWalker.scala b/tpParser/shared/src/main/scala/tigerpython/utilities/types/TypeAstWalker.scala index 7071967..804c375 100644 --- a/tpParser/shared/src/main/scala/tigerpython/utilities/types/TypeAstWalker.scala +++ b/tpParser/shared/src/main/scala/tigerpython/utilities/types/TypeAstWalker.scala @@ -57,6 +57,10 @@ class TypeAstWalker { getTypeOfList(list) case list: AstNode.ListComp => getTypeOfListComp(list) + case _: AstNode.Dict => + BuiltinTypes.DICT + case _: AstNode.DictComp => + BuiltinTypes.DICT case name: AstNode.Name => validateDataType(findName(name.name)) case subscript: AstNode.Subscript => diff --git a/tpParser/shared/src/test/programs/completer/completer_example_6.py b/tpParser/shared/src/test/programs/completer/completer_example_6.py new file mode 100644 index 0000000..5de095c --- /dev/null +++ b/tpParser/shared/src/test/programs/completer/completer_example_6.py @@ -0,0 +1,4 @@ +# 19 +# clear;copy;get;items;keys;pop;popitem;setdefault;update;values +dt = {1:2, 3:4} +dt.clear() \ No newline at end of file