| summaryrefslogtreecommitdiff | 
diff options
| author | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2020-08-07 11:18:18 +0200 | 
|---|---|---|
| committer | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2020-08-07 11:18:18 +0200 | 
| commit | b95d1790243c09901fa5175a9ec29fcdce0998a1 (patch) | |
| tree | 5e5c02810b6fa2fbe04a78ed7751d1146c59cdbf /src | |
| parent | 5ba737fbc8edac0eb5ed750d92a5b3158fb2a32e (diff) | |
...
Diffstat (limited to 'src')
16 files changed, 586 insertions, 53 deletions
| diff --git a/src/core/src/tonkadur/Main.java b/src/core/src/tonkadur/Main.java index cf8a8ae..ea02801 100644 --- a/src/core/src/tonkadur/Main.java +++ b/src/core/src/tonkadur/Main.java @@ -46,7 +46,6 @@ public class Main           );           System.out.println("Parsing completed."); -         System.out.println(fate_world.toString());        }        catch (final Exception e)        { @@ -88,15 +87,6 @@ public class Main           tp.post_wyrd_compile(wyrd_world);        } -      for -      ( -         final tonkadur.wyrd.v1.lang.meta.Instruction line: -            wyrd_world.get_code() -      ) -      { -         System.out.println(line.toString()); -      } -        for (final TonkadurPlugin tp: plugins)        {           tp.finalize(); diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/Assert.java b/src/core/src/tonkadur/fate/v1/lang/instruction/Assert.java index 2770fa1..7869ab4 100644 --- a/src/core/src/tonkadur/fate/v1/lang/instruction/Assert.java +++ b/src/core/src/tonkadur/fate/v1/lang/instruction/Assert.java @@ -12,6 +12,7 @@ import tonkadur.fate.v1.lang.type.Type;  import tonkadur.fate.v1.lang.meta.InstructionVisitor;  import tonkadur.fate.v1.lang.meta.Instruction; +import tonkadur.fate.v1.lang.meta.RichTextNode;  import tonkadur.fate.v1.lang.meta.Computation;  public class Assert extends Instruction @@ -20,6 +21,7 @@ public class Assert extends Instruction     /**** MEMBERS **************************************************************/     /***************************************************************************/     protected final Computation condition; +   protected final RichTextNode message;     /***************************************************************************/     /**** PROTECTED ************************************************************/ @@ -28,12 +30,14 @@ public class Assert extends Instruction     protected Assert     (        final Origin origin, -      final Computation condition +      final Computation condition, +      final RichTextNode message     )     {        super(origin);        this.condition = condition; +      this.message = message;     }     /***************************************************************************/ @@ -43,7 +47,8 @@ public class Assert extends Instruction     public static Assert build     (        final Origin origin, -      final Computation condition +      final Computation condition, +      final RichTextNode message     )     throws InvalidTypeException     { @@ -60,7 +65,7 @@ public class Assert extends Instruction           );        } -      return new Assert(origin, condition); +      return new Assert(origin, condition, message);     }     /**** Accessors ************************************************************/ @@ -76,6 +81,11 @@ public class Assert extends Instruction        return condition;     } +   public RichTextNode get_message () +   { +      return message; +   } +     /**** Misc. ****************************************************************/     @Override     public String toString () diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/ReverseList.java b/src/core/src/tonkadur/fate/v1/lang/instruction/ReverseList.java index e69de29..59a68e6 100644 --- a/src/core/src/tonkadur/fate/v1/lang/instruction/ReverseList.java +++ b/src/core/src/tonkadur/fate/v1/lang/instruction/ReverseList.java @@ -0,0 +1,101 @@ +package tonkadur.fate.v1.lang.instruction; + +import java.util.Collections; + +import tonkadur.error.ErrorManager; + +import tonkadur.parser.Origin; + +import tonkadur.fate.v1.error.InvalidTypeException; + +import tonkadur.fate.v1.lang.type.CollectionType; +import tonkadur.fate.v1.lang.type.Type; + +import tonkadur.fate.v1.lang.meta.InstructionVisitor; +import tonkadur.fate.v1.lang.meta.Instruction; +import tonkadur.fate.v1.lang.meta.Computation; + +public class ReverseList extends Instruction +{ +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final Computation collection; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected ReverseList +   ( +      final Origin origin, +      final Computation collection +   ) +   { +      super(origin); + +      this.collection = collection; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   public static ReverseList build +   ( +      final Origin origin, +      final Computation collection +   ) +   throws InvalidTypeException +   { +      final Type t; + +      t = collection.get_type(); + +      if +      ( +         !(t instanceof CollectionType) +         || ((CollectionType) t).is_set() +      ) +      { +         ErrorManager.handle +         ( +            new InvalidTypeException +            ( +               collection.get_origin(), +               collection.get_type(), +               Collections.singleton(Type.LIST) +            ) +         ); +      } + +      return new ReverseList(origin, collection); +   } + +   /**** Accessors ************************************************************/ +   @Override +   public void get_visited_by (final InstructionVisitor iv) +   throws Throwable +   { +      iv.visit_reverse_list(this); +   } + +   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/meta/InstructionVisitor.java b/src/core/src/tonkadur/fate/v1/lang/meta/InstructionVisitor.java index 41167bb..3f4f973 100644 --- a/src/core/src/tonkadur/fate/v1/lang/meta/InstructionVisitor.java +++ b/src/core/src/tonkadur/fate/v1/lang/meta/InstructionVisitor.java @@ -38,6 +38,9 @@ public interface InstructionVisitor     public void visit_clear (final Clear c)     throws Throwable; +   public void visit_reverse_list (final ReverseList n) +   throws Throwable; +     public void visit_cond_instruction (final CondInstruction ci)     throws Throwable; diff --git a/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 b/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 index ca10603..2759c92 100644 --- a/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 +++ b/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 @@ -14,6 +14,7 @@ L_PAREN: '(';  R_PAREN: ')'; +ABS_KW: L_PAREN 'abs'('olute'?) SEP+;  ACCESS_KW: L_PAREN 'access' SEP+;  ADD_KW: L_PAREN 'add' SEP+;  AND_KW: L_PAREN ('and'|'/\\') SEP+; @@ -63,6 +64,9 @@ IS_MEMBER_KW: L_PAREN ('is'US'member'|'contains'|'has') SEP+;  LOWER_EQUAL_THAN_KW: L_PAREN ('lower'US'equal'US'than'|'=<'|'<='|'le') SEP+;  LOWER_THAN_KW: L_PAREN ('lower'US'than'|'<'|'lt') SEP+;  MINUS_KW: L_PAREN ('minus'|'-') SEP+; +MIN_KW: L_PAREN ('min'('imum'?)) SEP+; +MAX_KW: L_PAREN ('max'('imum'?)) SEP+; +CLAMP_KW: L_PAREN ('clamp') SEP+;  MODULO_KW: L_PAREN ('modulo'|'%'|'mod') SEP+;  NEWLINE_KW: L_PAREN 'newline)';  NEW_KW: L_PAREN ('new'|'reserve'|'create') SEP+; diff --git a/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 b/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 index c8e42a6..ecf0c0d 100644 --- a/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 +++ b/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 @@ -847,6 +847,7 @@ returns [Instruction result]              elem_type = Type.ANY;           } +         /* TODO: error if there is already a parameter with that name */           PARAMETERS.add           (              CONTEXT.get_origin_at @@ -967,7 +968,7 @@ returns [Instruction result]        $result = new SequenceCall(origin, sequence_name);     } -   | ASSERT_KW value WS* R_PAREN +   | ASSERT_KW value WS+ paragraph WS* R_PAREN     {        $result =           Assert.build @@ -977,7 +978,8 @@ returns [Instruction result]                 ($ASSERT_KW.getLine()),                 ($ASSERT_KW.getCharPositionInLine())              ), -            ($value.result) +            ($value.result), +            ($paragraph.result)           );     } @@ -1910,6 +1912,66 @@ returns [Computation result]:           );     } +   | MIN_KW value_list WS* R_PAREN +   { +      $result = +         Operation.build +         ( +            CONTEXT.get_origin_at +            ( +               ($MIN_KW.getLine()), +               ($MIN_KW.getCharPositionInLine()) +            ), +            Operator.MIN, +            ($value_list.result) +         ); +   } + +   | MAX_KW value_list WS* R_PAREN +   { +      $result = +         Operation.build +         ( +            CONTEXT.get_origin_at +            ( +               ($MAX_KW.getLine()), +               ($MAX_KW.getCharPositionInLine()) +            ), +            Operator.MAX, +            ($value_list.result) +         ); +   } + +   | CLAMP_KW value_list WS* R_PAREN +   { +      $result = +         Operation.build +         ( +            CONTEXT.get_origin_at +            ( +               ($CLAMP_KW.getLine()), +               ($CLAMP_KW.getCharPositionInLine()) +            ), +            Operator.CLAMP, +            ($value_list.result) +         ); +   } + +   | ABS_KW value_list WS* R_PAREN +   { +      $result = +         Operation.build +         ( +            CONTEXT.get_origin_at +            ( +               ($ABS_KW.getLine()), +               ($ABS_KW.getCharPositionInLine()) +            ), +            Operator.ABS, +            ($value_list.result) +         ); +   } +     | MODULO_KW value_list WS* R_PAREN     {        $result = diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/Compiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/Compiler.java index 4a315e1..037a705 100644 --- a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/Compiler.java +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/Compiler.java @@ -3,6 +3,7 @@ package tonkadur.wyrd.v1.compiler.fate.v1;  import tonkadur.wyrd.v1.compiler.util.AnonymousVariableManager;  import tonkadur.wyrd.v1.compiler.util.InstructionManager; +import tonkadur.wyrd.v1.lang.Variable;  import tonkadur.wyrd.v1.lang.World;  public class Compiler @@ -32,6 +33,8 @@ public class Compiler        compiler.compile_sequences(fate_world); +      compiler.add_anonymous_variables(); +        return compiler.wyrd_world;     } @@ -116,6 +119,15 @@ public class Compiler        );     } +   protected void add_anonymous_variables () +   throws Throwable +   { +      for (final Variable variable: anonymous_variables.get_all_variables()) +      { +         wyrd_world.add_variable(variable); +      } +   } +     public World world ()     {        return wyrd_world; 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 f3e5ae1..c9740f8 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 @@ -858,7 +858,7 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor        {           final Computation zero, minus_one; -         if (operands.get(2).get_type().equals(Type.INT)) +         if (operands.get(0).get_type().equals(Type.INT))           {              zero = Constant.ZERO;              minus_one = new Constant(Type.INT, "-1"); diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/InstructionCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/InstructionCompiler.java index b4c0e38..d05a280 100644 --- a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/InstructionCompiler.java +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/InstructionCompiler.java @@ -14,6 +14,7 @@ import tonkadur.wyrd.v1.lang.meta.Computation;  import tonkadur.wyrd.v1.lang.meta.Instruction;  import tonkadur.wyrd.v1.lang.type.Type; +import tonkadur.wyrd.v1.lang.type.MapType;  import tonkadur.wyrd.v1.lang.computation.Cast;  import tonkadur.wyrd.v1.lang.computation.Constant; @@ -44,6 +45,7 @@ import tonkadur.wyrd.v1.compiler.util.While;  import tonkadur.wyrd.v1.compiler.util.Clear;  import tonkadur.wyrd.v1.compiler.util.IterativeSearch;  import tonkadur.wyrd.v1.compiler.util.RemoveAllOf; +import tonkadur.wyrd.v1.compiler.util.ReverseList;  import tonkadur.wyrd.v1.compiler.util.RemoveAt;  public class InstructionCompiler @@ -275,20 +277,31 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor         *         * Wyrd: (assert Computation)         */ -      final ComputationCompiler cc; +      final ComputationCompiler cond_cc, msg_cc; -      cc = new ComputationCompiler(compiler); +      cond_cc = new ComputationCompiler(compiler); +      msg_cc = new ComputationCompiler(compiler); -      a.get_condition().get_visited_by(cc); +      a.get_condition().get_visited_by(cond_cc); +      a.get_message().get_visited_by(msg_cc); -      if (cc.has_init()) +      if (cond_cc.has_init())        { -         result.add(cc.get_init()); +         result.add(cond_cc.get_init());        } -      result.add(new Assert(cc.get_computation())); +      if (msg_cc.has_init()) +      { +         result.add(msg_cc.get_init()); +      } -      cc.release_variables(); +      result.add +      ( +         new Assert(cond_cc.get_computation(), msg_cc.get_computation()) +      ); + +      cond_cc.release_variables(); +      msg_cc.release_variables();     }     @Override @@ -328,6 +341,45 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor        reference_compiler.release_variables();     } +   public void visit_reverse_list +   ( +      final tonkadur.fate.v1.lang.instruction.ReverseList n +   ) +   throws Throwable +   { +      /* +       * Fate: (reverse_list collection) +       * +       * Wyrd: <reverse_list collection> +       */ +      final ComputationCompiler reference_compiler; +      final Ref collection_ref; + +      reference_compiler = new ComputationCompiler(compiler); + +      n.get_collection().get_visited_by(reference_compiler); + +      collection_ref = reference_compiler.get_ref(); + +      if (reference_compiler.has_init()) +      { +         result.add(reference_compiler.get_init()); +      } + +      result.add +      ( +         ReverseList.generate +         ( +            compiler.anonymous_variables(), +            compiler.assembler(), +            new Size(collection_ref), +            collection_ref +         ) +      ); + +      reference_compiler.release_variables(); +   } +     @Override     public void visit_switch_instruction     ( @@ -374,7 +426,9 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor        Computation value_of_anon;        List<Instruction> current_branch, previous_else_branch; -      branches = new ArrayList(n.get_branches()); // shallow copy. +      branches = new ArrayList<>(n.get_branches()); // shallow copy. + +      Collections.reverse(branches);        previous_else_branch = new ArrayList<Instruction>(); @@ -415,6 +469,7 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor           cc = new ComputationCompiler(compiler);           branch.get_car().get_visited_by(cc); +         branch.get_cdr().get_visited_by(ic);           if (cc.has_init())           { @@ -664,13 +719,17 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor        {           result.add           ( -            While.generate +            compiler.assembler().mark_after              ( -               compiler.anonymous_variables(), -               compiler.assembler(), -               cc.get_init(), -               cc.get_computation(), -               compiler.assembler().merge(body) +               While.generate +               ( +                  compiler.anonymous_variables(), +                  compiler.assembler(), +                  cc.get_init(), +                  cc.get_computation(), +                  compiler.assembler().merge(body) +               ), +               end_of_loop_label              )           );        } @@ -678,12 +737,16 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor        {           result.add           ( -            While.generate +            compiler.assembler().mark_after              ( -               compiler.anonymous_variables(), -               compiler.assembler(), -               cc.get_computation(), -               compiler.assembler().merge(body) +               While.generate +               ( +                  compiler.anonymous_variables(), +                  compiler.assembler(), +                  cc.get_computation(), +                  compiler.assembler().merge(body) +               ), +               end_of_loop_label              )           );        } @@ -697,15 +760,107 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor     throws Throwable     {        final String end_of_loop_label; +      final ComputationCompiler cc; +      final List<Instruction> new_body; +      final Ref index, current_value, collection_size; +      final Ref collection; +      final Computation value_of_index; +      final Type member_type; -      end_of_loop_label = -         compiler.assembler().generate_label("<AfterFor>"); +      cc = new ComputationCompiler(compiler); +      new_body = new ArrayList<Instruction>(); + +      index = compiler.anonymous_variables().reserve(Type.INT); +      collection_size = compiler.anonymous_variables().reserve(Type.INT); + +      result.add(new SetValue(index, Constant.ZERO)); + +      n.get_collection().get_visited_by(cc); + +      if (cc.has_init()) +      { +         result.add(cc.get_init()); +      } + +      collection = cc.get_ref(); + +      result.add(new SetValue(collection_size, new Size(collection))); + +      value_of_index = new ValueOf(index); + +      member_type = ((MapType) collection.get_target_type()).get_member_type(); + +      current_value = compiler.anonymous_variables().reserve(member_type); + +      end_of_loop_label = compiler.assembler().generate_label("<AfterForEach>");        compiler.assembler().push_context_label("breakable", end_of_loop_label); +      compiler.macros().add_wild_parameter +      ( +         n.get_parameter_name(), +         current_value +      ); + +      for +      ( +         final tonkadur.fate.v1.lang.meta.Instruction fate_instr: n.get_body() +      ) +      { +         final InstructionCompiler ic; + +         ic = new InstructionCompiler(compiler); -      /* TODO */ +         fate_instr.get_visited_by(ic); +         new_body.add(ic.get_result()); +      } + +      new_body.add +      ( +         new SetValue +         ( +            current_value, +            new ValueOf +            ( +               new RelativeRef +               ( +                  collection, +                  new Cast(value_of_index, Type.STRING), +                  member_type +               ) +            ) +         ) +      ); + +      new_body.add +      ( +         new SetValue(index, Operation.plus(Constant.ONE, value_of_index)) +      ); + +      result.add +      ( +         compiler.assembler().mark_after +         ( +            While.generate +            ( +               compiler.anonymous_variables(), +               compiler.assembler(), +               Operation.less_than +               ( +                  value_of_index, +                  new ValueOf(collection_size) +               ), +               compiler.assembler.merge(new_body) +            ), +            end_of_loop_label +         ) +      ); +      compiler.macros().remove_wild_parameter(n.get_parameter_name());        compiler.assembler().pop_context_label("breakable"); + +      compiler.anonymous_variables().release(index); +      compiler.anonymous_variables().release(current_value); +      compiler.anonymous_variables().release(collection_size);     }     @Override @@ -1113,11 +1268,9 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor         * Wyrd (add_choice label i0)         */        final ComputationCompiler cc; -      final InstructionCompiler ic;        final String start_of_effect, end_of_effect;        cc = new ComputationCompiler(compiler); -      ic = new InstructionCompiler(compiler);        start_of_effect = compiler.assembler().generate_label("<choice#start>");        end_of_effect = compiler.assembler().generate_label("<choice#end>"); @@ -1155,7 +1308,7 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor              n.get_effects()        )        { -         fate_instruction.get_visited_by(ic); +         fate_instruction.get_visited_by(this);        }        result.add diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/MacroManager.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/MacroManager.java index 085524a..fe2ab67 100644 --- a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/MacroManager.java +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/MacroManager.java @@ -10,13 +10,30 @@ import tonkadur.wyrd.v1.lang.computation.Ref;  public class MacroManager  { +   protected final Map<String, Ref> wild_parameters;     protected final Stack<Map<String, Ref>> context_stack;     public MacroManager ()     { +      wild_parameters = new HashMap<String, Ref>();        context_stack = new Stack<Map<String, Ref>>();     } +   public void add_wild_parameter (final String name, final Ref ref) +   { +      if (wild_parameters.containsKey(name)) +      { +         System.err.println("[P] duplicate wild parameter '" + name + "'."); + +         return; +      } +   } + +   public void remove_wild_parameter (final String name) +   { +      wild_parameters.remove(name); +   } +     public void pop ()     {        context_stack.pop(); @@ -51,13 +68,18 @@ public class MacroManager     public Ref get_parameter_ref (final String parameter)     { -      final Ref result; +      Ref result; -      result = context_stack.peek().get(parameter); +      result = wild_parameters.get(parameter);        if (result == null)        { -         System.err.println("[P] No such parameter '" + parameter + "'."); +         result = context_stack.peek().get(parameter); + +         if (result == null) +         { +            System.err.println("[P] No such parameter '" + parameter + "'."); +         }        }        return result; diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/util/AnonymousVariableManager.java b/src/core/src/tonkadur/wyrd/v1/compiler/util/AnonymousVariableManager.java index df0da53..8bec40a 100644 --- a/src/core/src/tonkadur/wyrd/v1/compiler/util/AnonymousVariableManager.java +++ b/src/core/src/tonkadur/wyrd/v1/compiler/util/AnonymousVariableManager.java @@ -2,6 +2,7 @@ package tonkadur.wyrd.v1.compiler.util;  import java.util.Map;  import java.util.HashMap; +import java.util.Collection;  import java.util.List;  import java.util.ArrayList; @@ -32,6 +33,20 @@ public class AnonymousVariableManager        generated_variables = 0;     } +   public Collection<Variable> get_all_variables () +   { +      final Collection<Variable> result; + +      result = new ArrayList<Variable>(); + +      for (final Cons<Boolean, Variable> variable: by_name.values()) +      { +         result.add(variable.get_cdr()); +      } + +      return result; +   } +     public Ref reserve (final Type t)     {        final String name; diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/util/RemoveAt.java b/src/core/src/tonkadur/wyrd/v1/compiler/util/RemoveAt.java index 55f5f02..5fc7489 100644 --- a/src/core/src/tonkadur/wyrd/v1/compiler/util/RemoveAt.java +++ b/src/core/src/tonkadur/wyrd/v1/compiler/util/RemoveAt.java @@ -90,7 +90,7 @@ public class RemoveAt        /* (set .next (+ (val index) 1)) */        while_body.add        ( -         new SetValue(next, Operation.plus(value_of_end, Constant.ONE)) +         new SetValue(next, Operation.plus(value_of_index, Constant.ONE))        );        /* (set collection[index] (val collection[.next])) */ diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/util/ReverseList.java b/src/core/src/tonkadur/wyrd/v1/compiler/util/ReverseList.java new file mode 100644 index 0000000..226e86a --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/util/ReverseList.java @@ -0,0 +1,152 @@ +package tonkadur.wyrd.v1.compiler.util; + +import java.util.List; +import java.util.ArrayList; +import java.util.Collections; + +import tonkadur.wyrd.v1.lang.type.Type; +import tonkadur.wyrd.v1.lang.type.MapType; + +import tonkadur.wyrd.v1.lang.meta.Instruction; +import tonkadur.wyrd.v1.lang.meta.Computation; + +import tonkadur.wyrd.v1.lang.computation.Cast; +import tonkadur.wyrd.v1.lang.computation.Constant; +import tonkadur.wyrd.v1.lang.computation.Operation; +import tonkadur.wyrd.v1.lang.computation.Ref; +import tonkadur.wyrd.v1.lang.computation.RelativeRef; +import tonkadur.wyrd.v1.lang.computation.ValueOf; + +import tonkadur.wyrd.v1.lang.instruction.SetValue; + +public class ReverseList +{ +   /* Utility Class */ +   private ReverseList () {} + +   /* +    * (Computation int collection_size) +    * (declare_variable global <List E> collection) +    * +    * (declare_variable E .buffer +    * (declare_variable int .top) +    * (declare_variable int .bot) +    * +    * (set .top (- (collection_size) 1)) +    * (set .bot 0) +    * +    * (while (< (var .bot) (var .top)) +    *    (set .buffer collection[.top]) +    *    (set collection[.top] collection[.bot]) +    *    (set collection[.bot] .buffer) +    *    (set .bot (+ 1 (var .bot))) +    *    (set .top (- 1 (var .top))) +    * ) +    */ +   public static Instruction generate +   ( +      final AnonymousVariableManager anonymous_variables, +      final InstructionManager assembler, +      final Computation collection_size, +      final Ref collection +   ) +   { +      final List<Instruction> result, while_body; +      final Type element_type; +      final Ref buffer, top, bot; +      final Computation value_of_top, value_of_bot; +      final Ref collection_at_top, collection_at_bot; + +      result = new ArrayList<Instruction>(); +      while_body = new ArrayList<Instruction>(); + +      element_type = +         ((MapType) collection.get_target_type()).get_member_type(); + +      buffer = anonymous_variables.reserve(element_type); +      top = anonymous_variables.reserve(Type.INT); +      bot = anonymous_variables.reserve(Type.INT); + +      value_of_top = new ValueOf(top); +      value_of_bot = new ValueOf(bot); + +      collection_at_top = +         new RelativeRef +         ( +            collection, +            new Cast(value_of_top, Type.STRING), +            element_type +         ); + +      collection_at_bot = +         new RelativeRef +         ( +            collection, +            new Cast(value_of_bot, Type.STRING), +            element_type +         ); + +      /* (set .top (- (collection_size) 1)) */ +      result.add +      ( +         new SetValue(top, Operation.minus(collection_size, Constant.ONE)) +      ); + +      /* (set .bot 0) */ +      result.add(new SetValue(bot, Constant.ZERO)); + + +      /* (set .buffer collection[.top]) */ +      while_body.add(new SetValue(buffer, new ValueOf(collection_at_top))); + +      /* (set collection[.top] collection[.bot]) */ +      while_body.add +      ( +         new SetValue(collection_at_top, new ValueOf(collection_at_bot)) +      ); + +      /* (set collection[.bot] .buffer) */ +      while_body.add +      ( +         new SetValue(collection_at_bot, new ValueOf(buffer)) +      ); + +      /* (set .bot (+ 1 (var .bot))) */ +      while_body.add +      ( +         new SetValue(bot, Operation.plus(Constant.ONE, value_of_bot)) +      ); + +      /* (set .top (- 1 (var .top))) */ +      while_body.add +      ( +         new SetValue(top, Operation.minus(Constant.ONE, value_of_top)) +      ); + +      /* +       * (while (< (var .bot) (var .top)) +       *    (set .buffer collection[.top]) +       *    (set collection[.top] collection[.bot]) +       *    (set collection[.bot] .buffer) +       *    (set .bot (+ 1 (var .bot))) +       *    (set .top (- 1 (var .top))) +       * ) +       */ +      result.add +      ( +         While.generate +         ( +            anonymous_variables, +            assembler, +            Operation.less_than(value_of_bot, value_of_top), +            assembler.merge(while_body) +         ) +      ); + +      anonymous_variables.release(buffer); +      anonymous_variables.release(top); +      anonymous_variables.release(bot); + +      return assembler.merge(result); +   } +} diff --git a/src/core/src/tonkadur/wyrd/v1/lang/instruction/Assert.java b/src/core/src/tonkadur/wyrd/v1/lang/instruction/Assert.java index 7bfa25b..57dd7c1 100644 --- a/src/core/src/tonkadur/wyrd/v1/lang/instruction/Assert.java +++ b/src/core/src/tonkadur/wyrd/v1/lang/instruction/Assert.java @@ -10,14 +10,16 @@ public class Assert extends Instruction     /**** MEMBERS **************************************************************/     /***************************************************************************/     protected final Computation condition; +   protected final Computation message;     /***************************************************************************/     /**** PUBLIC ***************************************************************/     /***************************************************************************/     /**** Constructors *********************************************************/ -   public Assert (final Computation condition) +   public Assert (final Computation condition, final Computation message)     {        this.condition = condition; +      this.message = message;     }     /**** Accessors ************************************************************/ @@ -26,6 +28,11 @@ public class Assert extends Instruction        return condition;     } +   public Computation get_message () +   { +      return message; +   } +     @Override     public void get_visited_by (final InstructionVisitor iv)     throws Throwable @@ -43,6 +50,8 @@ public class Assert extends Instruction        sb.append("(Assert ");        sb.append(condition.toString()); +      sb.append(" "); +      sb.append(message.toString());        sb.append(")");        return sb.toString(); diff --git a/src/json-export/src/tonkadur/jsonexport/InstructionCompiler.java b/src/json-export/src/tonkadur/jsonexport/InstructionCompiler.java index 00445c7..8f7913b 100644 --- a/src/json-export/src/tonkadur/jsonexport/InstructionCompiler.java +++ b/src/json-export/src/tonkadur/jsonexport/InstructionCompiler.java @@ -33,16 +33,19 @@ public class InstructionCompiler implements InstructionVisitor     public void visit_assert (final Assert n)     throws Throwable     { -      final ComputationCompiler cc; +      final ComputationCompiler cond_cc, msg_cc; -      cc = new ComputationCompiler(); +      cond_cc = new ComputationCompiler(); +      msg_cc = new ComputationCompiler(); -      n.get_condition().get_visited_by(cc); +      n.get_condition().get_visited_by(cond_cc); +      n.get_message().get_visited_by(msg_cc);        result = new JSONObject();        result.put("category", "assert"); -      result.put("condition", cc.get_result()); +      result.put("condition", cond_cc.get_result()); +      result.put("message", msg_cc.get_result());     }     public void visit_display (final Display n) diff --git a/src/json-export/src/tonkadur/jsonexport/Translator.java b/src/json-export/src/tonkadur/jsonexport/Translator.java index edd0a7f..cc22e7c 100644 --- a/src/json-export/src/tonkadur/jsonexport/Translator.java +++ b/src/json-export/src/tonkadur/jsonexport/Translator.java @@ -36,9 +36,6 @@ public class Translator        out = new PrintWriter(output_file);        out.println(result.toJSONString());        out.close(); - -      System.out.println("JSON Result:"); -      System.out.println(result.toJSONString());     }     public static JSONArray get_compiled_variables (final World wyrd_world) | 


