From c44f60829dc93687fe7d904b60d154de58f0492d Mon Sep 17 00:00:00 2001 From: Nathanael Sensfelder Date: Sat, 14 Aug 2021 04:13:56 +0200 Subject: Adds the Wyrd compilers for all generic Fate computations. --- .../lang/computation/ExtraComputationInstance.java | 8 +- .../fate/v1/lang/computation/TextWithEffect.java | 2 +- .../lang/computation/generic/LambdaEvaluation.java | 8 +- .../fate/v1/lang/computation/generic/TextJoin.java | 2 +- .../fate/v1/lang/instruction/EventOption.java | 8 +- .../lang/instruction/ExtraInstructionInstance.java | 8 +- .../fate/v1/lang/instruction/SequenceCall.java | 2 +- .../fate/v1/lang/instruction/SequenceJump.java | 2 +- .../v1/lang/instruction/SequenceVariableCall.java | 2 +- .../v1/lang/instruction/SequenceVariableJump.java | 2 +- .../v1/compiler/fate/v1/ComputationCompiler.java | 3658 +++----------------- .../v1/computation/generic/AccessCompiler.java | 65 + .../generic/AddElementAtComputationCompiler.java | 130 + .../generic/AddElementComputationCompiler.java | 82 + .../generic/AddElementsOfComputationCompiler.java | 84 + .../v1/computation/generic/CarCdrCompiler.java | 58 + .../generic/ConsComputationCompiler.java | 77 + .../computation/generic/CountOperatorCompiler.java | 132 + .../generic/FilterComputationCompiler.java | 110 + .../fate/v1/computation/generic/FoldCompiler.java | 118 + .../generic/IndexOfOperatorCompiler.java | 70 + .../generic/IndexedFilterComputationCompiler.java | 109 + .../generic/IndexedMapComputationCompiler.java | 105 + .../generic/IndexedMergeComputationCompiler.java | 213 ++ .../IndexedPartitionComputationCompiler.java | 144 + .../v1/computation/generic/IsEmptyCompiler.java | 47 + .../generic/IsMemberOperatorCompiler.java | 124 + .../generic/LambdaEvaluationCompiler.java | 1 - .../generic/MapComputationCompiler.java | 102 + .../generic/MergeComputationCompiler.java | 208 ++ .../generic/PartitionComputationCompiler.java | 144 + .../generic/PopElementComputationCompiler.java | 115 + .../generic/PushElementComputationCompiler.java | 113 + .../fate/v1/computation/generic/RangeCompiler.java | 160 + .../RemoveAllOfElementComputationCompiler.java | 81 + .../RemoveElementAtComputationCompiler.java | 83 + .../generic/RemoveElementComputationCompiler.java | 82 + .../RemoveElementsOfComputationCompiler.java | 85 + .../generic/ReverseListComputationCompiler.java | 73 + .../generic/ShuffleComputationCompiler.java | 72 + .../computation/generic/SizeOperatorCompiler.java | 44 + .../generic/SortComputationCompiler.java | 101 + .../generic/SubListComputationCompiler.java | 84 + 43 files changed, 3641 insertions(+), 3277 deletions(-) create mode 100644 src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/AccessCompiler.java create mode 100644 src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/AddElementAtComputationCompiler.java create mode 100644 src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/AddElementComputationCompiler.java create mode 100644 src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/AddElementsOfComputationCompiler.java create mode 100644 src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/CarCdrCompiler.java create mode 100644 src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/ConsComputationCompiler.java create mode 100644 src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/CountOperatorCompiler.java create mode 100644 src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/FilterComputationCompiler.java create mode 100644 src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/FoldCompiler.java create mode 100644 src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/IndexOfOperatorCompiler.java create mode 100644 src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/IndexedFilterComputationCompiler.java create mode 100644 src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/IndexedMapComputationCompiler.java create mode 100644 src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/IndexedMergeComputationCompiler.java create mode 100644 src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/IndexedPartitionComputationCompiler.java create mode 100644 src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/IsEmptyCompiler.java create mode 100644 src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/IsMemberOperatorCompiler.java create mode 100644 src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/MapComputationCompiler.java create mode 100644 src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/MergeComputationCompiler.java create mode 100644 src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/PartitionComputationCompiler.java create mode 100644 src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/PopElementComputationCompiler.java create mode 100644 src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/PushElementComputationCompiler.java create mode 100644 src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/RangeCompiler.java create mode 100644 src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/RemoveAllOfElementComputationCompiler.java create mode 100644 src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/RemoveElementAtComputationCompiler.java create mode 100644 src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/RemoveElementComputationCompiler.java create mode 100644 src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/RemoveElementsOfComputationCompiler.java create mode 100644 src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/ReverseListComputationCompiler.java create mode 100644 src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/ShuffleComputationCompiler.java create mode 100644 src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/SizeOperatorCompiler.java create mode 100644 src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/SortComputationCompiler.java create mode 100644 src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/SubListComputationCompiler.java diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/ExtraComputationInstance.java b/src/core/src/tonkadur/fate/v1/lang/computation/ExtraComputationInstance.java index cbd0b6b..83960b7 100644 --- a/src/core/src/tonkadur/fate/v1/lang/computation/ExtraComputationInstance.java +++ b/src/core/src/tonkadur/fate/v1/lang/computation/ExtraComputationInstance.java @@ -45,13 +45,7 @@ public class ExtraComputationInstance extends Computation ) throws ParsingError { - RecurrentChecks.propagate_expected_types - ( - parameters, - computation.get_signature() - ); - - RecurrentChecks.assert_computations_matches_signature + RecurrentChecks.propagate_expected_types_and_assert_computations_matches_signature ( origin, parameters, diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/TextWithEffect.java b/src/core/src/tonkadur/fate/v1/lang/computation/TextWithEffect.java index d5647c0..a4f029f 100644 --- a/src/core/src/tonkadur/fate/v1/lang/computation/TextWithEffect.java +++ b/src/core/src/tonkadur/fate/v1/lang/computation/TextWithEffect.java @@ -54,7 +54,7 @@ public class TextWithEffect extends Computation ) throws ParsingError { - RecurrentChecks.assert_computations_matches_signature + RecurrentChecks.propagate_expected_types_and_assert_computations_matches_signature ( origin, parameters, diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/generic/LambdaEvaluation.java b/src/core/src/tonkadur/fate/v1/lang/computation/generic/LambdaEvaluation.java index f1af59f..9e1597f 100644 --- a/src/core/src/tonkadur/fate/v1/lang/computation/generic/LambdaEvaluation.java +++ b/src/core/src/tonkadur/fate/v1/lang/computation/generic/LambdaEvaluation.java @@ -63,13 +63,7 @@ public class LambdaEvaluation extends GenericComputation call_parameters.remove(0); - RecurrentChecks.propagate_expected_types - ( - call_parameters, - lambda_signature - ); - - RecurrentChecks.assert_computations_matches_signature + RecurrentChecks.propagate_expected_types_and_assert_computations_matches_signature ( origin, call_parameters, diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/generic/TextJoin.java b/src/core/src/tonkadur/fate/v1/lang/computation/generic/TextJoin.java index 4d10497..7d5d364 100644 --- a/src/core/src/tonkadur/fate/v1/lang/computation/generic/TextJoin.java +++ b/src/core/src/tonkadur/fate/v1/lang/computation/generic/TextJoin.java @@ -57,7 +57,7 @@ public class TextJoin extends GenericComputation text_collection.expect_non_string(); link.expect_string(); - RecurrentChecks.assert_is_a_collection_of + RecurrentChecks.propagate_expected_types_and_assert_is_a_collection_of ( origin, text_collection.get_type(), diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/EventOption.java b/src/core/src/tonkadur/fate/v1/lang/instruction/EventOption.java index d2d4fc3..6b4413e 100644 --- a/src/core/src/tonkadur/fate/v1/lang/instruction/EventOption.java +++ b/src/core/src/tonkadur/fate/v1/lang/instruction/EventOption.java @@ -70,13 +70,7 @@ public class EventOption extends Instruction ) throws ParsingError { - RecurrentChecks.propagate_expected_types - ( - parameters, - event.get_signature() - ); - - RecurrentChecks.assert_computations_matches_signature + RecurrentChecks.propagate_expected_types_and_assert_computations_matches_signature ( origin, parameters, diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/ExtraInstructionInstance.java b/src/core/src/tonkadur/fate/v1/lang/instruction/ExtraInstructionInstance.java index 2a5a13e..efeafaa 100644 --- a/src/core/src/tonkadur/fate/v1/lang/instruction/ExtraInstructionInstance.java +++ b/src/core/src/tonkadur/fate/v1/lang/instruction/ExtraInstructionInstance.java @@ -46,13 +46,7 @@ public class ExtraInstructionInstance extends Instruction ) throws ParsingError { - RecurrentChecks.propagate_expected_types - ( - parameters, - instruction.get_signature() - ); - - RecurrentChecks.assert_computations_matches_signature + RecurrentChecks.propagate_expected_types_and_assert_computations_matches_signature ( origin, parameters, diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/SequenceCall.java b/src/core/src/tonkadur/fate/v1/lang/instruction/SequenceCall.java index 1b93aba..2972417 100644 --- a/src/core/src/tonkadur/fate/v1/lang/instruction/SequenceCall.java +++ b/src/core/src/tonkadur/fate/v1/lang/instruction/SequenceCall.java @@ -65,7 +65,7 @@ public class SequenceCall extends Instruction signature ); - RecurrentChecks.assert_computations_matches_signature + RecurrentChecks.propagate_expected_types_and_assert_computations_matches_signature ( get_origin(), parameters, diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/SequenceJump.java b/src/core/src/tonkadur/fate/v1/lang/instruction/SequenceJump.java index 9d8513c..f7157ff 100644 --- a/src/core/src/tonkadur/fate/v1/lang/instruction/SequenceJump.java +++ b/src/core/src/tonkadur/fate/v1/lang/instruction/SequenceJump.java @@ -48,7 +48,7 @@ public class SequenceJump extends Instruction signature ); - RecurrentChecks.assert_computations_matches_signature + RecurrentChecks.propagate_expected_types_and_assert_computations_matches_signature ( get_origin(), parameters, diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/SequenceVariableCall.java b/src/core/src/tonkadur/fate/v1/lang/instruction/SequenceVariableCall.java index 0e4a654..7fb0d1c 100644 --- a/src/core/src/tonkadur/fate/v1/lang/instruction/SequenceVariableCall.java +++ b/src/core/src/tonkadur/fate/v1/lang/instruction/SequenceVariableCall.java @@ -79,7 +79,7 @@ public class SequenceVariableCall extends Instruction ); } - RecurrentChecks.assert_computations_matches_signature + RecurrentChecks.propagate_expected_types_and_assert_computations_matches_signature ( origin, parameters, diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/SequenceVariableJump.java b/src/core/src/tonkadur/fate/v1/lang/instruction/SequenceVariableJump.java index f0a0cae..72bb3a2 100644 --- a/src/core/src/tonkadur/fate/v1/lang/instruction/SequenceVariableJump.java +++ b/src/core/src/tonkadur/fate/v1/lang/instruction/SequenceVariableJump.java @@ -79,7 +79,7 @@ public class SequenceVariableJump extends Instruction ); } - RecurrentChecks.assert_computations_matches_signature + RecurrentChecks.propagate_expected_types_and_assert_computations_matches_signature ( origin, parameters, diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/ComputationCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/ComputationCompiler.java index f5bb4a0..b02b600 100644 --- a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/ComputationCompiler.java +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/ComputationCompiler.java @@ -164,37 +164,7 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor return result; } -/* - @Override - public void visit_at_reference - ( - final tonkadur.fate.v1.lang.computation.AtReference n - ) - throws Throwable - { - final ComputationCompiler n_cc; - - n_cc = new ComputationCompiler(compiler); - - n.get_parent().get_visited_by(n_cc); - - assimilate(n_cc); - result_as_address = - new Address - ( - n_cc.get_computation(), - TypeCompiler.compile - ( - compiler, - ( - (tonkadur.fate.v1.lang.type.PointerType) - n.get_parent().get_type() - ).get_referenced_type() - ) - ); - } -*/ @Override public void visit_cast ( @@ -374,105 +344,6 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor n.get_value_as_string() ); } -/* - @Override - public void visit_count_operator - ( - final tonkadur.fate.v1.lang.computation.CountOperator n - ) - throws Throwable - { - final ComputationCompiler collection_compiler, element_compiler; - - collection_compiler = new ComputationCompiler(compiler); - element_compiler = new ComputationCompiler(compiler); - - n.get_collection().get_visited_by(collection_compiler); - n.get_element().get_visited_by(element_compiler); - - collection_compiler.generate_address(); - - assimilate(collection_compiler); - assimilate(element_compiler); - - if - ( - ( - (tonkadur.fate.v1.lang.type.CollectionType) - n.get_collection().get_type() - ).is_set() - ) - { - final Register was_found, index, element; - - was_found = reserve(Type.BOOL); - index = reserve(Type.INT); - element = reserve(element_compiler.get_computation().get_type()); - - init_instructions.add - ( - new SetValue - ( - element.get_address(), - element_compiler.get_computation() - ) - ); - init_instructions.add - ( - BinarySearch.generate - ( - compiler.registers(), - compiler.assembler(), - element.get_value(), - new Size(collection_compiler.get_address()), - collection_compiler.get_address(), - was_found.get_address(), - index.get_address() - ) - ); - - result_as_computation = - new IfElseComputation - ( - was_found.get_value(), - Constant.ONE, - Constant.ZERO - ); - } - else - { - final Register result, element; - - result = reserve(Type.INT); - - result_as_address = result.get_address(); - result_as_computation = result.get_value(); - - element = reserve(element_compiler.get_computation().get_type()); - - init_instructions.add - ( - new SetValue - ( - element.get_address(), - element_compiler.get_computation() - ) - ); - init_instructions.add - ( - CountOccurrences.generate - ( - compiler.registers(), - compiler.assembler(), - element.get_value(), - new Size(collection_compiler.get_address()), - collection_compiler.get_address(), - result_as_address - ) - ); - } - } -*/ @Override public void visit_field_access @@ -614,3311 +485,614 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor } } -/* @Override - public void visit_is_member_operator + public void visit_newline ( - final tonkadur.fate.v1.lang.computation.IsMemberOperator n + final tonkadur.fate.v1.lang.computation.Newline n ) throws Throwable { - final Register result; - final ComputationCompiler collection_compiler, element_compiler; + result_as_computation = new Newline(); + } - collection_compiler = new ComputationCompiler(compiler); - element_compiler = new ComputationCompiler(compiler); + @Override + public void visit_extra_computation + ( + final tonkadur.fate.v1.lang.computation.ExtraComputationInstance n + ) + throws Throwable + { + final List parameters; - n.get_collection().get_visited_by(collection_compiler); - n.get_element().get_visited_by(element_compiler); + parameters = new ArrayList(); - collection_compiler.generate_address(); + for (final tonkadur.fate.v1.lang.meta.Computation p: n.get_parameters()) + { + final ComputationCompiler cc; - assimilate(collection_compiler); - assimilate(element_compiler); + cc = new ComputationCompiler(compiler); - result = reserve(Type.BOOL); - result_as_address = result.get_address(); - result_as_computation = result.get_value(); + p.get_visited_by(cc); - if - ( - ( - (tonkadur.fate.v1.lang.type.CollectionType) - n.get_collection().get_type() - ).is_set() - ) - { - final Register index, element; + assimilate(cc); - index = reserve(Type.INT); - element = reserve(element_compiler.get_computation().get_type()); + parameters.add(cc.get_computation()); + } - init_instructions.add - ( - new SetValue - ( - element.get_address(), - element_compiler.get_computation() - ) - ); - init_instructions.add + result_as_computation = + new ExtraComputation ( - BinarySearch.generate + TypeCompiler.compile ( - compiler.registers(), - compiler.assembler(), - element.get_value(), - new Size(collection_compiler.get_address()), - collection_compiler.get_address(), - result_as_address, - index.get_address() - ) + compiler, + n.get_computation_type().get_returned_type() + ), + n.get_computation_type().get_name(), + parameters ); + } - } - else - { - final Register index, element; + @Override + public void visit_default + ( + final tonkadur.fate.v1.lang.computation.Default n + ) + throws Throwable + { + final Register r; + + r = reserve(TypeCompiler.compile(compiler, n.get_type())); - index = reserve(Type.INT); - element = reserve(element_compiler.get_computation().get_type()); + init_instructions.add(new Initialize(r.get_address())); - init_instructions.add - ( - new SetValue - ( - element.get_address(), - element_compiler.get_computation() - ) - ); - init_instructions.add + result_as_address = r.get_address(); + result_as_computation = r.get_value(); + } + +/* + @Override + public void visit_access_pointer + ( + final tonkadur.fate.v1.lang.computation.AccessPointer n + ) + throws Throwable + { + final ComputationCompiler extra_address_cc, base_address_cc; + + base_address_cc = new ComputationCompiler(compiler); + extra_address_cc = new ComputationCompiler(compiler); + + n.get_parent().get_visited_by(base_address_cc); + n.get_index().get_visited_by(extra_address_cc); + + assimilate(base_address_cc); + assimilate(extra_address_cc); + + result_as_computation = + new RelativeAddress ( - IterativeSearch.generate + base_address_cc.get_address(), + new Cast(extra_address_cc.get_computation(), Type.STRING), + TypeCompiler.compile ( - compiler.registers(), - compiler.assembler(), - element.get_value(), - new Size(collection_compiler.get_address()), - collection_compiler.get_address(), - result_as_address, - index.get_address() + compiler, + ( + (tonkadur.fate.v1.lang.type.CollectionType) + n.get_parent().get_type() + ).get_content_type() ) ); - } } */ +/* @Override - public void visit_newline + public void visit_access_as_reference ( - final tonkadur.fate.v1.lang.computation.Newline n + final tonkadur.fate.v1.lang.computation.AccessAsReference n ) throws Throwable { - result_as_computation = new Newline(); + final ComputationCompiler extra_address_cc, base_address_cc; + + base_address_cc = new ComputationCompiler(compiler); + extra_address_cc = new ComputationCompiler(compiler); + + n.get_parent().get_visited_by(base_address_cc); + n.get_index().get_visited_by(extra_address_cc); + + assimilate(base_address_cc); + assimilate(extra_address_cc); + + result_as_address = + new RelativeAddress + ( + base_address_cc.get_address(), + new Cast(extra_address_cc.get_computation(), Type.STRING), + TypeCompiler.compile + ( + compiler, + ( + (tonkadur.fate.v1.lang.type.CollectionType) + n.get_parent().get_type() + ).get_content_type() + ) + ); } -/* +*/ @Override - public void visit_operation + public void visit_switch_value ( - final tonkadur.fate.v1.lang.computation.Operation n + final tonkadur.fate.v1.lang.computation.SwitchValue n ) throws Throwable { - final String fate_op_name; - final List operands; - - operands = new ArrayList(); + final ComputationCompiler target_cc, default_cc; + final List> cc_list; + boolean is_safe; - for (final tonkadur.fate.v1.lang.meta.Computation x: n.get_operands()) - { - final ComputationCompiler cc; + target_cc = new ComputationCompiler(compiler); + default_cc = new ComputationCompiler(compiler); - cc = new ComputationCompiler(compiler); + n.get_target().get_visited_by(target_cc); - x.get_visited_by(cc); + target_cc.generate_address(); - assimilate(cc); + assimilate(target_cc); - operands.add(cc.get_computation()); - } + cc_list = new ArrayList>(); + is_safe = true; - fate_op_name = n.get_operator().get_name(); + n.get_default().get_visited_by(default_cc); - if + for ( - fate_op_name.equals(tonkadur.fate.v1.lang.computation.Operator.PLUS.get_name()) + final Cons + < + tonkadur.fate.v1.lang.meta.Computation, + tonkadur.fate.v1.lang.meta.Computation + > + branch: + n.get_branches() ) { - final Iterator operands_it; + final ComputationCompiler candidate_cc, val_cc; - operands_it = operands.iterator(); + candidate_cc = new ComputationCompiler(compiler); + val_cc = new ComputationCompiler(compiler); - result_as_computation = operands_it.next(); + branch.get_car().get_visited_by(candidate_cc); + branch.get_cdr().get_visited_by(val_cc); - while (operands_it.hasNext()) - { - result_as_computation = - Operation.plus(operands_it.next(), result_as_computation); - } - } - else if - ( - fate_op_name.equals(tonkadur.fate.v1.lang.computation.Operator.MINUS.get_name()) - ) - { - final Iterator operands_it; - Computation sum; - - operands_it = operands.iterator(); - - result_as_computation = operands_it.next(); - sum = operands_it.next(); + is_safe = is_safe && !candidate_cc.has_init() && !val_cc.has_init(); - while (operands_it.hasNext()) - { - sum = Operation.plus(operands_it.next(), sum); - } + reserved_registers.addAll(candidate_cc.reserved_registers); + reserved_registers.addAll(val_cc.reserved_registers); - result_as_computation = Operation.minus(result_as_computation, sum); + cc_list.add(new Cons(candidate_cc, val_cc)); } - else if - ( - fate_op_name.equals(tonkadur.fate.v1.lang.computation.Operator.TIMES.get_name()) - ) - { - final Iterator operands_it; - operands_it = operands.iterator(); + is_safe = is_safe && !default_cc.has_init(); + reserved_registers.addAll(default_cc.reserved_registers); - result_as_computation = operands_it.next(); + Collections.reverse(cc_list); - while (operands_it.hasNext()) - { - result_as_computation = - Operation.times(operands_it.next(), result_as_computation); - } - } - else if - ( - fate_op_name.equals - ( - tonkadur.fate.v1.lang.computation.Operator.DIVIDE.get_name() - ) - ) - { - result_as_computation = - Operation.divide(operands.get(0), operands.get(1)); - } - else if - ( - fate_op_name.equals - ( - tonkadur.fate.v1.lang.computation.Operator.MIN.get_name() - ) - ) + if (is_safe) { - final Register candidate; + final Iterator> it; - candidate = reserve(operands.get(0).get_type()); - result_as_address = candidate.get_address(); - result_as_computation = candidate.get_value(); + it = cc_list.iterator(); - init_instructions.add - ( - new SetValue(result_as_address, operands.get(0)) - ); + result_as_computation = default_cc.get_computation(); - for (final Computation operand: operands) + while (it.hasNext()) { - init_instructions.add - ( - new SetValue + final Cons next; + + next = it.next(); + + result_as_computation = + new IfElseComputation ( - result_as_address, - new IfElseComputation + Operation.equals ( - Operation.less_than - ( - result_as_computation, - operand - ), - result_as_computation, - operand - ) - ) - ); + next.get_car().get_computation(), + target_cc.get_computation() + ), + next.get_cdr().get_computation(), + result_as_computation + ); } } - else if - ( - fate_op_name.equals - ( - tonkadur.fate.v1.lang.computation.Operator.MAX.get_name() - ) - ) + else { - final Register candidate; + final Iterator> it; + final Register result; + Cons next; + List new_value, new_cond; + Instruction prev_branch; - candidate = reserve(operands.get(0).get_type()); - result_as_address = candidate.get_address(); - result_as_computation = candidate.get_value(); + it = cc_list.iterator(); - init_instructions.add - ( - new SetValue(result_as_address, operands.get(0)) - ); - for (final Computation operand: operands) - { - init_instructions.add - ( - new SetValue - ( - result_as_address, - new IfElseComputation - ( - Operation.greater_than(result_as_computation, operand), - result_as_computation, - operand - ) - ) - ); - } - } - else if - ( - fate_op_name.equals - ( - tonkadur.fate.v1.lang.computation.Operator.ABS.get_name() - ) - ) - { - final Computation zero, minus_one; + new_value = new ArrayList(); - if (operands.get(0).get_type().equals(Type.INT)) - { - zero = Constant.ZERO; - minus_one = new Constant(Type.INT, "-1"); - } - else + if (default_cc.has_init()) { - zero = new Constant(Type.FLOAT, "0.0"); - minus_one = new Constant(Type.FLOAT, "-1.0"); + new_value.add(default_cc.get_init()); } - result_as_computation = - new IfElseComputation - ( - Operation.greater_than(zero, operands.get(0)), - Operation.times(minus_one, operands.get(0)), - operands.get(0) - ); - } - else if - ( - fate_op_name.equals - ( - tonkadur.fate.v1.lang.computation.Operator.CLAMP.get_name() - ) - ) - { - final Type t; - final Register candidate; - final Computation result_as_computation; - - t = operands.get(2).get_type(); - candidate = reserve(t); - result_as_address = candidate.get_address(); - result_as_computation = candidate.get_value(); + result = reserve(default_cc.get_computation().get_type()); + result_as_address = result.get_address(); - init_instructions.add - ( - new SetValue - ( - result_as_address, - new IfElseComputation - ( - Operation.greater_than(operands.get(2), operands.get(1)), - operands.get(1), - operands.get(2) - ) - ) - ); - init_instructions.add + new_value.add ( - new SetValue - ( - result_as_address, - new IfElseComputation - ( - Operation.less_than(result_as_computation, operands.get(0)), - operands.get(0), - result_as_computation - ) - ) + new SetValue(result_as_address, default_cc.get_computation()) ); - } - else if - ( - fate_op_name.equals - ( - tonkadur.fate.v1.lang.computation.Operator.MODULO.get_name() - ) - ) - { - result_as_computation = - Operation.modulo(operands.get(0), operands.get(1)); - } - else if - ( - fate_op_name.equals(tonkadur.fate.v1.lang.computation.Operator.POWER.get_name()) - ) - { - result_as_computation = - Operation.power(operands.get(0), operands.get(1)); - } - else if - ( - fate_op_name.equals - ( - tonkadur.fate.v1.lang.computation.Operator.RANDOM.get_name() - ) - ) - { - final List push_rand, pop_rand; - final Register result, zero_holder; - - push_rand = new ArrayList(); - pop_rand = new ArrayList(); - - result = reserve(Type.INT); - zero_holder = reserve(Type.INT); - result_as_computation = result.get_value(); - result_as_address = result.get_address(); + prev_branch = compiler.assembler().merge(new_value); - push_rand.add - ( - new SetValue - ( - result_as_address, - Operation.rand(operands.get(0), operands.get(1)) - ) - ); + while (it.hasNext()) + { + next = it.next(); - push_rand.add - ( - new SetValue - ( - new RelativeAddress - ( - compiler.registers().get_rand_value_holder().get_address(), - new Cast - ( - new Size - ( - compiler.registers().get_rand_value_holder - ( - ).get_address() - ), - Type.STRING - ), - Type.INT - ), - result_as_computation - ) - ); + new_value = new ArrayList(); + new_cond = new ArrayList(); - pop_rand.add - ( - new SetValue - ( - result_as_address, - new ValueOf - ( - new RelativeAddress - ( - compiler.registers().get_rand_value_holder().get_address(), - new Cast(Constant.ZERO, Type.STRING), - Type.INT - ) - ) - ) - ); + if (next.get_car().has_init()) + { + new_cond.add(next.get_car().get_init()); + } - pop_rand.add(new SetValue(zero_holder.get_address(), Constant.ZERO)); + if (next.get_cdr().has_init()) + { + new_value.add(next.get_cdr().get_init()); + } - pop_rand.add - ( - RemoveAt.generate + new_value.add ( - compiler.registers(), - compiler.assembler(), - zero_holder.get_address(), - new Size - ( - compiler.registers().get_rand_value_holder().get_address() - ), - compiler.registers().get_rand_value_holder().get_address() - ) - ); + new SetValue(result_as_address, next.get_cdr().get_computation()) + ); - init_instructions.add - ( - IfElse.generate + new_cond.add ( - compiler.registers(), - compiler.assembler(), - Operation.equals - ( - compiler.registers().get_rand_mode_holder().get_value(), - Constant.ZERO - ), - new SetValue - ( - result_as_address, - Operation.rand(operands.get(0), operands.get(1)) - ), IfElse.generate ( compiler.registers(), compiler.assembler(), Operation.equals ( - compiler.registers().get_rand_mode_holder().get_value(), - Constant.ONE + next.get_car().get_computation(), + target_cc.get_computation() ), - compiler.assembler().merge(push_rand), - compiler.assembler().merge(pop_rand) + compiler.assembler().merge(new_value), + prev_branch ) - ) - ); - } - else if - ( - fate_op_name.equals(tonkadur.fate.v1.lang.computation.Operator.AND.get_name()) - ) - { - final Iterator operands_it; + ); - operands_it = operands.iterator(); + prev_branch = compiler.assembler().merge(new_cond); + } - result_as_computation = operands_it.next(); + init_instructions.add(prev_branch); - while (operands_it.hasNext()) - { - result_as_computation = - Operation.and(operands_it.next(), result_as_computation); - } + result_as_computation = result.get_value(); } - else if - ( - fate_op_name.equals(tonkadur.fate.v1.lang.computation.Operator.OR.get_name()) - ) - { - final Iterator operands_it; + } - operands_it = operands.iterator(); + @Override + public void visit_paragraph + ( + final tonkadur.fate.v1.lang.computation.Paragraph n + ) + throws Throwable + { + final List content; - result_as_computation = operands_it.next(); + content = new ArrayList(); - while (operands_it.hasNext()) - { - result_as_computation = - Operation.or(operands_it.next(), result_as_computation); - } - } - else if - ( - fate_op_name.equals(tonkadur.fate.v1.lang.computation.Operator.NOT.get_name()) - ) - { - result_as_computation = Operation.not(operands.get(0)); - } - else if - ( - fate_op_name.equals - ( - tonkadur.fate.v1.lang.computation.Operator.IMPLIES.get_name() - ) - ) - { - result_as_computation = - Operation.implies(operands.get(0), operands.get(1)); - } - else if + for ( - fate_op_name.equals - ( - tonkadur.fate.v1.lang.computation.Operator.ONE_IN.get_name() - ) + final tonkadur.fate.v1.lang.meta.Computation fate_content: + n.get_content() ) { - final Iterator operand_it; + final ComputationCompiler content_cc; - operand_it = operands.iterator(); - - result_as_computation = - new IfElseComputation - ( - operand_it.next(), - Constant.ONE, - Constant.ZERO - ); - - while (operand_it.hasNext()) - { - result_as_computation = - Operation.plus - ( - new IfElseComputation - ( - operand_it.next(), - Constant.ONE, - Constant.ZERO - ), - result_as_computation - ); - } - - result_as_computation = - Operation.equals(result_as_computation, Constant.ONE); - } - else if - ( - fate_op_name.equals - ( - tonkadur.fate.v1.lang.computation.Operator.EQUALS.get_name() - ) - ) - { - final Iterator operands_it; - final Computation first_elem; - - operands_it = operands.iterator(); - - first_elem = operands_it.next(); - - result_as_computation = - Operation.equals(first_elem, operands_it.next()); - - while (operands_it.hasNext()) - { - result_as_computation = - Operation.and - ( - result_as_computation, - Operation.equals(first_elem, operands_it.next()) - ); - } - } - else if - ( - fate_op_name.equals - ( - tonkadur.fate.v1.lang.computation.Operator.LOWER_THAN.get_name() - ) - ) - { - result_as_computation = - Operation.less_than(operands.get(0), operands.get(1)); - } - else if - ( - fate_op_name.equals - ( - tonkadur.fate.v1.lang.computation.Operator.LOWER_EQUAL_THAN.get_name() - ) - ) - { - result_as_computation = - Operation.less_equal_than(operands.get(0), operands.get(1)); - } - else if - ( - fate_op_name.equals - ( - tonkadur.fate.v1.lang.computation.Operator.GREATER_EQUAL_THAN.get_name() - ) - ) - { - result_as_computation = - Operation.greater_equal_than(operands.get(0), operands.get(1)); - } - else if - ( - fate_op_name.equals - ( - tonkadur.fate.v1.lang.computation.Operator.GREATER_THAN.get_name() - ) - ) - { - result_as_computation = - Operation.greater_than(operands.get(0), operands.get(1)); - } - else - { - System.err.println("[P] Unknown Fate operator '" + fate_op_name+ "'."); - } - } -*/ -/* - @Override - public void visit_size_operator - ( - final tonkadur.fate.v1.lang.computation.SizeOperator n - ) - throws Throwable - { - final ComputationCompiler cc; - - cc = new ComputationCompiler(compiler); - - n.get_collection().get_visited_by(cc); - - assimilate(cc); - - result_as_computation = new Size(cc.get_address()); - } -*/ - @Override - public void visit_extra_computation - ( - final tonkadur.fate.v1.lang.computation.ExtraComputationInstance n - ) - throws Throwable - { - final List parameters; - - parameters = new ArrayList(); - - for (final tonkadur.fate.v1.lang.meta.Computation p: n.get_parameters()) - { - final ComputationCompiler cc; - - cc = new ComputationCompiler(compiler); - - p.get_visited_by(cc); - - assimilate(cc); - - parameters.add(cc.get_computation()); - } - - result_as_computation = - new ExtraComputation - ( - TypeCompiler.compile - ( - compiler, - n.get_computation_type().get_returned_type() - ), - n.get_computation_type().get_name(), - parameters - ); - } -/* - @Override - public void visit_is_empty - ( - final tonkadur.fate.v1.lang.computation.IsEmpty n - ) - throws Throwable - { - final ComputationCompiler cc; - - cc = new ComputationCompiler(compiler); - - n.get_collection().get_visited_by(cc); - - assimilate(cc); - - result_as_computation = - Operation.equals(new Size(cc.get_address()), Constant.ZERO); - } -*/ -/* - @Override - public void visit_index_of_operator - ( - final tonkadur.fate.v1.lang.computation.IndexOfOperator n - ) - throws Throwable - { - final ComputationCompiler elem_cc, collection_cc; - final Register result, result_found; - - result = reserve(Type.INT); - result_found = reserve(Type.BOOL); - - result_as_address = result.get_address(); - result_as_computation = result.get_value(); - - elem_cc = new ComputationCompiler(compiler); - collection_cc = new ComputationCompiler(compiler); - - n.get_element().get_visited_by(elem_cc); - n.get_collection().get_visited_by(collection_cc); - - assimilate(elem_cc); - assimilate(collection_cc); - - init_instructions.add - ( - IterativeSearch.generate - ( - compiler.registers(), - compiler.assembler(), - elem_cc.get_computation(), - new Size(collection_cc.get_address()), - collection_cc.get_address(), - result_found.get_address(), - result_as_address - ) - ); - } -*/ - @Override - public void visit_default - ( - final tonkadur.fate.v1.lang.computation.Default n - ) - throws Throwable - { - final Register r; - - r = reserve(TypeCompiler.compile(compiler, n.get_type())); - - init_instructions.add(new Initialize(r.get_address())); - - result_as_address = r.get_address(); - result_as_computation = r.get_value(); - } - -/* - @Override - public void visit_access_pointer - ( - final tonkadur.fate.v1.lang.computation.AccessPointer n - ) - throws Throwable - { - final ComputationCompiler extra_address_cc, base_address_cc; - - base_address_cc = new ComputationCompiler(compiler); - extra_address_cc = new ComputationCompiler(compiler); - - n.get_parent().get_visited_by(base_address_cc); - n.get_index().get_visited_by(extra_address_cc); - - assimilate(base_address_cc); - assimilate(extra_address_cc); - - result_as_computation = - new RelativeAddress - ( - base_address_cc.get_address(), - new Cast(extra_address_cc.get_computation(), Type.STRING), - TypeCompiler.compile - ( - compiler, - ( - (tonkadur.fate.v1.lang.type.CollectionType) - n.get_parent().get_type() - ).get_content_type() - ) - ); - } -*/ -/* - @Override - public void visit_access_as_reference - ( - final tonkadur.fate.v1.lang.computation.AccessAsReference n - ) - throws Throwable - { - final ComputationCompiler extra_address_cc, base_address_cc; - - base_address_cc = new ComputationCompiler(compiler); - extra_address_cc = new ComputationCompiler(compiler); - - n.get_parent().get_visited_by(base_address_cc); - n.get_index().get_visited_by(extra_address_cc); - - assimilate(base_address_cc); - assimilate(extra_address_cc); - - result_as_address = - new RelativeAddress - ( - base_address_cc.get_address(), - new Cast(extra_address_cc.get_computation(), Type.STRING), - TypeCompiler.compile - ( - compiler, - ( - (tonkadur.fate.v1.lang.type.CollectionType) - n.get_parent().get_type() - ).get_content_type() - ) - ); - } -*/ -/* - @Override - public void visit_access - ( - final tonkadur.fate.v1.lang.computation.Access n - ) - throws Throwable - { - final ComputationCompiler extra_address_cc, base_address_cc; - - base_address_cc = new ComputationCompiler(compiler); - extra_address_cc = new ComputationCompiler(compiler); - - n.get_parent().get_visited_by(base_address_cc); - n.get_index().get_visited_by(extra_address_cc); - - base_address_cc.generate_address(); - - assimilate(base_address_cc); - assimilate(extra_address_cc); - - result_as_address = - new RelativeAddress - ( - base_address_cc.get_address(), - new Cast(extra_address_cc.get_computation(), Type.STRING), - TypeCompiler.compile - ( - compiler, - ( - (tonkadur.fate.v1.lang.type.CollectionType) - n.get_parent().get_type() - ).get_content_type() - ) - ); - } -*/ - @Override - public void visit_switch_value - ( - final tonkadur.fate.v1.lang.computation.SwitchValue n - ) - throws Throwable - { - final ComputationCompiler target_cc, default_cc; - final List> cc_list; - boolean is_safe; - - target_cc = new ComputationCompiler(compiler); - default_cc = new ComputationCompiler(compiler); - - n.get_target().get_visited_by(target_cc); - - target_cc.generate_address(); - - assimilate(target_cc); - - cc_list = new ArrayList>(); - is_safe = true; - - n.get_default().get_visited_by(default_cc); - - for - ( - final Cons - < - tonkadur.fate.v1.lang.meta.Computation, - tonkadur.fate.v1.lang.meta.Computation - > - branch: - n.get_branches() - ) - { - final ComputationCompiler candidate_cc, val_cc; - - candidate_cc = new ComputationCompiler(compiler); - val_cc = new ComputationCompiler(compiler); - - branch.get_car().get_visited_by(candidate_cc); - branch.get_cdr().get_visited_by(val_cc); - - is_safe = is_safe && !candidate_cc.has_init() && !val_cc.has_init(); - - reserved_registers.addAll(candidate_cc.reserved_registers); - reserved_registers.addAll(val_cc.reserved_registers); - - cc_list.add(new Cons(candidate_cc, val_cc)); - } - - is_safe = is_safe && !default_cc.has_init(); - reserved_registers.addAll(default_cc.reserved_registers); - - Collections.reverse(cc_list); - - if (is_safe) - { - final Iterator> it; - - it = cc_list.iterator(); - - result_as_computation = default_cc.get_computation(); - - while (it.hasNext()) - { - final Cons next; - - next = it.next(); - - result_as_computation = - new IfElseComputation - ( - Operation.equals - ( - next.get_car().get_computation(), - target_cc.get_computation() - ), - next.get_cdr().get_computation(), - result_as_computation - ); - } - } - else - { - final Iterator> it; - final Register result; - Cons next; - List new_value, new_cond; - Instruction prev_branch; - - it = cc_list.iterator(); - - - new_value = new ArrayList(); - - if (default_cc.has_init()) - { - new_value.add(default_cc.get_init()); - } - - result = reserve(default_cc.get_computation().get_type()); - result_as_address = result.get_address(); - - new_value.add - ( - new SetValue(result_as_address, default_cc.get_computation()) - ); - - prev_branch = compiler.assembler().merge(new_value); - - while (it.hasNext()) - { - next = it.next(); - - new_value = new ArrayList(); - new_cond = new ArrayList(); - - if (next.get_car().has_init()) - { - new_cond.add(next.get_car().get_init()); - } - - if (next.get_cdr().has_init()) - { - new_value.add(next.get_cdr().get_init()); - } - - new_value.add - ( - new SetValue(result_as_address, next.get_cdr().get_computation()) - ); - - new_cond.add - ( - IfElse.generate - ( - compiler.registers(), - compiler.assembler(), - Operation.equals - ( - next.get_car().get_computation(), - target_cc.get_computation() - ), - compiler.assembler().merge(new_value), - prev_branch - ) - ); - - prev_branch = compiler.assembler().merge(new_cond); - } - - init_instructions.add(prev_branch); - - result_as_computation = result.get_value(); - } - } - - @Override - public void visit_paragraph - ( - final tonkadur.fate.v1.lang.computation.Paragraph n - ) - throws Throwable - { - final List content; - - content = new ArrayList(); - - for - ( - final tonkadur.fate.v1.lang.meta.Computation fate_content: - n.get_content() - ) - { - final ComputationCompiler content_cc; - - content_cc = new ComputationCompiler(compiler); - - fate_content.get_visited_by(content_cc); - - assimilate(content_cc); - - content.add(content_cc.get_computation()); - } - - result_as_computation = new Text(content); - } - - @Override - public void visit_text_with_effect - ( - final tonkadur.fate.v1.lang.computation.TextWithEffect n - ) - throws Throwable - { - final ComputationCompiler text_cc; - final List parameters; - - text_cc = new ComputationCompiler(compiler); - parameters = new ArrayList(); - - n.get_text().get_visited_by(text_cc); - - assimilate(text_cc); - - for - ( - final tonkadur.fate.v1.lang.meta.Computation fate_param: - n.get_parameters() - ) - { - final ComputationCompiler param_cc; - - param_cc = new ComputationCompiler(compiler); - - fate_param.get_visited_by(param_cc); - - assimilate(param_cc); - - parameters.add(param_cc.get_computation()); - } - - result_as_computation = - new AddTextEffect - ( - n.get_effect().get_name(), - parameters, - Collections.singletonList(text_cc.get_computation()) - ); - } - - @Override - public void visit_value_to_text - ( - final tonkadur.fate.v1.lang.computation.ValueToText n - ) - throws Throwable - { - final ComputationCompiler cc; - - cc = new ComputationCompiler(compiler); - - n.get_value().get_visited_by(cc); - - assimilate(cc); - - result_as_computation = - new Text(Collections.singletonList(cc.get_computation())); - } - - @Override - public void visit_lambda_expression - ( - final tonkadur.fate.v1.lang.computation.LambdaExpression n - ) - throws Throwable - { - final String out_label, in_label; - final List parameters; - final Register result_holder; - final ComputationCompiler expr_compiler; - final Type result_type; - final String context_name; - - out_label = compiler.assembler().generate_label(""); - in_label = compiler.assembler().generate_label(""); - - parameters = new ArrayList(); - - context_name = compiler.registers().create_stackable_context_name(); - - compiler.registers().create_stackable_context - ( - context_name, - init_instructions - ); - - init_instructions.add - ( - compiler.assembler().mark_after - ( - new SetPC(compiler.assembler().get_label_constant(out_label)), - in_label - ) - ); - - init_instructions.addAll - ( - compiler.registers().get_initialize_context_instructions(context_name) - ); - - compiler.registers().push_context(context_name); - - result_type = - TypeCompiler.compile(compiler, n.get_lambda_function().get_type()); - - result_holder = - /* Defining a context, don't use this.reserve(), otherwise this - * context-dependent variable will get released outside the context - * (where it doesn't exist) and that will cause an error. - */ - compiler.registers().reserve - ( - new PointerType(result_type), - init_instructions - ); - - parameters.add(result_holder); - - for (final tonkadur.fate.v1.lang.Variable param: n.get_parameters()) - { - final Register r; - - r = - /* Bound registers, can't use this.reserve() */ - compiler.registers().reserve - ( - TypeCompiler.compile(compiler, param.get_type()), - init_instructions - ); - - parameters.add(r); - - compiler.registers().bind(param.get_name(), r); - } - - init_instructions.addAll - ( - compiler.registers().read_parameters(parameters) - ); - - expr_compiler = new ComputationCompiler(compiler); - - n.get_lambda_function().get_visited_by(expr_compiler); - - /* Nope: its variables are removed when the context is left. */ - /* assimilate(expr_compiler); */ - if (expr_compiler.has_init()) - { - init_instructions.add(expr_compiler.get_init()); - } - - init_instructions.add - ( - new SetValue - ( - new Address(result_holder.get_value(), result_type), - expr_compiler.get_computation() - ) - ); -/* - * No need: it's getting finalized anyway. - for (final tonkadur.fate.v1.lang.Variable param: n.get_parameters()) - { - compiler.registers().unbind(param.get_name(), init_instructions); - } -*/ - init_instructions.addAll - ( - compiler.registers().get_finalize_context_instructions() - ); - - init_instructions.add - ( - compiler.assembler().mark_after - ( - compiler.assembler().merge - ( - compiler.registers().get_leave_context_instructions() - ), - out_label - ) - ); - - compiler.registers().pop_context(); - - result_as_computation = compiler.assembler().get_label_constant(in_label); - } -/* - @Override - public void visit_lambda_evaluation - ( - final tonkadur.fate.v1.lang.computation.LambdaEvaluation n - ) - throws Throwable - { - final ComputationCompiler target_line_cc; - final List parameters; - final Register result; - - parameters = new ArrayList(); - target_line_cc = new ComputationCompiler(compiler); - - n.get_lambda_function_reference().get_visited_by(target_line_cc); - - assimilate(target_line_cc); - - result = reserve(TypeCompiler.compile(compiler, n.get_type())); - - result_as_address = result.get_address(); - result_as_computation = result.get_value(); - - for - ( - final tonkadur.fate.v1.lang.meta.Computation param: n.get_parameters() - ) - { - final ComputationCompiler cc; - - cc = new ComputationCompiler(compiler); - - param.get_visited_by(cc); - - assimilate(cc); - - parameters.add(cc.get_computation()); - } - - init_instructions.add - ( - LambdaEvaluation.generate - ( - compiler.registers(), - compiler.assembler(), - target_line_cc.get_computation(), - result_as_address, - parameters - ) - ); - } -*/ - @Override - public void visit_generic_computation - ( - final tonkadur.fate.v1.lang.computation.GenericComputation n - ) - throws Throwable - { - final ComputationCompiler handler; - - handler = GenericComputationCompiler.handle(compiler, n); - - assimilate(handler); - - this.instructions_were_generated = handler.instructions_were_generated; - this.result_as_computation = handler.result_as_computation; - this.result_as_address = handler.result_as_address; - } - - @Override - public void visit_let - ( - final tonkadur.fate.v1.lang.computation.Let n - ) - throws Throwable - { - final Collection names; - - names = new ArrayList(); - - for - ( - final - Cons - < - tonkadur.fate.v1.lang.Variable, - tonkadur.fate.v1.lang.meta.Computation - > - a: - n.get_assignments() - ) - { - final String name; - final Register r; - final ComputationCompiler cc; - - name = a.get_car().get_name(); - r = - /* These are free by the unbind below */ - compiler.registers().reserve - ( - TypeCompiler.compile(compiler, a.get_car().get_type()), - init_instructions - ); - - compiler.registers().bind(name, r); - names.add(name); - - cc = new ComputationCompiler(compiler); - - a.get_cdr().get_visited_by(cc); - - assimilate(cc); - - init_instructions.add - ( - new SetValue(r.get_address(), cc.get_computation()) - ); - } - - n.get_computation().get_visited_by(this); - - for (final String name: names) - { - compiler.registers().unbind(name, init_instructions); - } - } - - @Override - public void visit_variable_reference - ( - final tonkadur.fate.v1.lang.computation.VariableReference n - ) - throws Throwable - { - final Register register; - - register = - compiler.registers().get_context_register(n.get_variable().get_name()); - - result_as_address = register.get_address(); - result_as_computation = register.get_value(); - } -/* - @Override - public void visit_car_cdr - ( - final tonkadur.fate.v1.lang.computation.CarCdr n - ) - throws Throwable - { - final ComputationCompiler address_cc; - - address_cc = new ComputationCompiler(compiler); - - n.get_parent().get_visited_by(address_cc); - - assimilate(address_cc); - - result_as_address = - new RelativeAddress - ( - address_cc.get_address(), - new Constant - ( - Type.STRING, - (n.is_car()? "0" : "1") - ), - TypeCompiler.compile(compiler, n.get_type()) - ); - } -*/ -/* - @Override - public void visit_cons - ( - final tonkadur.fate.v1.lang.computation.ConsComputation n - ) - throws Throwable - { - final ComputationCompiler car_compiler, cdr_compiler; - final Register result; - - result = reserve(DictType.WILD); - result_as_address = result.get_address(); - result_as_computation = result.get_value(); - - car_compiler = new ComputationCompiler(compiler); - - n.get_car().get_visited_by(car_compiler); - - if (car_compiler.has_init()) - { - init_instructions.add(car_compiler.get_init()); - } - - cdr_compiler = new ComputationCompiler(compiler); - - n.get_cdr().get_visited_by(cdr_compiler); - - if (cdr_compiler.has_init()) - { - init_instructions.add(cdr_compiler.get_init()); - } - - init_instructions.add - ( - CreateCons.generate - ( - compiler.registers(), - compiler.assembler(), - result_as_address, - car_compiler.get_computation(), - cdr_compiler.get_computation() - ) - ); - - car_compiler.release_registers(init_instructions); - cdr_compiler.release_registers(init_instructions); - } -*/ -/* - @Override - public void visit_add_element - ( - final tonkadur.fate.v1.lang.computation.AddElementComputation n - ) - throws Throwable - { - final ComputationCompiler address_compiler, element_compiler; - final Register result; - - result = reserve(TypeCompiler.compile(compiler, n.get_type())); - - result_as_address = result.get_address(); - result_as_computation = result.get_value(); - - address_compiler = new ComputationCompiler(compiler); - element_compiler = new ComputationCompiler(compiler); - - n.get_collection().get_visited_by(address_compiler); - - if (address_compiler.has_init()) - { - init_instructions.add(address_compiler.get_init()); - } - - init_instructions.add - ( - new SetValue(result_as_address, address_compiler.get_computation()) - ); - - address_compiler.release_registers(init_instructions); - - n.get_element().get_visited_by(element_compiler); - assimilate(element_compiler); - - init_instructions.add - ( - AddElement.generate - ( - compiler.registers(), - compiler.assembler(), - element_compiler.get_computation(), - result_as_address, - ( - (tonkadur.fate.v1.lang.type.CollectionType) - n.get_collection().get_type() - ).is_set() - ) - ); - } -*/ -/* - @Override - public void visit_add_element_at - ( - final tonkadur.fate.v1.lang.computation.AddElementAtComputation n - ) - throws Throwable - { - final ComputationCompiler address_compiler, index_compiler; - final ComputationCompiler element_compiler; - final Register result, index, collection_size; - - result = reserve(TypeCompiler.compile(compiler, n.get_type())); - result_as_address = result.get_address(); - result_as_computation = result.get_value(); - - address_compiler = new ComputationCompiler(compiler); - index_compiler = new ComputationCompiler(compiler); - element_compiler = new ComputationCompiler(compiler); - - n.get_collection().get_visited_by(address_compiler); - - if (address_compiler.has_init()) - { - init_instructions.add(address_compiler.get_init()); - } - - init_instructions.add - ( - new SetValue(result_as_address, address_compiler.get_computation()) - ); - - address_compiler.release_registers(init_instructions); - - n.get_index().get_visited_by(index_compiler); - - index_compiler.generate_address(); - assimilate(index_compiler); - - n.get_element().get_visited_by(element_compiler); - assimilate(element_compiler); - - collection_size = reserve(Type.INT); - index = reserve(Type.INT); - - init_instructions.add - ( - new SetValue - ( - collection_size.get_address(), - new Size(result_as_address) - ) - ); - - init_instructions.add - ( - new SetValue - ( - index.get_address(), - new IfElseComputation - ( - Operation.greater_than - ( - index_compiler.get_computation(), - collection_size.get_value() - ), - collection_size.get_value(), - new IfElseComputation - ( - Operation.less_than - ( - index_compiler.get_computation(), - Constant.ZERO - ), - Constant.ZERO, - index_compiler.get_computation() - ) - ) - ) - ); - - init_instructions.add - ( - InsertAt.generate - ( - compiler.registers(), - compiler.assembler(), - index.get_address(), - element_compiler.get_computation(), - collection_size.get_value(), - result_as_address - ) - ); - } -*/ -/* - @Override - public void visit_remove_elements_of - ( - final tonkadur.fate.v1.lang.computation.RemoveElementsOfComputation n - ) - throws Throwable - { - final ComputationCompiler collection_in_cc, collection_cc; - final Register result; - - result = reserve(TypeCompiler.compile(compiler, n.get_type())); - - result_as_address = result.get_address(); - result_as_computation = result.get_value(); - - collection_cc = new ComputationCompiler(compiler); - - n.get_target_collection().get_visited_by(collection_cc); - - if (collection_cc.has_init()) - { - init_instructions.add(collection_cc.get_init()); - } - - init_instructions.add - ( - new SetValue(result_as_address, collection_cc.get_computation()) - ); - - collection_cc.release_registers(init_instructions); - - collection_in_cc = new ComputationCompiler(compiler); - - n.get_source_collection().get_visited_by(collection_in_cc); - - assimilate(collection_in_cc); - - init_instructions.add - ( - RemoveElementsOf.generate - ( - compiler.registers(), - compiler.assembler(), - collection_in_cc.get_address(), - result_as_address, - ( - (tonkadur.fate.v1.lang.type.CollectionType) - n.get_target_collection().get_type() - ).is_set() - ) - ); - } -*/ -/* - @Override - public void visit_add_elements_of - ( - final tonkadur.fate.v1.lang.computation.AddElementsOfComputation n - ) - throws Throwable - { - final ComputationCompiler collection_in_cc, collection_cc; - final Register result; - - result = reserve(TypeCompiler.compile(compiler, n.get_type())); - - result_as_address = result.get_address(); - result_as_computation = result.get_value(); - - collection_cc = new ComputationCompiler(compiler); - - n.get_target_collection().get_visited_by(collection_cc); - - if (collection_cc.has_init()) - { - init_instructions.add(collection_cc.get_init()); - } - - init_instructions.add - ( - new SetValue(result_as_address, collection_cc.get_computation()) - ); - - collection_cc.release_registers(init_instructions); - - collection_in_cc = new ComputationCompiler(compiler); - - n.get_source_collection().get_visited_by(collection_in_cc); - - assimilate(collection_in_cc); - - init_instructions.add - ( - AddElementsOf.generate - ( - compiler.registers(), - compiler.assembler(), - collection_in_cc.get_address(), - result_as_address, - ( - (tonkadur.fate.v1.lang.type.CollectionType) - n.get_target_collection().get_type() - ).is_set() - ) - ); - } -*/ -/* - @Override - public void visit_fold - ( - final tonkadur.fate.v1.lang.computation.Fold n - ) - throws Throwable - { - final List params; - final ComputationCompiler lambda_cc, in_collection_cc, default_cc; - final Register result; - - result = reserve(TypeCompiler.compile(compiler, n.get_type())); - - result_as_address = result.get_address(); - result_as_computation = result.get_value(); - - params = new ArrayList(); - - for - ( - final tonkadur.fate.v1.lang.meta.Computation p: - n.get_extra_parameters() - ) - { - final ComputationCompiler param_cc; - - param_cc = new ComputationCompiler(compiler); - - p.get_visited_by(param_cc); - - // Let's not re-compute the parameters on every iteration. - param_cc.generate_address(); - - assimilate(param_cc); - - params.add(param_cc.get_computation()); - } - - default_cc = new ComputationCompiler(compiler); - - n.get_initial_value().get_visited_by(default_cc); - - if (default_cc.has_init()) - { - init_instructions.add(default_cc.get_init()); - } - - init_instructions.add - ( - new SetValue(result_as_address, default_cc.get_computation()) - ); - - default_cc.release_registers(init_instructions); - - lambda_cc = new ComputationCompiler(compiler); - - n.get_lambda_function().get_visited_by(lambda_cc); - - assimilate(lambda_cc); - - in_collection_cc = new ComputationCompiler(compiler); - - n.get_collection().get_visited_by(in_collection_cc); - - assimilate(in_collection_cc); - - init_instructions.add - ( - Fold.generate - ( - compiler.registers(), - compiler.assembler(), - lambda_cc.get_computation(), - result_as_address, - in_collection_cc.get_address(), - n.is_foldl(), - params - ) - ); - } -*/ -/* - @Override - public void visit_map - ( - final tonkadur.fate.v1.lang.computation.MapComputation n - ) - throws Throwable - { - final List params; - final ComputationCompiler lambda_cc, in_collection_cc; - final Register result; - - result = reserve(TypeCompiler.compile(compiler, n.get_type())); - - result_as_address = result.get_address(); - result_as_computation = result.get_value(); - - params = new ArrayList(); - - for - ( - final tonkadur.fate.v1.lang.meta.Computation p: - n.get_extra_parameters() - ) - { - final ComputationCompiler param_cc; - - param_cc = new ComputationCompiler(compiler); - - p.get_visited_by(param_cc); - - // Let's not re-compute the parameters on every iteration. - param_cc.generate_address(); - - assimilate(param_cc); - - params.add(param_cc.get_computation()); - } - - lambda_cc = new ComputationCompiler(compiler); - - n.get_lambda_function().get_visited_by(lambda_cc); - - assimilate(lambda_cc); - - in_collection_cc = new ComputationCompiler(compiler); - - n.get_collection().get_visited_by(in_collection_cc); - - assimilate(in_collection_cc); - - init_instructions.add - ( - MapLambda.generate - ( - compiler.registers(), - compiler.assembler(), - lambda_cc.get_computation(), - in_collection_cc.get_address(), - result_as_address, - ( - (tonkadur.fate.v1.lang.type.CollectionType) - n.get_collection().get_type() - ).is_set(), - params - ) - ); - } -*/ -/* - @Override - public void visit_range - ( - final tonkadur.fate.v1.lang.computation.Range n - ) - throws Throwable - { - final List while_body; - final ComputationCompiler start_cc, end_cc, inc_cc; - final Register result, iterator, accumulator; - final Address new_element_addr; - - while_body = new ArrayList(); - - result = reserve(MapType.MAP_TO_INT); - result_as_address = result.get_address(); - result_as_computation = result.get_value(); - - start_cc = new ComputationCompiler(compiler); - end_cc = new ComputationCompiler(compiler); - inc_cc = new ComputationCompiler(compiler); - - n.get_start().get_visited_by(start_cc); - start_cc.generate_address(); - - n.get_end().get_visited_by(end_cc); - end_cc.generate_address(); - - n.get_increment().get_visited_by(inc_cc); - inc_cc.generate_address(); - - assimilate(start_cc); - assimilate(end_cc); - assimilate(inc_cc); - - iterator = reserve(Type.INT); - accumulator = reserve(Type.INT); - - init_instructions.add - ( - new SetValue(iterator.get_address(), Constant.ZERO) - ); - init_instructions.add - ( - new SetValue(accumulator.get_address(), start_cc.get_computation()) - ); - - new_element_addr = - new RelativeAddress - ( - result_as_address, - new Cast(iterator.get_value(), Type.STRING), - Type.INT - ); - - while_body.add(new Initialize(new_element_addr)); - while_body.add - ( - new SetValue(new_element_addr, accumulator.get_value()) - ); - while_body.add - ( - new SetValue - ( - accumulator.get_address(), - Operation.plus(accumulator.get_value(), inc_cc.get_computation()) - ) - ); - while_body.add - ( - new SetValue - ( - iterator.get_address(), - Operation.plus(iterator.get_value(), Constant.ONE) - ) - ); - - init_instructions.add - ( - While.generate - ( - compiler.registers(), - compiler.assembler(), - Operation.or - ( - Operation.and - ( - Operation.greater_than - ( - inc_cc.get_computation(), - Constant.ZERO - ), - Operation.less_equal_than - ( - accumulator.get_value(), - end_cc.get_computation() - ) - ), - Operation.and - ( - Operation.less_equal_than - ( - inc_cc.get_computation(), - Constant.ZERO - ), - Operation.greater_equal_than - ( - accumulator.get_value(), - end_cc.get_computation() - ) - ) - ), - compiler.assembler.merge(while_body) - ) - ); - } -*/ -/* - @Override - public void visit_remove_all_of_element - ( - final tonkadur.fate.v1.lang.computation.RemoveAllOfElementComputation n - ) - throws Throwable - { - final ComputationCompiler address_compiler, target_compiler; - final Address collection_address; - final Register result; - - result = reserve(TypeCompiler.compile(compiler, n.get_type())); - result_as_address = result.get_address(); - result_as_computation = result.get_value(); - - address_compiler = new ComputationCompiler(compiler); - target_compiler = new ComputationCompiler(compiler); - - n.get_collection().get_visited_by(address_compiler); - - if (address_compiler.has_init()) - { - init_instructions.add(address_compiler.get_init()); - } - - init_instructions.add - ( - new SetValue(result_as_address, address_compiler.get_computation()) - ); - - address_compiler.release_registers(init_instructions); - - n.get_element().get_visited_by(target_compiler); - - assimilate(target_compiler); - - init_instructions.add - ( - RemoveAllOf.generate - ( - compiler.registers(), - compiler.assembler(), - target_compiler.get_computation(), - new Size(result_as_address), - result_as_address - ) - ); - } -*/ -/* - @Override - public void visit_remove_element_at - ( - final tonkadur.fate.v1.lang.computation.RemoveElementAtComputation n - ) - throws Throwable - { - final ComputationCompiler address_compiler, target_compiler; - final Address collection_address; - final Register result, target_index; - - result = reserve(TypeCompiler.compile(compiler, n.get_type())); - result_as_address = result.get_address(); - result_as_computation = result.get_value(); - - address_compiler = new ComputationCompiler(compiler); - target_compiler = new ComputationCompiler(compiler); - - n.get_collection().get_visited_by(address_compiler); - - if (address_compiler.has_init()) - { - init_instructions.add(address_compiler.get_init()); - } - - init_instructions.add - ( - new SetValue(result_as_address, address_compiler.get_computation()) - ); - - address_compiler.release_registers(init_instructions); - - n.get_index().get_visited_by(target_compiler); - - target_compiler.generate_address(); - - assimilate(target_compiler); - - init_instructions.add - ( - RemoveAt.generate - ( - compiler.registers(), - compiler.assembler(), - target_compiler.get_address(), - new Size(result_as_address), - result_as_address - ) - ); - } -*/ -/* - @Override - public void visit_remove_element - ( - final tonkadur.fate.v1.lang.computation.RemoveElementComputation n - ) - throws Throwable - { - final ComputationCompiler elem_cc, collection_cc; - final Register result; - - elem_cc = new ComputationCompiler(compiler); - collection_cc = new ComputationCompiler(compiler); - - result = reserve(TypeCompiler.compile(compiler, n.get_type())); - result_as_address = result.get_address(); - result_as_computation = result.get_value(); - - n.get_collection().get_visited_by(collection_cc); - - if (collection_cc.has_init()) - { - init_instructions.add(collection_cc.get_init()); - } - - init_instructions.add - ( - new SetValue(result_as_address, collection_cc.get_computation()) - ); - - collection_cc.release_registers(init_instructions); - - n.get_element().get_visited_by(elem_cc); - - elem_cc.generate_address(); - - assimilate(elem_cc); - - init_instructions.add - ( - RemoveOneOf.generate - ( - compiler.registers(), - compiler.assembler(), - elem_cc.get_computation(), - result_as_address, - ( - (tonkadur.fate.v1.lang.type.CollectionType) - n.get_collection().get_type() - ).is_set() - ) - ); - } -*/ -/* - @Override - public void visit_reverse_list - ( - final tonkadur.fate.v1.lang.computation.ReverseListComputation n - ) - throws Throwable - { - final ComputationCompiler address_compiler; - final Register result; - - result = reserve(TypeCompiler.compile(compiler, n.get_type())); - result_as_address = result.get_address(); - result_as_computation = result.get_value(); - - address_compiler = new ComputationCompiler(compiler); - - n.get_collection().get_visited_by(address_compiler); - - if (address_compiler.has_init()) - { - init_instructions.add(address_compiler.get_init()); - } - - init_instructions.add - ( - new SetValue(result_as_address, address_compiler.get_computation()) - ); - - address_compiler.release_registers(init_instructions); - - init_instructions.add - ( - ReverseList.generate - ( - compiler.registers(), - compiler.assembler(), - new Size(result_as_address), - result_as_address - ) - ); - } -*/ -/* - @Override - public void visit_shuffle - ( - final tonkadur.fate.v1.lang.computation.ShuffleComputation n - ) - throws Throwable - { - final ComputationCompiler address_compiler; - final Register result; - - result = reserve(TypeCompiler.compile(compiler, n.get_type())); - result_as_address = result.get_address(); - result_as_computation = result.get_value(); - - address_compiler = new ComputationCompiler(compiler); - - n.get_collection().get_visited_by(address_compiler); - - if (address_compiler.has_init()) - { - init_instructions.add(address_compiler.get_init()); - } - - init_instructions.add - ( - new SetValue(result_as_address, address_compiler.get_computation()) - ); - - address_compiler.release_registers(init_instructions); - - init_instructions.add - ( - Shuffle.generate - ( - compiler.registers(), - compiler.assembler(), - result_as_address - ) - ); - } -*/ -/* - private void visit_merge_with_defaults - ( - final tonkadur.fate.v1.lang.computation.MergeComputation n - ) - throws Throwable - { - final List params; - final ComputationCompiler lambda_cc, default_a_cc, default_b_cc; - final ComputationCompiler in_collection_a_cc, in_collection_b_cc; - final Register result; - - result = reserve(TypeCompiler.compile(compiler, n.get_type())); - - result_as_address = result.get_address(); - result_as_computation = result.get_value(); - - params = new ArrayList(); - - for - ( - final tonkadur.fate.v1.lang.meta.Computation p: - n.get_extra_parameters() - ) - { - final ComputationCompiler param_cc; - - param_cc = new ComputationCompiler(compiler); - - p.get_visited_by(param_cc); - - // Let's not re-compute the parameters on every iteration. - param_cc.generate_address(); - - assimilate(param_cc); - - params.add(param_cc.get_computation()); - } - - lambda_cc = new ComputationCompiler(compiler); - - n.get_lambda_function().get_visited_by(lambda_cc); - - assimilate(lambda_cc); - - default_a_cc = new ComputationCompiler(compiler); - - n.get_default_a().get_visited_by(default_a_cc); - - default_a_cc.generate_address(); - - assimilate(default_a_cc); - - default_b_cc = new ComputationCompiler(compiler); - - n.get_default_b().get_visited_by(default_b_cc); - - default_b_cc.generate_address(); - - assimilate(default_b_cc); - - in_collection_a_cc = new ComputationCompiler(compiler); - - n.get_collection_in_a().get_visited_by(in_collection_a_cc); - - assimilate(in_collection_a_cc); - - in_collection_b_cc = new ComputationCompiler(compiler); - - n.get_collection_in_b().get_visited_by(in_collection_b_cc); - - assimilate(in_collection_b_cc); - - init_instructions.add - ( - MergeLambda.generate - ( - compiler.registers(), - compiler.assembler(), - lambda_cc.get_computation(), - default_a_cc.get_computation(), - in_collection_a_cc.get_address(), - default_b_cc.get_computation(), - in_collection_b_cc.get_address(), - result_as_address, - n.to_set(), - params - ) - ); - } -*/ -/* - @Override - public void visit_merge - ( - final tonkadur.fate.v1.lang.computation.MergeComputation n - ) - throws Throwable - { - if (n.get_default_a() != null) - { - visit_merge_with_defaults(n); - - return; - } - - final List params; - final ComputationCompiler lambda_cc; - final ComputationCompiler in_collection_a_cc, in_collection_b_cc; - final Register result; - - result = reserve(TypeCompiler.compile(compiler, n.get_type())); - - result_as_address = result.get_address(); - result_as_computation = result.get_value(); - - params = new ArrayList(); - - for - ( - final tonkadur.fate.v1.lang.meta.Computation p: - n.get_extra_parameters() - ) - { - final ComputationCompiler param_cc; - - param_cc = new ComputationCompiler(compiler); - - p.get_visited_by(param_cc); - - // Let's not re-compute the parameters on every iteration. - param_cc.generate_address(); - - assimilate(param_cc); - - params.add(param_cc.get_computation()); - } - - lambda_cc = new ComputationCompiler(compiler); - - n.get_lambda_function().get_visited_by(lambda_cc); - - assimilate(lambda_cc); - - in_collection_a_cc = new ComputationCompiler(compiler); - - n.get_collection_in_a().get_visited_by(in_collection_a_cc); - - assimilate(in_collection_a_cc); - - in_collection_b_cc = new ComputationCompiler(compiler); - - n.get_collection_in_b().get_visited_by(in_collection_b_cc); - - assimilate(in_collection_b_cc); - - init_instructions.add - ( - MergeLambda.generate - ( - compiler.registers(), - compiler.assembler(), - lambda_cc.get_computation(), - in_collection_a_cc.get_address(), - in_collection_b_cc.get_address(), - result_as_address, - n.to_set(), - params - ) - ); - } -*/ -/* - private void visit_indexed_merge_with_defaults - ( - final tonkadur.fate.v1.lang.computation.IndexedMergeComputation n - ) - throws Throwable - { - final List params; - final ComputationCompiler lambda_cc, default_a_cc, default_b_cc; - final ComputationCompiler in_collection_a_cc, in_collection_b_cc; - final Register result; - - result = reserve(TypeCompiler.compile(compiler, n.get_type())); - - result_as_address = result.get_address(); - result_as_computation = result.get_value(); - - params = new ArrayList(); - - for - ( - final tonkadur.fate.v1.lang.meta.Computation p: - n.get_extra_parameters() - ) - { - final ComputationCompiler param_cc; - - param_cc = new ComputationCompiler(compiler); - - p.get_visited_by(param_cc); - - // Let's not re-compute the parameters on every iteration. - param_cc.generate_address(); - - assimilate(param_cc); - - params.add(param_cc.get_computation()); - } - - lambda_cc = new ComputationCompiler(compiler); - - n.get_lambda_function().get_visited_by(lambda_cc); - - assimilate(lambda_cc); - - default_a_cc = new ComputationCompiler(compiler); - - n.get_default_a().get_visited_by(default_a_cc); - - default_a_cc.generate_address(); - - assimilate(default_a_cc); - - default_b_cc = new ComputationCompiler(compiler); - - n.get_default_b().get_visited_by(default_b_cc); - - default_b_cc.generate_address(); - - assimilate(default_b_cc); - - in_collection_a_cc = new ComputationCompiler(compiler); - - n.get_collection_in_a().get_visited_by(in_collection_a_cc); - - assimilate(in_collection_a_cc); - - in_collection_b_cc = new ComputationCompiler(compiler); - - n.get_collection_in_b().get_visited_by(in_collection_b_cc); - - assimilate(in_collection_b_cc); - - init_instructions.add - ( - IndexedMergeLambda.generate - ( - compiler.registers(), - compiler.assembler(), - lambda_cc.get_computation(), - default_a_cc.get_computation(), - in_collection_a_cc.get_address(), - default_b_cc.get_computation(), - in_collection_b_cc.get_address(), - result_as_address, - n.to_set(), - params - ) - ); - } -*/ -/* - @Override - public void visit_indexed_merge - ( - final tonkadur.fate.v1.lang.computation.IndexedMergeComputation n - ) - throws Throwable - { - if (n.get_default_a() != null) - { - visit_indexed_merge_with_defaults(n); - - return; - } - - final List params; - final ComputationCompiler lambda_cc; - final ComputationCompiler in_collection_a_cc, in_collection_b_cc; - final Register result; - - result = reserve(TypeCompiler.compile(compiler, n.get_type())); - - result_as_address = result.get_address(); - result_as_computation = result.get_value(); - - params = new ArrayList(); - - for - ( - final tonkadur.fate.v1.lang.meta.Computation p: - n.get_extra_parameters() - ) - { - final ComputationCompiler param_cc; - - param_cc = new ComputationCompiler(compiler); - - p.get_visited_by(param_cc); - - // Let's not re-compute the parameters on every iteration. - param_cc.generate_address(); - - assimilate(param_cc); - - params.add(param_cc.get_computation()); - } - - lambda_cc = new ComputationCompiler(compiler); - - n.get_lambda_function().get_visited_by(lambda_cc); - - assimilate(lambda_cc); - - in_collection_a_cc = new ComputationCompiler(compiler); - - n.get_collection_in_a().get_visited_by(in_collection_a_cc); - - assimilate(in_collection_a_cc); - - in_collection_b_cc = new ComputationCompiler(compiler); - - n.get_collection_in_b().get_visited_by(in_collection_b_cc); - - assimilate(in_collection_b_cc); - - init_instructions.add - ( - IndexedMergeLambda.generate - ( - compiler.registers(), - compiler.assembler(), - lambda_cc.get_computation(), - in_collection_a_cc.get_address(), - in_collection_b_cc.get_address(), - result_as_address, - n.to_set(), - params - ) - ); - } -*/ -/* - @Override - public void visit_sublist - ( - final tonkadur.fate.v1.lang.computation.SubListComputation n - ) - throws Throwable - { - final ComputationCompiler address_compiler, start_compiler, end_compiler; - final Register result; - - result = reserve(TypeCompiler.compile(compiler, n.get_type())); - result_as_address = result.get_address(); - result_as_computation = result.get_value(); - - address_compiler = new ComputationCompiler(compiler); - start_compiler = new ComputationCompiler(compiler); - end_compiler = new ComputationCompiler(compiler); - - n.get_collection().get_visited_by(address_compiler); - - if (address_compiler.has_init()) - { - init_instructions.add(address_compiler.get_init()); - } - - n.get_start_index().get_visited_by(start_compiler); - - if (start_compiler.has_init()) - { - init_instructions.add(start_compiler.get_init()); - } - - n.get_end_index().get_visited_by(end_compiler); - - if (end_compiler.has_init()) - { - init_instructions.add(end_compiler.get_init()); - } - - init_instructions.add - ( - SubList.generate - ( - compiler.registers(), - compiler.assembler(), - start_compiler.get_computation(), - end_compiler.get_computation(), - address_compiler.get_address(), - result_as_address - ) - ); - - start_compiler.release_registers(init_instructions); - end_compiler.release_registers(init_instructions); - address_compiler.release_registers(init_instructions); - } -*/ -/* - @Override - public void visit_partition - ( - final tonkadur.fate.v1.lang.computation.PartitionComputation n - ) - throws Throwable - { - final List params; - final ComputationCompiler lambda_cc, in_collection_cc; - final Address car_addr, cdr_addr; - final Register result; - - result = reserve(DictType.WILD); - - result_as_address = result.get_address(); - result_as_computation = result.get_value(); - - params = new ArrayList(); - - for - ( - final tonkadur.fate.v1.lang.meta.Computation p: - n.get_extra_parameters() - ) - { - final ComputationCompiler param_cc; - - param_cc = new ComputationCompiler(compiler); - - p.get_visited_by(param_cc); - - // Let's not re-compute the parameters on every iteration. - param_cc.generate_address(); - - assimilate(param_cc); - - params.add(param_cc.get_computation()); - } - - lambda_cc = new ComputationCompiler(compiler); - - n.get_lambda_function().get_visited_by(lambda_cc); - - assimilate(lambda_cc); - - in_collection_cc = new ComputationCompiler(compiler); - - n.get_collection().get_visited_by(in_collection_cc); - - if (in_collection_cc.has_init()) - { - init_instructions.add(in_collection_cc.get_init()); - } - - car_addr = - new RelativeAddress - ( - result_as_address, - new Constant(Type.STRING, "0"), - in_collection_cc.get_computation().get_type() - ); - - init_instructions.add - ( - new SetValue - ( - car_addr, - in_collection_cc.get_computation() - ) - ); - - cdr_addr = - new RelativeAddress - ( - result_as_address, - new Constant(Type.STRING, "1"), - in_collection_cc.get_computation().get_type() - ); - - in_collection_cc.release_registers(init_instructions); - - init_instructions.add(new Initialize(cdr_addr)); - - init_instructions.add - ( - PartitionLambda.generate - ( - compiler.registers(), - compiler.assembler(), - lambda_cc.get_computation(), - car_addr, - cdr_addr, - ( - (tonkadur.fate.v1.lang.type.CollectionType) - n.get_collection().get_type() - ).is_set(), - params - ) - ); - } -*/ -/* - @Override - public void visit_indexed_partition - ( - final tonkadur.fate.v1.lang.computation.IndexedPartitionComputation n - ) - throws Throwable - { - final List params; - final ComputationCompiler lambda_cc, in_collection_cc; - final Address car_addr, cdr_addr; - final Register result; - - result = reserve(DictType.WILD); - - result_as_address = result.get_address(); - result_as_computation = result.get_value(); - - params = new ArrayList(); - - for - ( - final tonkadur.fate.v1.lang.meta.Computation p: - n.get_extra_parameters() - ) - { - final ComputationCompiler param_cc; - - param_cc = new ComputationCompiler(compiler); - - p.get_visited_by(param_cc); - - // Let's not re-compute the parameters on every iteration. - param_cc.generate_address(); - - assimilate(param_cc); - - params.add(param_cc.get_computation()); - } - - lambda_cc = new ComputationCompiler(compiler); - - n.get_lambda_function().get_visited_by(lambda_cc); - - assimilate(lambda_cc); - - in_collection_cc = new ComputationCompiler(compiler); - - n.get_collection().get_visited_by(in_collection_cc); - - if (in_collection_cc.has_init()) - { - init_instructions.add(in_collection_cc.get_init()); - } - - car_addr = - new RelativeAddress - ( - result_as_address, - new Constant(Type.STRING, "0"), - in_collection_cc.get_computation().get_type() - ); - - init_instructions.add - ( - new SetValue - ( - car_addr, - in_collection_cc.get_computation() - ) - ); - - cdr_addr = - new RelativeAddress - ( - result_as_address, - new Constant(Type.STRING, "1"), - in_collection_cc.get_computation().get_type() - ); - - in_collection_cc.release_registers(init_instructions); - - init_instructions.add(new Initialize(cdr_addr)); - - init_instructions.add - ( - IndexedPartitionLambda.generate - ( - compiler.registers(), - compiler.assembler(), - lambda_cc.get_computation(), - car_addr, - cdr_addr, - ( - (tonkadur.fate.v1.lang.type.CollectionType) - n.get_collection().get_type() - ).is_set(), - params - ) - ); - } -*/ -/* - @Override - public void visit_sort - ( - final tonkadur.fate.v1.lang.computation.SortComputation n - ) - throws Throwable - { - final List params; - final ComputationCompiler lambda_cc, in_collection_cc; - final Register result; - - result = reserve(TypeCompiler.compile(compiler, n.get_type())); - - result_as_address = result.get_address(); - result_as_computation = result.get_value(); - - params = new ArrayList(); - - for - ( - final tonkadur.fate.v1.lang.meta.Computation p: - n.get_extra_parameters() - ) - { - final ComputationCompiler param_cc; - - param_cc = new ComputationCompiler(compiler); - - p.get_visited_by(param_cc); - - // Let's not re-compute the parameters on every iteration. - param_cc.generate_address(); - - assimilate(param_cc); - - params.add(param_cc.get_computation()); - } - - lambda_cc = new ComputationCompiler(compiler); - - n.get_lambda_function().get_visited_by(lambda_cc); - - assimilate(lambda_cc); - - in_collection_cc = new ComputationCompiler(compiler); - - n.get_collection().get_visited_by(in_collection_cc); - - if (in_collection_cc.has_init()) - { - init_instructions.add(in_collection_cc.get_init()); - } - - init_instructions.add - ( - Sort.generate - ( - compiler.registers(), - compiler.assembler(), - lambda_cc.get_computation(), - in_collection_cc.get_address(), - result_as_address, - params - ) - ); - - in_collection_cc.release_registers(init_instructions); - } -*/ - -/* - @Override - public void visit_filter - ( - final tonkadur.fate.v1.lang.computation.FilterComputation n - ) - throws Throwable - { - final List params; - final ComputationCompiler lambda_cc, in_collection_cc; - final Register result; - - result = reserve(TypeCompiler.compile(compiler, n.get_type())); - - result_as_address = result.get_address(); - result_as_computation = result.get_value(); - - params = new ArrayList(); - - for - ( - final tonkadur.fate.v1.lang.meta.Computation p: - n.get_extra_parameters() - ) - { - final ComputationCompiler param_cc; - - param_cc = new ComputationCompiler(compiler); - - p.get_visited_by(param_cc); - - // Let's not re-compute the parameters on every iteration. - param_cc.generate_address(); - - assimilate(param_cc); - - params.add(param_cc.get_computation()); - } - - lambda_cc = new ComputationCompiler(compiler); - - n.get_lambda_function().get_visited_by(lambda_cc); - - assimilate(lambda_cc); - - in_collection_cc = new ComputationCompiler(compiler); - - n.get_collection().get_visited_by(in_collection_cc); - - if (in_collection_cc.has_init()) - { - init_instructions.add(in_collection_cc.get_init()); - } - - init_instructions.add - ( - new SetValue(result_as_address, in_collection_cc.get_computation()) - ); - - in_collection_cc.release_registers(init_instructions); - - init_instructions.add - ( - FilterLambda.generate - ( - compiler.registers(), - compiler.assembler(), - lambda_cc.get_computation(), - result_as_address, - params - ) - ); - } -*/ -/* - @Override - public void visit_indexed_filter - ( - final tonkadur.fate.v1.lang.computation.IndexedFilterComputation n - ) - throws Throwable - { - final List params; - final ComputationCompiler lambda_cc, in_collection_cc; - final Register result; - - result = reserve(TypeCompiler.compile(compiler, n.get_type())); - - result_as_address = result.get_address(); - result_as_computation = result.get_value(); - - params = new ArrayList(); - - for - ( - final tonkadur.fate.v1.lang.meta.Computation p: - n.get_extra_parameters() - ) - { - final ComputationCompiler param_cc; - - param_cc = new ComputationCompiler(compiler); - - p.get_visited_by(param_cc); - - // Let's not re-compute the parameters on every iteration. - param_cc.generate_address(); - - assimilate(param_cc); - - params.add(param_cc.get_computation()); - } - - lambda_cc = new ComputationCompiler(compiler); - - n.get_lambda_function().get_visited_by(lambda_cc); - - assimilate(lambda_cc); + content_cc = new ComputationCompiler(compiler); - in_collection_cc = new ComputationCompiler(compiler); + fate_content.get_visited_by(content_cc); - n.get_collection().get_visited_by(in_collection_cc); + assimilate(content_cc); - if (in_collection_cc.has_init()) - { - init_instructions.add(in_collection_cc.get_init()); + content.add(content_cc.get_computation()); } - init_instructions.add - ( - new SetValue(result_as_address, in_collection_cc.get_computation()) - ); - - in_collection_cc.release_registers(init_instructions); - - init_instructions.add - ( - IndexedFilterLambda.generate - ( - compiler.registers(), - compiler.assembler(), - lambda_cc.get_computation(), - result_as_address, - params - ) - ); - } -*/ -/* + result_as_computation = new Text(content); + } + @Override - public void visit_indexed_map + public void visit_text_with_effect ( - final tonkadur.fate.v1.lang.computation.IndexedMapComputation n + final tonkadur.fate.v1.lang.computation.TextWithEffect n ) throws Throwable { - final List params; - final ComputationCompiler lambda_cc, in_collection_cc; - final Register result; + final ComputationCompiler text_cc; + final List parameters; - result = reserve(TypeCompiler.compile(compiler, n.get_type())); + text_cc = new ComputationCompiler(compiler); + parameters = new ArrayList(); - result_as_address = result.get_address(); - result_as_computation = result.get_value(); + n.get_text().get_visited_by(text_cc); - params = new ArrayList(); + assimilate(text_cc); for ( - final tonkadur.fate.v1.lang.meta.Computation p: - n.get_extra_parameters() + final tonkadur.fate.v1.lang.meta.Computation fate_param: + n.get_parameters() ) { final ComputationCompiler param_cc; param_cc = new ComputationCompiler(compiler); - p.get_visited_by(param_cc); - - // Let's not re-compute the parameters on every iteration. - param_cc.generate_address(); + fate_param.get_visited_by(param_cc); assimilate(param_cc); - params.add(param_cc.get_computation()); + parameters.add(param_cc.get_computation()); } - lambda_cc = new ComputationCompiler(compiler); - - n.get_lambda_function().get_visited_by(lambda_cc); + result_as_computation = + new AddTextEffect + ( + n.get_effect().get_name(), + parameters, + Collections.singletonList(text_cc.get_computation()) + ); + } - assimilate(lambda_cc); + @Override + public void visit_value_to_text + ( + final tonkadur.fate.v1.lang.computation.ValueToText n + ) + throws Throwable + { + final ComputationCompiler cc; - in_collection_cc = new ComputationCompiler(compiler); + cc = new ComputationCompiler(compiler); - n.get_collection().get_visited_by(in_collection_cc); + n.get_value().get_visited_by(cc); - assimilate(in_collection_cc); + assimilate(cc); - init_instructions.add - ( - IndexedMapLambda.generate - ( - compiler.registers(), - compiler.assembler(), - lambda_cc.get_computation(), - in_collection_cc.get_address(), - result_as_address, - ( - (tonkadur.fate.v1.lang.type.CollectionType) - n.get_collection().get_type() - ).is_set(), - params - ) - ); + result_as_computation = + new Text(Collections.singletonList(cc.get_computation())); } -*/ -/* + @Override - public void visit_push_element + public void visit_lambda_expression ( - final tonkadur.fate.v1.lang.computation.PushElementComputation n + final tonkadur.fate.v1.lang.computation.LambdaExpression n ) throws Throwable { - final ComputationCompiler address_compiler, element_compiler; - final Register result, collection_size, index; + final String out_label, in_label; + final List parameters; + final Register result_holder; + final ComputationCompiler expr_compiler; + final Type result_type; + final String context_name; - result = reserve(TypeCompiler.compile(compiler, n.get_type())); - result_as_address = result.get_address(); - result_as_computation = result.get_value(); + out_label = compiler.assembler().generate_label(""); + in_label = compiler.assembler().generate_label(""); - address_compiler = new ComputationCompiler(compiler); - element_compiler = new ComputationCompiler(compiler); + parameters = new ArrayList(); - n.get_collection().get_visited_by(address_compiler); + context_name = compiler.registers().create_stackable_context_name(); - if (address_compiler.has_init()) - { - init_instructions.add(address_compiler.get_init()); - } + compiler.registers().create_stackable_context + ( + context_name, + init_instructions + ); init_instructions.add ( - new SetValue(result_as_address, address_compiler.get_computation()) + compiler.assembler().mark_after + ( + new SetPC(compiler.assembler().get_label_constant(out_label)), + in_label + ) + ); + + init_instructions.addAll + ( + compiler.registers().get_initialize_context_instructions(context_name) ); - address_compiler.release_registers(init_instructions); + compiler.registers().push_context(context_name); + + result_type = + TypeCompiler.compile(compiler, n.get_lambda_function().get_type()); + + result_holder = + /* Defining a context, don't use this.reserve(), otherwise this + * context-dependent variable will get released outside the context + * (where it doesn't exist) and that will cause an error. + */ + compiler.registers().reserve + ( + new PointerType(result_type), + init_instructions + ); - n.get_element().get_visited_by(element_compiler); + parameters.add(result_holder); - if (element_compiler.has_init()) + for (final tonkadur.fate.v1.lang.Variable param: n.get_parameters()) { - init_instructions.add(element_compiler.get_init()); - } + final Register r; + + r = + /* Bound registers, can't use this.reserve() */ + compiler.registers().reserve + ( + TypeCompiler.compile(compiler, param.get_type()), + init_instructions + ); + + parameters.add(r); - collection_size = reserve(Type.INT); - index = reserve(Type.INT); + compiler.registers().bind(param.get_name(), r); + } - init_instructions.add + init_instructions.addAll ( - new SetValue - ( - collection_size.get_address(), - new Size(result_as_address) - ) + compiler.registers().read_parameters(parameters) ); + expr_compiler = new ComputationCompiler(compiler); + + n.get_lambda_function().get_visited_by(expr_compiler); + + /* Nope: its variables are removed when the context is left. */ + /* assimilate(expr_compiler); */ + if (expr_compiler.has_init()) + { + init_instructions.add(expr_compiler.get_init()); + } + init_instructions.add ( new SetValue ( - index.get_address(), - (n.is_from_left() ? Constant.ZERO : collection_size.get_value()) + new Address(result_holder.get_value(), result_type), + expr_compiler.get_computation() ) ); +/* + * No need: it's getting finalized anyway. + for (final tonkadur.fate.v1.lang.Variable param: n.get_parameters()) + { + compiler.registers().unbind(param.get_name(), init_instructions); + } +*/ + init_instructions.addAll + ( + compiler.registers().get_finalize_context_instructions() + ); init_instructions.add ( - InsertAt.generate + compiler.assembler().mark_after ( - compiler.registers(), - compiler.assembler(), - index.get_address(), - element_compiler.get_computation(), - collection_size.get_value(), - result_as_address + compiler.assembler().merge + ( + compiler.registers().get_leave_context_instructions() + ), + out_label ) ); - element_compiler.release_registers(init_instructions); + compiler.registers().pop_context(); + + result_as_computation = compiler.assembler().get_label_constant(in_label); } -*/ -/* + @Override - public void visit_pop_element + public void visit_generic_computation ( - final tonkadur.fate.v1.lang.computation.PopElementComputation n + final tonkadur.fate.v1.lang.computation.GenericComputation n ) throws Throwable { - final ComputationCompiler address_compiler; - final Register result; - final Address car_addr, cdr_addr; + final ComputationCompiler handler; - result = reserve(DictType.WILD); - result_as_computation = result.get_value(); - result_as_address = result.get_address(); + handler = GenericComputationCompiler.handle(compiler, n); - address_compiler = new ComputationCompiler(compiler); + assimilate(handler); - n.get_collection().get_visited_by(address_compiler); + this.instructions_were_generated = handler.instructions_were_generated; + this.result_as_computation = handler.result_as_computation; + this.result_as_address = handler.result_as_address; + } - if (address_compiler.has_init()) - { - init_instructions.add(address_compiler.get_init()); - } + @Override + public void visit_let + ( + final tonkadur.fate.v1.lang.computation.Let n + ) + throws Throwable + { + final Collection names; - init_instructions.add + names = new ArrayList(); + + for ( - new SetValue(result_as_address, address_compiler.get_computation()) - ); + final + Cons + < + tonkadur.fate.v1.lang.Variable, + tonkadur.fate.v1.lang.meta.Computation + > + a: + n.get_assignments() + ) + { + final String name; + final Register r; + final ComputationCompiler cc; - car_addr = - new RelativeAddress - ( - result_as_address, - new Constant(Type.STRING, "0"), - address_compiler.get_computation().get_type() - ); + name = a.get_car().get_name(); + r = + /* These are free by the unbind below */ + compiler.registers().reserve + ( + TypeCompiler.compile(compiler, a.get_car().get_type()), + init_instructions + ); - init_instructions.add - ( - new SetValue - ( - car_addr, - address_compiler.get_computation() - ) - ); + compiler.registers().bind(name, r); + names.add(name); - address_compiler.release_registers(init_instructions); + cc = new ComputationCompiler(compiler); - cdr_addr = - new RelativeAddress + a.get_cdr().get_visited_by(cc); + + assimilate(cc); + + init_instructions.add ( - result_as_address, - new Constant(Type.STRING, "1"), - TypeCompiler.compile - ( - compiler, - ( - (tonkadur.fate.v1.lang.type.CollectionType) - n.get_collection().get_type() - ).get_content_type() - ) + new SetValue(r.get_address(), cc.get_computation()) ); + } - init_instructions.add(new Initialize(cdr_addr)); + n.get_computation().get_visited_by(this); - init_instructions.add - ( - PopElement.generate - ( - compiler.registers(), - compiler.assembler(), - car_addr, - cdr_addr, - n.is_from_left() - ) - ); + for (final String name: names) + { + compiler.registers().unbind(name, init_instructions); + } } -*/ + + @Override + public void visit_variable_reference + ( + final tonkadur.fate.v1.lang.computation.VariableReference n + ) + throws Throwable + { + final Register register; + + register = + compiler.registers().get_context_register(n.get_variable().get_name()); + + result_as_address = register.get_address(); + result_as_computation = register.get_value(); + } + @Override public void visit_set_fields ( diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/AccessCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/AccessCompiler.java new file mode 100644 index 0000000..1960da0 --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/AccessCompiler.java @@ -0,0 +1,65 @@ +package tonkadur.wyrd.v1.compiler.fate.v1.computation.generic; + +import tonkadur.fate.v1.lang.computation.generic.Access; + +import tonkadur.wyrd.v1.lang.computation.Cast; +import tonkadur.wyrd.v1.lang.computation.RelativeAddress; + +import tonkadur.wyrd.v1.lang.type.Type; + +import tonkadur.wyrd.v1.compiler.fate.v1.Compiler; +import tonkadur.wyrd.v1.compiler.fate.v1.TypeCompiler; +import tonkadur.wyrd.v1.compiler.fate.v1.ComputationCompiler; + +import tonkadur.wyrd.v1.compiler.fate.v1.computation.GenericComputationCompiler; + +public class AccessCompiler extends GenericComputationCompiler +{ + public static Class get_target_class () + { + return Access.class; + } + + public AccessCompiler (final Compiler compiler) + { + super(compiler); + } + + public void compile + ( + final tonkadur.fate.v1.lang.computation.GenericComputation computation + ) + throws Throwable + { + final Access source; + final ComputationCompiler extra_address_cc, base_address_cc; + + source = (Access) computation; + + base_address_cc = new ComputationCompiler(compiler); + extra_address_cc = new ComputationCompiler(compiler); + + source.get_parent().get_visited_by(base_address_cc); + source.get_index().get_visited_by(extra_address_cc); + + base_address_cc.generate_address(); + + assimilate(base_address_cc); + assimilate(extra_address_cc); + + result_as_address = + new RelativeAddress + ( + base_address_cc.get_address(), + new Cast(extra_address_cc.get_computation(), Type.STRING), + TypeCompiler.compile + ( + compiler, + ( + (tonkadur.fate.v1.lang.type.CollectionType) + source.get_parent().get_type() + ).get_content_type() + ) + ); + } +} diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/AddElementAtComputationCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/AddElementAtComputationCompiler.java new file mode 100644 index 0000000..dd774f5 --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/AddElementAtComputationCompiler.java @@ -0,0 +1,130 @@ +package tonkadur.wyrd.v1.compiler.fate.v1.computation.generic; + +import tonkadur.fate.v1.lang.computation.generic.AddElementAtComputation; + +import tonkadur.wyrd.v1.lang.instruction.SetValue; + +import tonkadur.wyrd.v1.lang.computation.Constant; +import tonkadur.wyrd.v1.lang.computation.IfElseComputation; +import tonkadur.wyrd.v1.lang.computation.Operation; +import tonkadur.wyrd.v1.lang.computation.Size; + +import tonkadur.wyrd.v1.lang.Register; + +import tonkadur.wyrd.v1.lang.type.Type; + +import tonkadur.wyrd.v1.compiler.fate.v1.Compiler; +import tonkadur.wyrd.v1.compiler.fate.v1.TypeCompiler; +import tonkadur.wyrd.v1.compiler.fate.v1.ComputationCompiler; + +import tonkadur.wyrd.v1.compiler.fate.v1.computation.GenericComputationCompiler; + +public class AddElementAtComputationCompiler +extends GenericComputationCompiler +{ + public static Class get_target_class () + { + return AddElementAtComputation.class; + } + + public AddElementAtComputationCompiler (final Compiler compiler) + { + super(compiler); + } + + public void compile + ( + final tonkadur.fate.v1.lang.computation.GenericComputation computation + ) + throws Throwable + { + final AddElementAtComputation source; + final ComputationCompiler address_compiler, index_compiler; + final ComputationCompiler element_compiler; + final Register result, index, collection_size; + + source = (AddElementAtComputation) computation; + + result = reserve(TypeCompiler.compile(compiler, source.get_type())); + result_as_address = result.get_address(); + result_as_computation = result.get_value(); + + address_compiler = new ComputationCompiler(compiler); + index_compiler = new ComputationCompiler(compiler); + element_compiler = new ComputationCompiler(compiler); + + source.get_collection().get_visited_by(address_compiler); + + if (address_compiler.has_init()) + { + init_instructions.add(address_compiler.get_init()); + } + + init_instructions.add + ( + new SetValue(result_as_address, address_compiler.get_computation()) + ); + + address_compiler.release_registers(init_instructions); + + source.get_index().get_visited_by(index_compiler); + + index_compiler.generate_address(); + assimilate(index_compiler); + + source.get_element().get_visited_by(element_compiler); + assimilate(element_compiler); + + collection_size = reserve(Type.INT); + index = reserve(Type.INT); + + init_instructions.add + ( + new SetValue + ( + collection_size.get_address(), + new Size(result_as_address) + ) + ); + + init_instructions.add + ( + new SetValue + ( + index.get_address(), + new IfElseComputation + ( + Operation.greater_than + ( + index_compiler.get_computation(), + collection_size.get_value() + ), + collection_size.get_value(), + new IfElseComputation + ( + Operation.less_than + ( + index_compiler.get_computation(), + Constant.ZERO + ), + Constant.ZERO, + index_compiler.get_computation() + ) + ) + ) + ); + + init_instructions.add + ( + tonkadur.wyrd.v1.compiler.util.InsertAt.generate + ( + compiler.registers(), + compiler.assembler(), + index.get_address(), + element_compiler.get_computation(), + collection_size.get_value(), + result_as_address + ) + ); + } +} diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/AddElementComputationCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/AddElementComputationCompiler.java new file mode 100644 index 0000000..b1a3742 --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/AddElementComputationCompiler.java @@ -0,0 +1,82 @@ +package tonkadur.wyrd.v1.compiler.fate.v1.computation.generic; + +import tonkadur.fate.v1.lang.computation.generic.AddElementComputation; + +import tonkadur.wyrd.v1.lang.instruction.SetValue; + +import tonkadur.wyrd.v1.lang.Register; + +import tonkadur.wyrd.v1.lang.type.Type; + +import tonkadur.wyrd.v1.compiler.fate.v1.Compiler; +import tonkadur.wyrd.v1.compiler.fate.v1.TypeCompiler; +import tonkadur.wyrd.v1.compiler.fate.v1.ComputationCompiler; + +import tonkadur.wyrd.v1.compiler.fate.v1.computation.GenericComputationCompiler; + +public class AddElementComputationCompiler +extends GenericComputationCompiler +{ + public static Class get_target_class () + { + return AddElementComputation.class; + } + + public AddElementComputationCompiler (final Compiler compiler) + { + super(compiler); + } + + public void compile + ( + final tonkadur.fate.v1.lang.computation.GenericComputation computation + ) + throws Throwable + { + final AddElementComputation source; + final ComputationCompiler address_compiler, element_compiler; + final Register result; + + source = (AddElementComputation) computation; + + result = reserve(TypeCompiler.compile(compiler, source.get_type())); + + result_as_address = result.get_address(); + result_as_computation = result.get_value(); + + address_compiler = new ComputationCompiler(compiler); + element_compiler = new ComputationCompiler(compiler); + + source.get_collection().get_visited_by(address_compiler); + + if (address_compiler.has_init()) + { + init_instructions.add(address_compiler.get_init()); + } + + init_instructions.add + ( + new SetValue(result_as_address, address_compiler.get_computation()) + ); + + address_compiler.release_registers(init_instructions); + + source.get_element().get_visited_by(element_compiler); + assimilate(element_compiler); + + init_instructions.add + ( + tonkadur.wyrd.v1.compiler.util.AddElement.generate + ( + compiler.registers(), + compiler.assembler(), + element_compiler.get_computation(), + result_as_address, + ( + (tonkadur.fate.v1.lang.type.CollectionType) + source.get_collection().get_type() + ).is_set() + ) + ); + } +} diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/AddElementsOfComputationCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/AddElementsOfComputationCompiler.java new file mode 100644 index 0000000..4e57413 --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/AddElementsOfComputationCompiler.java @@ -0,0 +1,84 @@ +package tonkadur.wyrd.v1.compiler.fate.v1.computation.generic; + +import tonkadur.fate.v1.lang.computation.generic.AddElementsOfComputation; + +import tonkadur.wyrd.v1.lang.instruction.SetValue; + +import tonkadur.wyrd.v1.lang.Register; + +import tonkadur.wyrd.v1.lang.type.Type; + +import tonkadur.wyrd.v1.compiler.fate.v1.Compiler; +import tonkadur.wyrd.v1.compiler.fate.v1.TypeCompiler; +import tonkadur.wyrd.v1.compiler.fate.v1.ComputationCompiler; + +import tonkadur.wyrd.v1.compiler.fate.v1.computation.GenericComputationCompiler; + +public class AddElementsOfComputationCompiler +extends GenericComputationCompiler +{ + public static Class get_target_class () + { + return AddElementsOfComputation.class; + } + + public AddElementsOfComputationCompiler (final Compiler compiler) + { + super(compiler); + } + + public void compile + ( + final tonkadur.fate.v1.lang.computation.GenericComputation computation + ) + throws Throwable + { + final AddElementsOfComputation source; + final ComputationCompiler collection_in_cc, collection_cc; + final Register result; + + source = (AddElementsOfComputation) computation; + + result = reserve(TypeCompiler.compile(compiler, source.get_type())); + + result_as_address = result.get_address(); + result_as_computation = result.get_value(); + + collection_cc = new ComputationCompiler(compiler); + + source.get_target_collection().get_visited_by(collection_cc); + + if (collection_cc.has_init()) + { + init_instructions.add(collection_cc.get_init()); + } + + init_instructions.add + ( + new SetValue(result_as_address, collection_cc.get_computation()) + ); + + collection_cc.release_registers(init_instructions); + + collection_in_cc = new ComputationCompiler(compiler); + + source.get_source_collection().get_visited_by(collection_in_cc); + + assimilate(collection_in_cc); + + init_instructions.add + ( + tonkadur.wyrd.v1.compiler.util.AddElementsOf.generate + ( + compiler.registers(), + compiler.assembler(), + collection_in_cc.get_address(), + result_as_address, + ( + (tonkadur.fate.v1.lang.type.CollectionType) + source.get_target_collection().get_type() + ).is_set() + ) + ); + } +} diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/CarCdrCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/CarCdrCompiler.java new file mode 100644 index 0000000..81a458a --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/CarCdrCompiler.java @@ -0,0 +1,58 @@ +package tonkadur.wyrd.v1.compiler.fate.v1.computation.generic; + +import tonkadur.fate.v1.lang.computation.generic.CarCdr; + +import tonkadur.wyrd.v1.lang.computation.Constant; +import tonkadur.wyrd.v1.lang.computation.RelativeAddress; + +import tonkadur.wyrd.v1.lang.type.Type; + +import tonkadur.wyrd.v1.compiler.fate.v1.Compiler; +import tonkadur.wyrd.v1.compiler.fate.v1.TypeCompiler; +import tonkadur.wyrd.v1.compiler.fate.v1.ComputationCompiler; + +import tonkadur.wyrd.v1.compiler.fate.v1.computation.GenericComputationCompiler; + +public class CarCdrCompiler +extends GenericComputationCompiler +{ + public static Class get_target_class () + { + return CarCdr.class; + } + + public CarCdrCompiler (final Compiler compiler) + { + super(compiler); + } + + public void compile + ( + final tonkadur.fate.v1.lang.computation.GenericComputation computation + ) + throws Throwable + { + final CarCdr source; + final ComputationCompiler address_cc; + + source = (CarCdr) computation; + + address_cc = new ComputationCompiler(compiler); + + source.get_parent().get_visited_by(address_cc); + + assimilate(address_cc); + + result_as_address = + new RelativeAddress + ( + address_cc.get_address(), + new Constant + ( + Type.STRING, + (source.is_car()? "0" : "1") + ), + TypeCompiler.compile(compiler, source.get_type()) + ); + } +} diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/ConsComputationCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/ConsComputationCompiler.java new file mode 100644 index 0000000..8d73c6a --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/ConsComputationCompiler.java @@ -0,0 +1,77 @@ +package tonkadur.wyrd.v1.compiler.fate.v1.computation.generic; + +import tonkadur.fate.v1.lang.computation.generic.ConsComputation; + +import tonkadur.wyrd.v1.lang.Register; + +import tonkadur.wyrd.v1.lang.type.DictType; + +import tonkadur.wyrd.v1.compiler.fate.v1.Compiler; +import tonkadur.wyrd.v1.compiler.fate.v1.TypeCompiler; +import tonkadur.wyrd.v1.compiler.fate.v1.ComputationCompiler; + +import tonkadur.wyrd.v1.compiler.fate.v1.computation.GenericComputationCompiler; + +public class ConsComputationCompiler +extends GenericComputationCompiler +{ + public static Class get_target_class () + { + return ConsComputation.class; + } + + public ConsComputationCompiler (final Compiler compiler) + { + super(compiler); + } + + public void compile + ( + final tonkadur.fate.v1.lang.computation.GenericComputation computation + ) + throws Throwable + { + final ConsComputation source; + final ComputationCompiler car_compiler, cdr_compiler; + final Register result; + + source = (ConsComputation) computation; + + result = reserve(DictType.WILD); + result_as_address = result.get_address(); + result_as_computation = result.get_value(); + + car_compiler = new ComputationCompiler(compiler); + + source.get_car().get_visited_by(car_compiler); + + if (car_compiler.has_init()) + { + init_instructions.add(car_compiler.get_init()); + } + + cdr_compiler = new ComputationCompiler(compiler); + + source.get_cdr().get_visited_by(cdr_compiler); + + if (cdr_compiler.has_init()) + { + init_instructions.add(cdr_compiler.get_init()); + } + + init_instructions.add + ( + tonkadur.wyrd.v1.compiler.util.CreateCons.generate + ( + compiler.registers(), + compiler.assembler(), + result_as_address, + car_compiler.get_computation(), + cdr_compiler.get_computation() + ) + ); + + car_compiler.release_registers(init_instructions); + cdr_compiler.release_registers(init_instructions); + } +} diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/CountOperatorCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/CountOperatorCompiler.java new file mode 100644 index 0000000..ed2e3fb --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/CountOperatorCompiler.java @@ -0,0 +1,132 @@ +package tonkadur.wyrd.v1.compiler.fate.v1.computation.generic; + +import tonkadur.fate.v1.lang.computation.generic.CountOperator; + +import tonkadur.wyrd.v1.lang.Register; + +import tonkadur.wyrd.v1.lang.type.Type; + +import tonkadur.wyrd.v1.lang.instruction.SetValue; + +import tonkadur.wyrd.v1.lang.computation.Size; +import tonkadur.wyrd.v1.lang.computation.IfElseComputation; +import tonkadur.wyrd.v1.lang.computation.Constant; + +import tonkadur.wyrd.v1.compiler.fate.v1.Compiler; +import tonkadur.wyrd.v1.compiler.fate.v1.ComputationCompiler; + +import tonkadur.wyrd.v1.compiler.fate.v1.computation.GenericComputationCompiler; + +public class CountOperatorCompiler +extends GenericComputationCompiler +{ + public static Class get_target_class () + { + return CountOperator.class; + } + + public CountOperatorCompiler (final Compiler compiler) + { + super(compiler); + } + + public void compile + ( + final tonkadur.fate.v1.lang.computation.GenericComputation computation + ) + throws Throwable + { + final CountOperator source; + final ComputationCompiler collection_compiler, element_compiler; + + source = (CountOperator) computation; + + collection_compiler = new ComputationCompiler(compiler); + element_compiler = new ComputationCompiler(compiler); + + source.get_collection().get_visited_by(collection_compiler); + source.get_element().get_visited_by(element_compiler); + + collection_compiler.generate_address(); + + assimilate(collection_compiler); + assimilate(element_compiler); + + if + ( + ( + (tonkadur.fate.v1.lang.type.CollectionType) + source.get_collection().get_type() + ).is_set() + ) + { + final Register was_found, index, element; + + was_found = reserve(Type.BOOL); + index = reserve(Type.INT); + element = reserve(element_compiler.get_computation().get_type()); + + init_instructions.add + ( + new SetValue + ( + element.get_address(), + element_compiler.get_computation() + ) + ); + init_instructions.add + ( + tonkadur.wyrd.v1.compiler.util.BinarySearch.generate + ( + compiler.registers(), + compiler.assembler(), + element.get_value(), + new Size(collection_compiler.get_address()), + collection_compiler.get_address(), + was_found.get_address(), + index.get_address() + ) + ); + + result_as_computation = + new IfElseComputation + ( + was_found.get_value(), + Constant.ONE, + Constant.ZERO + ); + } + else + { + final Register result, element; + + result = reserve(Type.INT); + + result_as_address = result.get_address(); + result_as_computation = result.get_value(); + + element = reserve(element_compiler.get_computation().get_type()); + + init_instructions.add + ( + new SetValue + ( + element.get_address(), + element_compiler.get_computation() + ) + ); + init_instructions.add + ( + tonkadur.wyrd.v1.compiler.util.CountOccurrences.generate + ( + compiler.registers(), + compiler.assembler(), + element.get_value(), + new Size(collection_compiler.get_address()), + collection_compiler.get_address(), + result_as_address + ) + ); + } + } +} diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/FilterComputationCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/FilterComputationCompiler.java new file mode 100644 index 0000000..b2123f7 --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/FilterComputationCompiler.java @@ -0,0 +1,110 @@ +package tonkadur.wyrd.v1.compiler.fate.v1.computation.generic; + +import java.util.List; +import java.util.ArrayList; + +import tonkadur.fate.v1.lang.computation.generic.FilterComputation; + + +import tonkadur.wyrd.v1.lang.Register; + +import tonkadur.wyrd.v1.lang.instruction.SetValue; + +import tonkadur.wyrd.v1.lang.meta.Computation; + +import tonkadur.wyrd.v1.lang.type.Type; + +import tonkadur.wyrd.v1.compiler.fate.v1.Compiler; +import tonkadur.wyrd.v1.compiler.fate.v1.TypeCompiler; +import tonkadur.wyrd.v1.compiler.fate.v1.ComputationCompiler; + +import tonkadur.wyrd.v1.compiler.fate.v1.computation.GenericComputationCompiler; + +public class FilterComputationCompiler +extends GenericComputationCompiler +{ + public static Class get_target_class () + { + return FilterComputation.class; + } + + public FilterComputationCompiler (final Compiler compiler) + { + super(compiler); + } + + public void compile + ( + final tonkadur.fate.v1.lang.computation.GenericComputation computation + ) + throws Throwable + { + final FilterComputation source; + final List params; + final ComputationCompiler lambda_cc, in_collection_cc; + final Register result; + + source = (FilterComputation) computation; + + result = reserve(TypeCompiler.compile(compiler, source.get_type())); + + result_as_address = result.get_address(); + result_as_computation = result.get_value(); + + params = new ArrayList(); + + for + ( + final tonkadur.fate.v1.lang.meta.Computation p: + source.get_extra_parameters() + ) + { + final ComputationCompiler param_cc; + + param_cc = new ComputationCompiler(compiler); + + p.get_visited_by(param_cc); + + // Let's not re-compute the parameters on every iteration. + param_cc.generate_address(); + + assimilate(param_cc); + + params.add(param_cc.get_computation()); + } + + lambda_cc = new ComputationCompiler(compiler); + + source.get_lambda_function().get_visited_by(lambda_cc); + + assimilate(lambda_cc); + + in_collection_cc = new ComputationCompiler(compiler); + + source.get_collection().get_visited_by(in_collection_cc); + + if (in_collection_cc.has_init()) + { + init_instructions.add(in_collection_cc.get_init()); + } + + init_instructions.add + ( + new SetValue(result_as_address, in_collection_cc.get_computation()) + ); + + in_collection_cc.release_registers(init_instructions); + + init_instructions.add + ( + tonkadur.wyrd.v1.compiler.util.FilterLambda.generate + ( + compiler.registers(), + compiler.assembler(), + lambda_cc.get_computation(), + result_as_address, + params + ) + ); + } +} diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/FoldCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/FoldCompiler.java new file mode 100644 index 0000000..9dc4106 --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/FoldCompiler.java @@ -0,0 +1,118 @@ +package tonkadur.wyrd.v1.compiler.fate.v1.computation.generic; + +import java.util.List; +import java.util.ArrayList; + +import tonkadur.fate.v1.lang.computation.generic.Fold; + + +import tonkadur.wyrd.v1.lang.Register; + +import tonkadur.wyrd.v1.lang.instruction.SetValue; + +import tonkadur.wyrd.v1.lang.meta.Computation; + +import tonkadur.wyrd.v1.lang.type.Type; + +import tonkadur.wyrd.v1.compiler.fate.v1.Compiler; +import tonkadur.wyrd.v1.compiler.fate.v1.TypeCompiler; +import tonkadur.wyrd.v1.compiler.fate.v1.ComputationCompiler; + +import tonkadur.wyrd.v1.compiler.fate.v1.computation.GenericComputationCompiler; + +public class FoldCompiler +extends GenericComputationCompiler +{ + public static Class get_target_class () + { + return Fold.class; + } + + public FoldCompiler (final Compiler compiler) + { + super(compiler); + } + + public void compile + ( + final tonkadur.fate.v1.lang.computation.GenericComputation computation + ) + throws Throwable + { + final Fold source; + final List params; + final ComputationCompiler lambda_cc, in_collection_cc, default_cc; + final Register result; + + source = (Fold) computation; + + result = reserve(TypeCompiler.compile(compiler, source.get_type())); + + result_as_address = result.get_address(); + result_as_computation = result.get_value(); + + params = new ArrayList(); + + for + ( + final tonkadur.fate.v1.lang.meta.Computation p: + source.get_extra_parameters() + ) + { + final ComputationCompiler param_cc; + + param_cc = new ComputationCompiler(compiler); + + p.get_visited_by(param_cc); + + // Let's not re-compute the parameters on every iteration. + param_cc.generate_address(); + + assimilate(param_cc); + + params.add(param_cc.get_computation()); + } + + default_cc = new ComputationCompiler(compiler); + + source.get_initial_value().get_visited_by(default_cc); + + if (default_cc.has_init()) + { + init_instructions.add(default_cc.get_init()); + } + + init_instructions.add + ( + new SetValue(result_as_address, default_cc.get_computation()) + ); + + default_cc.release_registers(init_instructions); + + lambda_cc = new ComputationCompiler(compiler); + + source.get_lambda_function().get_visited_by(lambda_cc); + + assimilate(lambda_cc); + + in_collection_cc = new ComputationCompiler(compiler); + + source.get_collection().get_visited_by(in_collection_cc); + + assimilate(in_collection_cc); + + init_instructions.add + ( + tonkadur.wyrd.v1.compiler.util.Fold.generate + ( + compiler.registers(), + compiler.assembler(), + lambda_cc.get_computation(), + result_as_address, + in_collection_cc.get_address(), + source.is_foldl(), + params + ) + ); + } +} diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/IndexOfOperatorCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/IndexOfOperatorCompiler.java new file mode 100644 index 0000000..61b8b05 --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/IndexOfOperatorCompiler.java @@ -0,0 +1,70 @@ +package tonkadur.wyrd.v1.compiler.fate.v1.computation.generic; + +import tonkadur.fate.v1.lang.computation.generic.IndexOfOperator; + +import tonkadur.wyrd.v1.lang.Register; + +import tonkadur.wyrd.v1.lang.type.Type; + +import tonkadur.wyrd.v1.lang.computation.Size; + +import tonkadur.wyrd.v1.compiler.fate.v1.Compiler; +import tonkadur.wyrd.v1.compiler.fate.v1.ComputationCompiler; + +import tonkadur.wyrd.v1.compiler.fate.v1.computation.GenericComputationCompiler; + +public class IndexOfOperatorCompiler +extends GenericComputationCompiler +{ + public static Class get_target_class () + { + return IndexOfOperator.class; + } + + public IndexOfOperatorCompiler (final Compiler compiler) + { + super(compiler); + } + + public void compile + ( + final tonkadur.fate.v1.lang.computation.GenericComputation computation + ) + throws Throwable + { + final IndexOfOperator source; + final ComputationCompiler elem_cc, collection_cc; + final Register result, result_found; + + source = (IndexOfOperator) computation; + + result = reserve(Type.INT); + result_found = reserve(Type.BOOL); + + result_as_address = result.get_address(); + result_as_computation = result.get_value(); + + elem_cc = new ComputationCompiler(compiler); + collection_cc = new ComputationCompiler(compiler); + + source.get_element().get_visited_by(elem_cc); + source.get_collection().get_visited_by(collection_cc); + + assimilate(elem_cc); + assimilate(collection_cc); + + init_instructions.add + ( + tonkadur.wyrd.v1.compiler.util.IterativeSearch.generate + ( + compiler.registers(), + compiler.assembler(), + elem_cc.get_computation(), + new Size(collection_cc.get_address()), + collection_cc.get_address(), + result_found.get_address(), + result_as_address + ) + ); + } +} diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/IndexedFilterComputationCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/IndexedFilterComputationCompiler.java new file mode 100644 index 0000000..6ebcd62 --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/IndexedFilterComputationCompiler.java @@ -0,0 +1,109 @@ +package tonkadur.wyrd.v1.compiler.fate.v1.computation.generic; + +import java.util.List; +import java.util.ArrayList; + +import tonkadur.fate.v1.lang.computation.generic.IndexedFilterComputation; + +import tonkadur.wyrd.v1.lang.Register; + +import tonkadur.wyrd.v1.lang.instruction.SetValue; + +import tonkadur.wyrd.v1.lang.meta.Computation; + +import tonkadur.wyrd.v1.lang.type.Type; + +import tonkadur.wyrd.v1.compiler.fate.v1.Compiler; +import tonkadur.wyrd.v1.compiler.fate.v1.TypeCompiler; +import tonkadur.wyrd.v1.compiler.fate.v1.ComputationCompiler; + +import tonkadur.wyrd.v1.compiler.fate.v1.computation.GenericComputationCompiler; + +public class IndexedFilterComputationCompiler +extends GenericComputationCompiler +{ + public static Class get_target_class () + { + return IndexedFilterComputation.class; + } + + public IndexedFilterComputationCompiler (final Compiler compiler) + { + super(compiler); + } + + public void compile + ( + final tonkadur.fate.v1.lang.computation.GenericComputation computation + ) + throws Throwable + { + final IndexedFilterComputation source; + final List params; + final ComputationCompiler lambda_cc, in_collection_cc; + final Register result; + + source = (IndexedFilterComputation) computation; + + result = reserve(TypeCompiler.compile(compiler, source.get_type())); + + result_as_address = result.get_address(); + result_as_computation = result.get_value(); + + params = new ArrayList(); + + for + ( + final tonkadur.fate.v1.lang.meta.Computation p: + source.get_extra_parameters() + ) + { + final ComputationCompiler param_cc; + + param_cc = new ComputationCompiler(compiler); + + p.get_visited_by(param_cc); + + // Let's not re-compute the parameters on every iteration. + param_cc.generate_address(); + + assimilate(param_cc); + + params.add(param_cc.get_computation()); + } + + lambda_cc = new ComputationCompiler(compiler); + + source.get_lambda_function().get_visited_by(lambda_cc); + + assimilate(lambda_cc); + + in_collection_cc = new ComputationCompiler(compiler); + + source.get_collection().get_visited_by(in_collection_cc); + + if (in_collection_cc.has_init()) + { + init_instructions.add(in_collection_cc.get_init()); + } + + init_instructions.add + ( + new SetValue(result_as_address, in_collection_cc.get_computation()) + ); + + in_collection_cc.release_registers(init_instructions); + + init_instructions.add + ( + tonkadur.wyrd.v1.compiler.util.IndexedFilterLambda.generate + ( + compiler.registers(), + compiler.assembler(), + lambda_cc.get_computation(), + result_as_address, + params + ) + ); + } +} diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/IndexedMapComputationCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/IndexedMapComputationCompiler.java new file mode 100644 index 0000000..c5b5e3c --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/IndexedMapComputationCompiler.java @@ -0,0 +1,105 @@ +package tonkadur.wyrd.v1.compiler.fate.v1.computation.generic; + +import java.util.List; +import java.util.ArrayList; + +import tonkadur.fate.v1.lang.computation.generic.IndexedMapComputation; + + +import tonkadur.wyrd.v1.lang.Register; + +import tonkadur.wyrd.v1.lang.instruction.SetValue; + +import tonkadur.wyrd.v1.lang.meta.Computation; + +import tonkadur.wyrd.v1.lang.type.Type; + +import tonkadur.wyrd.v1.compiler.fate.v1.Compiler; +import tonkadur.wyrd.v1.compiler.fate.v1.TypeCompiler; +import tonkadur.wyrd.v1.compiler.fate.v1.ComputationCompiler; + +import tonkadur.wyrd.v1.compiler.fate.v1.computation.GenericComputationCompiler; + +public class IndexedMapComputationCompiler +extends GenericComputationCompiler +{ + public static Class get_target_class () + { + return IndexedMapComputation.class; + } + + public IndexedMapComputationCompiler (final Compiler compiler) + { + super(compiler); + } + + public void compile + ( + final tonkadur.fate.v1.lang.computation.GenericComputation computation + ) + throws Throwable + { + final IndexedMapComputation source; + final List params; + final ComputationCompiler lambda_cc, in_collection_cc; + final Register result; + + source = (IndexedMapComputation) computation; + + result = reserve(TypeCompiler.compile(compiler, source.get_type())); + + result_as_address = result.get_address(); + result_as_computation = result.get_value(); + + params = new ArrayList(); + + for + ( + final tonkadur.fate.v1.lang.meta.Computation p: + source.get_extra_parameters() + ) + { + final ComputationCompiler param_cc; + + param_cc = new ComputationCompiler(compiler); + + p.get_visited_by(param_cc); + + // Let's not re-compute the parameters on every iteration. + param_cc.generate_address(); + + assimilate(param_cc); + + params.add(param_cc.get_computation()); + } + + lambda_cc = new ComputationCompiler(compiler); + + source.get_lambda_function().get_visited_by(lambda_cc); + + assimilate(lambda_cc); + + in_collection_cc = new ComputationCompiler(compiler); + + source.get_collection().get_visited_by(in_collection_cc); + + assimilate(in_collection_cc); + + init_instructions.add + ( + tonkadur.wyrd.v1.compiler.util.IndexedMapLambda.generate + ( + compiler.registers(), + compiler.assembler(), + lambda_cc.get_computation(), + in_collection_cc.get_address(), + result_as_address, + ( + (tonkadur.fate.v1.lang.type.CollectionType) + source.get_collection().get_type() + ).is_set(), + params + ) + ); + } +} diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/IndexedMergeComputationCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/IndexedMergeComputationCompiler.java new file mode 100644 index 0000000..a327d68 --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/IndexedMergeComputationCompiler.java @@ -0,0 +1,213 @@ +package tonkadur.wyrd.v1.compiler.fate.v1.computation.generic; + +import java.util.List; +import java.util.ArrayList; + +import tonkadur.fate.v1.lang.computation.generic.IndexedMergeComputation; + +import tonkadur.wyrd.v1.lang.Register; + +import tonkadur.wyrd.v1.lang.instruction.SetValue; + +import tonkadur.wyrd.v1.lang.meta.Computation; + +import tonkadur.wyrd.v1.lang.type.Type; + +import tonkadur.wyrd.v1.compiler.fate.v1.Compiler; +import tonkadur.wyrd.v1.compiler.fate.v1.TypeCompiler; +import tonkadur.wyrd.v1.compiler.fate.v1.ComputationCompiler; + +import tonkadur.wyrd.v1.compiler.fate.v1.computation.GenericComputationCompiler; + +public class IndexedMergeComputationCompiler +extends GenericComputationCompiler +{ + public static Class get_target_class () + { + return IndexedMergeComputation.class; + } + + public IndexedMergeComputationCompiler (final Compiler compiler) + { + super(compiler); + } + + protected void compile_with_defaults (final IndexedMergeComputation source) + throws Throwable + { + final List params; + final ComputationCompiler lambda_cc, default_a_cc, default_b_cc; + final ComputationCompiler in_collection_a_cc, in_collection_b_cc; + final Register result; + + result = reserve(TypeCompiler.compile(compiler, source.get_type())); + + result_as_address = result.get_address(); + result_as_computation = result.get_value(); + + params = new ArrayList(); + + for + ( + final tonkadur.fate.v1.lang.meta.Computation p: + source.get_extra_parameters() + ) + { + final ComputationCompiler param_cc; + + param_cc = new ComputationCompiler(compiler); + + p.get_visited_by(param_cc); + + // Let's not re-compute the parameters on every iteration. + param_cc.generate_address(); + + assimilate(param_cc); + + params.add(param_cc.get_computation()); + } + + lambda_cc = new ComputationCompiler(compiler); + + source.get_lambda_function().get_visited_by(lambda_cc); + + assimilate(lambda_cc); + + default_a_cc = new ComputationCompiler(compiler); + + source.get_default_a().get_visited_by(default_a_cc); + + default_a_cc.generate_address(); + + assimilate(default_a_cc); + + default_b_cc = new ComputationCompiler(compiler); + + source.get_default_b().get_visited_by(default_b_cc); + + default_b_cc.generate_address(); + + assimilate(default_b_cc); + + in_collection_a_cc = new ComputationCompiler(compiler); + + source.get_collection_in_a().get_visited_by(in_collection_a_cc); + + assimilate(in_collection_a_cc); + + in_collection_b_cc = new ComputationCompiler(compiler); + + source.get_collection_in_b().get_visited_by(in_collection_b_cc); + + assimilate(in_collection_b_cc); + + init_instructions.add + ( + tonkadur.wyrd.v1.compiler.util.IndexedMergeLambda.generate + ( + compiler.registers(), + compiler.assembler(), + lambda_cc.get_computation(), + default_a_cc.get_computation(), + in_collection_a_cc.get_address(), + default_b_cc.get_computation(), + in_collection_b_cc.get_address(), + result_as_address, + source.to_set(), + params + ) + ); + } + + protected void compile_without_defaults + ( + final IndexedMergeComputation source + ) + throws Throwable + { + final List params; + final ComputationCompiler lambda_cc; + final ComputationCompiler in_collection_a_cc, in_collection_b_cc; + final Register result; + + result = reserve(TypeCompiler.compile(compiler, source.get_type())); + + result_as_address = result.get_address(); + result_as_computation = result.get_value(); + + params = new ArrayList(); + + for + ( + final tonkadur.fate.v1.lang.meta.Computation p: + source.get_extra_parameters() + ) + { + final ComputationCompiler param_cc; + + param_cc = new ComputationCompiler(compiler); + + p.get_visited_by(param_cc); + + // Let's not re-compute the parameters on every iteration. + param_cc.generate_address(); + + assimilate(param_cc); + + params.add(param_cc.get_computation()); + } + + lambda_cc = new ComputationCompiler(compiler); + + source.get_lambda_function().get_visited_by(lambda_cc); + + assimilate(lambda_cc); + + in_collection_a_cc = new ComputationCompiler(compiler); + + source.get_collection_in_a().get_visited_by(in_collection_a_cc); + + assimilate(in_collection_a_cc); + + in_collection_b_cc = new ComputationCompiler(compiler); + + source.get_collection_in_b().get_visited_by(in_collection_b_cc); + + assimilate(in_collection_b_cc); + + init_instructions.add + ( + tonkadur.wyrd.v1.compiler.util.IndexedMergeLambda.generate + ( + compiler.registers(), + compiler.assembler(), + lambda_cc.get_computation(), + in_collection_a_cc.get_address(), + in_collection_b_cc.get_address(), + result_as_address, + source.to_set(), + params + ) + ); + } + + public void compile + ( + final tonkadur.fate.v1.lang.computation.GenericComputation computation + ) + throws Throwable + { + final IndexedMergeComputation source; + + source = (IndexedMergeComputation) computation; + + if (source.get_default_a() == null) + { + compile_without_defaults(source); + } + else + { + compile_with_defaults(source); + } + } +} diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/IndexedPartitionComputationCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/IndexedPartitionComputationCompiler.java new file mode 100644 index 0000000..196ae37 --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/IndexedPartitionComputationCompiler.java @@ -0,0 +1,144 @@ +package tonkadur.wyrd.v1.compiler.fate.v1.computation.generic; + +import java.util.List; +import java.util.ArrayList; + +import tonkadur.fate.v1.lang.computation.generic.IndexedPartitionComputation; + + +import tonkadur.wyrd.v1.lang.Register; + +import tonkadur.wyrd.v1.lang.instruction.SetValue; +import tonkadur.wyrd.v1.lang.instruction.Initialize; + +import tonkadur.wyrd.v1.lang.computation.Constant; +import tonkadur.wyrd.v1.lang.computation.Address; +import tonkadur.wyrd.v1.lang.computation.RelativeAddress; + +import tonkadur.wyrd.v1.lang.meta.Computation; + +import tonkadur.wyrd.v1.lang.type.Type; +import tonkadur.wyrd.v1.lang.type.DictType; + +import tonkadur.wyrd.v1.compiler.fate.v1.Compiler; +import tonkadur.wyrd.v1.compiler.fate.v1.TypeCompiler; +import tonkadur.wyrd.v1.compiler.fate.v1.ComputationCompiler; + +import tonkadur.wyrd.v1.compiler.fate.v1.computation.GenericComputationCompiler; + +public class IndexedPartitionComputationCompiler +extends GenericComputationCompiler +{ + public static Class get_target_class () + { + return IndexedPartitionComputation.class; + } + + public IndexedPartitionComputationCompiler (final Compiler compiler) + { + super(compiler); + } + + public void compile + ( + final tonkadur.fate.v1.lang.computation.GenericComputation computation + ) + throws Throwable + { + final IndexedPartitionComputation source; + final List params; + final ComputationCompiler lambda_cc, in_collection_cc; + final Address car_addr, cdr_addr; + final Register result; + + source = (IndexedPartitionComputation) computation; + + result = reserve(DictType.WILD); + + result_as_address = result.get_address(); + result_as_computation = result.get_value(); + + params = new ArrayList(); + + for + ( + final tonkadur.fate.v1.lang.meta.Computation p: + source.get_extra_parameters() + ) + { + final ComputationCompiler param_cc; + + param_cc = new ComputationCompiler(compiler); + + p.get_visited_by(param_cc); + + // Let's not re-compute the parameters on every iteration. + param_cc.generate_address(); + + assimilate(param_cc); + + params.add(param_cc.get_computation()); + } + + lambda_cc = new ComputationCompiler(compiler); + + source.get_lambda_function().get_visited_by(lambda_cc); + + assimilate(lambda_cc); + + in_collection_cc = new ComputationCompiler(compiler); + + source.get_collection().get_visited_by(in_collection_cc); + + if (in_collection_cc.has_init()) + { + init_instructions.add(in_collection_cc.get_init()); + } + + car_addr = + new RelativeAddress + ( + result_as_address, + new Constant(Type.STRING, "0"), + in_collection_cc.get_computation().get_type() + ); + + init_instructions.add + ( + new SetValue + ( + car_addr, + in_collection_cc.get_computation() + ) + ); + + cdr_addr = + new RelativeAddress + ( + result_as_address, + new Constant(Type.STRING, "1"), + in_collection_cc.get_computation().get_type() + ); + + in_collection_cc.release_registers(init_instructions); + + init_instructions.add(new Initialize(cdr_addr)); + + init_instructions.add + ( + tonkadur.wyrd.v1.compiler.util.IndexedPartitionLambda.generate + ( + compiler.registers(), + compiler.assembler(), + lambda_cc.get_computation(), + car_addr, + cdr_addr, + ( + (tonkadur.fate.v1.lang.type.CollectionType) + source.get_collection().get_type() + ).is_set(), + params + ) + ); + } +} diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/IsEmptyCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/IsEmptyCompiler.java new file mode 100644 index 0000000..80bbe9a --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/IsEmptyCompiler.java @@ -0,0 +1,47 @@ +package tonkadur.wyrd.v1.compiler.fate.v1.computation.generic; + +import tonkadur.fate.v1.lang.computation.generic.IsEmpty; + +import tonkadur.wyrd.v1.lang.computation.Size; +import tonkadur.wyrd.v1.lang.computation.Constant; +import tonkadur.wyrd.v1.lang.computation.Operation; + +import tonkadur.wyrd.v1.compiler.fate.v1.Compiler; +import tonkadur.wyrd.v1.compiler.fate.v1.ComputationCompiler; + +import tonkadur.wyrd.v1.compiler.fate.v1.computation.GenericComputationCompiler; + +public class IsEmptyCompiler +extends GenericComputationCompiler +{ + public static Class get_target_class () + { + return IsEmpty.class; + } + + public IsEmptyCompiler (final Compiler compiler) + { + super(compiler); + } + + public void compile + ( + final tonkadur.fate.v1.lang.computation.GenericComputation computation + ) + throws Throwable + { + final IsEmpty source; + final ComputationCompiler cc; + + source = (IsEmpty) computation; + + cc = new ComputationCompiler(compiler); + + source.get_collection().get_visited_by(cc); + + assimilate(cc); + + result_as_computation = + Operation.equals(new Size(cc.get_address()), Constant.ZERO); + } +} diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/IsMemberOperatorCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/IsMemberOperatorCompiler.java new file mode 100644 index 0000000..e5e1925 --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/IsMemberOperatorCompiler.java @@ -0,0 +1,124 @@ +package tonkadur.wyrd.v1.compiler.fate.v1.computation.generic; + +import tonkadur.fate.v1.lang.computation.generic.IsMemberOperator; + +import tonkadur.wyrd.v1.lang.computation.Size; + +import tonkadur.wyrd.v1.lang.instruction.SetValue; + +import tonkadur.wyrd.v1.lang.Register; + +import tonkadur.wyrd.v1.lang.type.Type; + +import tonkadur.wyrd.v1.compiler.fate.v1.Compiler; +import tonkadur.wyrd.v1.compiler.fate.v1.ComputationCompiler; + +import tonkadur.wyrd.v1.compiler.fate.v1.computation.GenericComputationCompiler; + +public class IsMemberOperatorCompiler +extends GenericComputationCompiler +{ + public static Class get_target_class () + { + return IsMemberOperator.class; + } + + public IsMemberOperatorCompiler (final Compiler compiler) + { + super(compiler); + } + + public void compile + ( + final tonkadur.fate.v1.lang.computation.GenericComputation computation + ) + throws Throwable + { + final IsMemberOperator source; + final Register result; + final ComputationCompiler collection_compiler, element_compiler; + + source = (IsMemberOperator) computation; + + collection_compiler = new ComputationCompiler(compiler); + element_compiler = new ComputationCompiler(compiler); + + source.get_collection().get_visited_by(collection_compiler); + source.get_element().get_visited_by(element_compiler); + + collection_compiler.generate_address(); + + assimilate(collection_compiler); + assimilate(element_compiler); + + result = reserve(Type.BOOL); + result_as_address = result.get_address(); + result_as_computation = result.get_value(); + + if + ( + ( + (tonkadur.fate.v1.lang.type.CollectionType) + source.get_collection().get_type() + ).is_set() + ) + { + final Register index, element; + + index = reserve(Type.INT); + element = reserve(element_compiler.get_computation().get_type()); + + init_instructions.add + ( + new SetValue + ( + element.get_address(), + element_compiler.get_computation() + ) + ); + init_instructions.add + ( + tonkadur.wyrd.v1.compiler.util.BinarySearch.generate + ( + compiler.registers(), + compiler.assembler(), + element.get_value(), + new Size(collection_compiler.get_address()), + collection_compiler.get_address(), + result_as_address, + index.get_address() + ) + ); + + } + else + { + final Register index, element; + + index = reserve(Type.INT); + element = reserve(element_compiler.get_computation().get_type()); + + init_instructions.add + ( + new SetValue + ( + element.get_address(), + element_compiler.get_computation() + ) + ); + init_instructions.add + ( + tonkadur.wyrd.v1.compiler.util.IterativeSearch.generate + ( + compiler.registers(), + compiler.assembler(), + element.get_value(), + new Size(collection_compiler.get_address()), + collection_compiler.get_address(), + result_as_address, + index.get_address() + ) + ); + } + } +} diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/LambdaEvaluationCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/LambdaEvaluationCompiler.java index c914d0c..fb74eb9 100644 --- a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/LambdaEvaluationCompiler.java +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/LambdaEvaluationCompiler.java @@ -44,7 +44,6 @@ public class LambdaEvaluationCompiler extends GenericComputationCompiler source = (LambdaEvaluation) computation; - parameters = new ArrayList(); target_line_cc = new ComputationCompiler(compiler); diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/MapComputationCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/MapComputationCompiler.java new file mode 100644 index 0000000..459936f --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/MapComputationCompiler.java @@ -0,0 +1,102 @@ +package tonkadur.wyrd.v1.compiler.fate.v1.computation.generic; + +import java.util.List; +import java.util.ArrayList; + +import tonkadur.fate.v1.lang.computation.generic.MapComputation; + +import tonkadur.wyrd.v1.lang.Register; + +import tonkadur.wyrd.v1.lang.meta.Computation; + +import tonkadur.wyrd.v1.lang.type.Type; + +import tonkadur.wyrd.v1.compiler.fate.v1.Compiler; +import tonkadur.wyrd.v1.compiler.fate.v1.TypeCompiler; +import tonkadur.wyrd.v1.compiler.fate.v1.ComputationCompiler; + +import tonkadur.wyrd.v1.compiler.fate.v1.computation.GenericComputationCompiler; + +public class MapComputationCompiler +extends GenericComputationCompiler +{ + public static Class get_target_class () + { + return MapComputation.class; + } + + public MapComputationCompiler (final Compiler compiler) + { + super(compiler); + } + + public void compile + ( + final tonkadur.fate.v1.lang.computation.GenericComputation computation + ) + throws Throwable + { + final MapComputation source; + final List params; + final ComputationCompiler lambda_cc, in_collection_cc; + final Register result; + + source = (MapComputation) computation; + + result = reserve(TypeCompiler.compile(compiler, source.get_type())); + + result_as_address = result.get_address(); + result_as_computation = result.get_value(); + + params = new ArrayList(); + + for + ( + final tonkadur.fate.v1.lang.meta.Computation p: + source.get_extra_parameters() + ) + { + final ComputationCompiler param_cc; + + param_cc = new ComputationCompiler(compiler); + + p.get_visited_by(param_cc); + + // Let's not re-compute the parameters on every iteration. + param_cc.generate_address(); + + assimilate(param_cc); + + params.add(param_cc.get_computation()); + } + + lambda_cc = new ComputationCompiler(compiler); + + source.get_lambda_function().get_visited_by(lambda_cc); + + assimilate(lambda_cc); + + in_collection_cc = new ComputationCompiler(compiler); + + source.get_collection().get_visited_by(in_collection_cc); + + assimilate(in_collection_cc); + + init_instructions.add + ( + tonkadur.wyrd.v1.compiler.util.MapLambda.generate + ( + compiler.registers(), + compiler.assembler(), + lambda_cc.get_computation(), + in_collection_cc.get_address(), + result_as_address, + ( + (tonkadur.fate.v1.lang.type.CollectionType) + source.get_collection().get_type() + ).is_set(), + params + ) + ); + } +} diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/MergeComputationCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/MergeComputationCompiler.java new file mode 100644 index 0000000..d0cde2f --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/MergeComputationCompiler.java @@ -0,0 +1,208 @@ +package tonkadur.wyrd.v1.compiler.fate.v1.computation.generic; + +import java.util.List; +import java.util.ArrayList; + +import tonkadur.fate.v1.lang.computation.generic.MergeComputation; + +import tonkadur.wyrd.v1.lang.Register; + +import tonkadur.wyrd.v1.lang.meta.Computation; + +import tonkadur.wyrd.v1.lang.type.Type; + +import tonkadur.wyrd.v1.compiler.fate.v1.Compiler; +import tonkadur.wyrd.v1.compiler.fate.v1.TypeCompiler; +import tonkadur.wyrd.v1.compiler.fate.v1.ComputationCompiler; + +import tonkadur.wyrd.v1.compiler.fate.v1.computation.GenericComputationCompiler; + +public class MergeComputationCompiler +extends GenericComputationCompiler +{ + public static Class get_target_class () + { + return MergeComputation.class; + } + + public MergeComputationCompiler (final Compiler compiler) + { + super(compiler); + } + + protected void compile_with_defaults (final MergeComputation source) + throws Throwable + { + final List params; + final ComputationCompiler lambda_cc, default_a_cc, default_b_cc; + final ComputationCompiler in_collection_a_cc, in_collection_b_cc; + final Register result; + + result = reserve(TypeCompiler.compile(compiler, source.get_type())); + + result_as_address = result.get_address(); + result_as_computation = result.get_value(); + + params = new ArrayList(); + + for + ( + final tonkadur.fate.v1.lang.meta.Computation p: + source.get_extra_parameters() + ) + { + final ComputationCompiler param_cc; + + param_cc = new ComputationCompiler(compiler); + + p.get_visited_by(param_cc); + + // Let's not re-compute the parameters on every iteration. + param_cc.generate_address(); + + assimilate(param_cc); + + params.add(param_cc.get_computation()); + } + + lambda_cc = new ComputationCompiler(compiler); + + source.get_lambda_function().get_visited_by(lambda_cc); + + assimilate(lambda_cc); + + default_a_cc = new ComputationCompiler(compiler); + + source.get_default_a().get_visited_by(default_a_cc); + + default_a_cc.generate_address(); + + assimilate(default_a_cc); + + default_b_cc = new ComputationCompiler(compiler); + + source.get_default_b().get_visited_by(default_b_cc); + + default_b_cc.generate_address(); + + assimilate(default_b_cc); + + in_collection_a_cc = new ComputationCompiler(compiler); + + source.get_collection_in_a().get_visited_by(in_collection_a_cc); + + assimilate(in_collection_a_cc); + + in_collection_b_cc = new ComputationCompiler(compiler); + + source.get_collection_in_b().get_visited_by(in_collection_b_cc); + + assimilate(in_collection_b_cc); + + init_instructions.add + ( + tonkadur.wyrd.v1.compiler.util.MergeLambda.generate + ( + compiler.registers(), + compiler.assembler(), + lambda_cc.get_computation(), + default_a_cc.get_computation(), + in_collection_a_cc.get_address(), + default_b_cc.get_computation(), + in_collection_b_cc.get_address(), + result_as_address, + source.to_set(), + params + ) + ); + } + + protected void compile_without_defaults (final MergeComputation source) + throws Throwable + { + final List params; + final ComputationCompiler lambda_cc; + final ComputationCompiler in_collection_a_cc, in_collection_b_cc; + final Register result; + + result = reserve(TypeCompiler.compile(compiler, source.get_type())); + + result_as_address = result.get_address(); + result_as_computation = result.get_value(); + + params = new ArrayList(); + + for + ( + final tonkadur.fate.v1.lang.meta.Computation p: + source.get_extra_parameters() + ) + { + final ComputationCompiler param_cc; + + param_cc = new ComputationCompiler(compiler); + + p.get_visited_by(param_cc); + + // Let's not re-compute the parameters on every iteration. + param_cc.generate_address(); + + assimilate(param_cc); + + params.add(param_cc.get_computation()); + } + + lambda_cc = new ComputationCompiler(compiler); + + source.get_lambda_function().get_visited_by(lambda_cc); + + assimilate(lambda_cc); + + in_collection_a_cc = new ComputationCompiler(compiler); + + source.get_collection_in_a().get_visited_by(in_collection_a_cc); + + assimilate(in_collection_a_cc); + + in_collection_b_cc = new ComputationCompiler(compiler); + + source.get_collection_in_b().get_visited_by(in_collection_b_cc); + + assimilate(in_collection_b_cc); + + init_instructions.add + ( + tonkadur.wyrd.v1.compiler.util.MergeLambda.generate + ( + compiler.registers(), + compiler.assembler(), + lambda_cc.get_computation(), + in_collection_a_cc.get_address(), + in_collection_b_cc.get_address(), + result_as_address, + source.to_set(), + params + ) + ); + } + + public void compile + ( + final tonkadur.fate.v1.lang.computation.GenericComputation computation + ) + throws Throwable + { + final MergeComputation source; + + source = (MergeComputation) computation; + + if (source.get_default_a() == null) + { + compile_without_defaults(source); + } + else + { + compile_with_defaults(source); + } + } +} diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/PartitionComputationCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/PartitionComputationCompiler.java new file mode 100644 index 0000000..d66c1dd --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/PartitionComputationCompiler.java @@ -0,0 +1,144 @@ +package tonkadur.wyrd.v1.compiler.fate.v1.computation.generic; + +import java.util.List; +import java.util.ArrayList; + +import tonkadur.fate.v1.lang.computation.generic.PartitionComputation; + + +import tonkadur.wyrd.v1.lang.Register; + +import tonkadur.wyrd.v1.lang.instruction.SetValue; +import tonkadur.wyrd.v1.lang.instruction.Initialize; + +import tonkadur.wyrd.v1.lang.computation.Constant; +import tonkadur.wyrd.v1.lang.computation.Address; +import tonkadur.wyrd.v1.lang.computation.RelativeAddress; + +import tonkadur.wyrd.v1.lang.meta.Computation; + +import tonkadur.wyrd.v1.lang.type.Type; +import tonkadur.wyrd.v1.lang.type.DictType; + +import tonkadur.wyrd.v1.compiler.fate.v1.Compiler; +import tonkadur.wyrd.v1.compiler.fate.v1.TypeCompiler; +import tonkadur.wyrd.v1.compiler.fate.v1.ComputationCompiler; + +import tonkadur.wyrd.v1.compiler.fate.v1.computation.GenericComputationCompiler; + +public class PartitionComputationCompiler +extends GenericComputationCompiler +{ + public static Class get_target_class () + { + return PartitionComputation.class; + } + + public PartitionComputationCompiler (final Compiler compiler) + { + super(compiler); + } + + public void compile + ( + final tonkadur.fate.v1.lang.computation.GenericComputation computation + ) + throws Throwable + { + final PartitionComputation source; + final List params; + final ComputationCompiler lambda_cc, in_collection_cc; + final Address car_addr, cdr_addr; + final Register result; + + source = (PartitionComputation) computation; + + result = reserve(DictType.WILD); + + result_as_address = result.get_address(); + result_as_computation = result.get_value(); + + params = new ArrayList(); + + for + ( + final tonkadur.fate.v1.lang.meta.Computation p: + source.get_extra_parameters() + ) + { + final ComputationCompiler param_cc; + + param_cc = new ComputationCompiler(compiler); + + p.get_visited_by(param_cc); + + // Let's not re-compute the parameters on every iteration. + param_cc.generate_address(); + + assimilate(param_cc); + + params.add(param_cc.get_computation()); + } + + lambda_cc = new ComputationCompiler(compiler); + + source.get_lambda_function().get_visited_by(lambda_cc); + + assimilate(lambda_cc); + + in_collection_cc = new ComputationCompiler(compiler); + + source.get_collection().get_visited_by(in_collection_cc); + + if (in_collection_cc.has_init()) + { + init_instructions.add(in_collection_cc.get_init()); + } + + car_addr = + new RelativeAddress + ( + result_as_address, + new Constant(Type.STRING, "0"), + in_collection_cc.get_computation().get_type() + ); + + init_instructions.add + ( + new SetValue + ( + car_addr, + in_collection_cc.get_computation() + ) + ); + + cdr_addr = + new RelativeAddress + ( + result_as_address, + new Constant(Type.STRING, "1"), + in_collection_cc.get_computation().get_type() + ); + + in_collection_cc.release_registers(init_instructions); + + init_instructions.add(new Initialize(cdr_addr)); + + init_instructions.add + ( + tonkadur.wyrd.v1.compiler.util.PartitionLambda.generate + ( + compiler.registers(), + compiler.assembler(), + lambda_cc.get_computation(), + car_addr, + cdr_addr, + ( + (tonkadur.fate.v1.lang.type.CollectionType) + source.get_collection().get_type() + ).is_set(), + params + ) + ); + } +} diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/PopElementComputationCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/PopElementComputationCompiler.java new file mode 100644 index 0000000..b6a0e1b --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/PopElementComputationCompiler.java @@ -0,0 +1,115 @@ +package tonkadur.wyrd.v1.compiler.fate.v1.computation.generic; + +import tonkadur.fate.v1.lang.computation.generic.PopElementComputation; + +import tonkadur.wyrd.v1.lang.Register; + +import tonkadur.wyrd.v1.lang.instruction.SetValue; +import tonkadur.wyrd.v1.lang.instruction.Initialize; + +import tonkadur.wyrd.v1.lang.type.DictType; +import tonkadur.wyrd.v1.lang.type.Type; + +import tonkadur.wyrd.v1.lang.computation.Address; +import tonkadur.wyrd.v1.lang.computation.RelativeAddress; +import tonkadur.wyrd.v1.lang.computation.Constant; + +import tonkadur.wyrd.v1.compiler.fate.v1.Compiler; +import tonkadur.wyrd.v1.compiler.fate.v1.TypeCompiler; +import tonkadur.wyrd.v1.compiler.fate.v1.ComputationCompiler; + +import tonkadur.wyrd.v1.compiler.fate.v1.computation.GenericComputationCompiler; + +public class PopElementComputationCompiler +extends GenericComputationCompiler +{ + public static Class get_target_class () + { + return PopElementComputation.class; + } + + public PopElementComputationCompiler (final Compiler compiler) + { + super(compiler); + } + + public void compile + ( + final tonkadur.fate.v1.lang.computation.GenericComputation computation + ) + throws Throwable + { + final PopElementComputation source; + final ComputationCompiler address_compiler; + final Register result; + final Address car_addr, cdr_addr; + + source = (PopElementComputation) computation; + + result = reserve(DictType.WILD); + result_as_computation = result.get_value(); + result_as_address = result.get_address(); + + address_compiler = new ComputationCompiler(compiler); + + source.get_collection().get_visited_by(address_compiler); + + if (address_compiler.has_init()) + { + init_instructions.add(address_compiler.get_init()); + } + + init_instructions.add + ( + new SetValue(result_as_address, address_compiler.get_computation()) + ); + + car_addr = + new RelativeAddress + ( + result_as_address, + new Constant(Type.STRING, "0"), + address_compiler.get_computation().get_type() + ); + + init_instructions.add + ( + new SetValue + ( + car_addr, + address_compiler.get_computation() + ) + ); + + address_compiler.release_registers(init_instructions); + + cdr_addr = + new RelativeAddress + ( + result_as_address, + new Constant(Type.STRING, "1"), + TypeCompiler.compile + ( + compiler, + ( + (tonkadur.fate.v1.lang.type.CollectionType) + source.get_collection().get_type() + ).get_content_type() + ) + ); + + init_instructions.add(new Initialize(cdr_addr)); + + init_instructions.add + ( + tonkadur.wyrd.v1.compiler.util.PopElement.generate + ( + compiler.registers(), + compiler.assembler(), + car_addr, + cdr_addr, + source.is_from_left() + ) + ); + } +} diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/PushElementComputationCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/PushElementComputationCompiler.java new file mode 100644 index 0000000..7b7169e --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/PushElementComputationCompiler.java @@ -0,0 +1,113 @@ +package tonkadur.wyrd.v1.compiler.fate.v1.computation.generic; + +import tonkadur.fate.v1.lang.computation.generic.PushElementComputation; + +import tonkadur.wyrd.v1.lang.Register; + +import tonkadur.wyrd.v1.lang.instruction.SetValue; + +import tonkadur.wyrd.v1.lang.type.Type; + +import tonkadur.wyrd.v1.lang.computation.Size; +import tonkadur.wyrd.v1.lang.computation.Constant; + +import tonkadur.wyrd.v1.compiler.fate.v1.Compiler; +import tonkadur.wyrd.v1.compiler.fate.v1.TypeCompiler; +import tonkadur.wyrd.v1.compiler.fate.v1.ComputationCompiler; + +import tonkadur.wyrd.v1.compiler.fate.v1.computation.GenericComputationCompiler; + +public class PushElementComputationCompiler +extends GenericComputationCompiler +{ + public static Class get_target_class () + { + return PushElementComputation.class; + } + + public PushElementComputationCompiler (final Compiler compiler) + { + super(compiler); + } + + public void compile + ( + final tonkadur.fate.v1.lang.computation.GenericComputation computation + ) + throws Throwable + { + final PushElementComputation source; + final ComputationCompiler address_compiler, element_compiler; + final Register result, collection_size, index; + + source = (PushElementComputation) computation; + + result = reserve(TypeCompiler.compile(compiler, source.get_type())); + result_as_address = result.get_address(); + result_as_computation = result.get_value(); + + address_compiler = new ComputationCompiler(compiler); + element_compiler = new ComputationCompiler(compiler); + + source.get_collection().get_visited_by(address_compiler); + + if (address_compiler.has_init()) + { + init_instructions.add(address_compiler.get_init()); + } + + init_instructions.add + ( + new SetValue(result_as_address, address_compiler.get_computation()) + ); + + address_compiler.release_registers(init_instructions); + + source.get_element().get_visited_by(element_compiler); + + if (element_compiler.has_init()) + { + init_instructions.add(element_compiler.get_init()); + } + + collection_size = reserve(Type.INT); + index = reserve(Type.INT); + + init_instructions.add + ( + new SetValue + ( + collection_size.get_address(), + new Size(result_as_address) + ) + ); + + init_instructions.add + ( + new SetValue + ( + index.get_address(), + ( + source.is_from_left() ? + Constant.ZERO : + collection_size.get_value() + ) + ) + ); + + init_instructions.add + ( + tonkadur.wyrd.v1.compiler.util.InsertAt.generate + ( + compiler.registers(), + compiler.assembler(), + index.get_address(), + element_compiler.get_computation(), + collection_size.get_value(), + result_as_address + ) + ); + + element_compiler.release_registers(init_instructions); + } +} diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/RangeCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/RangeCompiler.java new file mode 100644 index 0000000..a6abeb3 --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/RangeCompiler.java @@ -0,0 +1,160 @@ +package tonkadur.wyrd.v1.compiler.fate.v1.computation.generic; + +import java.util.ArrayList; +import java.util.List; + +import tonkadur.fate.v1.lang.computation.generic.Range; + +import tonkadur.wyrd.v1.lang.instruction.SetValue; +import tonkadur.wyrd.v1.lang.instruction.Initialize; + +import tonkadur.wyrd.v1.lang.Register; + +import tonkadur.wyrd.v1.lang.computation.Address; +import tonkadur.wyrd.v1.lang.computation.RelativeAddress; +import tonkadur.wyrd.v1.lang.computation.Cast; +import tonkadur.wyrd.v1.lang.computation.Constant; +import tonkadur.wyrd.v1.lang.computation.Operation; + +import tonkadur.wyrd.v1.lang.meta.Instruction; + +import tonkadur.wyrd.v1.lang.type.Type; +import tonkadur.wyrd.v1.lang.type.MapType; + +import tonkadur.wyrd.v1.compiler.fate.v1.Compiler; +import tonkadur.wyrd.v1.compiler.fate.v1.ComputationCompiler; + +import tonkadur.wyrd.v1.compiler.fate.v1.computation.GenericComputationCompiler; + +public class RangeCompiler +extends GenericComputationCompiler +{ + public static Class get_target_class () + { + return Range.class; + } + + public RangeCompiler (final Compiler compiler) + { + super(compiler); + } + + public void compile + ( + final tonkadur.fate.v1.lang.computation.GenericComputation computation + ) + throws Throwable + { + final Range source; + final List while_body; + final ComputationCompiler start_cc, end_cc, inc_cc; + final Register result, iterator, accumulator; + final Address new_element_addr; + + source = (Range) computation; + + while_body = new ArrayList(); + + result = reserve(MapType.MAP_TO_INT); + result_as_address = result.get_address(); + result_as_computation = result.get_value(); + + start_cc = new ComputationCompiler(compiler); + end_cc = new ComputationCompiler(compiler); + inc_cc = new ComputationCompiler(compiler); + + source.get_start().get_visited_by(start_cc); + start_cc.generate_address(); + + source.get_end().get_visited_by(end_cc); + end_cc.generate_address(); + + source.get_increment().get_visited_by(inc_cc); + inc_cc.generate_address(); + + assimilate(start_cc); + assimilate(end_cc); + assimilate(inc_cc); + + iterator = reserve(Type.INT); + accumulator = reserve(Type.INT); + + init_instructions.add + ( + new SetValue(iterator.get_address(), Constant.ZERO) + ); + init_instructions.add + ( + new SetValue(accumulator.get_address(), start_cc.get_computation()) + ); + + new_element_addr = + new RelativeAddress + ( + result_as_address, + new Cast(iterator.get_value(), Type.STRING), + Type.INT + ); + + while_body.add(new Initialize(new_element_addr)); + while_body.add + ( + new SetValue(new_element_addr, accumulator.get_value()) + ); + while_body.add + ( + new SetValue + ( + accumulator.get_address(), + Operation.plus(accumulator.get_value(), inc_cc.get_computation()) + ) + ); + while_body.add + ( + new SetValue + ( + iterator.get_address(), + Operation.plus(iterator.get_value(), Constant.ONE) + ) + ); + + init_instructions.add + ( + tonkadur.wyrd.v1.compiler.util.While.generate + ( + compiler.registers(), + compiler.assembler(), + Operation.or + ( + Operation.and + ( + Operation.greater_than + ( + inc_cc.get_computation(), + Constant.ZERO + ), + Operation.less_equal_than + ( + accumulator.get_value(), + end_cc.get_computation() + ) + ), + Operation.and + ( + Operation.less_equal_than + ( + inc_cc.get_computation(), + Constant.ZERO + ), + Operation.greater_equal_than + ( + accumulator.get_value(), + end_cc.get_computation() + ) + ) + ), + compiler.assembler().merge(while_body) + ) + ); + } +} diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/RemoveAllOfElementComputationCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/RemoveAllOfElementComputationCompiler.java new file mode 100644 index 0000000..651f4aa --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/RemoveAllOfElementComputationCompiler.java @@ -0,0 +1,81 @@ +package tonkadur.wyrd.v1.compiler.fate.v1.computation.generic; + +import tonkadur.fate.v1.lang.computation.generic.RemoveAllOfElementComputation; + +import tonkadur.wyrd.v1.lang.computation.Size; +import tonkadur.wyrd.v1.lang.computation.Address; + +import tonkadur.wyrd.v1.lang.instruction.SetValue; + +import tonkadur.wyrd.v1.lang.Register; + +import tonkadur.wyrd.v1.compiler.fate.v1.Compiler; +import tonkadur.wyrd.v1.compiler.fate.v1.ComputationCompiler; +import tonkadur.wyrd.v1.compiler.fate.v1.TypeCompiler; + +import tonkadur.wyrd.v1.compiler.fate.v1.computation.GenericComputationCompiler; + +public class RemoveAllOfElementComputationCompiler +extends GenericComputationCompiler +{ + public static Class get_target_class () + { + return RemoveAllOfElementComputation.class; + } + + public RemoveAllOfElementComputationCompiler (final Compiler compiler) + { + super(compiler); + } + + public void compile + ( + final tonkadur.fate.v1.lang.computation.GenericComputation computation + ) + throws Throwable + { + final RemoveAllOfElementComputation source; + final ComputationCompiler address_compiler, target_compiler; + final Address collection_address; + final Register result; + + source = (RemoveAllOfElementComputation) computation; + + result = reserve(TypeCompiler.compile(compiler, source.get_type())); + result_as_address = result.get_address(); + result_as_computation = result.get_value(); + + address_compiler = new ComputationCompiler(compiler); + target_compiler = new ComputationCompiler(compiler); + + source.get_collection().get_visited_by(address_compiler); + + if (address_compiler.has_init()) + { + init_instructions.add(address_compiler.get_init()); + } + + init_instructions.add + ( + new SetValue(result_as_address, address_compiler.get_computation()) + ); + + address_compiler.release_registers(init_instructions); + + source.get_element().get_visited_by(target_compiler); + + assimilate(target_compiler); + + init_instructions.add + ( + tonkadur.wyrd.v1.compiler.util.RemoveAllOf.generate + ( + compiler.registers(), + compiler.assembler(), + target_compiler.get_computation(), + new Size(result_as_address), + result_as_address + ) + ); + } +} diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/RemoveElementAtComputationCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/RemoveElementAtComputationCompiler.java new file mode 100644 index 0000000..2670814 --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/RemoveElementAtComputationCompiler.java @@ -0,0 +1,83 @@ +package tonkadur.wyrd.v1.compiler.fate.v1.computation.generic; + +import tonkadur.fate.v1.lang.computation.generic.RemoveElementAtComputation; + +import tonkadur.wyrd.v1.lang.computation.Size; +import tonkadur.wyrd.v1.lang.computation.Address; + +import tonkadur.wyrd.v1.lang.instruction.SetValue; + +import tonkadur.wyrd.v1.lang.Register; + +import tonkadur.wyrd.v1.compiler.fate.v1.Compiler; +import tonkadur.wyrd.v1.compiler.fate.v1.ComputationCompiler; +import tonkadur.wyrd.v1.compiler.fate.v1.TypeCompiler; + +import tonkadur.wyrd.v1.compiler.fate.v1.computation.GenericComputationCompiler; + +public class RemoveElementAtComputationCompiler +extends GenericComputationCompiler +{ + public static Class get_target_class () + { + return RemoveElementAtComputation.class; + } + + public RemoveElementAtComputationCompiler (final Compiler compiler) + { + super(compiler); + } + + public void compile + ( + final tonkadur.fate.v1.lang.computation.GenericComputation computation + ) + throws Throwable + { + final RemoveElementAtComputation source; + final ComputationCompiler address_compiler, target_compiler; + final Address collection_address; + final Register result, target_index; + + source = (RemoveElementAtComputation) computation; + + result = reserve(TypeCompiler.compile(compiler, source.get_type())); + result_as_address = result.get_address(); + result_as_computation = result.get_value(); + + address_compiler = new ComputationCompiler(compiler); + target_compiler = new ComputationCompiler(compiler); + + source.get_collection().get_visited_by(address_compiler); + + if (address_compiler.has_init()) + { + init_instructions.add(address_compiler.get_init()); + } + + init_instructions.add + ( + new SetValue(result_as_address, address_compiler.get_computation()) + ); + + address_compiler.release_registers(init_instructions); + + source.get_index().get_visited_by(target_compiler); + + target_compiler.generate_address(); + + assimilate(target_compiler); + + init_instructions.add + ( + tonkadur.wyrd.v1.compiler.util.RemoveAt.generate + ( + compiler.registers(), + compiler.assembler(), + target_compiler.get_address(), + new Size(result_as_address), + result_as_address + ) + ); + } +} diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/RemoveElementComputationCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/RemoveElementComputationCompiler.java new file mode 100644 index 0000000..4a5d4c8 --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/RemoveElementComputationCompiler.java @@ -0,0 +1,82 @@ +package tonkadur.wyrd.v1.compiler.fate.v1.computation.generic; + +import tonkadur.fate.v1.lang.computation.generic.RemoveElementComputation; + +import tonkadur.wyrd.v1.lang.instruction.SetValue; + +import tonkadur.wyrd.v1.lang.Register; + +import tonkadur.wyrd.v1.compiler.fate.v1.Compiler; +import tonkadur.wyrd.v1.compiler.fate.v1.ComputationCompiler; +import tonkadur.wyrd.v1.compiler.fate.v1.TypeCompiler; + +import tonkadur.wyrd.v1.compiler.fate.v1.computation.GenericComputationCompiler; + +public class RemoveElementComputationCompiler +extends GenericComputationCompiler +{ + public static Class get_target_class () + { + return RemoveElementComputation.class; + } + + public RemoveElementComputationCompiler (final Compiler compiler) + { + super(compiler); + } + + public void compile + ( + final tonkadur.fate.v1.lang.computation.GenericComputation computation + ) + throws Throwable + { + final RemoveElementComputation source; + final ComputationCompiler elem_cc, collection_cc; + final Register result; + + source = (RemoveElementComputation) computation; + + elem_cc = new ComputationCompiler(compiler); + collection_cc = new ComputationCompiler(compiler); + + result = reserve(TypeCompiler.compile(compiler, source.get_type())); + result_as_address = result.get_address(); + result_as_computation = result.get_value(); + + source.get_collection().get_visited_by(collection_cc); + + if (collection_cc.has_init()) + { + init_instructions.add(collection_cc.get_init()); + } + + init_instructions.add + ( + new SetValue(result_as_address, collection_cc.get_computation()) + ); + + collection_cc.release_registers(init_instructions); + + source.get_element().get_visited_by(elem_cc); + + elem_cc.generate_address(); + + assimilate(elem_cc); + + init_instructions.add + ( + tonkadur.wyrd.v1.compiler.util.RemoveOneOf.generate + ( + compiler.registers(), + compiler.assembler(), + elem_cc.get_computation(), + result_as_address, + ( + (tonkadur.fate.v1.lang.type.CollectionType) + source.get_collection().get_type() + ).is_set() + ) + ); + } +} diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/RemoveElementsOfComputationCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/RemoveElementsOfComputationCompiler.java new file mode 100644 index 0000000..6415dac --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/RemoveElementsOfComputationCompiler.java @@ -0,0 +1,85 @@ +package tonkadur.wyrd.v1.compiler.fate.v1.computation.generic; + +import tonkadur.fate.v1.lang.computation.generic.RemoveElementsOfComputation; + +import tonkadur.wyrd.v1.lang.computation.Size; +import tonkadur.wyrd.v1.lang.computation.Address; + +import tonkadur.wyrd.v1.lang.instruction.SetValue; + +import tonkadur.wyrd.v1.lang.Register; + +import tonkadur.wyrd.v1.compiler.fate.v1.Compiler; +import tonkadur.wyrd.v1.compiler.fate.v1.ComputationCompiler; +import tonkadur.wyrd.v1.compiler.fate.v1.TypeCompiler; + +import tonkadur.wyrd.v1.compiler.fate.v1.computation.GenericComputationCompiler; + +public class RemoveElementsOfComputationCompiler +extends GenericComputationCompiler +{ + public static Class get_target_class () + { + return RemoveElementsOfComputation.class; + } + + public RemoveElementsOfComputationCompiler (final Compiler compiler) + { + super(compiler); + } + + public void compile + ( + final tonkadur.fate.v1.lang.computation.GenericComputation computation + ) + throws Throwable + { + final RemoveElementsOfComputation source; + final ComputationCompiler collection_in_cc, collection_cc; + final Register result; + + source = (RemoveElementsOfComputation) computation; + + result = reserve(TypeCompiler.compile(compiler, source.get_type())); + + result_as_address = result.get_address(); + result_as_computation = result.get_value(); + + collection_cc = new ComputationCompiler(compiler); + + source.get_target_collection().get_visited_by(collection_cc); + + if (collection_cc.has_init()) + { + init_instructions.add(collection_cc.get_init()); + } + + init_instructions.add + ( + new SetValue(result_as_address, collection_cc.get_computation()) + ); + + collection_cc.release_registers(init_instructions); + + collection_in_cc = new ComputationCompiler(compiler); + + source.get_source_collection().get_visited_by(collection_in_cc); + + assimilate(collection_in_cc); + + init_instructions.add + ( + tonkadur.wyrd.v1.compiler.util.RemoveElementsOf.generate + ( + compiler.registers(), + compiler.assembler(), + collection_in_cc.get_address(), + result_as_address, + ( + (tonkadur.fate.v1.lang.type.CollectionType) + source.get_target_collection().get_type() + ).is_set() + ) + ); + } +} diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/ReverseListComputationCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/ReverseListComputationCompiler.java new file mode 100644 index 0000000..0c39eee --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/ReverseListComputationCompiler.java @@ -0,0 +1,73 @@ +package tonkadur.wyrd.v1.compiler.fate.v1.computation.generic; + +import tonkadur.fate.v1.lang.computation.generic.ReverseListComputation; + +import tonkadur.wyrd.v1.lang.Register; + +import tonkadur.wyrd.v1.lang.instruction.SetValue; + +import tonkadur.wyrd.v1.lang.computation.Size; + +import tonkadur.wyrd.v1.compiler.fate.v1.Compiler; +import tonkadur.wyrd.v1.compiler.fate.v1.TypeCompiler; +import tonkadur.wyrd.v1.compiler.fate.v1.ComputationCompiler; + +import tonkadur.wyrd.v1.compiler.fate.v1.computation.GenericComputationCompiler; + +public class ReverseListComputationCompiler +extends GenericComputationCompiler +{ + public static Class get_target_class () + { + return ReverseListComputation.class; + } + + public ReverseListComputationCompiler (final Compiler compiler) + { + super(compiler); + } + + public void compile + ( + final tonkadur.fate.v1.lang.computation.GenericComputation computation + ) + throws Throwable + { + final ReverseListComputation source; + final ComputationCompiler address_compiler; + final Register result; + + source = (ReverseListComputation) computation; + + result = reserve(TypeCompiler.compile(compiler, source.get_type())); + result_as_address = result.get_address(); + result_as_computation = result.get_value(); + + address_compiler = new ComputationCompiler(compiler); + + source.get_collection().get_visited_by(address_compiler); + + if (address_compiler.has_init()) + { + init_instructions.add(address_compiler.get_init()); + } + + init_instructions.add + ( + new SetValue(result_as_address, address_compiler.get_computation()) + ); + + address_compiler.release_registers(init_instructions); + + init_instructions.add + ( + tonkadur.wyrd.v1.compiler.util.ReverseList.generate + ( + compiler.registers(), + compiler.assembler(), + new Size(result_as_address), + result_as_address + ) + ); + } +} diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/ShuffleComputationCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/ShuffleComputationCompiler.java new file mode 100644 index 0000000..b8477c8 --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/ShuffleComputationCompiler.java @@ -0,0 +1,72 @@ +package tonkadur.wyrd.v1.compiler.fate.v1.computation.generic; + +import tonkadur.fate.v1.lang.computation.generic.ShuffleComputation; + +import tonkadur.wyrd.v1.lang.Register; + +import tonkadur.wyrd.v1.lang.instruction.SetValue; + +import tonkadur.wyrd.v1.lang.computation.Size; + +import tonkadur.wyrd.v1.compiler.fate.v1.Compiler; +import tonkadur.wyrd.v1.compiler.fate.v1.TypeCompiler; +import tonkadur.wyrd.v1.compiler.fate.v1.ComputationCompiler; + +import tonkadur.wyrd.v1.compiler.fate.v1.computation.GenericComputationCompiler; + +public class ShuffleComputationCompiler +extends GenericComputationCompiler +{ + public static Class get_target_class () + { + return ShuffleComputation.class; + } + + public ShuffleComputationCompiler (final Compiler compiler) + { + super(compiler); + } + + public void compile + ( + final tonkadur.fate.v1.lang.computation.GenericComputation computation + ) + throws Throwable + { + final ShuffleComputation source; + final ComputationCompiler address_compiler; + final Register result; + + source = (ShuffleComputation) computation; + + result = reserve(TypeCompiler.compile(compiler, source.get_type())); + result_as_address = result.get_address(); + result_as_computation = result.get_value(); + + address_compiler = new ComputationCompiler(compiler); + + source.get_collection().get_visited_by(address_compiler); + + if (address_compiler.has_init()) + { + init_instructions.add(address_compiler.get_init()); + } + + init_instructions.add + ( + new SetValue(result_as_address, address_compiler.get_computation()) + ); + + address_compiler.release_registers(init_instructions); + + init_instructions.add + ( + tonkadur.wyrd.v1.compiler.util.Shuffle.generate + ( + compiler.registers(), + compiler.assembler(), + result_as_address + ) + ); + } +} diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/SizeOperatorCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/SizeOperatorCompiler.java new file mode 100644 index 0000000..2c8bfff --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/SizeOperatorCompiler.java @@ -0,0 +1,44 @@ +package tonkadur.wyrd.v1.compiler.fate.v1.computation.generic; + +import tonkadur.fate.v1.lang.computation.generic.SizeOperator; + +import tonkadur.wyrd.v1.lang.computation.Size; + +import tonkadur.wyrd.v1.compiler.fate.v1.Compiler; +import tonkadur.wyrd.v1.compiler.fate.v1.ComputationCompiler; + +import tonkadur.wyrd.v1.compiler.fate.v1.computation.GenericComputationCompiler; + +public class SizeOperatorCompiler +extends GenericComputationCompiler +{ + public static Class get_target_class () + { + return SizeOperator.class; + } + + public SizeOperatorCompiler (final Compiler compiler) + { + super(compiler); + } + + public void compile + ( + final tonkadur.fate.v1.lang.computation.GenericComputation computation + ) + throws Throwable + { + final SizeOperator source; + final ComputationCompiler cc; + + source = (SizeOperator) computation; + + cc = new ComputationCompiler(compiler); + + source.get_collection().get_visited_by(cc); + + assimilate(cc); + + result_as_computation = new Size(cc.get_address()); + } +} diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/SortComputationCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/SortComputationCompiler.java new file mode 100644 index 0000000..f4805e1 --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/SortComputationCompiler.java @@ -0,0 +1,101 @@ +package tonkadur.wyrd.v1.compiler.fate.v1.computation.generic; + +import java.util.ArrayList; +import java.util.List; + +import tonkadur.fate.v1.lang.computation.generic.SortComputation; + +import tonkadur.wyrd.v1.lang.Register; + +import tonkadur.wyrd.v1.lang.meta.Computation; + +import tonkadur.wyrd.v1.compiler.fate.v1.Compiler; +import tonkadur.wyrd.v1.compiler.fate.v1.TypeCompiler; +import tonkadur.wyrd.v1.compiler.fate.v1.ComputationCompiler; + +import tonkadur.wyrd.v1.compiler.fate.v1.computation.GenericComputationCompiler; + +public class SortComputationCompiler +extends GenericComputationCompiler +{ + public static Class get_target_class () + { + return SortComputation.class; + } + + public SortComputationCompiler (final Compiler compiler) + { + super(compiler); + } + + public void compile + ( + final tonkadur.fate.v1.lang.computation.GenericComputation computation + ) + throws Throwable + { + final SortComputation source; + final List params; + final ComputationCompiler lambda_cc, in_collection_cc; + final Register result; + + source = (SortComputation) computation; + + result = reserve(TypeCompiler.compile(compiler, source.get_type())); + + result_as_address = result.get_address(); + result_as_computation = result.get_value(); + + params = new ArrayList(); + + for + ( + final tonkadur.fate.v1.lang.meta.Computation p: + source.get_extra_parameters() + ) + { + final ComputationCompiler param_cc; + + param_cc = new ComputationCompiler(compiler); + + p.get_visited_by(param_cc); + + // Let's not re-compute the parameters on every iteration. + param_cc.generate_address(); + + assimilate(param_cc); + + params.add(param_cc.get_computation()); + } + + lambda_cc = new ComputationCompiler(compiler); + + source.get_lambda_function().get_visited_by(lambda_cc); + + assimilate(lambda_cc); + + in_collection_cc = new ComputationCompiler(compiler); + + source.get_collection().get_visited_by(in_collection_cc); + + if (in_collection_cc.has_init()) + { + init_instructions.add(in_collection_cc.get_init()); + } + + init_instructions.add + ( + tonkadur.wyrd.v1.compiler.util.Sort.generate + ( + compiler.registers(), + compiler.assembler(), + lambda_cc.get_computation(), + in_collection_cc.get_address(), + result_as_address, + params + ) + ); + + in_collection_cc.release_registers(init_instructions); + } +} diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/SubListComputationCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/SubListComputationCompiler.java new file mode 100644 index 0000000..929eddb --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/SubListComputationCompiler.java @@ -0,0 +1,84 @@ +package tonkadur.wyrd.v1.compiler.fate.v1.computation.generic; + +import tonkadur.fate.v1.lang.computation.generic.SubListComputation; + +import tonkadur.wyrd.v1.lang.Register; + +import tonkadur.wyrd.v1.compiler.fate.v1.Compiler; +import tonkadur.wyrd.v1.compiler.fate.v1.TypeCompiler; +import tonkadur.wyrd.v1.compiler.fate.v1.ComputationCompiler; + +import tonkadur.wyrd.v1.compiler.fate.v1.computation.GenericComputationCompiler; + +public class SubListComputationCompiler +extends GenericComputationCompiler +{ + public static Class get_target_class () + { + return SubListComputation.class; + } + + public SubListComputationCompiler (final Compiler compiler) + { + super(compiler); + } + + public void compile + ( + final tonkadur.fate.v1.lang.computation.GenericComputation computation + ) + throws Throwable + { + final SubListComputation source; + final ComputationCompiler address_compiler, start_compiler, end_compiler; + final Register result; + + source = (SubListComputation) computation; + + result = reserve(TypeCompiler.compile(compiler, source.get_type())); + result_as_address = result.get_address(); + result_as_computation = result.get_value(); + + address_compiler = new ComputationCompiler(compiler); + start_compiler = new ComputationCompiler(compiler); + end_compiler = new ComputationCompiler(compiler); + + source.get_collection().get_visited_by(address_compiler); + + if (address_compiler.has_init()) + { + init_instructions.add(address_compiler.get_init()); + } + + source.get_start_index().get_visited_by(start_compiler); + + if (start_compiler.has_init()) + { + init_instructions.add(start_compiler.get_init()); + } + + source.get_end_index().get_visited_by(end_compiler); + + if (end_compiler.has_init()) + { + init_instructions.add(end_compiler.get_init()); + } + + init_instructions.add + ( + tonkadur.wyrd.v1.compiler.util.SubList.generate + ( + compiler.registers(), + compiler.assembler(), + start_compiler.get_computation(), + end_compiler.get_computation(), + address_compiler.get_address(), + result_as_address + ) + ); + + start_compiler.release_registers(init_instructions); + end_compiler.release_registers(init_instructions); + address_compiler.release_registers(init_instructions); + } +} -- cgit v1.2.3-70-g09d2