| summaryrefslogtreecommitdiff | 
diff options
Diffstat (limited to 'src/core')
22 files changed, 2653 insertions, 28 deletions
| diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/FilterComputation.java b/src/core/src/tonkadur/fate/v1/lang/computation/FilterComputation.java new file mode 100644 index 0000000..8884941 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/FilterComputation.java @@ -0,0 +1,87 @@ +package tonkadur.fate.v1.lang.computation; + +import tonkadur.parser.Origin; + +import tonkadur.error.ErrorManager; + +import tonkadur.fate.v1.lang.type.Type; +import tonkadur.fate.v1.lang.type.LambdaType; +import tonkadur.fate.v1.lang.type.CollectionType; + +import tonkadur.fate.v1.lang.instruction.Filter; + +import tonkadur.fate.v1.lang.meta.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.Reference; + +public class FilterComputation extends Computation +{ +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final Filter instruction; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected FilterComputation +   ( +      final Filter instruction, +      final Type output_type +   ) +   { +      super(instruction.get_origin(), output_type); + +      this.instruction = instruction; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   public static FilterComputation build +   ( +      final Origin origin, +      final Computation lambda_function, +      final Reference collection_in +   ) +   throws Throwable +   { +      final Type type; +      final Filter parent; + +      parent = Filter.build(origin, lambda_function, collection_in); + +      type = collection_in.get_type(); + +      return new FilterComputation(parent, type); +   } + +   /**** Accessors ************************************************************/ +   @Override +   public void get_visited_by (final ComputationVisitor cv) +   throws Throwable +   { +      cv.visit_filter(this); +   } + +   public Filter get_instruction () +   { +      return instruction; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      sb.append("(ComputationOf "); +      sb.append(instruction.toString()); + +      sb.append(")"); + +      return sb.toString(); +   } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/IndexedMapComputation.java b/src/core/src/tonkadur/fate/v1/lang/computation/IndexedMapComputation.java new file mode 100644 index 0000000..dc3b9e4 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/IndexedMapComputation.java @@ -0,0 +1,94 @@ +package tonkadur.fate.v1.lang.computation; + +import tonkadur.parser.Origin; + +import tonkadur.error.ErrorManager; + +import tonkadur.fate.v1.lang.type.Type; +import tonkadur.fate.v1.lang.type.LambdaType; +import tonkadur.fate.v1.lang.type.CollectionType; + +import tonkadur.fate.v1.lang.instruction.IndexedMap; + +import tonkadur.fate.v1.lang.meta.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.Reference; + +public class IndexedMapComputation extends Computation +{ +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final IndexedMap instruction; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected IndexedMapComputation +   ( +      final IndexedMap instruction, +      final Type output_type +   ) +   { +      super(instruction.get_origin(), output_type); + +      this.instruction = instruction; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   public static IndexedMapComputation build +   ( +      final Origin origin, +      final Computation lambda_function, +      final Reference collection_in +   ) +   throws Throwable +   { +      final Type type; +      final IndexedMap parent; + +      parent = IndexedMap.build(origin, lambda_function, collection_in, null); + +      type = +         CollectionType.build +         ( +            origin, +            ((LambdaType) lambda_function.get_type()).get_return_type(), +            ((CollectionType) collection_in.get_type()).is_set(), +            "auto generated" +         ); + +      return new IndexedMapComputation(parent, type); +   } + +   /**** Accessors ************************************************************/ +   @Override +   public void get_visited_by (final ComputationVisitor cv) +   throws Throwable +   { +      cv.visit_indexed_map(this); +   } + +   public IndexedMap get_instruction () +   { +      return instruction; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      sb.append("(ComputationOf "); +      sb.append(instruction.toString()); + +      sb.append(")"); + +      return sb.toString(); +   } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/MergeComputation.java b/src/core/src/tonkadur/fate/v1/lang/computation/MergeComputation.java new file mode 100644 index 0000000..3b742c1 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/MergeComputation.java @@ -0,0 +1,110 @@ +package tonkadur.fate.v1.lang.computation; + +import tonkadur.parser.Origin; + +import tonkadur.error.ErrorManager; + +import tonkadur.fate.v1.lang.type.Type; +import tonkadur.fate.v1.lang.type.LambdaType; +import tonkadur.fate.v1.lang.type.CollectionType; + +import tonkadur.fate.v1.lang.instruction.Merge; + +import tonkadur.fate.v1.lang.meta.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.Reference; + +public class MergeComputation extends Computation +{ +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final Merge instruction; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected MergeComputation +   ( +      final Merge instruction, +      final Type output_type +   ) +   { +      super(instruction.get_origin(), output_type); + +      this.instruction = instruction; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   public static MergeComputation build +   ( +      final Origin origin, +      final Computation lambda_function, +      final Reference collection_in_a, +      final Computation default_a, +      final Reference collection_in_b, +      final Computation default_b +   ) +   throws Throwable +   { +      final Type type; +      final Merge parent; + +      parent = +         Merge.build +         ( +            origin, +            lambda_function, +            collection_in_a, +            default_a, +            collection_in_b, +            default_b, +            null +         ); + +      type = +         CollectionType.build +         ( +            origin, +            ((LambdaType) lambda_function.get_type()).get_return_type(), +            ( +               ((CollectionType) collection_in_a.get_type()).is_set() +               || ((CollectionType) collection_in_b.get_type()).is_set() +            ), +            "auto generated" +         ); + +      return new MergeComputation(parent, type); +   } + +   /**** Accessors ************************************************************/ +   @Override +   public void get_visited_by (final ComputationVisitor cv) +   throws Throwable +   { +      cv.visit_merge(this); +   } + +   public Merge get_instruction () +   { +      return instruction; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      sb.append("(ComputationOf "); +      sb.append(instruction.toString()); + +      sb.append(")"); + +      return sb.toString(); +   } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/PartitionComputation.java b/src/core/src/tonkadur/fate/v1/lang/computation/PartitionComputation.java new file mode 100644 index 0000000..ed578dd --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/PartitionComputation.java @@ -0,0 +1,102 @@ +package tonkadur.fate.v1.lang.computation; + +import tonkadur.parser.Origin; + +import tonkadur.error.ErrorManager; + +import tonkadur.fate.v1.lang.type.CollectionType; +import tonkadur.fate.v1.lang.type.ConsType; +import tonkadur.fate.v1.lang.type.LambdaType; +import tonkadur.fate.v1.lang.type.Type; + +import tonkadur.fate.v1.lang.instruction.Partition; + +import tonkadur.fate.v1.lang.meta.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.Reference; + +public class PartitionComputation extends Computation +{ +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final Partition instruction; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected PartitionComputation +   ( +      final Partition instruction, +      final Type output_type +   ) +   { +      super(instruction.get_origin(), output_type); + +      this.instruction = instruction; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   public static PartitionComputation build +   ( +      final Origin origin, +      final Computation lambda_function, +      final Reference collection_in +   ) +   throws Throwable +   { +      final Type type; +      final Partition parent; + +      parent = +         Partition.build +         ( +            origin, +            lambda_function, +            collection_in, +            null +         ); + +      type = +         new ConsType +         ( +            origin, +            collection_in.get_type(), +            collection_in.get_type(), +            "auto generated" +         ); + +      return new PartitionComputation(parent, type); +   } + +   /**** Accessors ************************************************************/ +   @Override +   public void get_visited_by (final ComputationVisitor cv) +   throws Throwable +   { +      cv.visit_partition(this); +   } + +   public Partition get_instruction () +   { +      return instruction; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      sb.append("(ComputationOf "); +      sb.append(instruction.toString()); + +      sb.append(")"); + +      return sb.toString(); +   } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/PopElementComputation.java b/src/core/src/tonkadur/fate/v1/lang/computation/PopElementComputation.java new file mode 100644 index 0000000..6e0a7f0 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/PopElementComputation.java @@ -0,0 +1,79 @@ +package tonkadur.fate.v1.lang.computation; + +import tonkadur.parser.Origin; + +import tonkadur.fate.v1.error.InvalidTypeException; + +import tonkadur.fate.v1.lang.instruction.PopElement; + +import tonkadur.fate.v1.lang.meta.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; + +public class PopElementComputation extends Computation +{ +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final PopElement instruction; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected PopElementComputation +   ( +      final PopElement instruction +   ) +   { +      super(instruction.get_origin(), instruction.get_collection().get_type()); + +      this.instruction = instruction; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   public static PopElementComputation build +   ( +      final Origin origin, +      final Computation collection, +      final boolean is_from_left +   ) +   throws +      InvalidTypeException +   { +      return +         new PopElementComputation +         ( +            PopElement.build(origin, collection, is_from_left) +         ); +   } + +   /**** Accessors ************************************************************/ +   @Override +   public void get_visited_by (final ComputationVisitor cv) +   throws Throwable +   { +      cv.visit_pop_element(this); +   } + +   public PopElement get_instruction () +   { +      return instruction; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      sb.append("(ComputationOf "); +      sb.append(instruction.toString()); + +      sb.append(")"); + +      return sb.toString(); +   } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/PushElementComputation.java b/src/core/src/tonkadur/fate/v1/lang/computation/PushElementComputation.java new file mode 100644 index 0000000..5347f0a --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/PushElementComputation.java @@ -0,0 +1,84 @@ +package tonkadur.fate.v1.lang.computation; + +import tonkadur.parser.Origin; + +import tonkadur.fate.v1.error.ConflictingTypeException; +import tonkadur.fate.v1.error.IncomparableTypeException; +import tonkadur.fate.v1.error.InvalidTypeException; + +import tonkadur.fate.v1.lang.instruction.PushElement; + +import tonkadur.fate.v1.lang.meta.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; + +public class PushElementComputation extends Computation +{ +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final PushElement instruction; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected PushElementComputation +   ( +      final PushElement instruction +   ) +   { +      super(instruction.get_origin(), instruction.get_collection().get_type()); + +      this.instruction = instruction; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   public static PushElementComputation build +   ( +      final Origin origin, +      final Computation element, +      final Computation collection, +      final boolean is_from_left +   ) +   throws +      InvalidTypeException, +      ConflictingTypeException, +      IncomparableTypeException +   { +      return +         new PushElementComputation +         ( +            PushElement.build(origin, element, collection, is_from_left) +         ); +   } + +   /**** Accessors ************************************************************/ +   @Override +   public void get_visited_by (final ComputationVisitor cv) +   throws Throwable +   { +      cv.visit_push_element(this); +   } + +   public PushElement get_instruction () +   { +      return instruction; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      sb.append("(ComputationOf "); +      sb.append(instruction.toString()); + +      sb.append(")"); + +      return sb.toString(); +   } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/SortComputation.java b/src/core/src/tonkadur/fate/v1/lang/computation/SortComputation.java new file mode 100644 index 0000000..fab922b --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/SortComputation.java @@ -0,0 +1,87 @@ +package tonkadur.fate.v1.lang.computation; + +import tonkadur.parser.Origin; + +import tonkadur.error.ErrorManager; + +import tonkadur.fate.v1.lang.type.Type; +import tonkadur.fate.v1.lang.type.LambdaType; +import tonkadur.fate.v1.lang.type.CollectionType; + +import tonkadur.fate.v1.lang.instruction.Sort; + +import tonkadur.fate.v1.lang.meta.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.Reference; + +public class SortComputation extends Computation +{ +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final Sort instruction; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected SortComputation +   ( +      final Sort instruction +   ) +   { +      super +      ( +         instruction.get_origin(), +         instruction.get_collection().get_type() +      ); + +      this.instruction = instruction; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   public static SortComputation build +   ( +      final Origin origin, +      final Computation lambda_function, +      final Reference collection +   ) +   throws Throwable +   { +      final Sort parent; + +      parent = Sort.build(origin, lambda_function, collection); + +      return new SortComputation(parent); +   } + +   /**** Accessors ************************************************************/ +   @Override +   public void get_visited_by (final ComputationVisitor cv) +   throws Throwable +   { +      cv.visit_sort(this); +   } + +   public Sort get_instruction () +   { +      return instruction; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      sb.append("(ComputationOf "); +      sb.append(instruction.toString()); + +      sb.append(")"); + +      return sb.toString(); +   } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/SubListComputation.java b/src/core/src/tonkadur/fate/v1/lang/computation/SubListComputation.java new file mode 100644 index 0000000..39133c2 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/SubListComputation.java @@ -0,0 +1,87 @@ +package tonkadur.fate.v1.lang.computation; + +import tonkadur.parser.Origin; + +import tonkadur.error.ErrorManager; + +import tonkadur.fate.v1.lang.type.Type; +import tonkadur.fate.v1.lang.type.CollectionType; + +import tonkadur.fate.v1.lang.instruction.SubList; + +import tonkadur.fate.v1.lang.meta.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.Reference; + +public class SubListComputation extends Computation +{ +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final SubList instruction; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected SubListComputation +   ( +      final SubList instruction, +      final Type output_type +   ) +   { +      super(instruction.get_origin(), output_type); + +      this.instruction = instruction; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   public static SubListComputation build +   ( +      final Origin origin, +      final Computation start, +      final Computation end, +      final Reference collection_in +   ) +   throws Throwable +   { +      final Type type; +      final SubList parent; + +      parent = SubList.build(origin, start, end, collection_in, null); + +      type = collection_in.get_type(); + +      return new SubListComputation(parent, type); +   } + +   /**** Accessors ************************************************************/ +   @Override +   public void get_visited_by (final ComputationVisitor cv) +   throws Throwable +   { +      cv.visit_sublist(this); +   } + +   public SubList get_instruction () +   { +      return instruction; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      sb.append("(ComputationOf "); +      sb.append(instruction.toString()); + +      sb.append(")"); + +      return sb.toString(); +   } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/Filter.java b/src/core/src/tonkadur/fate/v1/lang/instruction/Filter.java new file mode 100644 index 0000000..d5f75b5 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/instruction/Filter.java @@ -0,0 +1,177 @@ +package tonkadur.fate.v1.lang.instruction; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import tonkadur.parser.Origin; +import tonkadur.parser.ParsingError; + +import tonkadur.error.ErrorManager; + +import tonkadur.fate.v1.error.IncompatibleTypeException; +import tonkadur.fate.v1.error.IncomparableTypeException; +import tonkadur.fate.v1.error.InvalidArityException; +import tonkadur.fate.v1.error.InvalidTypeException; + +import tonkadur.fate.v1.lang.type.Type; +import tonkadur.fate.v1.lang.type.LambdaType; +import tonkadur.fate.v1.lang.type.CollectionType; + +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.Instruction; +import tonkadur.fate.v1.lang.meta.InstructionVisitor; +import tonkadur.fate.v1.lang.meta.Reference; + +public class Filter extends Instruction +{ +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final Computation lambda_function; +   protected final Reference collection; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected Filter +   ( +      final Origin origin, +      final Computation lambda_function, +      final Reference collection +   ) +   { +      super(origin); + +      this.lambda_function = lambda_function; +      this.collection = collection; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   public static Filter build +   ( +      final Origin origin, +      final Computation lambda_function, +      final Reference collection +   ) +   throws Throwable +   { +      final Type var_type, collection_generic_type; +      final Type collection_out_generic_type; +      final CollectionType collection_type; +      final LambdaType lambda_type; +      final List<Type> signature; + +      var_type = lambda_function.get_type(); + +      if (!(var_type instanceof LambdaType)) +      { +         ErrorManager.handle +         ( +            new InvalidTypeException +            ( +               origin, +               var_type, +               Collections.singleton(Type.LAMBDA) +            ) +         ); + +         return null; +      } + +      lambda_type = (LambdaType) var_type; + +      signature = lambda_type.get_signature(); + +      if (signature.size() != 1) +      { +         ErrorManager.handle +         ( +            new InvalidArityException +            ( +               lambda_function.get_origin(), +               signature.size(), +               1, +               1 +            ) +         ); +      } + +      collection_generic_type = collection.get_type(); + +      if (!(collection_generic_type instanceof CollectionType)) +      { +         ErrorManager.handle +         ( +            new InvalidTypeException +            ( +               collection.get_origin(), +               collection_generic_type, +               Type.COLLECTION_TYPES +            ) +         ); + +         return null; +      } + +      collection_type = (CollectionType) collection_generic_type; + +      if +      ( +         !collection_type.get_content_type().can_be_used_as(signature.get(0)) +      ) +      { +         /* TODO */ +      } + +      if (lambda_type.get_return_type().can_be_used_as(Type.BOOL)) +      { +         /* TODO */ +      } + +      return +         new Filter +         ( +            origin, +            lambda_function, +            collection +         ); +   } + +   /**** Accessors ************************************************************/ +   @Override +   public void get_visited_by (final InstructionVisitor iv) +   throws Throwable +   { +      iv.visit_filter(this); +   } + +   public Computation get_lambda_function () +   { +      return lambda_function; +   } + +   public Reference get_collection () +   { +      return collection; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      sb.append("(Filter "); +      sb.append(lambda_function.toString()); +      sb.append(" "); +      sb.append(collection.toString()); +      sb.append(")"); + +      return sb.toString(); +   } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/IndexedMap.java b/src/core/src/tonkadur/fate/v1/lang/instruction/IndexedMap.java new file mode 100644 index 0000000..d031559 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/instruction/IndexedMap.java @@ -0,0 +1,223 @@ +package tonkadur.fate.v1.lang.instruction; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import tonkadur.parser.Origin; +import tonkadur.parser.ParsingError; + +import tonkadur.error.ErrorManager; + +import tonkadur.fate.v1.error.IncompatibleTypeException; +import tonkadur.fate.v1.error.IncomparableTypeException; +import tonkadur.fate.v1.error.InvalidArityException; +import tonkadur.fate.v1.error.InvalidTypeException; + +import tonkadur.fate.v1.lang.type.Type; +import tonkadur.fate.v1.lang.type.LambdaType; +import tonkadur.fate.v1.lang.type.CollectionType; + +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.Instruction; +import tonkadur.fate.v1.lang.meta.InstructionVisitor; +import tonkadur.fate.v1.lang.meta.Reference; + +public class IndexedMap extends Instruction +{ +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final Computation lambda_function; +   protected final Reference collection_in; +   protected final Reference collection_out; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected IndexedMap +   ( +      final Origin origin, +      final Computation lambda_function, +      final Reference collection_in, +      final Reference collection_out +   ) +   { +      super(origin); + +      this.lambda_function = lambda_function; +      this.collection_in = collection_in; +      this.collection_out = collection_out; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   public static IndexedMap build +   ( +      final Origin origin, +      final Computation lambda_function, +      final Reference collection_in, +      final Reference collection_out +   ) +   throws Throwable +   { +      final Type var_type, collection_in_generic_type; +      final Type collection_out_generic_type; +      final CollectionType collection_in_type; +      final CollectionType collection_out_type; +      final LambdaType lambda_type; +      final List<Type> signature; + +      var_type = lambda_function.get_type(); + +      if (!(var_type instanceof LambdaType)) +      { +         ErrorManager.handle +         ( +            new InvalidTypeException +            ( +               origin, +               var_type, +               Collections.singleton(Type.LAMBDA) +            ) +         ); + +         return null; +      } + +      lambda_type = (LambdaType) var_type; + +      signature = lambda_type.get_signature(); + +      if (signature.size() != 1) +      { +         ErrorManager.handle +         ( +            new InvalidArityException +            ( +               lambda_function.get_origin(), +               signature.size(), +               1, +               1 +            ) +         ); +      } + +      collection_in_generic_type = collection_in.get_type(); + +      if (!(collection_in_generic_type instanceof CollectionType)) +      { +         ErrorManager.handle +         ( +            new InvalidTypeException +            ( +               collection_in.get_origin(), +               collection_in_generic_type, +               Type.COLLECTION_TYPES +            ) +         ); + +         return null; +      } + +      collection_in_type = (CollectionType) collection_in_generic_type; + +      if +      ( +         Type.INT.can_be_used_as(signature.get(0)) +      ) +      { +         /* TODO */ +      } + +      if +      ( +         !collection_in_type.get_content_type().can_be_used_as(signature.get(1)) +      ) +      { +         /* TODO */ +      } + +      collection_out_generic_type = collection_out.get_type(); + +      if (!(collection_out_generic_type instanceof CollectionType)) +      { +         ErrorManager.handle +         ( +            new InvalidTypeException +            ( +               collection_out.get_origin(), +               collection_out_generic_type, +               Type.COLLECTION_TYPES +            ) +         ); + +         return null; +      } + +      collection_out_type = (CollectionType) collection_out_generic_type; + +      if +      ( +         !collection_out_type.get_content_type().can_be_used_as +         ( +            lambda_type.get_return_type() +         ) +      ) +      { +         /* TODO */ +      } + +      return +         new IndexedMap +         ( +            origin, +            lambda_function, +            collection_in, +            collection_out +         ); +   } + +   /**** Accessors ************************************************************/ +   @Override +   public void get_visited_by (final InstructionVisitor iv) +   throws Throwable +   { +      iv.visit_indexed_map(this); +   } + +   public Computation get_lambda_function () +   { +      return lambda_function; +   } + +   public Reference get_collection_in () +   { +      return collection_in; +   } + +   public Reference get_collection_out () +   { +      return collection_out; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      sb.append("(IndexedMap "); +      sb.append(lambda_function.toString()); +      sb.append(" "); +      sb.append(collection_in.toString()); +      sb.append(" "); +      sb.append(collection_out.toString()); +      sb.append(")"); + +      return sb.toString(); +   } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/Merge.java b/src/core/src/tonkadur/fate/v1/lang/instruction/Merge.java new file mode 100644 index 0000000..f2fe200 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/instruction/Merge.java @@ -0,0 +1,344 @@ +package tonkadur.fate.v1.lang.instruction; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import tonkadur.parser.Origin; +import tonkadur.parser.ParsingError; + +import tonkadur.error.ErrorManager; + +import tonkadur.fate.v1.error.IncompatibleTypeException; +import tonkadur.fate.v1.error.IncomparableTypeException; +import tonkadur.fate.v1.error.InvalidArityException; +import tonkadur.fate.v1.error.InvalidTypeException; + +import tonkadur.fate.v1.lang.type.Type; +import tonkadur.fate.v1.lang.type.LambdaType; +import tonkadur.fate.v1.lang.type.CollectionType; + +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.Instruction; +import tonkadur.fate.v1.lang.meta.InstructionVisitor; +import tonkadur.fate.v1.lang.meta.Reference; + +public class Merge extends Instruction +{ +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final Computation lambda_function; +   protected final Reference collection_in_a; +   protected final Computation default_a; +   protected final Reference collection_in_b; +   protected final Computation default_b; +   protected final Reference collection_out; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected Merge +   ( +      final Origin origin, +      final Computation lambda_function, +      final Reference collection_in_a, +      final Computation default_a, +      final Reference collection_in_b, +      final Computation default_b, +      final Reference collection_out +   ) +   { +      super(origin); + +      this.lambda_function = lambda_function; +      this.collection_in_a = collection_in_a; +      this.default_a = default_a; +      this.collection_in_b = collection_in_b; +      this.default_b = default_b; +      this.collection_out = collection_out; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   public static Merge build +   ( +      final Origin origin, +      final Computation lambda_function, +      final Reference collection_in_a, +      final Computation default_a, +      final Reference collection_in_b, +      final Computation default_b, +      final Reference collection_out +   ) +   throws Throwable +   { +      final Type var_type, collection_in_a_generic_type; +      final Type collection_in_b_generic_type, collection_out_generic_type; +      final CollectionType collection_in_a_type, collection_in_b_type; +      final CollectionType collection_out_type; +      final LambdaType lambda_type; +      final List<Type> signature; + +      var_type = lambda_function.get_type(); + +      if (!(var_type instanceof LambdaType)) +      { +         ErrorManager.handle +         ( +            new InvalidTypeException +            ( +               origin, +               var_type, +               Collections.singleton(Type.LAMBDA) +            ) +         ); + +         return null; +      } + +      lambda_type = (LambdaType) var_type; + +      signature = lambda_type.get_signature(); + +      if (signature.size() != 2) +      { +         ErrorManager.handle +         ( +            new InvalidArityException +            ( +               lambda_function.get_origin(), +               signature.size(), +               2, +               2 +            ) +         ); +      } + +      collection_in_a_generic_type = collection_in_a.get_type(); + +      if (!(collection_in_a_generic_type instanceof CollectionType)) +      { +         ErrorManager.handle +         ( +            new InvalidTypeException +            ( +               collection_in_a.get_origin(), +               collection_in_a_generic_type, +               Type.COLLECTION_TYPES +            ) +         ); + +         return null; +      } + +      collection_in_a_type = (CollectionType) collection_in_a_generic_type; + +      if +      ( +         !collection_in_a_type.get_content_type().can_be_used_as +         ( +            signature.get(0) +         ) +      ) +      { +         /* TODO */ +      } + +      collection_in_b_generic_type = collection_in_b.get_type(); + +      if (!(collection_in_b_generic_type instanceof CollectionType)) +      { +         ErrorManager.handle +         ( +            new InvalidTypeException +            ( +               collection_in_b.get_origin(), +               collection_in_b_generic_type, +               Type.COLLECTION_TYPES +            ) +         ); + +         return null; +      } + +      collection_in_b_type = (CollectionType) collection_in_b_generic_type; + +      if +      ( +         !collection_in_b_type.get_content_type().can_be_used_as +         ( +            signature.get(1) +         ) +      ) +      { +         /* TODO */ +      } + +      if +      ( +         (default_a != null) +         && +         ( +            collection_in_a_type.get_content_type().can_be_used_as +            ( +               default_a.get_type() +            ) +         ) +      ) +      { +         /* TODO */ +      } + +      if +      ( +         (default_b != null) +         && +         ( +            collection_in_b_type.get_content_type().can_be_used_as +            ( +               default_b.get_type() +            ) +         ) +      ) +      { +         /* TODO */ +      } + +      if (collection_out != null) +      { +         collection_out_generic_type = collection_out.get_type(); + +         if (!(collection_out_generic_type instanceof CollectionType)) +         { +            ErrorManager.handle +            ( +               new InvalidTypeException +               ( +                  collection_out.get_origin(), +                  collection_out_generic_type, +                  Type.COLLECTION_TYPES +               ) +            ); + +            return null; +         } + +         collection_out_type = (CollectionType) collection_out_generic_type; + +         if +         ( +            !collection_out_type.get_content_type().can_be_used_as +            ( +               lambda_type.get_return_type() +            ) +         ) +         { +            /* TODO */ +         } +      } + +      return +         new Merge +         ( +            origin, +            lambda_function, +            collection_in_a, +            default_a, +            collection_in_b, +            default_b, +            collection_out +         ); +   } + +   /**** Accessors ************************************************************/ +   @Override +   public void get_visited_by (final InstructionVisitor iv) +   throws Throwable +   { +      iv.visit_merge(this); +   } + +   public Computation get_lambda_function () +   { +      return lambda_function; +   } + +   public Reference get_collection_in_a () +   { +      return collection_in_a; +   } + +   public Computation get_default_a () +   { +      return default_a; +   } + +   public Reference get_collection_in_b () +   { +      return collection_in_b; +   } + +   public Computation get_default_b () +   { +      return default_b; +   } + +   public Reference get_collection_out () +   { +      return collection_out; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      sb.append("(Merge "); +      sb.append(lambda_function.toString()); +      sb.append(" "); +      sb.append(collection_in_a.toString()); +      sb.append(" "); + +      if (default_a == null) +      { +         sb.append("null"); +      } +      else +      { +         sb.append(default_a.toString()); +      } + +      sb.append(" "); +      sb.append(collection_in_b.toString()); +      sb.append(" "); + +      if (default_b == null) +      { +         sb.append("null"); +      } +      else +      { +         sb.append(default_b.toString()); +      } + +      sb.append(" "); + +      if (collection_out == null) +      { +         sb.append("null"); +      } +      else +      { +         sb.append(collection_out.toString()); +      } + +      sb.append(")"); + +      return sb.toString(); +   } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/Partition.java b/src/core/src/tonkadur/fate/v1/lang/instruction/Partition.java new file mode 100644 index 0000000..220f345 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/instruction/Partition.java @@ -0,0 +1,193 @@ +package tonkadur.fate.v1.lang.instruction; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import tonkadur.parser.Origin; +import tonkadur.parser.ParsingError; + +import tonkadur.error.ErrorManager; + +import tonkadur.fate.v1.error.IncompatibleTypeException; +import tonkadur.fate.v1.error.IncomparableTypeException; +import tonkadur.fate.v1.error.InvalidArityException; +import tonkadur.fate.v1.error.InvalidTypeException; + +import tonkadur.fate.v1.lang.type.Type; +import tonkadur.fate.v1.lang.type.LambdaType; +import tonkadur.fate.v1.lang.type.CollectionType; + +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.Instruction; +import tonkadur.fate.v1.lang.meta.InstructionVisitor; +import tonkadur.fate.v1.lang.meta.Reference; + +public class Partition extends Instruction +{ +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final Computation lambda_function; +   protected final Reference collection_in; +   protected final Reference collection_out; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected Partition +   ( +      final Origin origin, +      final Computation lambda_function, +      final Reference collection_in, +      final Reference collection_out +   ) +   { +      super(origin); + +      this.lambda_function = lambda_function; +      this.collection_in = collection_in; +      this.collection_out = collection_out; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   public static Partition build +   ( +      final Origin origin, +      final Computation lambda_function, +      final Reference collection_in, +      final Reference collection_out +   ) +   throws Throwable +   { +      final Type var_type, collection_in_generic_type; +      final CollectionType collection_in_type; +      final LambdaType lambda_type; +      final List<Type> signature; + +      var_type = lambda_function.get_type(); + +      if (!(var_type instanceof LambdaType)) +      { +         ErrorManager.handle +         ( +            new InvalidTypeException +            ( +               origin, +               var_type, +               Collections.singleton(Type.LAMBDA) +            ) +         ); + +         return null; +      } + +      lambda_type = (LambdaType) var_type; + +      signature = lambda_type.get_signature(); + +      if (signature.size() != 1) +      { +         ErrorManager.handle +         ( +            new InvalidArityException +            ( +               lambda_function.get_origin(), +               signature.size(), +               1, +               1 +            ) +         ); +      } + +      collection_in_generic_type = collection_in.get_type(); + +      if (!(collection_in_generic_type instanceof CollectionType)) +      { +         ErrorManager.handle +         ( +            new InvalidTypeException +            ( +               collection_in.get_origin(), +               collection_in_generic_type, +               Type.COLLECTION_TYPES +            ) +         ); + +         return null; +      } + +      collection_in_type = (CollectionType) collection_in_generic_type; + +      if +      ( +         !collection_in_type.get_content_type().can_be_used_as(signature.get(0)) +      ) +      { +         /* TODO */ +      } + +      if (lambda_type.get_return_type().can_be_used_as(Type.BOOL)) +      { +         /* TODO */ +      } + +      if (!collection_in_type.can_be_used_as(collection_out.get_type())) +      { +         /* TODO */ +      } + +      return +         new Partition +         ( +            origin, +            lambda_function, +            collection_in, +            collection_out +         ); +   } + +   /**** Accessors ************************************************************/ +   @Override +   public void get_visited_by (final InstructionVisitor iv) +   throws Throwable +   { +      iv.visit_partition(this); +   } + +   public Computation get_lambda_function () +   { +      return lambda_function; +   } + +   public Reference get_collection_in () +   { +      return collection_in; +   } + +   public Reference get_collection_out () +   { +      return collection_out; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      sb.append("(Partition "); +      sb.append(lambda_function.toString()); +      sb.append(" "); +      sb.append(collection_in.toString()); +      sb.append(" "); +      sb.append(collection_out.toString()); +      sb.append(")"); + +      return sb.toString(); +   } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/PopElement.java b/src/core/src/tonkadur/fate/v1/lang/instruction/PopElement.java new file mode 100644 index 0000000..7729164 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/instruction/PopElement.java @@ -0,0 +1,114 @@ +package tonkadur.fate.v1.lang.instruction; + +import tonkadur.error.ErrorManager; + +import tonkadur.parser.Origin; + +import tonkadur.fate.v1.error.InvalidTypeException; + +import tonkadur.fate.v1.lang.type.CollectionType; +import tonkadur.fate.v1.lang.type.Type; + +import tonkadur.fate.v1.lang.meta.InstructionVisitor; +import tonkadur.fate.v1.lang.meta.Instruction; +import tonkadur.fate.v1.lang.meta.Computation; + +public class PopElement extends Instruction +{ +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final Computation collection; +   protected final boolean is_from_left; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected PopElement +   ( +      final Origin origin, +      final Computation collection, +      final boolean is_from_left +   ) +   { +      super(origin); + +      this.collection = collection; +      this.is_from_left = is_from_left; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   public static PopElement build +   ( +      final Origin origin, +      final Computation collection, +      final boolean is_from_left +   ) +   throws InvalidTypeException +   { +      if +      ( +         !Type.COLLECTION_TYPES.contains +         ( +            collection.get_type().get_act_as_type() +         ) +      ) +      { +         ErrorManager.handle +         ( +            new InvalidTypeException +            ( +               collection.get_origin(), +               collection.get_type(), +               Type.COLLECTION_TYPES +            ) +         ); +      } + +      return new PopElement(origin, collection, is_from_left); +   } + +   /**** Accessors ************************************************************/ +   @Override +   public void get_visited_by (final InstructionVisitor iv) +   throws Throwable +   { +      iv.visit_pop_element(this); +   } + +   public Computation get_collection () +   { +      return collection; +   } + +   public boolean is_from_left () +   { +      return is_from_left; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      if (is_from_left) +      { +         sb.append("(PopLeftElement "); +      } +      else +      { +         sb.append("(PopRightElement "); +      } + +      sb.append(collection.toString()); + +      sb.append(")"); + +      return sb.toString(); +   } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/PushElement.java b/src/core/src/tonkadur/fate/v1/lang/instruction/PushElement.java new file mode 100644 index 0000000..8f0474a --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/instruction/PushElement.java @@ -0,0 +1,198 @@ +package tonkadur.fate.v1.lang.instruction; + +import java.util.Collections; + +import tonkadur.error.ErrorManager; + +import tonkadur.parser.Origin; + +import tonkadur.fate.v1.error.ConflictingTypeException; +import tonkadur.fate.v1.error.IncomparableTypeException; +import tonkadur.fate.v1.error.InvalidTypeException; + +import tonkadur.fate.v1.lang.type.CollectionType; +import tonkadur.fate.v1.lang.type.Type; + +import tonkadur.fate.v1.lang.meta.InstructionVisitor; +import tonkadur.fate.v1.lang.meta.Instruction; +import tonkadur.fate.v1.lang.meta.Computation; + +public class PushElement extends Instruction +{ +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final Computation element; +   protected final Computation collection; +   protected final boolean is_from_left; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected PushElement +   ( +      final Origin origin, +      final Computation element, +      final Computation collection, +      final boolean is_from_left +   ) +   { +      super(origin); + +      this.collection = collection; +      this.element = element; +      this.is_from_left = is_from_left; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   public static PushElement build +   ( +      final Origin origin, +      final Computation element, +      final Computation collection, +      final boolean is_from_left +   ) +   throws +      InvalidTypeException, +      ConflictingTypeException, +      IncomparableTypeException +   { +      final Type hint; +      final Type collection_type; +      final CollectionType collection_true_type; +      final Type collection_element_type; + +      collection_type = collection.get_type(); + +      if (!(collection_type instanceof CollectionType)) +      { +         ErrorManager.handle +         ( +            new InvalidTypeException +            ( +               collection.get_origin(), +               collection.get_type(), +               Collections.singletonList(Type.LIST) +            ) +         ); +      } + +      collection_true_type = (CollectionType) collection_type; + +      if (collection_true_type.is_set()) +      { +         ErrorManager.handle +         ( +            new InvalidTypeException +            ( +               collection.get_origin(), +               collection.get_type(), +               Collections.singletonList(Type.LIST) +            ) +         ); +      } + +      collection_element_type = collection_true_type.get_content_type(); + +      if +      ( +         element.get_type().can_be_used_as(collection_element_type) +         || +         (element.get_type().try_merging_with(collection_element_type) != null) +      ) +      { +         return new PushElement(origin, element, collection, is_from_left); +      } + +      ErrorManager.handle +      ( +         new ConflictingTypeException +         ( +            element.get_origin(), +            element.get_type(), +            collection_element_type +         ) +      ); + +      hint = +         (Type) element.get_type().generate_comparable_to +         ( +            collection_element_type +         ); + +      if (hint.equals(Type.ANY)) +      { +         ErrorManager.handle +         ( +            new IncomparableTypeException +            ( +               element.get_origin(), +               element.get_type(), +               collection_element_type +            ) +         ); +      } + +      return new PushElement(origin, element, collection, is_from_left); +   } + +   /**** Accessors ************************************************************/ +   @Override +   public void get_visited_by (final InstructionVisitor iv) +   throws Throwable +   { +      iv.visit_push_element(this); +   } + +   public Computation get_collection () +   { +      return collection; +   } + +   public Computation get_element () +   { +      return element; +   } + +   public boolean  is_from_left () +   { +      return is_from_left; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      if (is_from_left) +      { +         sb.append("(LeftPushElement"); +      } +      else +      { +         sb.append("(RightPushElement"); +      } + +      sb.append(System.lineSeparator()); +      sb.append(System.lineSeparator()); + +      sb.append("element:"); +      sb.append(System.lineSeparator()); +      sb.append(element.toString()); +      sb.append(System.lineSeparator()); +      sb.append(System.lineSeparator()); + +      sb.append("collection:"); +      sb.append(System.lineSeparator()); +      sb.append(collection.toString()); + +      sb.append(")"); + +      return sb.toString(); +   } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/Sort.java b/src/core/src/tonkadur/fate/v1/lang/instruction/Sort.java new file mode 100644 index 0000000..d9f3397 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/instruction/Sort.java @@ -0,0 +1,172 @@ +package tonkadur.fate.v1.lang.instruction; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import tonkadur.parser.Origin; +import tonkadur.parser.ParsingError; + +import tonkadur.error.ErrorManager; + +import tonkadur.fate.v1.error.IncompatibleTypeException; +import tonkadur.fate.v1.error.IncomparableTypeException; +import tonkadur.fate.v1.error.InvalidArityException; +import tonkadur.fate.v1.error.InvalidTypeException; + +import tonkadur.fate.v1.lang.type.Type; +import tonkadur.fate.v1.lang.type.LambdaType; +import tonkadur.fate.v1.lang.type.CollectionType; + +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.Instruction; +import tonkadur.fate.v1.lang.meta.InstructionVisitor; +import tonkadur.fate.v1.lang.meta.Reference; + +public class Sort extends Instruction +{ +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final Computation lambda_function; +   protected final Reference collection; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected Sort +   ( +      final Origin origin, +      final Computation lambda_function, +      final Reference collection +   ) +   { +      super(origin); + +      this.lambda_function = lambda_function; +      this.collection = collection; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   public static Sort build +   ( +      final Origin origin, +      final Computation lambda_function, +      final Reference collection +   ) +   throws Throwable +   { +      final Type var_type, collection_generic_type; +      final CollectionType collection_type; +      final LambdaType lambda_type; +      final List<Type> signature; + +      var_type = lambda_function.get_type(); + +      if (!(var_type instanceof LambdaType)) +      { +         ErrorManager.handle +         ( +            new InvalidTypeException +            ( +               origin, +               var_type, +               Collections.singleton(Type.LAMBDA) +            ) +         ); + +         return null; +      } + +      lambda_type = (LambdaType) var_type; + +      signature = lambda_type.get_signature(); + +      if (signature.size() != 2) +      { +         ErrorManager.handle +         ( +            new InvalidArityException +            ( +               lambda_function.get_origin(), +               signature.size(), +               1, +               1 +            ) +         ); +      } + +      collection_generic_type = collection.get_type(); + +      if (!(collection_generic_type instanceof CollectionType)) +      { +         ErrorManager.handle +         ( +            new InvalidTypeException +            ( +               collection.get_origin(), +               collection_generic_type, +               Type.COLLECTION_TYPES +            ) +         ); + +         return null; +      } + +      collection_type = (CollectionType) collection_generic_type; + +      if (!collection_type.get_content_type().can_be_used_as(signature.get(0))) +      { +         /* TODO */ +      } + +      if (!collection_type.get_content_type().can_be_used_as(signature.get(1))) +      { +         /* TODO */ +      } + +      if (!lambda_type.get_return_type().can_be_used_as(Type.INT)) +      { +         /* TODO */ +      } + +      return new Sort(origin, lambda_function, collection); +   } + +   /**** Accessors ************************************************************/ +   @Override +   public void get_visited_by (final InstructionVisitor iv) +   throws Throwable +   { +      iv.visit_sort(this); +   } + +   public Computation get_lambda_function () +   { +      return lambda_function; +   } + +   public Reference get_collection () +   { +      return collection; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      sb.append("(Sort "); +      sb.append(lambda_function.toString()); +      sb.append(" "); +      sb.append(collection.toString()); +      sb.append(")"); + +      return sb.toString(); +   } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/SubList.java b/src/core/src/tonkadur/fate/v1/lang/instruction/SubList.java new file mode 100644 index 0000000..607f7c1 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/instruction/SubList.java @@ -0,0 +1,143 @@ +package tonkadur.fate.v1.lang.instruction; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import tonkadur.parser.Origin; +import tonkadur.parser.ParsingError; + +import tonkadur.error.ErrorManager; + +import tonkadur.fate.v1.error.IncompatibleTypeException; +import tonkadur.fate.v1.error.IncomparableTypeException; +import tonkadur.fate.v1.error.InvalidArityException; +import tonkadur.fate.v1.error.InvalidTypeException; + +import tonkadur.fate.v1.lang.type.Type; +import tonkadur.fate.v1.lang.type.CollectionType; + +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.Instruction; +import tonkadur.fate.v1.lang.meta.InstructionVisitor; +import tonkadur.fate.v1.lang.meta.Reference; + +public class SubList extends Instruction +{ +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final Computation start; +   protected final Computation end; +   protected final Reference collection_in; +   protected final Reference collection_out; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected SubList +   ( +      final Origin origin, +      final Computation start, +      final Computation end, +      final Reference collection_in, +      final Reference collection_out +   ) +   { +      super(origin); + +      this.start = start; +      this.end = end; +      this.collection_in = collection_in; +      this.collection_out = collection_out; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   public static SubList build +   ( +      final Origin origin, +      final Computation start, +      final Computation end, +      final Reference collection_in, +      final Reference collection_out +   ) +   throws Throwable +   { +      final Type collection_generic_type; + +      collection_generic_type = collection_in.get_type(); + +      if (!(collection_generic_type instanceof CollectionType)) +      { +         ErrorManager.handle +         ( +            new InvalidTypeException +            ( +               collection_in.get_origin(), +               collection_generic_type, +               Type.COLLECTION_TYPES +            ) +         ); + +         return null; +      } + +      if (!collection_generic_type.can_be_used_as(collection_out.get_type())) +      { +         /* TODO */ +      } + +      return new SubList(origin, start, end, collection_in, collection_out); +   } + +   /**** Accessors ************************************************************/ +   @Override +   public void get_visited_by (final InstructionVisitor iv) +   throws Throwable +   { +      iv.visit_sublist(this); +   } + +   public Computation get_start_index () +   { +      return start; +   } + +   public Computation get_end_index () +   { +      return end; +   } + +   public Reference get_collection_in () +   { +      return collection_in; +   } + +   public Reference get_collection_out () +   { +      return collection_out; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      sb.append("(SubList "); +      sb.append(start.toString()); +      sb.append(" "); +      sb.append(end.toString()); +      sb.append(" "); +      sb.append(collection_in.toString()); +      sb.append(" "); +      sb.append(collection_out.toString()); +      sb.append(")"); + +      return sb.toString(); +   } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/meta/ComputationVisitor.java b/src/core/src/tonkadur/fate/v1/lang/meta/ComputationVisitor.java index 8988ff7..f40b2bf 100644 --- a/src/core/src/tonkadur/fate/v1/lang/meta/ComputationVisitor.java +++ b/src/core/src/tonkadur/fate/v1/lang/meta/ComputationVisitor.java @@ -97,6 +97,12 @@ public interface ComputationVisitor     public void visit_map (final MapComputation n)     throws Throwable; +   public void visit_indexed_map (final IndexedMapComputation n) +   throws Throwable; + +   public void visit_sort (final SortComputation n) +   throws Throwable; +     public void visit_range (final Range n)     throws Throwable; @@ -117,4 +123,22 @@ public interface ComputationVisitor     public void visit_shuffle (final ShuffleComputation n)     throws Throwable; + +   public void visit_merge (final MergeComputation n) +   throws Throwable; + +   public void visit_filter (final FilterComputation n) +   throws Throwable; + +   public void visit_sublist (final SubListComputation n) +   throws Throwable; + +   public void visit_partition (final PartitionComputation n) +   throws Throwable; + +   public void visit_push_element (final PushElementComputation n) +   throws Throwable; + +   public void visit_pop_element (final PopElementComputation n) +   throws Throwable;  } diff --git a/src/core/src/tonkadur/fate/v1/lang/meta/InstructionVisitor.java b/src/core/src/tonkadur/fate/v1/lang/meta/InstructionVisitor.java index 9a18682..22bedb1 100644 --- a/src/core/src/tonkadur/fate/v1/lang/meta/InstructionVisitor.java +++ b/src/core/src/tonkadur/fate/v1/lang/meta/InstructionVisitor.java @@ -5,7 +5,7 @@ import tonkadur.fate.v1.lang.instruction.*;  public interface InstructionVisitor  {     /* Instruction Nodes */ -   public void visit_add_element (final AddElement ae) +   public void visit_add_element (final AddElement n)     throws Throwable;     public void visit_add_element_at (final AddElementAt n) @@ -14,7 +14,7 @@ public interface InstructionVisitor     public void visit_add_elements_of (final AddElementsOf n)     throws Throwable; -   public void visit_assert (final Assert a) +   public void visit_assert (final Assert n)     throws Throwable;     public void visit_break (final Break n) @@ -44,22 +44,46 @@ public interface InstructionVisitor     public void visit_for_each (final ForEach n)     throws Throwable; -   public void visit_clear (final Clear c) +   public void visit_clear (final Clear n)     throws Throwable; -   public void visit_map (final Map c) +   public void visit_map (final Map n) +   throws Throwable; + +   public void visit_merge (final Merge n) +   throws Throwable; + +   public void visit_filter (final Filter n) +   throws Throwable; + +   public void visit_sublist (final SubList n) +   throws Throwable; + +   public void visit_partition (final Partition n) +   throws Throwable; + +   public void visit_sort (final Sort n) +   throws Throwable; + +   public void visit_indexed_map (final IndexedMap c)     throws Throwable;     public void visit_shuffle (final Shuffle c)     throws Throwable; +   public void visit_pop_element (final PopElement c) +   throws Throwable; + +   public void visit_push_element (final PushElement c) +   throws Throwable; +     public void visit_reverse_list (final ReverseList n)     throws Throwable; -   public void visit_cond_instruction (final CondInstruction ci) +   public void visit_cond_instruction (final CondInstruction n)     throws Throwable; -   public void visit_switch_instruction (final SwitchInstruction ci) +   public void visit_switch_instruction (final SwitchInstruction n)     throws Throwable;     public void visit_display (final Display n) diff --git a/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 b/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 index 09e417e..1c11821 100644 --- a/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 +++ b/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 @@ -95,14 +95,14 @@ OR_KW: L_PAREN ('or'|'\\/') SEP+;  RICH_TEXT_KW: L_PAREN (('rich'US)?'text') SEP+;  PARTITION_KW: L_PAREN 'partition' SEP+;  IMP_PARTITION_KW: L_PAREN 'partition!' SEP+; -POP_LEFT: L_PAREN 'pop'US'left' SEP+; -IMP_POP_LEFT: L_PAREN 'pop'US'left!' SEP+; -POP_RIGHT: L_PAREN 'pop'US'right' SEP+; -IMP_POP_RIGHT: L_PAREN 'pop'US'right!' SEP+; -PUSH_LEFT: L_PAREN 'push'US'left' SEP+; -IMP_PUSH_LEFT: L_PAREN 'push'US'left!' SEP+; -PUSH_RIGHT: L_PAREN 'push'US'right' SEP+; -IMP_PUSH_RIGHT: L_PAREN 'push'US'right!' SEP+; +POP_LEFT_KW: L_PAREN 'pop'US'left' SEP+; +IMP_POP_LEFT_KW: L_PAREN 'pop'US'left!' SEP+; +POP_RIGHT_KW: L_PAREN 'pop'US'right' SEP+; +IMP_POP_RIGHT_KW: L_PAREN 'pop'US'right!' SEP+; +PUSH_LEFT_KW: L_PAREN 'push'US'left' SEP+; +IMP_PUSH_LEFT_KW: L_PAREN 'push'US'left!' SEP+; +PUSH_RIGHT_KW: L_PAREN 'push'US'right' SEP+; +IMP_PUSH_RIGHT_KW: L_PAREN 'push'US'right!' SEP+;  PLAYER_CHOICE_KW: L_PAREN ('choice'|'user'US'choice'|'player'US'choice') SEP+;  PLUS_KW: L_PAREN ('plus'|'+') SEP+;  POWER_KW: L_PAREN ('power'|'^'|'**'|'pow') SEP+; diff --git a/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 b/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 index 1e0c245..43d284b 100644 --- a/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 +++ b/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 @@ -741,6 +741,68 @@ returns [Instruction result]           );     } +   | IMP_PUSH_LEFT_KW value WS+ value_reference WS* R_PAREN +   { +      $result = +         PushElement.build +         ( +            CONTEXT.get_origin_at +            ( +               ($IMP_PUSH_LEFT_KW.getLine()), +               ($IMP_PUSH_LEFT_KW.getCharPositionInLine()) +            ), +            ($value.result), +            ($value_reference.result), +            true +         ); +   } + +   | IMP_PUSH_RIGHT_KW value WS+ value_reference WS* R_PAREN +   { +      $result = +         PushElement.build +         ( +            CONTEXT.get_origin_at +            ( +               ($IMP_PUSH_RIGHT_KW.getLine()), +               ($IMP_PUSH_RIGHT_KW.getCharPositionInLine()) +            ), +            ($value.result), +            ($value_reference.result), +            false +         ); +   } + +   | IMP_POP_RIGHT_KW value_reference WS* R_PAREN +   { +      $result = +         PopElement.build +         ( +            CONTEXT.get_origin_at +            ( +               ($IMP_POP_RIGHT_KW.getLine()), +               ($IMP_POP_RIGHT_KW.getCharPositionInLine()) +            ), +            ($value_reference.result), +            false +         ); +   } + +   | IMP_POP_LEFT_KW value_reference WS* R_PAREN +   { +      $result = +         PopElement.build +         ( +            CONTEXT.get_origin_at +            ( +               ($IMP_POP_LEFT_KW.getLine()), +               ($IMP_POP_LEFT_KW.getCharPositionInLine()) +            ), +            ($value_reference.result), +            true +         ); +   } +     | IMP_MAP_KW           value WS+           inr=value_reference WS+ @@ -784,7 +846,6 @@ returns [Instruction result]     | IMP_MERGE_KW        fun=value WS+ -      init=value WS+        inr0=value_reference WS+        inr1=value_reference WS+        outr=value_reference WS* @@ -799,16 +860,16 @@ returns [Instruction result]                 ($IMP_MERGE_KW.getCharPositionInLine())              ),              ($fun.result), -            ($init.result),              ($inr0.result), +            null,              ($inr1.result), +            null,              ($outr.result)           );     }     | IMP_MERGE_KW        fun=value WS+ -      init=value WS+        def0=value WS+        inr0=value_reference WS+        def1=value WS+ @@ -825,11 +886,10 @@ returns [Instruction result]                 ($IMP_MERGE_KW.getCharPositionInLine())              ),              ($fun.result), -            ($init.result), -            ($def0.result),              ($inr0.result), -            ($def1.result), +            ($def0.result),              ($inr1.result), +            ($def1.result),              ($outr.result)           );     } @@ -3355,6 +3415,68 @@ returns [Computation result]           );     } +   | PUSH_LEFT_KW value WS+ value_reference WS* R_PAREN +   { +      $result = +         PushElementComputation.build +         ( +            CONTEXT.get_origin_at +            ( +               ($PUSH_LEFT_KW.getLine()), +               ($PUSH_LEFT_KW.getCharPositionInLine()) +            ), +            ($value.result), +            ($value_reference.result), +            true +         ); +   } + +   | PUSH_RIGHT_KW value WS+ value_reference WS* R_PAREN +   { +      $result = +         PushElementComputation.build +         ( +            CONTEXT.get_origin_at +            ( +               ($PUSH_RIGHT_KW.getLine()), +               ($PUSH_RIGHT_KW.getCharPositionInLine()) +            ), +            ($value.result), +            ($value_reference.result), +            false +         ); +   } + +   | POP_LEFT_KW value_reference WS* R_PAREN +   { +      $result = +         PopElementComputation.build +         ( +            CONTEXT.get_origin_at +            ( +               ($POP_LEFT_KW.getLine()), +               ($POP_LEFT_KW.getCharPositionInLine()) +            ), +            ($value_reference.result), +            true +         ); +   } + +   | POP_RIGHT_KW value_reference WS* R_PAREN +   { +      $result = +         PopElementComputation.build +         ( +            CONTEXT.get_origin_at +            ( +               ($POP_RIGHT_KW.getLine()), +               ($POP_RIGHT_KW.getCharPositionInLine()) +            ), +            ($value_reference.result), +            true +         ); +   } +     | MAP_KW           value WS+           inr=value_reference WS* @@ -3391,7 +3513,6 @@ returns [Computation result]     | MERGE_KW           fun=value WS+ -         init=value WS+           inr0=value_reference WS+           inr1=value_reference WS*        R_PAREN @@ -3405,15 +3526,15 @@ returns [Computation result]                 ($MERGE_KW.getCharPositionInLine())              ),              ($fun.result), -            ($init.result),              ($inr0.result), -            ($inr1.result) +            null, +            ($inr1.result), +            null           );     }     | MERGE_KW           fun=value WS+ -         init=value WS+           def0=value WS+           inr0=value_reference WS+           def1=value WS+ @@ -3429,11 +3550,10 @@ returns [Computation result]                 ($MERGE_KW.getCharPositionInLine())              ),              ($fun.result), -            ($init.result), -            ($def0.result),              ($inr0.result), -            ($def1.result), -            ($inr1.result) +            ($def0.result), +            ($inr1.result), +            ($def1.result)           );     } 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 5d961a4..936ab5e 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 @@ -1892,6 +1892,15 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor     }     @Override +   public void visit_add_element +   ( +      final tonkadur.fate.v1.lang.computation.AddElementComputation n +   ) +   { +      /* TODO */ +   } + +   @Override     public void visit_add_element_at     (        final tonkadur.fate.v1.lang.computation.AddElementAtComputation n @@ -1980,4 +1989,76 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor     {        /* TODO */     } + +   @Override +   public void visit_merge +   ( +      final tonkadur.fate.v1.lang.computation.MergeComputation n +   ) +   { +      /* TODO */ +   } + +   @Override +   public void visit_sublist +   ( +      final tonkadur.fate.v1.lang.computation.SubListComputation n +   ) +   { +      /* TODO */ +   } + +   @Override +   public void visit_partition +   ( +      final tonkadur.fate.v1.lang.computation.PartitionComputation n +   ) +   { +      /* TODO */ +   } + +   @Override +   public void visit_sort +   ( +      final tonkadur.fate.v1.lang.computation.SortComputation n +   ) +   { +      /* TODO */ +   } + +   @Override +   public void visit_filter +   ( +      final tonkadur.fate.v1.lang.computation.FilterComputation n +   ) +   { +      /* TODO */ +   } + +   @Override +   public void visit_indexed_map +   ( +      final tonkadur.fate.v1.lang.computation.IndexedMapComputation n +   ) +   { +      /* TODO */ +   } + +   @Override +   public void visit_push_element +   ( +      final tonkadur.fate.v1.lang.computation.PushElementComputation n +   ) +   { +      /* TODO */ +   } + +   @Override +   public void visit_pop_element +   ( +      final tonkadur.fate.v1.lang.computation.PopElementComputation n +   ) +   { +      /* TODO */ +   }  } diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/InstructionCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/InstructionCompiler.java index 4b88a53..2bafc69 100644 --- a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/InstructionCompiler.java +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/InstructionCompiler.java @@ -546,6 +546,7 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor        address_compiler.release_registers(result);     } +   @Override     public void visit_shuffle     (        final tonkadur.fate.v1.lang.instruction.Shuffle n @@ -588,6 +589,7 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor        address_compiler.release_registers(result);     } +   @Override     public void visit_map     (        final tonkadur.fate.v1.lang.instruction.Map n @@ -598,6 +600,86 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor     }     @Override +   public void visit_sort +   ( +      final tonkadur.fate.v1.lang.instruction.Sort n +   ) +   throws Throwable +   { +      /* TODO */ +   } + +   @Override +   public void visit_merge +   ( +      final tonkadur.fate.v1.lang.instruction.Merge n +   ) +   throws Throwable +   { +      /* TODO */ +   } + +   @Override +   public void visit_partition +   ( +      final tonkadur.fate.v1.lang.instruction.Partition n +   ) +   throws Throwable +   { +      /* TODO */ +   } + +   @Override +   public void visit_sublist +   ( +      final tonkadur.fate.v1.lang.instruction.SubList n +   ) +   throws Throwable +   { +      /* TODO */ +   } + +   @Override +   public void visit_push_element +   ( +      final tonkadur.fate.v1.lang.instruction.PushElement n +   ) +   throws Throwable +   { +      /* TODO */ +   } + +   @Override +   public void visit_pop_element +   ( +      final tonkadur.fate.v1.lang.instruction.PopElement n +   ) +   throws Throwable +   { +      /* TODO */ +   } + +   @Override +   public void visit_filter +   ( +      final tonkadur.fate.v1.lang.instruction.Filter n +   ) +   throws Throwable +   { +      /* TODO */ +   } + +   @Override +   public void visit_indexed_map +   ( +      final tonkadur.fate.v1.lang.instruction.IndexedMap n +   ) +   throws Throwable +   { +      /* TODO */ +   } + +   @Override     public void visit_switch_instruction     (        final tonkadur.fate.v1.lang.instruction.SwitchInstruction n | 


