Skip to content

Commit

Permalink
Reverse iteration (#13)
Browse files Browse the repository at this point in the history
* first version of reverse iteration

* No significant changes
  • Loading branch information
ViliamVadocz authored Apr 6, 2020
1 parent 3c19651 commit 26ffc30
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 29 deletions.
16 changes: 1 addition & 15 deletions examples/test.vlp
Original file line number Diff line number Diff line change
@@ -1,15 +1 @@
f = (x, [y, z]) => [&x..10, y..z]
a = f(0, [0, 10])
[b, c] = a

# functions
# :> f

# tuples
# :> [1 > 0, [1, 0.0], f, b, c]

# iterators
# :> c :: i => [i, i.0, f, f(i-5, [0, 2])]

# lists
:> b
:> [&0..5, &5..0, &3..5, &5..3, &0..0, &0..1, &1..0, &-20..-5]
33 changes: 25 additions & 8 deletions volpe/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,33 @@ def number_iter(self, tree: TypeTree):
module = self.builder.module
env_types = [int32]

with build_closure(module, closure_type, env_types) as (b, args, closure, c_func):
start = read_environment(b, args[0], env_types)[0]
b.ret(b.add(start, args[1]))
# TODO compact or simplify the following code:

# TODO going in reverse
with options(self.builder, tree.return_type) as (ret, phi):
# reverse = a > b in (a..b).
reverse = self.builder.icmp_signed(">", values[0], values[1])
with self.builder.if_then(reverse):
# Going in reverse (10..0).
with build_closure(module, closure_type, env_types) as (b, args, closure, c_func):
start = read_environment(b, args[0], env_types)[0]
b.ret(b.sub(start, b.add(args[1], int32(1))))

env_ptr = write_environment(self.builder, [values[0]])
list_value = tree.return_type(ir.Undefined)
list_value = self.builder.insert_value(list_value, self.builder.insert_value(closure, env_ptr, 3), 0)
ret(self.builder.insert_value(list_value, self.builder.sub(values[0], values[1]), 1))

# Going forwards (0..10).
with build_closure(module, closure_type, env_types) as (b, args, closure, c_func):
start = read_environment(b, args[0], env_types)[0]
b.ret(b.add(start, args[1]))

env_ptr = write_environment(self.builder, [values[0]])
list_value = tree.return_type(ir.Undefined)
list_value = self.builder.insert_value(list_value, self.builder.insert_value(closure, env_ptr, 3), 0)
ret(self.builder.insert_value(list_value, self.builder.sub(values[1], values[0]), 1))

env_ptr = write_environment(self.builder, [values[0]])
list_value = tree.return_type(ir.Undefined)
list_value = self.builder.insert_value(list_value, self.builder.insert_value(closure, env_ptr, 3), 0)
return self.builder.insert_value(list_value, self.builder.sub(values[1], values[0]), 1)
return phi

def list_index(self, tree: TypeTree):
list_value, i = self.visit_children(tree)
Expand Down
10 changes: 5 additions & 5 deletions volpe/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def __repr__(self):
class CList(Structure):
_fields_ = [("elems", POINTER(element_type)), ("length", c_int32)]
def __repr__(self):
if depth == 0:
if depth < 2:
elems = getattr(self, "elems")
length = getattr(self, "length")
return "&<" + ", ".join([str(elem) for elem in elems[:length]]) + ">"
Expand All @@ -99,10 +99,10 @@ class CIterator(Structure):
_fields_ = [("func", CFunc), ("length", c_int32)]
def __repr__(self):
if depth == 0:
# TODO maybe include length?
# TODO Maybe include length.
func = volpe_type.elements[0].elements[0].pointee
return_type = get_type_name(func.return_type)
return f"iterator of {return_type}"
return f"iterator {return_type}"
return get_type_name(volpe_type)
return CIterator

Expand All @@ -129,9 +129,9 @@ def get_type_name(volpe_type):
if isinstance(volpe_type, VolpeList):
return "*list*"
if isinstance(volpe_type, VolpeClosure):
return "*function*"
return "*func*"
if isinstance(volpe_type, VolpeIterator):
return "*iterator*"
return "*iter*"

# Unknown type
return None
2 changes: 1 addition & 1 deletion volpe/run_volpe.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ def scope(name):
with build_func(main_func) as (b, _):
b: ir.IRBuilder
res = b.call(func, [pint8(ir.Undefined)])
# TODO return vector as pointer if length is needed in print
if isinstance(res.type, (VolpeTuple, VolpeList)):
ptr = b.bitcast(b.call(module.malloc, [int32(res.type.get_abi_size(target_data))]), return_type)
b.store(res, ptr)
res = ptr
# TODO return list and vector as pointer
b.ret(res)

if verbose:
Expand Down

0 comments on commit 26ffc30

Please sign in to comment.