| summaryrefslogtreecommitdiff | 
diff options
Diffstat (limited to 'src')
34 files changed, 5075 insertions, 137 deletions
| diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/generic/Access.java b/src/core/src/tonkadur/fate/v1/lang/computation/generic/Access.java new file mode 100644 index 0000000..11ff4d1 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/generic/Access.java @@ -0,0 +1,166 @@ +package tonkadur.fate.v1.lang.computation.generic; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +import tonkadur.parser.Origin; + +import tonkadur.error.ErrorManager; + +import tonkadur.fate.v1.error.InvalidTypeException; + +import tonkadur.fate.v1.lang.meta.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.RecurrentChecks; + +import tonkadur.fate.v1.lang.type.CollectionType; +import tonkadur.fate.v1.lang.type.PointerType; +import tonkadur.fate.v1.lang.type.Type; + +import tonkadur.fate.v1.lang.computation.GenericComputation; + +public class Access extends GenericComputation +{ +   public static Collection<String> get_aliases () +   { +      final Collection<String> aliases; + +      aliases = new ArrayList<String>(); + +      aliases.add("list:access"); +      aliases.add("list:at_index"); +      aliases.add("list:atindex"); +      aliases.add("list:atIndex"); +      aliases.add("set:access"); +      aliases.add("set:at_index"); +      aliases.add("set:atindex"); +      aliases.add("set:atIndex"); + +      return aliases; +   } + +   public static GenericComputation build +   ( +      final Origin origin, +      final String alias, +      final List<Computation> call_parameters +   ) +   throws Throwable +   { +      final Computation index; +      Computation parent; +      Type current_type; + +      if (call_parameters.size() != 2) +      { +         // TODO: Error. +         System.err.println("Wrong number of params at " + origin.toString()); + +         return null; +      } + +      index = call_parameters.get(0); +      parent = call_parameters.get(1); + +      index.expect_non_string(); +      parent.expect_non_string(); + +      current_type = parent.get_type(); + +      while (current_type.can_be_used_as(PointerType.ARCHETYPE)) +      { +         parent = +            AtReference.build +            ( +               origin, +               "at", +               Collections.singletonList(parent) +            ); + +         current_type = parent.get_type(); +      } + +      if (!index.get_type().can_be_used_as(Type.INT)) +      { +         ErrorManager.handle +         ( +            new InvalidTypeException +            ( +               index.get_origin(), +               current_type, +               Collections.singleton(Type.INT), +               index.toString() +            ) +         ); +      } + +      if (alias.startsWith("set:")) +      { +         RecurrentChecks.assert_is_a_set(parent); +      } +      else +      { +         RecurrentChecks.assert_is_a_list(parent); +      } + +      return new Access(origin, parent, current_type, index); +   } + +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final Computation parent; +   protected final Computation index; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected Access +   ( +      final Origin origin, +      final Computation parent, +      final Type type, +      final Computation index +   ) +   { +      super(origin, type); + +      this.parent = parent; +      this.index = index; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ + +   /**** Accessors ************************************************************/ +   public Computation get_index () +   { +      return index; +   } + +   public Computation get_parent () +   { +      return parent; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      sb.append("(Access ("); +      sb.append(type.get_name()); +      sb.append(") "); +      sb.append(parent.toString()); +      sb.append("."); +      sb.append(index.toString()); +      sb.append(")"); + +      return sb.toString(); +   } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/generic/AddElementAtComputation.java b/src/core/src/tonkadur/fate/v1/lang/computation/generic/AddElementAtComputation.java new file mode 100644 index 0000000..aa50009 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/generic/AddElementAtComputation.java @@ -0,0 +1,149 @@ +package tonkadur.fate.v1.lang.computation.generic; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import tonkadur.parser.Origin; +import tonkadur.parser.ParsingError; + +import tonkadur.fate.v1.lang.type.Type; + +import tonkadur.fate.v1.lang.meta.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.RecurrentChecks; + +import tonkadur.fate.v1.lang.computation.GenericComputation; + +public class AddElementAtComputation extends GenericComputation +{ +   public static Collection<String> get_aliases () +   { +      final Collection<String> aliases; + +      aliases = new ArrayList<String>(); + +      aliases.add("list:add_element_at"); +      aliases.add("list:addelementat"); +      aliases.add("list:addElementAt"); +      aliases.add("list:add_at"); +      aliases.add("list:addat"); +      aliases.add("list:addAt"); + +      return aliases; +   } + +   public static GenericComputation build +   ( +      final Origin origin, +      final String _alias, +      final List<Computation> call_parameters +   ) +   throws Throwable +   { +      final Computation index; +      final Computation element; +      final Computation collection; + +      if (call_parameters.size() != 3) +      { +         // TODO: Error. +         System.err.println("Wrong number of params at " + origin.toString()); + +         return null; +      } + +      index = call_parameters.get(0); +      element = call_parameters.get(1); +      collection = call_parameters.get(2); + +      index.expect_non_string(); + +      RecurrentChecks.propagate_expected_types_and_assert_is_a_list_of +      ( +         collection, +         element +      ); + +      RecurrentChecks.assert_can_be_used_as(index, Type.INT); + +      return new AddElementAtComputation(origin, index, element, collection); +   } + +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final Computation index; +   protected final Computation element; +   protected final Computation collection; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected AddElementAtComputation +   ( +      final Origin origin, +      final Computation index, +      final Computation element, +      final Computation collection +   ) +   { +      super(origin, collection.get_type()); + +      this.index = index; +      this.collection = collection; +      this.element = element; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Accessors ************************************************************/ +   public Computation get_collection () +   { +      return collection; +   } + +   public Computation get_index () +   { +      return index; +   } + +   public Computation get_element () +   { +      return element; +   } + + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      sb.append("(AddElementAt"); +      sb.append(System.lineSeparator()); +      sb.append(System.lineSeparator()); + +      sb.append("index:"); +      sb.append(System.lineSeparator()); +      sb.append(index.toString()); +      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/computation/generic/AddElementComputation.java b/src/core/src/tonkadur/fate/v1/lang/computation/generic/AddElementComputation.java new file mode 100644 index 0000000..ef52eb4 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/generic/AddElementComputation.java @@ -0,0 +1,141 @@ +package tonkadur.fate.v1.lang.computation.generic; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import tonkadur.parser.Origin; +import tonkadur.parser.ParsingError; + +import tonkadur.fate.v1.lang.meta.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.RecurrentChecks; + +import tonkadur.fate.v1.lang.computation.GenericComputation; + +public class AddElementComputation extends GenericComputation +{ +   public static Collection<String> get_aliases () +   { +      final Collection<String> aliases; + +      aliases = new ArrayList<String>(); + +      aliases.add("list:add"); +      aliases.add("list:add_element"); +      aliases.add("list:addelement"); +      aliases.add("list:addElement"); +      aliases.add("set:add"); +      aliases.add("set:add_element"); +      aliases.add("set:addelement"); +      aliases.add("set:addElement"); + +      return aliases; +   } + +   public static GenericComputation build +   ( +      final Origin origin, +      final String alias, +      final List<Computation> call_parameters +   ) +   throws Throwable +   { +      final Computation element; +      final Computation collection; + +      if (call_parameters.size() != 2) +      { +         // TODO: Error. +         System.err.println("Wrong number of params at " + origin.toString()); + +         return null; +      } + +      element = call_parameters.get(0); +      collection = call_parameters.get(1); + +      if (alias.startsWith("set:")) +      { +         RecurrentChecks.propagate_expected_types_and_assert_is_a_set_of +         ( +            collection, +            element +         ); +      } +      else +      { +         RecurrentChecks.propagate_expected_types_and_assert_is_a_list_of +         ( +            collection, +            element +         ); +      } + +      return new AddElementComputation(origin, element, collection); +   } + +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final Computation element; +   protected final Computation collection; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected AddElementComputation +   ( +      final Origin origin, +      final Computation element, +      final Computation collection +   ) +   { +      super(origin, collection.get_type()); + +      this.collection = collection; +      this.element = element; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ + +   /**** Accessors ************************************************************/ + +   public Computation get_collection () +   { +      return collection; +   } + +   public Computation get_element () +   { +      return element; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      sb.append("(AddElement"); +      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/computation/generic/AddElementsOfComputation.java b/src/core/src/tonkadur/fate/v1/lang/computation/generic/AddElementsOfComputation.java new file mode 100644 index 0000000..4458df5 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/generic/AddElementsOfComputation.java @@ -0,0 +1,185 @@ +package tonkadur.fate.v1.lang.computation.generic; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import tonkadur.parser.Origin; +import tonkadur.parser.ParsingError; + +import tonkadur.fate.v1.lang.type.CollectionType; + +import tonkadur.fate.v1.lang.meta.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.RecurrentChecks; + +import tonkadur.fate.v1.lang.computation.GenericComputation; + +public class AddElementsOfComputation extends GenericComputation +{ +   public static Collection<String> get_aliases () +   { +      final Collection<String> aliases; + +      aliases = new ArrayList<String>(); + +      aliases.add("list:add_all"); +      aliases.add("list:addall"); +      aliases.add("list:addAll"); + +      aliases.add("list:add_all_of"); +      aliases.add("list:addallof"); +      aliases.add("list:addAllOf"); + +      aliases.add("list:add_elements"); +      aliases.add("list:addelements"); +      aliases.add("list:addElements"); + +      aliases.add("list:add_elements_of"); +      aliases.add("list:addelementsof"); +      aliases.add("list:addElementsOf"); + +      aliases.add("list:add_all_elements"); +      aliases.add("list:addallelements"); +      aliases.add("list:addAllElements"); + +      aliases.add("list:add_all_elements_of"); +      aliases.add("list:addallelementsof"); +      aliases.add("list:addAllElementsOf"); + +      aliases.add("set:add_all"); +      aliases.add("set:addall"); +      aliases.add("set:addAll"); + +      aliases.add("set:add_all_of"); +      aliases.add("set:addallof"); +      aliases.add("set:addAllOf"); + +      aliases.add("set:add_elements"); +      aliases.add("set:addelements"); +      aliases.add("set:addElements"); + +      aliases.add("set:add_elements_of"); +      aliases.add("set:addelementsof"); +      aliases.add("set:addElementsOf"); + +      aliases.add("set:add_all_elements"); +      aliases.add("set:addallelements"); +      aliases.add("set:addAllElements"); + +      aliases.add("set:add_all_elements_of"); +      aliases.add("set:addallelementsof"); +      aliases.add("set:addAllElementsOf"); + +      return aliases; +   } + +   public static GenericComputation build +   ( +      final Origin origin, +      final String alias, +      final List<Computation> call_parameters +   ) +   throws Throwable +   { +      final Computation other_collection; +      final Computation collection; + +      if (call_parameters.size() != 2) +      { +         // TODO: Error. +         System.err.println("Wrong number of params at " + origin.toString()); + +         return null; +      } + +      other_collection = call_parameters.get(0); +      collection = call_parameters.get(1); + +      other_collection.expect_non_string(); +      collection.expect_non_string(); + +      if (alias.startsWith("set:")) +      { +         RecurrentChecks.assert_is_a_set(collection); +      } +      else +      { +         RecurrentChecks.assert_is_a_list(collection); +      } + +      RecurrentChecks.assert_is_a_collection(other_collection); +      RecurrentChecks.assert_can_be_used_as +      ( +         other_collection.get_origin(), +         ((CollectionType) other_collection.get_type()).get_content_type(), +         ((CollectionType) collection.get_type()).get_content_type() +      ); + +      return new AddElementsOfComputation(origin, other_collection, collection); +   } + +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final Computation other_collection; +   protected final Computation collection; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected AddElementsOfComputation +   ( +      final Origin origin, +      final Computation other_collection, +      final Computation collection +   ) +   { +      super(origin, collection.get_type()); + +      this.collection = collection; +      this.other_collection = other_collection; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Accessors ************************************************************/ + +   public Computation get_source_collection () +   { +      return other_collection; +   } + +   public Computation get_target_collection () +   { +      return collection; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      sb.append("(AddElementsOf"); +      sb.append(System.lineSeparator()); +      sb.append(System.lineSeparator()); + +      sb.append("other_collection:"); +      sb.append(System.lineSeparator()); +      sb.append(other_collection.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/computation/generic/CarCdr.java b/src/core/src/tonkadur/fate/v1/lang/computation/generic/CarCdr.java new file mode 100644 index 0000000..aae1102 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/generic/CarCdr.java @@ -0,0 +1,150 @@ +package tonkadur.fate.v1.lang.computation.generic; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +import tonkadur.parser.Origin; + +import tonkadur.error.ErrorManager; + +import tonkadur.fate.v1.error.InvalidTypeException; + +import tonkadur.fate.v1.lang.meta.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; + +import tonkadur.fate.v1.lang.type.ConsType; +import tonkadur.fate.v1.lang.type.Type; + +import tonkadur.fate.v1.lang.computation.GenericComputation; + +public class CarCdr extends GenericComputation +{ +   public static Collection<String> get_aliases () +   { +      final Collection<String> aliases; + +      aliases = new ArrayList<String>(); + +      aliases.add("car"); +      aliases.add("cdr"); + +      return aliases; +   } + +   public static GenericComputation build +   ( +      final Origin origin, +      final String alias, +      final List<Computation> call_parameters +   ) +   throws Throwable +   { +      final Computation parent; +      final boolean is_car; +      Type current_type; + +      if (call_parameters.size() != 1) +      { +         // TODO: Error. +         System.err.println("Wrong number of params at " + origin.toString()); + +         return null; +      } + +      parent = call_parameters.get(0); + +      parent.expect_non_string(); + +      is_car = alias.equals("car"); + +      current_type = parent.get_type(); + +      if (!(current_type instanceof ConsType)) +      { +         ErrorManager.handle +         ( +            new InvalidTypeException +            ( +               origin, +               current_type, +               Collections.singletonList(ConsType.ARCHETYPE) +            ) +         ); + +         current_type = Type.ANY; +      } +      else if (is_car) +      { +         current_type = ((ConsType) current_type).get_car_type(); +      } +      else +      { +         current_type = ((ConsType) current_type).get_cdr_type(); +      } + +      return new CarCdr(origin, parent, is_car, current_type); +   } + +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final Computation parent; +   protected final boolean is_car; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected CarCdr +   ( +      final Origin origin, +      final Computation parent, +      final boolean is_car, +      final Type type +   ) +   { +      super(origin, type); + +      this.parent = parent; +      this.is_car = is_car; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Accessors ************************************************************/ +   public Computation get_parent () +   { +      return parent; +   } + +   public boolean is_car () +   { +      return is_car; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      if (is_car) +      { +         sb.append("(car ("); +      } +      else +      { +         sb.append("(cdr ("); +      } + +      sb.append(type.get_name()); +      sb.append(") "); +      sb.append(parent); +      sb.append(")"); + +      return sb.toString(); +   } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/generic/ConsComputation.java b/src/core/src/tonkadur/fate/v1/lang/computation/generic/ConsComputation.java new file mode 100644 index 0000000..09488d4 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/generic/ConsComputation.java @@ -0,0 +1,112 @@ +package tonkadur.fate.v1.lang.computation.generic; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import tonkadur.parser.Origin; + +import tonkadur.fate.v1.lang.type.ConsType; + +import tonkadur.fate.v1.lang.meta.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; + +import tonkadur.fate.v1.lang.computation.GenericComputation; + +public class ConsComputation extends GenericComputation +{ +   public static Collection<String> get_aliases () +   { +      final Collection<String> aliases; + +      aliases = new ArrayList<String>(); + +      aliases.add("cons"); + +      return aliases; +   } + +   public static GenericComputation build +   ( +      final Origin origin, +      final String alias, +      final List<Computation> call_parameters +   ) +   throws Throwable +   { +      final Computation car; +      final Computation cdr; + +      if (call_parameters.size() != 2) +      { +         // TODO: Error. +         System.err.println("Wrong number of params at " + origin.toString()); + +         return null; +      } + +      car = call_parameters.get(0); +      cdr = call_parameters.get(1); + +      car.expect_non_string(); +      cdr.expect_non_string(); + +      return new ConsComputation(origin, car, cdr); +   } + +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final Computation car; +   protected final Computation cdr; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected ConsComputation +   ( +      final Origin origin, +      final Computation car, +      final Computation cdr +   ) +   { +      super +      ( +         origin, +         new ConsType(origin, car.get_type(), cdr.get_type(), "auto generated") +      ); + +      this.car = car; +      this.cdr = cdr; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Accessors ************************************************************/ +   public Computation get_car () +   { +      return car; +   } + +   public Computation get_cdr () +   { +      return cdr; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      sb.append("(Cons "); +      sb.append(car.toString()); +      sb.append(" "); +      sb.append(car.toString()); +      sb.append(") "); + +      return sb.toString(); +   } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/generic/CountOperator.java b/src/core/src/tonkadur/fate/v1/lang/computation/generic/CountOperator.java new file mode 100644 index 0000000..da473d3 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/generic/CountOperator.java @@ -0,0 +1,143 @@ +package tonkadur.fate.v1.lang.computation.generic; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import tonkadur.error.ErrorManager; + +import tonkadur.parser.Origin; +import tonkadur.parser.ParsingError; + +import tonkadur.fate.v1.lang.type.Type; + +import tonkadur.fate.v1.lang.meta.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.RecurrentChecks; + +import tonkadur.fate.v1.lang.computation.GenericComputation; + +public class CountOperator extends GenericComputation +{ +   public static Collection<String> get_aliases () +   { +      final Collection<String> aliases; + +      aliases = new ArrayList<String>(); + +      aliases.add("list:count"); +      aliases.add("list:number_of"); +      aliases.add("list:numberof"); +      aliases.add("list:numberOf"); +      aliases.add("set:count"); +      aliases.add("set:number_of"); +      aliases.add("set:numberof"); +      aliases.add("set:numberOf"); + +      return aliases; +   } + +   public static GenericComputation build +   ( +      final Origin origin, +      final String alias, +      final List<Computation> call_parameters +   ) +   throws Throwable +   { +      final Computation element; +      final Computation collection; + +      if (call_parameters.size() != 2) +      { +         // TODO: Error. +         System.err.println("Wrong number of params at " + origin.toString()); + +         return null; +      } + +      element = call_parameters.get(0); +      collection = call_parameters.get(1); + +      if (alias.startsWith("set:")) +      { +         RecurrentChecks.propagate_expected_types_and_assert_is_a_set_of +         ( +            collection, +            element +         ); +      } +      else +      { +         RecurrentChecks.propagate_expected_types_and_assert_is_a_list_of +         ( +            collection, +            element +         ); +      } + +      return new CountOperator(origin, element, collection); +   } + +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final Computation element; +   protected final Computation collection; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected CountOperator +   ( +      final Origin origin, +      final Computation element, +      final Computation collection +   ) +   { +      super(origin, Type.INT); + +      this.collection = collection; +      this.element = element; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Accessors ************************************************************/ +   public Computation get_collection () +   { +      return collection; +   } + +   public Computation get_element () +   { +      return element; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      sb.append("(CountOperator"); +      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/computation/generic/FilterComputation.java b/src/core/src/tonkadur/fate/v1/lang/computation/generic/FilterComputation.java new file mode 100644 index 0000000..ec84cd0 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/generic/FilterComputation.java @@ -0,0 +1,190 @@ +package tonkadur.fate.v1.lang.computation.generic; + +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.ArrayList; + +import tonkadur.parser.Origin; +import tonkadur.parser.ParsingError; + +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.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.RecurrentChecks; + +import tonkadur.fate.v1.lang.computation.GenericComputation; + +public class FilterComputation extends GenericComputation +{ +   public static Collection<String> get_aliases () +   { +      final Collection<String> aliases; + +      aliases = new ArrayList<String>(); + +      aliases.add("list:filter"); +      aliases.add("set:filter"); + +      return aliases; +   } + +   public static GenericComputation build +   ( +      final Origin origin, +      final String alias, +      final List<Computation> call_parameters +   ) +   throws Throwable +   { +      final Computation lambda_function; +      final Computation collection; +      final List<Computation> extra_params; +      final List<Type> signature; + +      if (call_parameters.size() < 2) +      { +         // TODO: Error. +         System.err.println("Wrong number of params at " + origin.toString()); + +         return null; +      } + +      lambda_function = call_parameters.get(0); +      collection = call_parameters.get(1); + +      if (call_parameters.size() == 2) +      { +         extra_params = new ArrayList<Computation>(); +      } +      else +      { +         extra_params = call_parameters.subList(2, call_parameters.size()); +      } + +      lambda_function.expect_non_string(); +      collection.expect_non_string(); + +      RecurrentChecks.assert_is_a_lambda_function(lambda_function); + +      signature = ((LambdaType) lambda_function.get_type()).get_signature(); + +      if (signature.size() < 1) +      { +         // TODO: Error. +         System.err.println +         ( +            "Lambda signature too small at " +            + lambda_function.get_origin().toString() +         ); + +         return null; +      } + +      if (signature.size() > 2) +      { +         RecurrentChecks.propagate_expected_types_and_assert_computations_matches_signature +         ( +            origin, +            extra_params, +            signature.subList(1, signature.size()) +         ); +      } + +      if (alias.startsWith("set:")) +      { +         RecurrentChecks.assert_is_a_set_of(collection, signature.get(0)); +      } +      else +      { +         RecurrentChecks.assert_is_a_list_of(collection, signature.get(0)); +      } + +      RecurrentChecks.assert_can_be_used_as +      ( +         lambda_function.get_origin(), +         ((LambdaType) lambda_function.get_type()).get_return_type(), +         Type.BOOL +      ); + +      return +         new FilterComputation +         ( +            origin, +            lambda_function, +            collection, +            extra_params +         ); +   } + +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final List<Computation> extra_params; +   protected final Computation lambda_function; +   protected final Computation collection; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected FilterComputation +   ( +      final Origin origin, +      final Computation lambda_function, +      final Computation collection, +      final List<Computation> extra_params +   ) +   { +      super(origin, collection.get_type()); + +      this.lambda_function = lambda_function; +      this.collection = collection; +      this.extra_params = extra_params; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Accessors ************************************************************/ +   public Computation get_lambda_function () +   { +      return lambda_function; +   } + +   public Computation get_collection () +   { +      return collection; +   } + +   public List<Computation> get_extra_parameters () +   { +      return extra_params; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      sb.append("(Filter "); +      sb.append(lambda_function.toString()); +      sb.append(" "); +      sb.append(collection.toString()); + +      for (final Computation c: extra_params) +      { +         sb.append(" "); +         sb.append(c.toString()); +      } + +      sb.append(")"); + +      return sb.toString(); +   } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/generic/Fold.java b/src/core/src/tonkadur/fate/v1/lang/computation/generic/Fold.java new file mode 100644 index 0000000..cb0f317 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/generic/Fold.java @@ -0,0 +1,251 @@ +package tonkadur.fate.v1.lang.computation.generic; + +import java.util.Collection; +import java.util.ArrayList; +import java.util.List; + +import tonkadur.parser.Origin; +import tonkadur.parser.ParsingError; + +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.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.RecurrentChecks; + +import tonkadur.fate.v1.lang.computation.GenericComputation; + +public class Fold extends GenericComputation +{ +   public static Collection<String> get_aliases () +   { +      final Collection<String> aliases; + +      aliases = new ArrayList<String>(); + +      aliases.add("list:foldl"); +      aliases.add("list:foldr"); +      aliases.add("list:fold_left"); +      aliases.add("list:foldleft"); +      aliases.add("list:foldLeft"); +      aliases.add("list:fold_right"); +      aliases.add("list:foldright"); +      aliases.add("list:foldRight"); + +      aliases.add("set:foldl"); +      aliases.add("set:foldr"); +      aliases.add("set:fold_left"); +      aliases.add("set:foldleft"); +      aliases.add("set:foldLeft"); +      aliases.add("set:fold_right"); +      aliases.add("set:foldright"); +      aliases.add("set:foldRight"); + +      return aliases; +   } + +   public static GenericComputation build +   ( +      final Origin origin, +      final String alias, +      final List<Computation> call_parameters +   ) +   throws Throwable +   { +      final Computation lambda_function; +      final Computation initial_value; +      final Computation collection; +      final boolean is_foldl; +      final List<Computation> extra_params; +      final List<Type> signature; +      final List<Type> expected_signature; + +      if (call_parameters.size() < 3) +      { +         // TODO: Error. +         System.err.println("Wrong number of params at " + origin.toString()); + +         return null; +      } + +      lambda_function = call_parameters.get(0); +      initial_value = call_parameters.get(1); +      collection = call_parameters.get(2); + +      lambda_function.expect_non_string(); +      collection.expect_non_string(); + +      is_foldl = (alias.contains("foldl") || alias.contains("eft")); + +      if (call_parameters.size() == 3) +      { +         extra_params = new ArrayList<Computation>(); +      } +      else +      { +         extra_params = call_parameters.subList(3, call_parameters.size()); +      } + +      signature = ((LambdaType) lambda_function.get_type()).get_signature(); + +      if (signature.size() < 2) +      { +         // TODO: Error. +         System.err.println +         ( +            "Lambda signature too small at " +            + lambda_function.get_origin().toString() +         ); + +         return null; +      } + +      if (signature.size() > 2) +      { +         RecurrentChecks.propagate_expected_types_and_assert_computations_matches_signature +         ( +            origin, +            extra_params, +            signature.subList(2, signature.size()) +         ); +      } + +      if (alias.startsWith("set:")) +      { +         RecurrentChecks.assert_is_a_set_of(collection, signature.get(0)); +      } +      else +      { +         RecurrentChecks.assert_is_a_list_of(collection, signature.get(0)); +      } + +      RecurrentChecks.handle_expected_type_propagation +      ( +         initial_value, +         signature.get(1) +      ); + +      RecurrentChecks.assert_can_be_used_as +      ( +         initial_value, +         signature.get(1) +      ); + +      RecurrentChecks.assert_can_be_used_as +      ( +         lambda_function.get_origin(), +         ((LambdaType) lambda_function.get_type()).get_return_type(), +         initial_value.get_type() +      ); + +      return +         new Fold +         ( +            origin, +            lambda_function, +            initial_value, +            collection, +            is_foldl, +            extra_params, +            initial_value.get_type() +         ); +   } + +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final List<Computation> extra_params; +   protected final Computation lambda_function; +   protected final Computation initial_value; +   protected final Computation collection; +   protected final boolean is_foldl; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected Fold +   ( +      final Origin origin, +      final Computation lambda_function, +      final Computation initial_value, +      final Computation collection, +      final boolean is_foldl, +      final List<Computation> extra_params, +      final Type act_as +   ) +   { +      super(origin, act_as); + +      this.lambda_function = lambda_function; +      this.initial_value = initial_value; +      this.collection = collection; +      this.is_foldl = is_foldl; +      this.extra_params = extra_params; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Accessors ************************************************************/ +   public Computation get_lambda_function () +   { +      return lambda_function; +   } + +   public Computation get_initial_value () +   { +      return initial_value; +   } + +   public Computation get_collection () +   { +      return collection; +   } + +   public boolean is_foldl () +   { +      return is_foldl; +   } + +   public List<Computation> get_extra_parameters () +   { +      return extra_params; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      if (is_foldl) +      { +         sb.append("(Foldl "); +      } +      else +      { +         sb.append("(Foldr "); +      } + +      sb.append(lambda_function.toString()); + +      sb.append(" "); +      sb.append(initial_value.toString()); +      sb.append(" "); +      sb.append(collection.toString()); + +      for (final Computation c: extra_params) +      { +         sb.append(" "); +         sb.append(c.toString()); +      } + +      sb.append(")"); + +      return sb.toString(); +   } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/generic/IndexOfOperator.java b/src/core/src/tonkadur/fate/v1/lang/computation/generic/IndexOfOperator.java new file mode 100644 index 0000000..c042e18 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/generic/IndexOfOperator.java @@ -0,0 +1,138 @@ +package tonkadur.fate.v1.lang.computation.generic; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import tonkadur.parser.Origin; +import tonkadur.parser.ParsingError; + +import tonkadur.fate.v1.lang.type.Type; + +import tonkadur.fate.v1.lang.meta.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.RecurrentChecks; + +import tonkadur.fate.v1.lang.computation.GenericComputation; + +public class IndexOfOperator extends GenericComputation +{ +   public static Collection<String> get_aliases () +   { +      final Collection<String> aliases; + +      aliases = new ArrayList<String>(); + +      aliases.add("list:index_of"); +      aliases.add("list:indexof"); +      aliases.add("list:indexOf"); +      aliases.add("set:index_of"); +      aliases.add("set:indexof"); +      aliases.add("set:indexOf"); + +      return aliases; +   } + +   public static GenericComputation build +   ( +      final Origin origin, +      final String alias, +      final List<Computation> call_parameters +   ) +   throws Throwable +   { +      final Computation element; +      final Computation collection; + +      if (call_parameters.size() != 2) +      { +         // TODO: Error. +         System.err.println("Wrong number of params at " + origin.toString()); + +         return null; +      } + +      element = call_parameters.get(0); +      collection = call_parameters.get(1); + +      if (alias.startsWith("set:")) +      { +         RecurrentChecks.propagate_expected_types_and_assert_is_a_set_of +         ( +            collection, +            element +         ); +      } +      else +      { +         RecurrentChecks.propagate_expected_types_and_assert_is_a_list_of +         ( +            collection, +            element +         ); +      } + +      return new IndexOfOperator(origin, element, collection); +   } +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final Computation element; +   protected final Computation collection; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected IndexOfOperator +   ( +      final Origin origin, +      final Computation element, +      final Computation collection +   ) +   { +      super(origin, Type.INT); + +      this.collection = collection; +      this.element = element; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Accessors ************************************************************/ +   public Computation get_collection () +   { +      return collection; +   } + +   public Computation get_element () +   { +      return element; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      sb.append("(IndexOfOperator"); +      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/computation/generic/IndexedFilterComputation.java b/src/core/src/tonkadur/fate/v1/lang/computation/generic/IndexedFilterComputation.java new file mode 100644 index 0000000..1d17a99 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/generic/IndexedFilterComputation.java @@ -0,0 +1,153 @@ +package tonkadur.fate.v1.lang.computation.generic; + +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.ArrayList; + +import tonkadur.parser.Origin; +import tonkadur.parser.ParsingError; + +import tonkadur.fate.v1.lang.type.Type; +import tonkadur.fate.v1.lang.type.CollectionType; + +import tonkadur.fate.v1.lang.meta.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.RecurrentChecks; + +import tonkadur.fate.v1.lang.computation.GenericComputation; + +public class IndexedFilterComputation extends GenericComputation +{ +   public static Collection<String> get_aliases () +   { +      final Collection<String> aliases; + +      aliases = new ArrayList<String>(); + +      aliases.add("list:indexed_filter"); +      aliases.add("list:indexedfilter"); +      aliases.add("list:indexedFilter"); +      aliases.add("list:ifilter"); +      aliases.add("set:indexed_filter"); +      aliases.add("set:indexedfilter"); +      aliases.add("set:indexedFilter"); +      aliases.add("set:ifilter"); + +      return aliases; +   } + +   public static GenericComputation build +   ( +      final Origin origin, +      final String alias, +      final List<Computation> call_parameters +   ) +   throws Throwable +   { +      // TODO: implement +      final Computation lambda_function = null; +      final Computation collection = null; +      final List<Computation> extra_params = null; +      final List<Type> target_signature; + +      target_signature = new ArrayList<Type>(); + +      RecurrentChecks.assert_is_a_collection(collection); + +      target_signature.add(Type.INT); +      target_signature.add +      ( +         ((CollectionType) collection.get_type()).get_content_type() +      ); + +      for (final Computation c: extra_params) +      { +         target_signature.add(c.get_type()); +      } + +      RecurrentChecks.assert_lambda_matches_types +      ( +         lambda_function, +         Type.BOOL, +         target_signature +      ); + +      return +         new IndexedFilterComputation +         ( +            origin, +            lambda_function, +            collection, +            extra_params +         ); +   } + +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final List<Computation> extra_params; +   protected final Computation lambda_function; +   protected final Computation collection; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected IndexedFilterComputation +   ( +      final Origin origin, +      final Computation lambda_function, +      final Computation collection, +      final List<Computation> extra_params +   ) +   { +      super(origin, collection.get_type()); + +      this.lambda_function = lambda_function; +      this.collection = collection; +      this.extra_params = extra_params; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Accessors ************************************************************/ +   public Computation get_lambda_function () +   { +      return lambda_function; +   } + +   public Computation get_collection () +   { +      return collection; +   } + +   public List<Computation> get_extra_parameters () +   { +      return extra_params; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      sb.append("(IndexedFilter "); +      sb.append(lambda_function.toString()); +      sb.append(" "); +      sb.append(collection.toString()); + +      for (final Computation c: extra_params) +      { +         sb.append(" "); +         sb.append(c.toString()); +      } + +      sb.append(")"); + +      return sb.toString(); +   } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/generic/IndexedMapComputation.java b/src/core/src/tonkadur/fate/v1/lang/computation/generic/IndexedMapComputation.java new file mode 100644 index 0000000..717dc53 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/generic/IndexedMapComputation.java @@ -0,0 +1,156 @@ +package tonkadur.fate.v1.lang.computation.generic; + +import java.util.Collection; +import java.util.List; +import java.util.ArrayList; + +import tonkadur.parser.Origin; +import tonkadur.parser.ParsingError; + +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.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.RecurrentChecks; + +import tonkadur.fate.v1.lang.computation.GenericComputation; + +public class IndexedMapComputation extends GenericComputation +{ +   public static Collection<String> get_aliases () +   { +      final Collection<String> aliases; + +      aliases = new ArrayList<String>(); + +      aliases.add("list:indexed_map"); +      aliases.add("list:indexedmap"); +      aliases.add("list:indexedMap"); +      aliases.add("list:imap"); +      aliases.add("set:indexed_map"); +      aliases.add("set:indexedmap"); +      aliases.add("set:indexedMap"); +      aliases.add("set:imap"); + +      return aliases; +   } + +   public static GenericComputation build +   ( +      final Origin origin, +      final String alias, +      final List<Computation> call_parameters +   ) +   throws Throwable +   { +      // TODO: implement +      final Computation lambda_function = null; +      final Computation collection = null; +      final List<Computation> extra_params = null; +      final List<Type> in_types; + +      in_types = new ArrayList<Type>(); + +      RecurrentChecks.assert_is_a_collection(collection); + +      in_types.add(Type.INT); +      in_types.add +      ( +         ((CollectionType) collection.get_type()).get_content_type() +      ); + +      for (final Computation c: extra_params) +      { +         in_types.add(c.get_type()); +      } + +      RecurrentChecks.assert_lambda_matches_types(lambda_function, in_types); + +      return +         new IndexedMapComputation +         ( +            origin, +            lambda_function, +            collection, +            extra_params, +            CollectionType.build +            ( +               origin, +               ((LambdaType) lambda_function.get_type()).get_return_type(), +               ((CollectionType) collection.get_type()).is_set(), +               "auto generated" +            ) +         ); +   } + +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final List<Computation> extra_params; +   protected final Computation lambda_function; +   protected final Computation collection; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected IndexedMapComputation +   ( +      final Origin origin, +      final Computation lambda_function, +      final Computation collection, +      final List<Computation> extra_params, +      final Type output_type +   ) +   { +      super(origin, output_type); + +      this.lambda_function = lambda_function; +      this.collection = collection; +      this.extra_params = extra_params; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Accessors ************************************************************/ +   public Computation get_lambda_function () +   { +      return lambda_function; +   } + +   public Computation get_collection () +   { +      return collection; +   } + +   public List<Computation> get_extra_parameters () +   { +      return extra_params; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      sb.append("(IndexedMap "); +      sb.append(lambda_function.toString()); +      sb.append(" "); +      sb.append(collection.toString()); + +      for (final Computation c: extra_params) +      { +         sb.append(" "); +         sb.append(c.toString()); +      } + +      sb.append(")"); + +      return sb.toString(); +   } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/generic/IndexedMergeComputation.java b/src/core/src/tonkadur/fate/v1/lang/computation/generic/IndexedMergeComputation.java new file mode 100644 index 0000000..021c4ce --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/generic/IndexedMergeComputation.java @@ -0,0 +1,266 @@ +package tonkadur.fate.v1.lang.computation.generic; + +import java.util.Collection; +import java.util.ArrayList; +import java.util.List; + +import tonkadur.parser.Origin; +import tonkadur.parser.ParsingError; + +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.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.RecurrentChecks; + +import tonkadur.fate.v1.lang.computation.GenericComputation; + +public class IndexedMergeComputation extends GenericComputation +{ +   public static Collection<String> get_aliases () +   { +      final Collection<String> aliases; + +      aliases = new ArrayList<String>(); + +      aliases.add("list:indexed_merge"); +      aliases.add("list:indexedmerge"); +      aliases.add("list:indexedMerge"); +      aliases.add("list:imerge"); +      aliases.add("set:indexed_merge"); +      aliases.add("set:indexedmerge"); +      aliases.add("set:indexedMerge"); +      aliases.add("set:imerge"); + +      return aliases; +   } + +   public static GenericComputation build +   ( +      final Origin origin, +      final String alias, +      final List<Computation> call_parameters +   ) +   throws Throwable +   { +      // TODO: implement +      final Computation lambda_function = null; +      final Computation collection_in_a = null; +      final Computation default_a = null; +      final Computation collection_in_b = null; +      final Computation default_b = null; +      final boolean to_set = false; +      final List<Computation> extra_params = null; +      final List<Type> types_in; + +      types_in = new ArrayList<Type>(); + +      if (default_a == null) +      { +         RecurrentChecks.assert_is_a_collection(collection_in_a); +      } +      else +      { +         RecurrentChecks.propagate_expected_types_and_assert_is_a_collection_of +         ( +            collection_in_a, +            default_a +         ); +      } + +      if (default_b == null) +      { +         RecurrentChecks.assert_is_a_collection(collection_in_b); +      } +      else +      { +         RecurrentChecks.propagate_expected_types_and_assert_is_a_collection_of +         ( +            collection_in_b, +            default_b +         ); +      } + +      types_in.add(Type.INT); + +      types_in.add +      ( +         ((CollectionType) collection_in_a.get_type()).get_content_type() +      ); + +      if (default_b != null) +      { +         /* +          * Safe-Mode: two indices. +          * Unsafe-Mode: only one index, since out-of-bound means stopping. +          */ +         types_in.add(Type.INT); +      } + +      types_in.add +      ( +         ((CollectionType) collection_in_b.get_type()).get_content_type() +      ); + +      for (final Computation c: extra_params) +      { +         types_in.add(c.get_type()); +      } + +      RecurrentChecks.assert_lambda_matches_types(lambda_function, types_in); + +      return +         new IndexedMergeComputation +         ( +            origin, +            lambda_function, +            collection_in_a, +            default_a, +            collection_in_b, +            default_b, +            to_set, +            extra_params, +            CollectionType.build +            ( +               origin, +               ((LambdaType) lambda_function.get_type()).get_return_type(), +               to_set, +               "auto generated" +            ) +         ); +   } + +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final List<Computation> extra_params; +   protected final Computation lambda_function; +   protected final Computation collection_in_a; +   protected final Computation default_a; +   protected final Computation collection_in_b; +   protected final Computation default_b; +   protected final boolean to_set; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected IndexedMergeComputation +   ( +      final Origin origin, +      final Computation lambda_function, +      final Computation collection_in_a, +      final Computation default_a, +      final Computation collection_in_b, +      final Computation default_b, +      final boolean to_set, +      final List<Computation> extra_params, +      final Type output_type +   ) +   { +      super(origin, output_type); + +      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.to_set = to_set; +      this.extra_params = extra_params; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Accessors ************************************************************/ +   public Computation get_lambda_function () +   { +      return lambda_function; +   } + +   public Computation get_collection_in_a () +   { +      return collection_in_a; +   } + +   public Computation get_default_a () +   { +      return default_a; +   } + +   public Computation get_collection_in_b () +   { +      return collection_in_b; +   } + +   public Computation get_default_b () +   { +      return default_b; +   } + +   public List<Computation> get_extra_parameters () +   { +      return extra_params; +   } + +   public boolean to_set () +   { +      return to_set; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      if (to_set) +      { +         sb.append("(IndexedMergeToSet "); +      } +      else +      { +         sb.append("(IndexedMergeToList "); +      } + +      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()); +      } + +      for (final Computation c: extra_params) +      { +         sb.append(" "); +         sb.append(c.toString()); +      } + +      sb.append(")"); + +      return sb.toString(); +   } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/generic/IndexedPartitionComputation.java b/src/core/src/tonkadur/fate/v1/lang/computation/generic/IndexedPartitionComputation.java new file mode 100644 index 0000000..46df979 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/generic/IndexedPartitionComputation.java @@ -0,0 +1,167 @@ +package tonkadur.fate.v1.lang.computation.generic; + +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.ArrayList; + +import tonkadur.parser.Origin; +import tonkadur.parser.ParsingError; + +import tonkadur.fate.v1.lang.type.CollectionType; +import tonkadur.fate.v1.lang.type.ConsType; +import tonkadur.fate.v1.lang.type.Type; + +import tonkadur.fate.v1.lang.meta.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.RecurrentChecks; + +import tonkadur.fate.v1.lang.computation.GenericComputation; + +public class IndexedPartitionComputation extends GenericComputation +{ +   public static Collection<String> get_aliases () +   { +      final Collection<String> aliases; + +      aliases = new ArrayList<String>(); + +      aliases.add("list:indexed_partition"); +      aliases.add("list:indexedpartition"); +      aliases.add("list:indexedPartition"); +      aliases.add("list:ipartition"); +      aliases.add("set:indexed_partition"); +      aliases.add("set:indexedpartition"); +      aliases.add("set:indexedPartition"); +      aliases.add("set:ipartition"); + +      return aliases; +   } + +   public static GenericComputation build +   ( +      final Origin origin, +      final String alias, +      final List<Computation> call_parameters +   ) +   throws Throwable +   { +      // TODO: implement +      final Computation lambda_function = null; +      final Computation collection = null; +      final List<Computation> extra_params = null; +      final Type type; +      final List<Type> target_signature; + +      target_signature = new ArrayList<Type>(); + +      RecurrentChecks.assert_is_a_collection(collection); + +      target_signature.add(Type.INT); + +      target_signature.add +      ( +         ((CollectionType) collection.get_type()).get_content_type() +      ); + +      for (final Computation c: extra_params) +      { +         target_signature.add(c.get_type()); +      } + +      RecurrentChecks.assert_lambda_matches_types +      ( +         lambda_function, +         Type.BOOL, +         target_signature +      ); + +      type = +         new ConsType +         ( +            origin, +            collection.get_type(), +            collection.get_type(), +            "auto generated" +         ); + +      return +         new IndexedPartitionComputation +         ( +            origin, +            lambda_function, +            collection, +            extra_params, +            type +         ); +   } + +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final List<Computation> extra_params; +   protected final Computation lambda_function; +   protected final Computation collection; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected IndexedPartitionComputation +   ( +      final Origin origin, +      final Computation lambda_function, +      final Computation collection, +      final List<Computation> extra_params, +      final Type output_type +   ) +   { +      super(origin, output_type); + +      this.lambda_function = lambda_function; +      this.collection = collection; +      this.extra_params = extra_params; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Accessors ************************************************************/ +   public Computation get_lambda_function () +   { +      return lambda_function; +   } + +   public Computation get_collection () +   { +      return collection; +   } + +   public List<Computation> get_extra_parameters () +   { +      return extra_params; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      sb.append("(IndexedPartition "); +      sb.append(lambda_function.toString()); +      sb.append(" "); +      sb.append(collection.toString()); + +      for (final Computation c: extra_params) +      { +         sb.append(" "); +         sb.append(c.toString()); +      } + +      sb.append(")"); + +      return sb.toString(); +   } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/generic/IsEmpty.java b/src/core/src/tonkadur/fate/v1/lang/computation/generic/IsEmpty.java new file mode 100644 index 0000000..6b2d177 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/generic/IsEmpty.java @@ -0,0 +1,125 @@ +package tonkadur.fate.v1.lang.computation.generic; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import tonkadur.parser.Origin; +import tonkadur.parser.ParsingError; + +import tonkadur.fate.v1.lang.type.Type; + +import tonkadur.fate.v1.lang.meta.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.RecurrentChecks; + +import tonkadur.fate.v1.lang.computation.GenericComputation; + +public class IsEmpty extends GenericComputation +{ +   public static Collection<String> get_aliases () +   { +      final Collection<String> aliases; + +      aliases = new ArrayList<String>(); + +      aliases.add("list:empty?"); +      aliases.add("list:empty"); +      aliases.add("list:is_empty?"); +      aliases.add("list:isempty?"); +      aliases.add("list:isEmpty?"); +      aliases.add("list:is_empty"); +      aliases.add("list:isempty"); +      aliases.add("list:isEmpty"); + +      aliases.add("set:empty?"); +      aliases.add("set:empty"); +      aliases.add("set:is_empty?"); +      aliases.add("set:isempty?"); +      aliases.add("set:isEmpty?"); +      aliases.add("set:is_empty"); +      aliases.add("set:isempty"); +      aliases.add("set:isEmpty"); + +      return aliases; +   } + +   public static GenericComputation build +   ( +      final Origin origin, +      final String alias, +      final List<Computation> call_parameters +   ) +   throws Throwable +   { +      final Computation collection; + +      if (call_parameters.size() != 1) +      { +         // TODO: Error. +         System.err.println("Wrong number of params at " + origin.toString()); + +         return null; +      } + +      collection = call_parameters.get(0); + +      if (alias.startsWith("set:")) +      { +         RecurrentChecks.assert_is_a_set(collection); +      } +      else +      { +         RecurrentChecks.assert_is_a_list(collection); +      } + +      return new IsEmpty(origin, collection); +   } +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final Computation collection; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected IsEmpty +   ( +      final Origin origin, +      final Computation collection +   ) +   { +      super(origin, Type.BOOL); + +      this.collection = collection; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Accessors ************************************************************/ +   public Computation get_collection () +   { +      return collection; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      sb.append("(IsEmpty"); +      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/computation/generic/IsMemberOperator.java b/src/core/src/tonkadur/fate/v1/lang/computation/generic/IsMemberOperator.java new file mode 100644 index 0000000..0670d63 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/generic/IsMemberOperator.java @@ -0,0 +1,148 @@ +package tonkadur.fate.v1.lang.computation.generic; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import tonkadur.parser.Origin; +import tonkadur.parser.ParsingError; + +import tonkadur.fate.v1.lang.type.Type; + +import tonkadur.fate.v1.lang.meta.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.RecurrentChecks; + +import tonkadur.fate.v1.lang.computation.GenericComputation; + +public class IsMemberOperator extends GenericComputation +{ +   public static Collection<String> get_aliases () +   { +      final Collection<String> aliases; + +      aliases = new ArrayList<String>(); + +      aliases.add("list:is_member?"); +      aliases.add("list:ismember?"); +      aliases.add("list:isMember?"); +      aliases.add("list:member?"); +      aliases.add("list:member"); +      aliases.add("list:is_member"); +      aliases.add("list:ismember"); +      aliases.add("list:isMember"); +      aliases.add("set:is_member?"); +      aliases.add("set:ismember?"); +      aliases.add("set:isMember?"); +      aliases.add("set:member?"); +      aliases.add("set:member"); +      aliases.add("set:is_member"); +      aliases.add("set:ismember"); +      aliases.add("set:isMember"); + +      return aliases; +   } + +   public static GenericComputation build +   ( +      final Origin origin, +      final String alias, +      final List<Computation> call_parameters +   ) +   throws Throwable +   { +      final Computation element; +      final Computation collection; + +      if (call_parameters.size() != 2) +      { +         // TODO: Error. +         System.err.println("Wrong number of params at " + origin.toString()); + +         return null; +      } + +      element = call_parameters.get(0); +      collection = call_parameters.get(1); + +      if (alias.startsWith("set:")) +      { +         RecurrentChecks.propagate_expected_types_and_assert_is_a_set_of +         ( +            collection, +            element +         ); +      } +      else +      { +         RecurrentChecks.propagate_expected_types_and_assert_is_a_list_of +         ( +            collection, +            element +         ); +      } + +      return new IsMemberOperator(origin, element, collection); +   } +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final Computation element; +   protected final Computation collection; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected IsMemberOperator +   ( +      final Origin origin, +      final Computation element, +      final Computation collection +   ) +   { +      super(origin, Type.BOOL); + +      this.collection = collection; +      this.element = element; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Accessors ************************************************************/ +   public Computation get_collection () +   { +      return collection; +   } + +   public Computation get_element () +   { +      return element; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      sb.append("(IsMemberOperator"); +      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/computation/generic/MapComputation.java b/src/core/src/tonkadur/fate/v1/lang/computation/generic/MapComputation.java new file mode 100644 index 0000000..e271285 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/generic/MapComputation.java @@ -0,0 +1,153 @@ +package tonkadur.fate.v1.lang.computation.generic; + +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.ArrayList; + +import tonkadur.parser.Origin; +import tonkadur.parser.ParsingError; + +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.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.RecurrentChecks; + +import tonkadur.fate.v1.lang.computation.GenericComputation; + +public class MapComputation extends GenericComputation +{ +   public static Collection<String> get_aliases () +   { +      final Collection<String> aliases; + +      aliases = new ArrayList<String>(); + +      aliases.add("list:map"); +      aliases.add("set:map"); + +      return aliases; +   } + +   public static GenericComputation build +   ( +      final Origin origin, +      final String alias, +      final List<Computation> call_parameters +   ) +   throws Throwable +   { +      // TODO: implement +      final Computation lambda_function = null; +      final Computation collection = null; +      final List<Computation> extra_params = null; +      final List<Type> target_signature; + +      target_signature = new ArrayList<Type>(); + +      RecurrentChecks.assert_is_a_collection(collection); + +      target_signature.add +      ( +         ((CollectionType) collection.get_type()).get_content_type() +      ); + +      for (final Computation c: extra_params) +      { +         target_signature.add(c.get_type()); +      } + +      RecurrentChecks.assert_lambda_matches_types +      ( +         lambda_function, +         target_signature +      ); + +      return +         new MapComputation +         ( +            origin, +            lambda_function, +            collection, +            extra_params, +            CollectionType.build +            ( +               origin, +               ((LambdaType) lambda_function.get_type()).get_return_type(), +               ((CollectionType) collection.get_type()).is_set(), +               "auto generated" +            ) +         ); +   } +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final List<Computation> extra_params; +   protected final Computation lambda_function; +   protected final Computation collection; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected MapComputation +   ( +      final Origin origin, +      final Computation lambda_function, +      final Computation collection, +      final List<Computation> extra_params, +      final Type output_type +   ) +   { +      super(origin, output_type); + +      this.lambda_function = lambda_function; +      this.collection = collection; +      this.extra_params = extra_params; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Accessors ************************************************************/ +   public Computation get_lambda_function () +   { +      return lambda_function; +   } + +   public Computation get_collection () +   { +      return collection; +   } + +   public List<Computation> get_extra_parameters () +   { +      return extra_params; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      sb.append("(Map "); +      sb.append(lambda_function.toString()); +      sb.append(" "); +      sb.append(collection.toString()); + +      for (final Computation c: extra_params) +      { +         sb.append(" "); +         sb.append(c.toString()); +      } + +      sb.append(")"); + +      return sb.toString(); +   } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/generic/MergeComputation.java b/src/core/src/tonkadur/fate/v1/lang/computation/generic/MergeComputation.java new file mode 100644 index 0000000..2354ba3 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/generic/MergeComputation.java @@ -0,0 +1,248 @@ +package tonkadur.fate.v1.lang.computation.generic; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import tonkadur.parser.Origin; +import tonkadur.parser.ParsingError; + +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.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.RecurrentChecks; + +import tonkadur.fate.v1.lang.computation.GenericComputation; + +public class MergeComputation extends GenericComputation +{ +   public static Collection<String> get_aliases () +   { +      final Collection<String> aliases; + +      aliases = new ArrayList<String>(); + +      aliases.add("list:merge"); +      aliases.add("set:merge"); + +      return aliases; +   } + +   public static GenericComputation build +   ( +      final Origin origin, +      final String alias, +      final List<Computation> call_parameters +   ) +   throws Throwable +   { +      // TODO: implement +      final Computation lambda_function = null; +      final Computation collection_in_a = null; +      final Computation default_a = null; +      final Computation collection_in_b = null; +      final Computation default_b = null; +      final boolean to_set = false; +      final List<Computation> extra_params = null; +      final List<Type> types_in; + +      types_in = new ArrayList<Type>(); + +      if (default_a == null) +      { +         RecurrentChecks.assert_is_a_collection(collection_in_a); +      } +      else +      { +         RecurrentChecks.propagate_expected_types_and_assert_is_a_collection_of +         ( +            collection_in_a, +            default_a +         ); +      } + +      if (default_b == null) +      { +         RecurrentChecks.assert_is_a_collection(collection_in_b); +      } +      else +      { +         RecurrentChecks.propagate_expected_types_and_assert_is_a_collection_of +         ( +            collection_in_b, +            default_b +         ); +      } + +      types_in.add +      ( +         ((CollectionType) collection_in_a.get_type()).get_content_type() +      ); + +      types_in.add +      ( +         ((CollectionType) collection_in_b.get_type()).get_content_type() +      ); + +      for (final Computation c: extra_params) +      { +         types_in.add(c.get_type()); +      } + +      RecurrentChecks.assert_lambda_matches_types(lambda_function, types_in); + +      return +         new MergeComputation +         ( +            origin, +            lambda_function, +            collection_in_a, +            default_a, +            collection_in_b, +            default_b, +            to_set, +            extra_params, +            CollectionType.build +            ( +               origin, +               ((LambdaType) lambda_function.get_type()).get_return_type(), +               to_set, +               "auto generated" +            ) +         ); +   } +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final List<Computation> extra_params; +   protected final Computation lambda_function; +   protected final Computation collection_in_a; +   protected final Computation default_a; +   protected final Computation collection_in_b; +   protected final Computation default_b; +   protected final boolean to_set; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected MergeComputation +   ( +      final Origin origin, +      final Computation lambda_function, +      final Computation collection_in_a, +      final Computation default_a, +      final Computation collection_in_b, +      final Computation default_b, +      final boolean to_set, +      final List<Computation> extra_params, +      final Type output_type +   ) +   { +      super(origin, output_type); + +      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.to_set = to_set; +      this.extra_params = extra_params; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Accessors ************************************************************/ +   public Computation get_lambda_function () +   { +      return lambda_function; +   } + +   public Computation get_collection_in_a () +   { +      return collection_in_a; +   } + +   public Computation get_default_a () +   { +      return default_a; +   } + +   public Computation get_collection_in_b () +   { +      return collection_in_b; +   } + +   public Computation get_default_b () +   { +      return default_b; +   } + +   public List<Computation> get_extra_parameters () +   { +      return extra_params; +   } + +   public boolean to_set () +   { +      return to_set; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      if (to_set) +      { +         sb.append("(MergeToSet "); +      } +      else +      { +         sb.append("(MergeToList "); +      } + +      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()); +      } + +      for (final Computation c: extra_params) +      { +         sb.append(" "); +         sb.append(c.toString()); +      } + +      sb.append(")"); + +      return sb.toString(); +   } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/generic/PartitionComputation.java b/src/core/src/tonkadur/fate/v1/lang/computation/generic/PartitionComputation.java new file mode 100644 index 0000000..32e655d --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/generic/PartitionComputation.java @@ -0,0 +1,159 @@ +package tonkadur.fate.v1.lang.computation.generic; + +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.ArrayList; + +import tonkadur.parser.Origin; +import tonkadur.parser.ParsingError; + +import tonkadur.fate.v1.lang.type.CollectionType; +import tonkadur.fate.v1.lang.type.ConsType; +import tonkadur.fate.v1.lang.type.Type; + +import tonkadur.fate.v1.lang.meta.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.RecurrentChecks; + +import tonkadur.fate.v1.lang.computation.GenericComputation; + +public class PartitionComputation extends GenericComputation +{ +   public static Collection<String> get_aliases () +   { +      final Collection<String> aliases; + +      aliases = new ArrayList<String>(); + +      aliases.add("list:partition"); +      aliases.add("set:partition"); + +      return aliases; +   } + +   public static GenericComputation build +   ( +      final Origin origin, +      final String alias, +      final List<Computation> call_parameters +   ) +   throws Throwable +   { +      // TODO: implement +      final Computation lambda_function = null; +      final Computation collection = null; +      final List<Computation> extra_params = null; +      final Type type; +      final List<Type> target_signature; + +      target_signature = new ArrayList<Type>(); + +      RecurrentChecks.assert_is_a_collection(collection); + +      target_signature.add +      ( +         ((CollectionType) collection.get_type()).get_content_type() +      ); + +      for (final Computation c: extra_params) +      { +         target_signature.add(c.get_type()); +      } + +      RecurrentChecks.assert_lambda_matches_types +      ( +         lambda_function, +         Type.BOOL, +         target_signature +      ); + +      type = +         new ConsType +         ( +            origin, +            collection.get_type(), +            collection.get_type(), +            "auto generated" +         ); + +      return +         new PartitionComputation +         ( +            origin, +            lambda_function, +            collection, +            extra_params, +            type +         ); +   } + +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final List<Computation> extra_params; +   protected final Computation lambda_function; +   protected final Computation collection; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected PartitionComputation +   ( +      final Origin origin, +      final Computation lambda_function, +      final Computation collection, +      final List<Computation> extra_params, +      final Type output_type +   ) +   { +      super(origin, output_type); + +      this.lambda_function = lambda_function; +      this.collection = collection; +      this.extra_params = extra_params; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Accessors ************************************************************/ +   public Computation get_lambda_function () +   { +      return lambda_function; +   } + +   public Computation get_collection () +   { +      return collection; +   } + +   public List<Computation> get_extra_parameters () +   { +      return extra_params; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      sb.append("(Partition "); +      sb.append(lambda_function.toString()); +      sb.append(" "); +      sb.append(collection.toString()); + +      for (final Computation c: extra_params) +      { +         sb.append(" "); +         sb.append(c.toString()); +      } + +      sb.append(")"); + +      return sb.toString(); +   } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/generic/PopElementComputation.java b/src/core/src/tonkadur/fate/v1/lang/computation/generic/PopElementComputation.java new file mode 100644 index 0000000..667ad6b --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/generic/PopElementComputation.java @@ -0,0 +1,146 @@ +package tonkadur.fate.v1.lang.computation.generic; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import tonkadur.parser.Origin; +import tonkadur.parser.ParsingError; + +import tonkadur.fate.v1.lang.meta.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.RecurrentChecks; + +import tonkadur.fate.v1.lang.computation.GenericComputation; + +public class PopElementComputation extends GenericComputation +{ +   public static Collection<String> get_aliases () +   { +      final Collection<String> aliases; + +      aliases = new ArrayList<String>(); + +      aliases.add("list:popleft"); +      aliases.add("list:pop_left"); +      aliases.add("list:popLeft"); +      aliases.add("list:popleftelement"); +      aliases.add("list:pop_left_element"); +      aliases.add("list:popLeftElement"); +      aliases.add("list:popright"); +      aliases.add("list:pop_right"); +      aliases.add("list:popRight"); +      aliases.add("list:poprightelement"); +      aliases.add("list:pop_right_element"); +      aliases.add("list:popRightElement"); +      aliases.add("set:popleft"); +      aliases.add("set:pop_left"); +      aliases.add("set:popLeft"); +      aliases.add("set:popleftelement"); +      aliases.add("set:pop_left_element"); +      aliases.add("set:popLeftElement"); +      aliases.add("set:popright"); +      aliases.add("set:pop_right"); +      aliases.add("set:popRight"); +      aliases.add("set:poprightelement"); +      aliases.add("set:pop_right_element"); +      aliases.add("set:popRightElement"); + +      return aliases; +   } + +   public static GenericComputation build +   ( +      final Origin origin, +      final String alias, +      final List<Computation> call_parameters +   ) +   throws Throwable +   { +      final Computation collection; +      final boolean is_from_left; + +      if (call_parameters.size() != 1) +      { +         // TODO: Error. +         System.err.println("Wrong number of params at " + origin.toString()); + +         return null; +      } + +      collection = call_parameters.get(0); + +      collection.expect_non_string(); + +      is_from_left = alias.contains("eft"); + +      if (alias.startsWith("set:")) +      { +         RecurrentChecks.assert_is_a_set(collection); +      } +      else +      { +         RecurrentChecks.assert_is_a_list(collection); +      } + +      return new PopElementComputation(origin, collection, is_from_left); +   } +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final Computation collection; +   protected final boolean is_from_left; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected PopElementComputation +   ( +      final Origin origin, +      final Computation collection, +      final boolean is_from_left +   ) +   { +      super(origin, collection.get_type()); + +      this.collection = collection; +      this.is_from_left = is_from_left; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Accessors ************************************************************/ +   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/computation/generic/PushElementComputation.java b/src/core/src/tonkadur/fate/v1/lang/computation/generic/PushElementComputation.java new file mode 100644 index 0000000..69c7e7f --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/generic/PushElementComputation.java @@ -0,0 +1,166 @@ +package tonkadur.fate.v1.lang.computation.generic; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import tonkadur.parser.Origin; +import tonkadur.parser.ParsingError; + +import tonkadur.fate.v1.lang.meta.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.RecurrentChecks; + +import tonkadur.fate.v1.lang.computation.GenericComputation; + +public class PushElementComputation extends GenericComputation +{ +   public static Collection<String> get_aliases () +   { +      final Collection<String> aliases; + +      aliases = new ArrayList<String>(); + +      aliases.add("list:push_left"); +      aliases.add("list:pushleft"); +      aliases.add("list:pushLeft"); +      aliases.add("list:push_right"); +      aliases.add("list:pushright"); +      aliases.add("list:pushRight"); +      aliases.add("set:push_left"); +      aliases.add("set:pushleft"); +      aliases.add("set:pushLeft"); +      aliases.add("set:push_right"); +      aliases.add("set:pushright"); +      aliases.add("set:pushRight"); + +      return aliases; +   } + +   public static GenericComputation build +   ( +      final Origin origin, +      final String alias, +      final List<Computation> call_parameters +   ) +   throws Throwable +   { +      final Computation element; +      final Computation collection; +      final boolean is_from_left; + +      if (call_parameters.size() != 2) +      { +         // TODO: Error. +         System.err.println("Wrong number of params at " + origin.toString()); + +         return null; +      } + +      collection = call_parameters.get(0); +      element = call_parameters.get(1); +      is_from_left = alias.contains("eft"); + +      if (alias.startsWith("set:")) +      { +         RecurrentChecks.propagate_expected_types_and_assert_is_a_set_of +         ( +            collection, +            element +         ); +      } +      else +      { +         RecurrentChecks.propagate_expected_types_and_assert_is_a_list_of +         ( +            collection, +            element +         ); +      } +      return +         new PushElementComputation +         ( +            origin, +            element, +            collection, +            is_from_left +         ); +   } +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final Computation element; +   protected final Computation collection; +   protected final boolean is_from_left; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected PushElementComputation +   ( +      final Origin origin, +      final Computation element, +      final Computation collection, +      final boolean is_from_left +   ) +   { +      super(origin, collection.get_type()); + +      this.collection = collection; +      this.element = element; +      this.is_from_left = is_from_left; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Accessors ************************************************************/ +   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/computation/generic/Range.java b/src/core/src/tonkadur/fate/v1/lang/computation/generic/Range.java new file mode 100644 index 0000000..f58cb1c --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/generic/Range.java @@ -0,0 +1,137 @@ +package tonkadur.fate.v1.lang.computation.generic; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import tonkadur.parser.Origin; +import tonkadur.parser.ParsingError; + +import tonkadur.fate.v1.lang.type.CollectionType; +import tonkadur.fate.v1.lang.type.Type; + +import tonkadur.fate.v1.lang.meta.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.RecurrentChecks; + +import tonkadur.fate.v1.lang.computation.GenericComputation; + +public class Range extends GenericComputation +{ +   public static Collection<String> get_aliases () +   { +      final Collection<String> aliases; + +      aliases = new ArrayList<String>(); + +      aliases.add("list:range"); + +      return aliases; +   } + +   public static GenericComputation build +   ( +      final Origin origin, +      final String alias, +      final List<Computation> call_parameters +   ) +   throws Throwable +   { +      final Computation start; +      final Computation end; +      final Computation increment; + +      if (call_parameters.size() != 3) +      { +         // TODO: Error. +         System.err.println("Wrong number of params at " + origin.toString()); + +         return null; +      } + +      start = call_parameters.get(0); +      end = call_parameters.get(1); +      increment = call_parameters.get(2); + +      start.expect_non_string(); +      end.expect_non_string(); +      increment.expect_non_string(); + +      RecurrentChecks.assert_can_be_used_as(start, Type.INT); +      RecurrentChecks.assert_can_be_used_as(end, Type.INT); +      RecurrentChecks.assert_can_be_used_as(increment, Type.INT); + +      return +         new Range +         ( +            origin, +            start, +            end, +            increment, +            CollectionType.build(origin, Type.INT, false, "auto generated") +         ); +   } + +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final Computation start; +   protected final Computation end; +   protected final Computation increment; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected Range +   ( +      final Origin origin, +      final Computation start, +      final Computation end, +      final Computation increment, +      final Type target_type +   ) +   { +      super(origin, target_type); + +      this.start = start; +      this.end = end; +      this.increment = increment; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Accessors ************************************************************/ +   public Computation get_start () +   { +      return start; +   } + +   public Computation get_end () +   { +      return end; +   } + +   public Computation get_increment () +   { +      return increment; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      sb.append("(Range "); +      sb.append(start.toString()); +      sb.append(" "); +      sb.append(end.toString()); +      sb.append(" "); +      sb.append(increment.toString()); +      sb.append(")"); + +      return sb.toString(); +   } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/generic/RemoveAllOfElementComputation.java b/src/core/src/tonkadur/fate/v1/lang/computation/generic/RemoveAllOfElementComputation.java new file mode 100644 index 0000000..858aeb5 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/generic/RemoveAllOfElementComputation.java @@ -0,0 +1,137 @@ +package tonkadur.fate.v1.lang.computation.generic; + +import java.util.ArrayList; +import java.util.List; +import java.util.Collection; + +import tonkadur.parser.Origin; +import tonkadur.parser.ParsingError; + +import tonkadur.fate.v1.lang.meta.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.RecurrentChecks; + +import tonkadur.fate.v1.lang.computation.GenericComputation; + +public class RemoveAllOfElementComputation extends GenericComputation +{ +   public static Collection<String> get_aliases () +   { +      final Collection<String> aliases; + +      aliases = new ArrayList<String>(); + +      aliases.add("list:remove_each"); +      aliases.add("list:removeeach"); +      aliases.add("list:removeEach"); +      aliases.add("set:remove_each"); +      aliases.add("set:removeeach"); +      aliases.add("set:removeEach"); + +      return aliases; +   } + +   public static GenericComputation build +   ( +      final Origin origin, +      final String alias, +      final List<Computation> call_parameters +   ) +   throws Throwable +   { +      final Computation element; +      final Computation collection; + +      if (call_parameters.size() != 2) +      { +         // TODO: Error. +         System.err.println("Wrong number of params at " + origin.toString()); + +         return null; +      } + +      element = call_parameters.get(0); +      collection = call_parameters.get(1); + +      if (alias.startsWith("set:")) +      { +         RecurrentChecks.propagate_expected_types_and_assert_is_a_set_of +         ( +            collection, +            element +         ); +      } +      else +      { +         RecurrentChecks.propagate_expected_types_and_assert_is_a_list_of +         ( +            collection, +            element +         ); +      } + +      return new RemoveAllOfElementComputation(origin, element, collection); +   } + +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final Computation element; +   protected final Computation collection; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected RemoveAllOfElementComputation +   ( +      final Origin origin, +      final Computation element, +      final Computation collection +   ) +   { +      super(origin, collection.get_type()); + +      this.collection = collection; +      this.element = element; +   } +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Accessors ************************************************************/ +   public Computation get_element () +   { +      return element; +   } + +   public Computation get_collection () +   { +      return collection; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      sb.append("(RemoveAllOfElement"); +      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/computation/generic/RemoveElementAtComputation.java b/src/core/src/tonkadur/fate/v1/lang/computation/generic/RemoveElementAtComputation.java new file mode 100644 index 0000000..781f4e1 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/generic/RemoveElementAtComputation.java @@ -0,0 +1,137 @@ +package tonkadur.fate.v1.lang.computation.generic; + +import java.util.ArrayList; +import java.util.List; +import java.util.Collection; + +import tonkadur.parser.Origin; +import tonkadur.parser.ParsingError; + +import tonkadur.fate.v1.lang.type.Type; + +import tonkadur.fate.v1.lang.meta.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.RecurrentChecks; + +import tonkadur.fate.v1.lang.computation.GenericComputation; + +public class RemoveElementAtComputation extends GenericComputation +{ +   public static Collection<String> get_aliases () +   { +      final Collection<String> aliases; + +      aliases = new ArrayList<String>(); + +      aliases.add("list:remove_at"); +      aliases.add("list:removeat"); +      aliases.add("list:removeAt"); +      aliases.add("set:remove_at"); +      aliases.add("set:removeat"); +      aliases.add("set:removeAt"); + +      return aliases; +   } + +   public static GenericComputation build +   ( +      final Origin origin, +      final String alias, +      final List<Computation> call_parameters +   ) +   throws Throwable +   { +      final Computation index; +      final Computation collection; + +      if (call_parameters.size() != 2) +      { +         // TODO: Error. +         System.err.println("Wrong number of params at " + origin.toString()); + +         return null; +      } + +      index = call_parameters.get(0); +      collection = call_parameters.get(1); + +      index.expect_non_string(); +      collection.expect_non_string(); + +      if (alias.startsWith("set:")) +      { +         RecurrentChecks.assert_is_a_set(collection); +      } +      else +      { +         RecurrentChecks.assert_is_a_list(collection); +      } + +      RecurrentChecks.assert_can_be_used_as(index, Type.INT); + +      return new RemoveElementAtComputation(origin, index, collection); +   } + +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final Computation index; +   protected final Computation collection; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected RemoveElementAtComputation +   ( +      final Origin origin, +      final Computation index, +      final Computation collection +   ) +   { +      super(origin, collection.get_type()); + +      this.collection = collection; +      this.index = index; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Accessors ************************************************************/ +   public Computation get_index () +   { +      return index; +   } + +   public Computation get_collection () +   { +      return collection; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      sb.append("(RemoveElementAt"); +      sb.append(System.lineSeparator()); +      sb.append(System.lineSeparator()); + +      sb.append("index:"); +      sb.append(System.lineSeparator()); +      sb.append(index.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/computation/generic/RemoveElementComputation.java b/src/core/src/tonkadur/fate/v1/lang/computation/generic/RemoveElementComputation.java new file mode 100644 index 0000000..a75f0cd --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/generic/RemoveElementComputation.java @@ -0,0 +1,145 @@ +package tonkadur.fate.v1.lang.computation.generic; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import tonkadur.parser.Origin; +import tonkadur.parser.ParsingError; + +import tonkadur.fate.v1.lang.meta.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.RecurrentChecks; + +import tonkadur.fate.v1.lang.computation.GenericComputation; + +public class RemoveElementComputation extends GenericComputation +{ +   public static Collection<String> get_aliases () +   { +      final Collection<String> aliases; + +      aliases = new ArrayList<String>(); + +      aliases.add("list:remove"); +      aliases.add("list:remove_one"); +      aliases.add("list:remove_once"); +      aliases.add("list:removeone"); +      aliases.add("list:removeonce"); +      aliases.add("list:removeOne"); +      aliases.add("list:removeOnce"); +      aliases.add("set:remove"); +      aliases.add("set:remove_one"); +      aliases.add("set:remove_once"); +      aliases.add("set:removeone"); +      aliases.add("set:removeonce"); +      aliases.add("set:removeOne"); +      aliases.add("set:removeOnce"); + +      return aliases; +   } + +   public static GenericComputation build +   ( +      final Origin origin, +      final String alias, +      final List<Computation> call_parameters +   ) +   throws Throwable +   { +      final Computation element; +      final Computation collection; + +      if (call_parameters.size() != 1) +      { +         // TODO: Error. +         System.err.println("Wrong number of params at " + origin.toString()); + +         return null; +      } + +      element = call_parameters.get(0); +      collection = call_parameters.get(1); + +      if (alias.startsWith("set:")) +      { +         RecurrentChecks.propagate_expected_types_and_assert_is_a_set_of +         ( +            collection, +            element +         ); +      } +      else +      { +         RecurrentChecks.propagate_expected_types_and_assert_is_a_list_of +         ( +            collection, +            element +         ); +      } + +      return new RemoveElementComputation(origin, element, collection); +   } + +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final Computation element; +   protected final Computation collection; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected RemoveElementComputation +   ( +      final Origin origin, +      final Computation element, +      final Computation collection +   ) +   { +      super(origin, collection.get_type()); + +      this.collection = collection; +      this.element = element; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Accessors ************************************************************/ +   public Computation get_element () +   { +      return element; +   } + +   public Computation get_collection () +   { +      return collection; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      sb.append("(RemoveElement"); +      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/computation/generic/RemoveElementsOfComputation.java b/src/core/src/tonkadur/fate/v1/lang/computation/generic/RemoveElementsOfComputation.java new file mode 100644 index 0000000..66eb7a3 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/generic/RemoveElementsOfComputation.java @@ -0,0 +1,144 @@ +package tonkadur.fate.v1.lang.computation.generic; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import tonkadur.parser.Origin; +import tonkadur.parser.ParsingError; + +import tonkadur.fate.v1.lang.type.CollectionType; + +import tonkadur.fate.v1.lang.meta.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.RecurrentChecks; + +import tonkadur.fate.v1.lang.computation.GenericComputation; + +public class RemoveElementsOfComputation extends GenericComputation +{ +   public static Collection<String> get_aliases () +   { +      final Collection<String> aliases; + +      aliases = new ArrayList<String>(); + +      aliases.add("list:remove_all"); +      aliases.add("list:removeall"); +      aliases.add("list:removeAll"); +      aliases.add("set:remove_all"); +      aliases.add("set:removeall"); +      aliases.add("set:removeAll"); + +      return aliases; +   } + +   public static GenericComputation build +   ( +      final Origin origin, +      final String alias, +      final List<Computation> call_parameters +   ) +   throws Throwable +   { +      final Computation other_collection; +      final Computation collection; + +      if (call_parameters.size() != 2) +      { +         // TODO: Error. +         System.err.println("Wrong number of params at " + origin.toString()); + +         return null; +      } + +      other_collection = call_parameters.get(0); +      collection = call_parameters.get(1); + +      other_collection.expect_non_string(); +      collection.expect_non_string(); + +      if (alias.startsWith("set:")) +      { +         RecurrentChecks.assert_is_a_set(collection); +      } +      else +      { +         RecurrentChecks.assert_is_a_list(collection); +      } + +      RecurrentChecks.assert_is_a_collection(other_collection); +      RecurrentChecks.assert_can_be_used_as +      ( +         other_collection.get_origin(), +         ((CollectionType) other_collection.get_type()).get_content_type(), +         ((CollectionType) collection.get_type()).get_content_type() +      ); + +      return +         new RemoveElementsOfComputation(origin, other_collection, collection); +   } + +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final Computation other_collection; +   protected final Computation collection; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected RemoveElementsOfComputation +   ( +      final Origin origin, +      final Computation other_collection, +      final Computation collection +   ) +   { +      super(origin, collection.get_type()); + +      this.collection = collection; +      this.other_collection = other_collection; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Accessors ************************************************************/ +   public Computation get_source_collection () +   { +      return other_collection; +   } + +   public Computation get_target_collection () +   { +      return collection; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      sb.append("(RemoveElementsOf"); +      sb.append(System.lineSeparator()); +      sb.append(System.lineSeparator()); + +      sb.append("other_collection:"); +      sb.append(System.lineSeparator()); +      sb.append(other_collection.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/computation/generic/ReverseListComputation.java b/src/core/src/tonkadur/fate/v1/lang/computation/generic/ReverseListComputation.java new file mode 100644 index 0000000..5df91d0 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/generic/ReverseListComputation.java @@ -0,0 +1,122 @@ +package tonkadur.fate.v1.lang.computation.generic; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import tonkadur.parser.Origin; +import tonkadur.parser.ParsingError; + +import tonkadur.fate.v1.lang.type.CollectionType; +import tonkadur.fate.v1.lang.type.Type; + +import tonkadur.fate.v1.lang.meta.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.RecurrentChecks; + +import tonkadur.fate.v1.lang.computation.GenericComputation; + +public class ReverseListComputation extends GenericComputation +{ +   public static Collection<String> get_aliases () +   { +      final Collection<String> aliases; + +      aliases = new ArrayList<String>(); + +      aliases.add("list:reverse"); +      aliases.add("set:reverse"); + +      return aliases; +   } + +   public static GenericComputation build +   ( +      final Origin origin, +      final String alias, +      final List<Computation> call_parameters +   ) +   throws Throwable +   { +      final Computation collection; + +      if (call_parameters.size() != 1) +      { +         // TODO: Error. +         System.err.println("Wrong number of params at " + origin.toString()); + +         return null; +      } + +      collection = call_parameters.get(0); + +      collection.expect_non_string(); + +      if (alias.startsWith("set:")) +      { +         RecurrentChecks.assert_is_a_set(collection); +      } +      else +      { +         RecurrentChecks.assert_is_a_list(collection); +      } + +      return +         new ReverseListComputation +         ( +            origin, +            collection, +            CollectionType.build +            ( +               origin, +               (((CollectionType) collection.get_type()).get_content_type()), +               false, +               "auto generated" +            ) +         ); +   } + +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final Computation collection; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected ReverseListComputation +   ( +      final Origin origin, +      final Computation collection, +      final Type result_type +   ) +   { +      super(origin, result_type); + +      this.collection = collection; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Accessors ************************************************************/ +   public Computation get_collection () +   { +      return collection; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      sb.append("(ReverseList "); +      sb.append(collection.toString()); + +      sb.append(")"); + +      return sb.toString(); +   } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/generic/ShuffleComputation.java b/src/core/src/tonkadur/fate/v1/lang/computation/generic/ShuffleComputation.java new file mode 100644 index 0000000..f5ed5a6 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/generic/ShuffleComputation.java @@ -0,0 +1,122 @@ +package tonkadur.fate.v1.lang.computation.generic; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import tonkadur.parser.Origin; +import tonkadur.parser.ParsingError; + +import tonkadur.fate.v1.lang.type.CollectionType; +import tonkadur.fate.v1.lang.type.Type; + +import tonkadur.fate.v1.lang.meta.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.RecurrentChecks; + +import tonkadur.fate.v1.lang.computation.GenericComputation; + +public class ShuffleComputation extends GenericComputation +{ +   public static Collection<String> get_aliases () +   { +      final Collection<String> aliases; + +      aliases = new ArrayList<String>(); + +      aliases.add("list:shuffle"); +      aliases.add("set:shuffle"); + +      return aliases; +   } + +   public static GenericComputation build +   ( +      final Origin origin, +      final String alias, +      final List<Computation> call_parameters +   ) +   throws Throwable +   { +      final Computation collection; + +      if (call_parameters.size() != 1) +      { +         // TODO: Error. +         System.err.println("Wrong number of params at " + origin.toString()); + +         return null; +      } + +      collection = call_parameters.get(0); + +      collection.expect_non_string(); + +      if (alias.startsWith("set:")) +      { +         RecurrentChecks.assert_is_a_set(collection); +      } +      else +      { +         RecurrentChecks.assert_is_a_list(collection); +      } + +      return +         new ShuffleComputation +         ( +            origin, +            collection, +            CollectionType.build +            ( +               origin, +               (((CollectionType) collection.get_type()).get_content_type()), +               false, +               "auto generated" +            ) +         ); +   } +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final Computation collection; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected ShuffleComputation +   ( +      final Origin origin, +      final Computation collection, +      final Type result_type +   ) +   { +      super(origin, result_type); + +      this.collection = collection; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Accessors ************************************************************/ + +   public Computation get_collection () +   { +      return collection; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      sb.append("(Shuffle "); +      sb.append(collection.toString()); + +      sb.append(")"); + +      return sb.toString(); +   } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/generic/SizeOperator.java b/src/core/src/tonkadur/fate/v1/lang/computation/generic/SizeOperator.java new file mode 100644 index 0000000..92b71df --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/generic/SizeOperator.java @@ -0,0 +1,115 @@ +package tonkadur.fate.v1.lang.computation.generic; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import tonkadur.parser.Origin; +import tonkadur.parser.ParsingError; + +import tonkadur.fate.v1.lang.type.Type; + +import tonkadur.fate.v1.lang.meta.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.RecurrentChecks; + +import tonkadur.fate.v1.lang.computation.GenericComputation; + +public class SizeOperator extends GenericComputation +{ +   public static Collection<String> get_aliases () +   { +      final Collection<String> aliases; + +      aliases = new ArrayList<String>(); + +      aliases.add("list:length"); +      aliases.add("list:size"); +      aliases.add("set:length"); +      aliases.add("set:size"); + +      return aliases; +   } + +   public static GenericComputation build +   ( +      final Origin origin, +      final String alias, +      final List<Computation> call_parameters +   ) +   throws Throwable +   { +      final Computation collection; + +      if (call_parameters.size() != 1) +      { +         // TODO: Error. +         System.err.println("Wrong number of params at " + origin.toString()); + +         return null; +      } + +      collection = call_parameters.get(0); + +      collection.expect_non_string(); + +      if (alias.startsWith("set:")) +      { +         RecurrentChecks.assert_is_a_set(collection); +      } +      else +      { +         RecurrentChecks.assert_is_a_list(collection); +      } + +      return new SizeOperator(origin, collection); +   } + +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final Computation collection; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected SizeOperator +   ( +      final Origin origin, +      final Computation collection +   ) +   { +      super(origin, Type.INT); + +      this.collection = collection; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Accessors ************************************************************/ +   public Computation get_collection () +   { +      return collection; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      sb.append("(SizeOperator"); +      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/computation/generic/SortComputation.java b/src/core/src/tonkadur/fate/v1/lang/computation/generic/SortComputation.java new file mode 100644 index 0000000..8e004d2 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/generic/SortComputation.java @@ -0,0 +1,136 @@ +package tonkadur.fate.v1.lang.computation.generic; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import tonkadur.parser.Origin; +import tonkadur.parser.ParsingError; + +import tonkadur.fate.v1.lang.type.Type; +import tonkadur.fate.v1.lang.type.CollectionType; + +import tonkadur.fate.v1.lang.meta.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.RecurrentChecks; + +import tonkadur.fate.v1.lang.computation.GenericComputation; + +public class SortComputation extends GenericComputation +{ +   public static Collection<String> get_aliases () +   { +      final Collection<String> aliases; + +      aliases = new ArrayList<String>(); + +      aliases.add("list:sort"); + +      return aliases; +   } + +   public static GenericComputation build +   ( +      final Origin origin, +      final String alias, +      final List<Computation> call_parameters +   ) +   throws Throwable +   { +      // TODO: implement +      final Computation lambda_function = null; +      final Computation collection = null; +      final List<Computation> extra_params = null; +      final List<Type> types_in; + +      types_in = new ArrayList<Type>(); + +      RecurrentChecks.assert_is_a_list(collection); + +      types_in.add(((CollectionType) collection.get_type()).get_content_type()); +      types_in.add(types_in.get(0)); + +      for (final Computation c: extra_params) +      { +         types_in.add(c.get_type()); +      } + +      RecurrentChecks.assert_lambda_matches_types +      ( +         lambda_function, +         Type.INT, +         types_in +      ); + +      return +         new SortComputation(origin, lambda_function, collection, extra_params); +   } + +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final List<Computation> extra_params; +   protected final Computation lambda_function; +   protected final Computation collection; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected SortComputation +   ( +      final Origin origin, +      final Computation lambda_function, +      final Computation collection, +      final List<Computation> extra_params +   ) +   { +      super(origin, collection.get_type()); + +      this.lambda_function = lambda_function; +      this.collection = collection; +      this.extra_params = extra_params; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Accessors ************************************************************/ +   public Computation get_lambda_function () +   { +      return lambda_function; +   } + +   public Computation get_collection () +   { +      return collection; +   } + +   public List<Computation> get_extra_parameters () +   { +      return extra_params; +   } + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      sb.append("(Sort "); +      sb.append(lambda_function.toString()); +      sb.append(" "); +      sb.append(collection.toString()); + +      for (final Computation c: extra_params) +      { +         sb.append(" "); +         sb.append(c.toString()); +      } + +      sb.append(")"); + +      return sb.toString(); +   } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/generic/SubListComputation.java b/src/core/src/tonkadur/fate/v1/lang/computation/generic/SubListComputation.java new file mode 100644 index 0000000..f9c20bf --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/generic/SubListComputation.java @@ -0,0 +1,133 @@ +package tonkadur.fate.v1.lang.computation.generic; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import tonkadur.parser.Origin; +import tonkadur.parser.ParsingError; + +import tonkadur.fate.v1.lang.type.Type; + +import tonkadur.fate.v1.lang.meta.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.RecurrentChecks; + +import tonkadur.fate.v1.lang.computation.GenericComputation; + +public class SubListComputation extends GenericComputation +{ +   public static Collection<String> get_aliases () +   { +      final Collection<String> aliases; + +      aliases = new ArrayList<String>(); + +      aliases.add("list:sublist"); +      aliases.add("list:sub_list"); +      aliases.add("list:subList"); +      aliases.add("set:subset"); +      aliases.add("set:sub_set"); +      aliases.add("set:subList"); + +      return aliases; +   } + +   public static GenericComputation build +   ( +      final Origin origin, +      final String alias, +      final List<Computation> call_parameters +   ) +   throws Throwable +   { +      final Computation start; +      final Computation end; +      final Computation collection; + +      if (call_parameters.size() != 3) +      { +         // TODO: Error. +         System.err.println("Wrong number of params at " + origin.toString()); + +         return null; +      } + +      start = call_parameters.get(0); +      end = call_parameters.get(1); +      collection = call_parameters.get(2); + +      start.expect_non_string(); +      end.expect_non_string(); +      collection.expect_non_string(); + +      RecurrentChecks.assert_can_be_used_as(start, Type.INT); +      RecurrentChecks.assert_can_be_used_as(end, Type.INT); +      RecurrentChecks.assert_is_a_collection(collection); + +      return new SubListComputation(origin, start, end, collection); +   } + +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final Computation start; +   protected final Computation end; +   protected final Computation collection; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected SubListComputation +   ( +      final Origin origin, +      final Computation start, +      final Computation end, +      final Computation collection +   ) +   { +      super(origin, collection.get_type()); + +      this.start = start; +      this.end = end; +      this.collection = collection; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Accessors ************************************************************/ +   public Computation get_collection () +   { +      return collection; +   } + +   public Computation get_start_index () +   { +      return start; +   } + +   public Computation get_end_index () +   { +      return end; +   } + +   /**** 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.toString()); +      sb.append(")"); + +      return sb.toString(); +   } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/meta/RecurrentChecks.java b/src/core/src/tonkadur/fate/v1/lang/meta/RecurrentChecks.java index 40a85bb..ae905ef 100644 --- a/src/core/src/tonkadur/fate/v1/lang/meta/RecurrentChecks.java +++ b/src/core/src/tonkadur/fate/v1/lang/meta/RecurrentChecks.java @@ -160,20 +160,24 @@ public class RecurrentChecks        }     } -   public static void assert_is_a_list_of +   public static void propagate_expected_types_and_assert_is_a_list_of     (        final Computation c,        final Computation e     )     throws ParsingError     { -      assert_is_a_list_of -      ( -         c.get_origin(), -         c.get_type(), -         e.get_origin(), -         e.get_type() -      ); +      final Type content_type; + +      c.expect_non_string(); + +      assert_is_a_list(c); + +      content_type = ((CollectionType) c.get_type()).get_content_type(); + +      handle_expected_type_propagation(e, content_type); + +      assert_can_be_used_as(e, content_type);     }     public static void assert_is_a_list_of @@ -194,20 +198,24 @@ public class RecurrentChecks        );     } -   public static void assert_is_a_set_of +   public static void propagate_expected_types_and_assert_is_a_set_of     (        final Computation c,        final Computation e     )     throws ParsingError     { -      assert_is_a_set_of -      ( -         c.get_origin(), -         c.get_type(), -         e.get_origin(), -         e.get_type() -      ); +      final Type content_type; + +      c.expect_non_string(); + +      assert_is_a_set(c); + +      content_type = ((CollectionType) c.get_type()).get_content_type(); + +      handle_expected_type_propagation(e, content_type); + +      assert_can_be_used_as(e, content_type);     }     public static void assert_is_a_set_of @@ -228,23 +236,59 @@ public class RecurrentChecks        );     } -   public static void assert_is_a_collection_of +   public static void assert_is_a_set_of (final Computation c, final Type e) +   throws ParsingError +   { +      assert_can_be_used_as +      ( +         c, +         CollectionType.build +         ( +            c.get_origin(), +            e, +            true, +            "RecurrentChecks is_a_set_of test type" +         ) +      ); +   } + +   public static void assert_is_a_list_of (final Computation c, final Type e) +   throws ParsingError +   { +      assert_can_be_used_as +      ( +         c, +         CollectionType.build +         ( +            c.get_origin(), +            e, +            false, +            "RecurrentChecks is_a_set_of test type" +         ) +      ); +   } + +   public static void propagate_expected_types_and_assert_is_a_collection_of     (        final Computation c,        final Computation e     )     throws ParsingError     { -      assert_is_a_collection_of -      ( -         c.get_origin(), -         c.get_type(), -         e.get_origin(), -         e.get_type() -      ); +      final Type content_type; + +      c.expect_non_string(); + +      assert_is_a_collection(c); + +      content_type = ((CollectionType) c.get_type()).get_content_type(); + +      handle_expected_type_propagation(e, content_type); + +      assert_can_be_used_as(e, content_type);     } -   public static void assert_is_a_collection_of +   public static void propagate_expected_types_and_assert_is_a_collection_of     (        final Origin oc,        final Type c, @@ -262,7 +306,8 @@ public class RecurrentChecks        );     } -   public static void assert_computations_matches_signature +   public static void +   propagate_expected_types_and_assert_computations_matches_signature     (        final Origin o,        final List<Computation> c, @@ -292,6 +337,7 @@ public class RecurrentChecks              public Boolean risky_lambda (final Type t, final Computation p)              throws ParsingError              { +               handle_expected_type_propagation(p, t);                 assert_can_be_used_as(p, t);                 return Boolean.TRUE; @@ -463,7 +509,8 @@ public class RecurrentChecks        }     } -   public static void assert_lambda_matches_computations +   public static void +   propagate_expected_types_and_assert_lambda_matches_computations     (        final Computation l,        final Type r, @@ -471,9 +518,11 @@ public class RecurrentChecks     )     throws ParsingError     { +      l.expect_non_string(); +        assert_is_a_lambda_function(l);        assert_return_type_is(l, r); -      assert_computations_matches_signature +      propagate_expected_types_and_assert_computations_matches_signature        (           l.get_origin(),           c, @@ -481,15 +530,18 @@ public class RecurrentChecks        );     } -   public static void assert_lambda_matches_computations +   public static void +   propagate_expected_types_and_assert_lambda_matches_computations     (        final Computation l,        final List<Computation> c     )     throws ParsingError     { +      l.expect_non_string(); +        assert_is_a_lambda_function(l); -      assert_computations_matches_signature +      propagate_expected_types_and_assert_computations_matches_signature        (           l.get_origin(),           c, 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 84b0ebc..f5bb4a0 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 @@ -1281,113 +1281,6 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor        result_as_computation = new Size(cc.get_address());     }  */ -/* -   @Override -   public void visit_text_join -   ( -      final tonkadur.fate.v1.lang.computation.TextJoin n -   ) -   throws Throwable -   { -      final Register i, collection_size, accumulator; -      final List<Instruction> while_body; -      final List<Computation> next_chain; -      final ComputationCompiler link_cc, collection_cc; - -      link_cc = new ComputationCompiler(compiler); -      collection_cc = new ComputationCompiler(compiler); - -      n.get_link().get_visited_by(link_cc); -      n.get_text_collection().get_visited_by(collection_cc); - -      assimilate(link_cc); -      assimilate(collection_cc); - -      next_chain = new ArrayList<Computation>(); -      while_body = new ArrayList<Instruction>(); - -      i = reserve(Type.INT); -      collection_size = reserve(Type.INT); -      accumulator = reserve(Type.TEXT); - -      init_instructions.add -      ( -         new SetValue(i.get_address(), Constant.ONE) -      ); -      init_instructions.add -      ( -         new SetValue -         ( -            collection_size.get_address(), -            new Size(collection_cc.get_address()) -         ) -      ); - -      init_instructions.add -      ( -         new SetValue -         ( -            accumulator.get_address(), -            new IfElseComputation -            ( -               Operation.equals(collection_size.get_value(), Constant.ZERO), -               new Text(new ArrayList<Computation>()), -               new ValueOf -               ( -                  new RelativeAddress -                  ( -                     collection_cc.get_address(), -                     new Cast(Constant.ZERO, Type.STRING), -                     Type.TEXT -                  ) -               ) -            ) -         ) -      ); - -      next_chain.add(accumulator.get_value()); -      next_chain.add(link_cc.get_computation()); -      next_chain.add -      ( -         new ValueOf -         ( -            new RelativeAddress -            ( -               collection_cc.get_address(), -               new Cast(i.get_value(), Type.STRING), -               Type.TEXT -            ) -         ) -      ); - -      while_body.add -      ( -         new SetValue(accumulator.get_address(), new Text(next_chain)) -      ); - -      while_body.add -      ( -         new SetValue -         ( -            i.get_address(), -            Operation.plus(i.get_value(), Constant.ONE) -         ) -      ); - -      init_instructions.add -      ( -         While.generate -         ( -            compiler.registers(), -            compiler.assembler(), -            Operation.less_than(i.get_value(), collection_size.get_value()), -            compiler.assembler().merge(while_body) -         ) -      ); - -      result_as_computation = accumulator.get_value(); -   } -*/     @Override     public void visit_extra_computation     ( diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/TextJoinCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/TextJoinCompiler.java new file mode 100644 index 0000000..38fb3a3 --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/TextJoinCompiler.java @@ -0,0 +1,153 @@ +package tonkadur.wyrd.v1.compiler.fate.v1.computation.generic; + +import java.util.List; +import java.util.ArrayList; + +import tonkadur.fate.v1.lang.computation.generic.TextJoin; + +import tonkadur.wyrd.v1.lang.Register; + +import tonkadur.wyrd.v1.lang.meta.Computation; +import tonkadur.wyrd.v1.lang.meta.Instruction; + +import tonkadur.wyrd.v1.lang.computation.Address; +import tonkadur.wyrd.v1.lang.computation.Cast; +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.RelativeAddress; +import tonkadur.wyrd.v1.lang.computation.Size; +import tonkadur.wyrd.v1.lang.computation.Text; +import tonkadur.wyrd.v1.lang.computation.ValueOf; + +import tonkadur.wyrd.v1.lang.instruction.SetValue; + +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 TextJoinCompiler extends GenericComputationCompiler +{ +   public static Class get_target_class () +   { +      return TextJoin.class; +   } + +   public TextJoinCompiler (final Compiler compiler) +   { +      super(compiler); +   } + +   public void compile +   ( +      final tonkadur.fate.v1.lang.computation.GenericComputation computation +   ) +   throws Throwable +   { +      final TextJoin source; +      final Register i, collection_size, accumulator; +      final List<Instruction> while_body; +      final List<Computation> next_chain; +      final ComputationCompiler link_cc, collection_cc; + +      source = (TextJoin) computation; + +      link_cc = new ComputationCompiler(compiler); +      collection_cc = new ComputationCompiler(compiler); + +      source.get_link().get_visited_by(link_cc); +      source.get_text_collection().get_visited_by(collection_cc); + +      assimilate(link_cc); +      assimilate(collection_cc); + +      next_chain = new ArrayList<Computation>(); +      while_body = new ArrayList<Instruction>(); + +      i = reserve(Type.INT); +      collection_size = reserve(Type.INT); +      accumulator = reserve(Type.TEXT); + +      init_instructions.add +      ( +         new SetValue(i.get_address(), Constant.ONE) +      ); +      init_instructions.add +      ( +         new SetValue +         ( +            collection_size.get_address(), +            new Size(collection_cc.get_address()) +         ) +      ); + +      init_instructions.add +      ( +         new SetValue +         ( +            accumulator.get_address(), +            new IfElseComputation +            ( +               Operation.equals(collection_size.get_value(), Constant.ZERO), +               new Text(new ArrayList<Computation>()), +               new ValueOf +               ( +                  new RelativeAddress +                  ( +                     collection_cc.get_address(), +                     new Cast(Constant.ZERO, Type.STRING), +                     Type.TEXT +                  ) +               ) +            ) +         ) +      ); + +      next_chain.add(accumulator.get_value()); +      next_chain.add(link_cc.get_computation()); +      next_chain.add +      ( +         new ValueOf +         ( +            new RelativeAddress +            ( +               collection_cc.get_address(), +               new Cast(i.get_value(), Type.STRING), +               Type.TEXT +            ) +         ) +      ); + +      while_body.add +      ( +         new SetValue(accumulator.get_address(), new Text(next_chain)) +      ); + +      while_body.add +      ( +         new SetValue +         ( +            i.get_address(), +            Operation.plus(i.get_value(), Constant.ONE) +         ) +      ); + +      init_instructions.add +      ( +         tonkadur.wyrd.v1.compiler.util.While.generate +         ( +            compiler.registers(), +            compiler.assembler(), +            Operation.less_than(i.get_value(), collection_size.get_value()), +            compiler.assembler().merge(while_body) +         ) +      ); + +      result_as_computation = accumulator.get_value(); +   } +} | 


