| summaryrefslogtreecommitdiff |
diff options
| author | nsensfel <SpamShield0@noot-noot.org> | 2020-09-16 17:46:39 +0200 |
|---|---|---|
| committer | nsensfel <SpamShield0@noot-noot.org> | 2020-09-16 17:46:39 +0200 |
| commit | 60ed0ee5e2b4913aec9813b494d7cfa7747efdae (patch) | |
| tree | 890cdb0dd29502b08177397d6c979b7c2c0cae69 | |
| parent | 3821bf0f0c7276eb0fbe8e7cf27c41531d4dd381 (diff) | |
Makes functional imperative equivalents in-place.
Although the actual implementation cheats by making a copy to avoid
messy situations.
6 files changed, 170 insertions, 186 deletions
diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/IndexedMap.java b/src/core/src/tonkadur/fate/v1/lang/instruction/IndexedMap.java index 950418e..094561b 100644 --- a/src/core/src/tonkadur/fate/v1/lang/instruction/IndexedMap.java +++ b/src/core/src/tonkadur/fate/v1/lang/instruction/IndexedMap.java @@ -21,8 +21,7 @@ public class IndexedMap extends Instruction /**** MEMBERS **************************************************************/ /***************************************************************************/ protected final Computation lambda_function; - protected final Computation collection_in; - protected final Reference collection_out; + protected final Reference collection; /***************************************************************************/ /**** PROTECTED ************************************************************/ @@ -32,15 +31,13 @@ public class IndexedMap extends Instruction ( final Origin origin, final Computation lambda_function, - final Computation collection_in, - final Reference collection_out + final Reference collection ) { super(origin); this.lambda_function = lambda_function; - this.collection_in = collection_in; - this.collection_out = collection_out; + this.collection = collection; } /***************************************************************************/ @@ -51,8 +48,7 @@ public class IndexedMap extends Instruction ( final Origin origin, final Computation lambda_function, - final Computation collection_in, - final Reference collection_out + final Reference collection ) throws Throwable { @@ -60,24 +56,22 @@ public class IndexedMap extends Instruction in_types = new ArrayList<Type>(); - RecurrentChecks.assert_is_a_collection(collection_in); - RecurrentChecks.assert_is_a_collection(collection_out); + RecurrentChecks.assert_is_a_collection(collection); in_types.add(Type.INT); in_types.add ( - ((CollectionType) collection_in.get_type()).get_content_type() + ((CollectionType) collection.get_type()).get_content_type() ); RecurrentChecks.assert_lambda_matches_types ( lambda_function, - ((CollectionType) collection_out.get_type()).get_content_type(), + ((CollectionType) collection.get_type()).get_content_type(), in_types ); - return - new IndexedMap(origin, lambda_function, collection_in, collection_out); + return new IndexedMap(origin, lambda_function, collection); } /**** Accessors ************************************************************/ @@ -93,14 +87,9 @@ public class IndexedMap extends Instruction return lambda_function; } - public Computation get_collection_in () + public Reference get_collection () { - return collection_in; - } - - public Reference get_collection_out () - { - return collection_out; + return collection; } /**** Misc. ****************************************************************/ @@ -112,9 +101,7 @@ public class IndexedMap extends Instruction sb.append("(IndexedMap "); sb.append(lambda_function.toString()); sb.append(" "); - sb.append(collection_in.toString()); - sb.append(" "); - sb.append(collection_out.toString()); + sb.append(collection.toString()); sb.append(")"); return sb.toString(); diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/Map.java b/src/core/src/tonkadur/fate/v1/lang/instruction/Map.java index cba32f5..0cc941f 100644 --- a/src/core/src/tonkadur/fate/v1/lang/instruction/Map.java +++ b/src/core/src/tonkadur/fate/v1/lang/instruction/Map.java @@ -21,8 +21,7 @@ public class Map extends Instruction /**** MEMBERS **************************************************************/ /***************************************************************************/ protected final Computation lambda_function; - protected final Computation collection_in; - protected final Reference collection_out; + protected final Reference collection; /***************************************************************************/ /**** PROTECTED ************************************************************/ @@ -32,15 +31,13 @@ public class Map extends Instruction ( final Origin origin, final Computation lambda_function, - final Computation collection_in, - final Reference collection_out + final Reference collection ) { super(origin); this.lambda_function = lambda_function; - this.collection_in = collection_in; - this.collection_out = collection_out; + this.collection = collection; } /***************************************************************************/ @@ -51,24 +48,22 @@ public class Map extends Instruction ( final Origin origin, final Computation lambda_function, - final Computation collection_in, - final Reference collection_out + final Reference collection ) throws ParsingError { - RecurrentChecks.assert_is_a_collection(collection_in); - RecurrentChecks.assert_is_a_collection(collection_out); + RecurrentChecks.assert_is_a_collection(collection); RecurrentChecks.assert_lambda_matches_types ( lambda_function, - ((CollectionType) collection_out.get_type()).get_content_type(), + ((CollectionType) collection.get_type()).get_content_type(), Collections.singletonList ( - ((CollectionType) collection_in.get_type()).get_content_type() + ((CollectionType) collection.get_type()).get_content_type() ) ); - return new Map(origin, lambda_function, collection_in, collection_out); + return new Map(origin, lambda_function, collection); } /**** Accessors ************************************************************/ @@ -84,14 +79,9 @@ public class Map extends Instruction return lambda_function; } - public Computation get_collection_in () + public Reference get_collection () { - return collection_in; - } - - public Reference get_collection_out () - { - return collection_out; + return collection; } /**** Misc. ****************************************************************/ @@ -103,9 +93,7 @@ public class Map extends Instruction sb.append("(Map "); sb.append(lambda_function.toString()); sb.append(" "); - sb.append(collection_in.toString()); - sb.append(" "); - sb.append(collection_out.toString()); + sb.append(collection.toString()); sb.append(")"); return sb.toString(); diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/Merge.java b/src/core/src/tonkadur/fate/v1/lang/instruction/Merge.java index e070cdc..b5821ed 100644 --- a/src/core/src/tonkadur/fate/v1/lang/instruction/Merge.java +++ b/src/core/src/tonkadur/fate/v1/lang/instruction/Merge.java @@ -21,11 +21,10 @@ public class Merge extends Instruction /**** MEMBERS **************************************************************/ /***************************************************************************/ protected final Computation lambda_function; - protected final Computation collection_in_a; + protected final Reference collection; protected final Computation default_a; protected final Computation collection_in_b; protected final Computation default_b; - protected final Reference collection_out; /***************************************************************************/ /**** PROTECTED ************************************************************/ @@ -35,21 +34,19 @@ public class Merge extends Instruction ( final Origin origin, final Computation lambda_function, - final Computation collection_in_a, + final Reference collection, final Computation default_a, final Computation collection_in_b, - final Computation default_b, - final Reference collection_out + final Computation default_b ) { super(origin); this.lambda_function = lambda_function; - this.collection_in_a = collection_in_a; + this.collection = collection; this.default_a = default_a; this.collection_in_b = collection_in_b; this.default_b = default_b; - this.collection_out = collection_out; } /***************************************************************************/ @@ -60,11 +57,10 @@ public class Merge extends Instruction ( final Origin origin, final Computation lambda_function, - final Computation collection_in_a, + final Reference collection, final Computation default_a, final Computation collection_in_b, - final Computation default_b, - final Reference collection_out + final Computation default_b ) throws Throwable { @@ -72,15 +68,13 @@ public class Merge extends Instruction types_in = new ArrayList<Type>(); - RecurrentChecks.assert_is_a_collection(collection_out); - if (default_a == null) { - RecurrentChecks.assert_is_a_collection(collection_in_a); + RecurrentChecks.assert_is_a_collection(collection); } else { - RecurrentChecks.assert_is_a_collection_of(collection_in_a, default_a); + RecurrentChecks.assert_is_a_collection_of(collection, default_a); } if (default_b == null) @@ -94,7 +88,7 @@ public class Merge extends Instruction types_in.add ( - ((CollectionType) collection_in_a.get_type()).get_content_type() + ((CollectionType) collection.get_type()).get_content_type() ); types_in.add @@ -105,7 +99,7 @@ public class Merge extends Instruction RecurrentChecks.assert_lambda_matches_types ( lambda_function, - ((CollectionType) collection_out.get_type()).get_content_type(), + ((CollectionType) collection.get_type()).get_content_type(), types_in ); @@ -114,11 +108,10 @@ public class Merge extends Instruction ( origin, lambda_function, - collection_in_a, + collection, default_a, collection_in_b, - default_b, - collection_out + default_b ); } @@ -135,11 +128,6 @@ public class Merge extends Instruction return lambda_function; } - public Computation get_collection_in_a () - { - return collection_in_a; - } - public Computation get_default_a () { return default_a; @@ -155,9 +143,9 @@ public class Merge extends Instruction return default_b; } - public Reference get_collection_out () + public Reference get_collection () { - return collection_out; + return collection; } /**** Misc. ****************************************************************/ @@ -169,7 +157,7 @@ public class Merge extends Instruction sb.append("(Merge "); sb.append(lambda_function.toString()); sb.append(" "); - sb.append(collection_in_a.toString()); + sb.append(collection.toString()); sb.append(" "); if (default_a == null) @@ -194,17 +182,6 @@ public class Merge extends Instruction sb.append(default_b.toString()); } - sb.append(" "); - - if (collection_out == null) - { - sb.append("null"); - } - else - { - sb.append(collection_out.toString()); - } - sb.append(")"); return sb.toString(); diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/SubList.java b/src/core/src/tonkadur/fate/v1/lang/instruction/SubList.java index b2e5176..a80325b 100644 --- a/src/core/src/tonkadur/fate/v1/lang/instruction/SubList.java +++ b/src/core/src/tonkadur/fate/v1/lang/instruction/SubList.java @@ -18,8 +18,7 @@ public class SubList extends Instruction /***************************************************************************/ protected final Computation start; protected final Computation end; - protected final Computation collection_in; - protected final Reference collection_out; + protected final Reference collection; /***************************************************************************/ /**** PROTECTED ************************************************************/ @@ -30,16 +29,14 @@ public class SubList extends Instruction final Origin origin, final Computation start, final Computation end, - final Computation collection_in, - final Reference collection_out + final Reference collection ) { super(origin); this.start = start; this.end = end; - this.collection_in = collection_in; - this.collection_out = collection_out; + this.collection = collection; } /***************************************************************************/ @@ -51,22 +48,15 @@ public class SubList extends Instruction final Origin origin, final Computation start, final Computation end, - final Computation collection_in, - final Reference collection_out + final Reference collection ) throws Throwable { - RecurrentChecks.assert_is_a_collection(collection_in); - RecurrentChecks.assert_is_a_collection(collection_out); - RecurrentChecks.assert_can_be_used_as - ( - collection_in, - collection_out.get_type() - ); + RecurrentChecks.assert_is_a_collection(collection); RecurrentChecks.assert_can_be_used_as(start, Type.INT); RecurrentChecks.assert_can_be_used_as(end, Type.INT); - return new SubList(origin, start, end, collection_in, collection_out); + return new SubList(origin, start, end, collection); } /**** Accessors ************************************************************/ @@ -87,14 +77,9 @@ public class SubList extends Instruction return end; } - public Computation get_collection_in () + public Reference get_collection () { - return collection_in; - } - - public Reference get_collection_out () - { - return collection_out; + return collection; } /**** Misc. ****************************************************************/ @@ -108,9 +93,7 @@ public class SubList extends Instruction sb.append(" "); sb.append(end.toString()); sb.append(" "); - sb.append(collection_in.toString()); - sb.append(" "); - sb.append(collection_out.toString()); + sb.append(collection.toString()); sb.append(")"); return sb.toString(); diff --git a/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 b/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 index f914451..1d2213e 100644 --- a/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 +++ b/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 @@ -805,11 +805,7 @@ returns [Instruction result] ); } - | IMP_MAP_KW - fun=value WS+ - in=value WS+ - outr=value_reference WS* - R_PAREN + | IMP_MAP_KW value WS+ value_reference WS* R_PAREN { $result = tonkadur.fate.v1.lang.instruction.Map.build @@ -819,17 +815,12 @@ returns [Instruction result] ($IMP_MAP_KW.getLine()), ($IMP_MAP_KW.getCharPositionInLine()) ), - ($fun.result), - ($in.result), - ($outr.result) + ($value.result), + ($value_reference.result) ); } - | IMP_INDEXED_MAP_KW - fun=value WS+ - in=value WS+ - outr=value_reference WS* - R_PAREN + | IMP_INDEXED_MAP_KW value WS+ value_reference WS* R_PAREN { $result = IndexedMap.build @@ -839,19 +830,12 @@ returns [Instruction result] ($IMP_INDEXED_MAP_KW.getLine()), ($IMP_INDEXED_MAP_KW.getCharPositionInLine()) ), - ($fun.result), - ($in.result), - ($outr.result) + ($value.result), + ($value_reference.result) ); } - - | IMP_MERGE_KW - fun=value WS+ - inv0=value WS+ - inv1=value WS+ - outr=value_reference WS* - R_PAREN + | IMP_MERGE_KW fun=value WS+ value_reference WS+ inv1=value WS* R_PAREN { $result = Merge.build @@ -862,21 +846,19 @@ returns [Instruction result] ($IMP_MERGE_KW.getCharPositionInLine()) ), ($fun.result), - ($inv0.result), + ($value_reference.result), null, ($inv1.result), - null, - ($outr.result) + null ); } | IMP_MERGE_KW fun=value WS+ def0=value WS+ - inv0=value WS+ + value_reference WS+ def1=value WS+ - inv1=value WS+ - outr=value_reference WS* + inv1=value WS* R_PAREN { $result = @@ -888,20 +870,14 @@ returns [Instruction result] ($IMP_MERGE_KW.getCharPositionInLine()) ), ($fun.result), - ($inv0.result), + ($value_reference.result), ($def0.result), ($inv1.result), - ($def1.result), - ($outr.result) + ($def1.result) ); } - | IMP_SUB_LIST_KW - vstart=value WS+ - vend=value WS+ - inv=value WS+ - outr=value_reference WS* - R_PAREN + | IMP_SUB_LIST_KW vstart=value WS+ vend=value WS+ value_reference WS* R_PAREN { $result = SubList.build @@ -913,8 +889,7 @@ returns [Instruction result] ), ($vstart.result), ($vend.result), - ($inv.result), - ($outr.result) + ($value_reference.result) ); } diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/InstructionCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/InstructionCompiler.java index b6fd8bf..3d0630b 100644 --- a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/InstructionCompiler.java +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/InstructionCompiler.java @@ -52,6 +52,7 @@ import tonkadur.wyrd.v1.compiler.util.While; import tonkadur.wyrd.v1.compiler.util.Shuffle; import tonkadur.wyrd.v1.compiler.util.Clear; import tonkadur.wyrd.v1.compiler.util.MapLambda; +import tonkadur.wyrd.v1.compiler.util.MergeLambda; import tonkadur.wyrd.v1.compiler.util.IndexedMapLambda; import tonkadur.wyrd.v1.compiler.util.IterativeSearch; import tonkadur.wyrd.v1.compiler.util.RemoveAllOf; @@ -478,7 +479,9 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor ) throws Throwable { - final ComputationCompiler lambda_cc, in_collection_cc, out_collection_cc; + /* This is one dangerous operation to do in-place, so we don't. */ + final Register holder; + final ComputationCompiler lambda_cc, collection_cc; lambda_cc = new ComputationCompiler(compiler); @@ -489,23 +492,26 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor result.add(lambda_cc.get_init()); } - in_collection_cc = new ComputationCompiler(compiler); + collection_cc = new ComputationCompiler(compiler); - n.get_collection_in().get_visited_by(in_collection_cc); + n.get_collection().get_visited_by(collection_cc); - if (in_collection_cc.has_init()) + if (collection_cc.has_init()) { - result.add(in_collection_cc.get_init()); + result.add(collection_cc.get_init()); } - out_collection_cc = new ComputationCompiler(compiler); - - n.get_collection_out().get_visited_by(out_collection_cc); + holder = + compiler.registers().reserve + ( + collection_cc.get_computation().get_type(), + result + ); - if (out_collection_cc.has_init()) - { - result.add(out_collection_cc.get_init()); - } + result.add + ( + new SetValue(holder.get_address(), collection_cc.get_computation()) + ); result.add ( @@ -514,18 +520,18 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor compiler.registers(), compiler.assembler(), lambda_cc.get_computation(), - in_collection_cc.get_address(), - out_collection_cc.get_address(), + holder.get_address(), + collection_cc.get_address(), ( (tonkadur.fate.v1.lang.type.CollectionType) - n.get_collection_out().get_type() + n.get_collection().get_type() ).is_set() ) ); lambda_cc.release_registers(result); - in_collection_cc.release_registers(result); - out_collection_cc.release_registers(result); + collection_cc.release_registers(result); + compiler.registers().release(holder, result); } @Override @@ -545,7 +551,70 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor ) throws Throwable { - /* TODO */ + /* This is one dangerous operation to do in-place, so we don't. */ + final Register holder; + final ComputationCompiler lambda_cc; + final ComputationCompiler collection_cc, in_collection_b_cc; + + lambda_cc = new ComputationCompiler(compiler); + + n.get_lambda_function().get_visited_by(lambda_cc); + + if (lambda_cc.has_init()) + { + result.add(lambda_cc.get_init()); + } + + collection_cc = new ComputationCompiler(compiler); + + n.get_collection().get_visited_by(collection_cc); + + if (collection_cc.has_init()) + { + result.add(collection_cc.get_init()); + } + + holder = + compiler.registers().reserve + ( + collection_cc.get_computation().get_type(), + result + ); + + result.add + ( + new SetValue(holder.get_address(), collection_cc.get_computation()) + ); + + in_collection_b_cc = new ComputationCompiler(compiler); + + n.get_collection_in_b().get_visited_by(in_collection_b_cc); + + if (in_collection_b_cc.has_init()) + { + result.add(in_collection_b_cc.get_init()); + } + + result.add + ( + MergeLambda.generate + ( + compiler.registers(), + compiler.assembler(), + lambda_cc.get_computation(), + holder.get_address(), + in_collection_b_cc.get_address(), + collection_cc.get_address(), + ( + (tonkadur.fate.v1.lang.type.CollectionType) + n.get_collection().get_type() + ).is_set() + ) + ); + + collection_cc.release_registers(result); + in_collection_b_cc.release_registers(result); + compiler.registers().release(holder, result); } @Override @@ -605,7 +674,9 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor ) throws Throwable { - final ComputationCompiler lambda_cc, in_collection_cc, out_collection_cc; + /* This is one dangerous operation to do in-place, so we don't. */ + final Register holder; + final ComputationCompiler lambda_cc, collection_cc; lambda_cc = new ComputationCompiler(compiler); @@ -616,23 +687,26 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor result.add(lambda_cc.get_init()); } - in_collection_cc = new ComputationCompiler(compiler); + collection_cc = new ComputationCompiler(compiler); - n.get_collection_in().get_visited_by(in_collection_cc); + n.get_collection().get_visited_by(collection_cc); - if (in_collection_cc.has_init()) + if (collection_cc.has_init()) { - result.add(in_collection_cc.get_init()); + result.add(collection_cc.get_init()); } - out_collection_cc = new ComputationCompiler(compiler); - - n.get_collection_out().get_visited_by(out_collection_cc); + holder = + compiler.registers().reserve + ( + collection_cc.get_computation().get_type(), + result + ); - if (out_collection_cc.has_init()) - { - result.add(out_collection_cc.get_init()); - } + result.add + ( + new SetValue(holder.get_address(), collection_cc.get_computation()) + ); result.add ( @@ -641,18 +715,18 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor compiler.registers(), compiler.assembler(), lambda_cc.get_computation(), - in_collection_cc.get_address(), - out_collection_cc.get_address(), + holder.get_address(), + collection_cc.get_address(), ( (tonkadur.fate.v1.lang.type.CollectionType) - n.get_collection_out().get_type() + n.get_collection().get_type() ).is_set() ) ); lambda_cc.release_registers(result); - in_collection_cc.release_registers(result); - out_collection_cc.release_registers(result); + collection_cc.release_registers(result); + compiler.registers().release(holder, result); } @Override |


