Skip to content

Commit

Permalink
[gc_heap] Fix List::index() method to take negative numbers
Browse files Browse the repository at this point in the history
examples/loops now works.

Also test out global container constants.
  • Loading branch information
Andy Chu committed Dec 13, 2020
1 parent bf6aa6a commit e8a0402
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
13 changes: 9 additions & 4 deletions mycpp/examples/containers.py
Expand Up @@ -10,11 +10,11 @@
from typing import List, Tuple, Dict, Optional


mystr = 'foo' # type: str
mylist = [1, 2] # type: List[int]
mylist2 = ['spam', 'eggs'] # type: List[str]
gstr = 'foo' # type: str
glist_int = [1, 2] # type: List[int]
glist_str = ['spam', 'eggs'] # type: List[str]

#mydict = {'a': 42, 'b': 43} # type: Dict[str, int]
gdict = {'a': 42, 'b': 43} # type: Dict[str, int]


def ListDemo():
Expand Down Expand Up @@ -103,6 +103,8 @@ def TupleDemo():
else:
print('no')

log("glist_int = %d", len(glist_int))
log("glist_str = %d", len(glist_str))


def DictDemo():
Expand All @@ -121,6 +123,9 @@ def DictDemo():
del d['foo']
log('len(d) = %d', len(d))

# TODO: fix this
# log("gdict = %d", len(gdict))


def run_tests():
# type: () -> None
Expand Down
5 changes: 5 additions & 0 deletions mycpp/examples/loops.py
Expand Up @@ -27,6 +27,11 @@ def TestListComp():
log('--- list comprehension changing type')

z = ['[%d]' % i for i in x[1:-1]]

# I think this rewrite might be necessary?
#tmp1 = x[1:-1]
#z = ['[%d]' % i for i in tmp1]

if mylib.PYTHON:
#log("z = %s", z)
pass
Expand Down
9 changes: 6 additions & 3 deletions mycpp/gc_heap.h
Expand Up @@ -796,12 +796,15 @@ class List : public gc_heap::Obj {

// Implements L[i]
T index(int i) {
if (i < 0) {
i += len_;
}
if (i < len_) {
return slab_->items_[i];
} else {
log("i = %d, len_ = %d", i, len_);
assert(0); // Out of bounds
}

log("i = %d, len_ = %d", i, len_);
assert(0); // Out of bounds
}

// Implements L[i] = item
Expand Down

0 comments on commit e8a0402

Please sign in to comment.