compiler: Implement len() for ndarrays

This commit is contained in:
David Nadlinger 2020-07-25 22:34:52 +01:00
parent 575be2aeca
commit d882f8a3f0
2 changed files with 6 additions and 0 deletions

View File

@ -1170,6 +1170,10 @@ class LLVMIRGenerator:
return get_outer(self.map(env), env.type) return get_outer(self.map(env), env.type)
elif insn.op == "len": elif insn.op == "len":
collection, = insn.operands collection, = insn.operands
if builtins.is_array(collection.type):
# Return length of outermost dimension.
shape = self.llbuilder.extract_value(self.map(collection), 0)
return self.llbuilder.load(self.llbuilder.extract_value(shape, 0))
return self.llbuilder.extract_value(self.map(collection), 1) return self.llbuilder.extract_value(self.map(collection), 1)
elif insn.op in ("printf", "rtio_log"): elif insn.op in ("printf", "rtio_log"):
# We only get integers, floats, pointers and strings here. # We only get integers, floats, pointers and strings here.

View File

@ -2,7 +2,9 @@
# REQUIRES: exceptions # REQUIRES: exceptions
ary = array([1, 2, 3]) ary = array([1, 2, 3])
assert len(ary) == 3
# FIXME: Implement ndarray indexing # FIXME: Implement ndarray indexing
# assert [x*x for x in ary] == [1, 4, 9] # assert [x*x for x in ary] == [1, 4, 9]
matrix = array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]) matrix = array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
assert len(matrix) == 2