From 26ffc303a04341c05d0eee87d4fc9089e40018a3 Mon Sep 17 00:00:00 2001 From: Viliam Vadocz Date: Mon, 6 Apr 2020 20:01:55 +0200 Subject: [PATCH] Reverse iteration (#13) * first version of reverse iteration * No significant changes --- examples/test.vlp | 16 +--------------- volpe/builder.py | 33 +++++++++++++++++++++++++-------- volpe/compile.py | 10 +++++----- volpe/run_volpe.py | 2 +- 4 files changed, 32 insertions(+), 29 deletions(-) diff --git a/examples/test.vlp b/examples/test.vlp index d525def..4e2cca1 100644 --- a/examples/test.vlp +++ b/examples/test.vlp @@ -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] \ No newline at end of file diff --git a/volpe/builder.py b/volpe/builder.py index 037dfd7..353b9a9 100644 --- a/volpe/builder.py +++ b/volpe/builder.py @@ -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) diff --git a/volpe/compile.py b/volpe/compile.py index b872d7c..cd7f1c9 100644 --- a/volpe/compile.py +++ b/volpe/compile.py @@ -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]]) + ">" @@ -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 @@ -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 diff --git a/volpe/run_volpe.py b/volpe/run_volpe.py index 6c9f1fb..667aa65 100644 --- a/volpe/run_volpe.py +++ b/volpe/run_volpe.py @@ -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: