| summaryrefslogtreecommitdiff | 
diff options
| author | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2020-07-29 19:11:34 +0200 | 
|---|---|---|
| committer | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2020-07-29 19:11:34 +0200 | 
| commit | 44deeb7463e63eea1f7c6724dda2d12885c2b1e8 (patch) | |
| tree | 7cea0937c00b00aa7252abdf448936027a90e2d1 /src/core | |
| parent | 06d5119ca19c68c6b2f45d79e2339e82278508fd (diff) | |
...
Diffstat (limited to 'src/core')
26 files changed, 1148 insertions, 772 deletions
| diff --git a/src/core/src/tonkadur/fate/v1/lang/meta/Instruction.java b/src/core/src/tonkadur/fate/v1/lang/meta/Instruction.java index 457c355..7782a97 100644 --- a/src/core/src/tonkadur/fate/v1/lang/meta/Instruction.java +++ b/src/core/src/tonkadur/fate/v1/lang/meta/Instruction.java @@ -10,8 +10,6 @@ public abstract class Instruction extends Node     /***************************************************************************/     /**** MEMBERS **************************************************************/     /***************************************************************************/ -   protected final Collection<Instruction> parents; -   protected Instruction child;     /***************************************************************************/     /**** PROTECTED ************************************************************/ @@ -20,9 +18,6 @@ public abstract class Instruction extends Node     protected Instruction (final Origin origin)     {        super(origin); - -      parents = new HashSet<Instruction>(); -      child = null;     }     /***************************************************************************/ @@ -35,23 +30,6 @@ public abstract class Instruction extends Node     } -   public void link_parent (final Instruction parent) -   { -      parent.child = this; - -      parents.add(parent); -   } - -   public Collection<Instruction> get_parents () -   { -      return parents; -   } - -   public Instruction get_child () -   { -      return child; -   } -     /**** Misc. ****************************************************************/     @Override     public String toString () 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 39707da..d516cb3 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 @@ -1,34 +1,47 @@  package tonkadur.wyrd.v1.compiler.fate.v1; -import tonkadur.error.Error; +import tonkadur.wyrd.v1.compiler.util.AnonymousVariableManager; +import tonkadur.wyrd.v1.compiler.util.InstructionManager;  import tonkadur.wyrd.v1.lang.World;  public class Compiler  { -   /* Utility Class */ -   private Compiler () { } +   protected final AnonymousVariableManager anonymous_variables; +   protected final MacroManager macro_manager; +   protected final InstructionManager assembler; +   protected final World wyrd_world; +     public static World compile (final tonkadur.fate.v1.lang.World fate_world) -   throws Error +   throws Throwable     { -      final World wyrd_world; +      final Compiler compiler; -      wyrd_world = new World(); +      compiler = new Compiler(); -      compile_extensions(fate_world, wyrd_world); -      compile_types(fate_world, wyrd_world); -      compile_variables(fate_world, wyrd_world); -      compile_sequences(fate_world, wyrd_world); -      compile_main_sequence(fate_world, wyrd_world); +      compiler.compile_extensions(fate_world); +      compiler.compile_types(fate_world); +      compiler.compile_variables(fate_world); -      return wyrd_world; +      compiler.compile_main_sequence(fate_world); + +      compiler.compile_sequences(fate_world); + +      return compiler.wyrd_world;     } -   protected static void compile_extensions +   protected Compiler () +   { +      macro_manager = new MacroManager(); +      anonymous_variables = new AnonymousVariableManager(); +      assembler = new InstructionManager(); +      wyrd_world = new World(); +   } + +   protected void compile_extensions     ( -      final tonkadur.fate.v1.lang.World fate_world, -      final World wyrd_world +      final tonkadur.fate.v1.lang.World fate_world     )     {        for (final String extension: fate_world.get_required_extensions()) @@ -37,12 +50,11 @@ public class Compiler        }     } -   protected static void compile_types +   protected void compile_types     ( -      final tonkadur.fate.v1.lang.World fate_world, -      final World wyrd_world +      final tonkadur.fate.v1.lang.World fate_world     ) -   throws Error +   throws Throwable     {        for        ( @@ -50,16 +62,15 @@ public class Compiler              fate_world.types().get_all()        )        { -         TypeCompiler.compile(type, wyrd_world); +         TypeCompiler.compile(this, type);        }     } -   protected static void compile_variables +   protected void compile_variables     ( -      final tonkadur.fate.v1.lang.World fate_world, -      final World wyrd_world +      final tonkadur.fate.v1.lang.World fate_world     ) -   throws Error +   throws Throwable     {        for        ( @@ -67,16 +78,15 @@ public class Compiler              fate_world.variables().get_all()        )        { -         VariableCompiler.compile(variable, wyrd_world); +         VariableCompiler.compile(this, variable);        }     } -   protected static void compile_sequences +   protected void compile_sequences     ( -      final tonkadur.fate.v1.lang.World fate_world, -      final World wyrd_world +      final tonkadur.fate.v1.lang.World fate_world     ) -   throws Error +   throws Throwable     {        for        ( @@ -84,21 +94,40 @@ public class Compiler              fate_world.sequences().get_all()        )        { -         SequenceCompiler.compile(sequence, wyrd_world); +         SequenceCompiler.compile(this, sequence);        }     } -   protected static void compile_main_sequence +   protected void compile_main_sequence     ( -      final tonkadur.fate.v1.lang.World fate_world, -      final World wyrd_world +      final tonkadur.fate.v1.lang.World fate_world     ) -   throws Error +   throws Throwable     {        SequenceCompiler.compile_main_sequence        ( -         fate_world.get_global_instructions(), -         wyrd_world +         this, +         fate_world.get_global_instructions()        );     } + +   public World world () +   { +      return wyrd_world; +   } + +   public AnonymousVariableManager anonymous_variables () +   { +      return anonymous_variables; +   } + +   public InstructionManager assembler () +   { +      return assembler; +   } + +   public MacroManager macros () +   { +      return macro_manager; +   }  } 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 ebfc865..1cc3af8 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 @@ -13,14 +13,10 @@ import tonkadur.wyrd.v1.lang.computation.ValueOf;  import tonkadur.wyrd.v1.lang.World; -import tonkadur.wyrd.v1.compiler.util.AnonymousVariableManager; -  public class ComputationCompiler  implements tonkadur.fate.v1.lang.meta.ComputationVisitor  { -   protected final MacroManager macro_manager; -   protected final AnonymousVariableManager anonymous_variables; -   protected final World wyrd_world; +   protected final Compiler compiler;     protected final List<Instruction> pre_computation_instructions;     protected final List<Ref> allocated_variables;     protected final boolean expect_ref; @@ -29,15 +25,11 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor     public ComputationCompiler     ( -      final MacroManager macro_manager, -      final AnonymousVariableManager anonymous_variables, -      final World wyrd_world, +      final Compiler compiler,        final boolean expect_ref     )     { -      this.macro_manager = macro_manager; -      this.anonymous_variables = anonymous_variables; -      this.wyrd_world = wyrd_world; +      this.compiler = compiler;        this.expect_ref = expect_ref;        allocated_variables = new ArrayList<Ref>(); @@ -46,9 +38,9 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor        result_as_computation = null;     } -   public List<Instruction> get_pre_instructions () +   public Instruction get_init ()     { -      return pre_computation_instructions; +      return compiler.assembler().merge(pre_computation_instructions);     }     public Computation get_computation () @@ -67,6 +59,11 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor     public Ref get_ref ()     { +      if ((result_as_ref == null) && (result_as_computation != null)) +      { +         generate_ref(); +      } +        return result_as_ref;     } @@ -74,18 +71,22 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor     {        final Ref result; -      result = anonymous_variables.reserve(result_as_computation.get_type()); +      result = +         compiler.anonymous_variables().reserve +         ( +            result_as_computation.get_type() +         );        allocated_variables.add(result);        result_as_ref = result;     } -   public void free_anonymous_variables () +   public void release_variables ()     {        for (final Ref ref: allocated_variables)        { -         anonymous_variables.release(ref); +         compiler.anonymous_variables().release(ref);        }     } 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 914a836..b4038dd 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 @@ -29,17 +29,20 @@ import tonkadur.wyrd.v1.lang.instruction.AddChoice;  import tonkadur.wyrd.v1.lang.instruction.Assert;  import tonkadur.wyrd.v1.lang.instruction.Display;  import tonkadur.wyrd.v1.lang.instruction.EventCall; -import tonkadur.wyrd.v1.lang.instruction.IfElseInstruction; -import tonkadur.wyrd.v1.lang.instruction.NOP;  import tonkadur.wyrd.v1.lang.instruction.Remove;  import tonkadur.wyrd.v1.lang.instruction.ResolveChoices; -import tonkadur.wyrd.v1.lang.instruction.SequenceCall; +import tonkadur.wyrd.v1.lang.instruction.SetPC;  import tonkadur.wyrd.v1.lang.instruction.SetValue; -import tonkadur.wyrd.v1.lang.instruction.While;  import tonkadur.wyrd.v1.compiler.util.AnonymousVariableManager;  import tonkadur.wyrd.v1.compiler.util.BinarySearch;  import tonkadur.wyrd.v1.compiler.util.InsertAt; +import tonkadur.wyrd.v1.compiler.util.If; +import tonkadur.wyrd.v1.compiler.util.IfElse; +import tonkadur.wyrd.v1.compiler.util.InstructionManager; +import tonkadur.wyrd.v1.compiler.util.NOP; +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.RemoveAt; @@ -47,82 +50,28 @@ import tonkadur.wyrd.v1.compiler.util.RemoveAt;  public class InstructionCompiler  implements tonkadur.fate.v1.lang.meta.InstructionVisitor  { -   protected final AnonymousVariableManager anonymous_variables; -   protected final World wyrd_world; -   protected final MacroManager macro_manager; +   protected final Compiler compiler;     protected final List<Instruction> result; -   public InstructionCompiler -   ( -      final MacroManager macro_manager, -      final AnonymousVariableManager anonymous_variables, -      final World wyrd_world -   ) +   public InstructionCompiler (final Compiler compiler)     { -      this.macro_manager = macro_manager; -      this.anonymous_variables = anonymous_variables; -      this.wyrd_world = wyrd_world; +      this.compiler = compiler;        result = new ArrayList<Instruction>();     }     public InstructionCompiler     ( -      final MacroManager macro_manager, -      final AnonymousVariableManager anonymous_variables, -      final World wyrd_world, +      final Compiler compiler,        final List<Instruction> result     )     { -      this.macro_manager = macro_manager; -      this.anonymous_variables = anonymous_variables; -      this.wyrd_world = wyrd_world; +      this.compiler = compiler;        this.result = result;     } -   protected List<Instruction> get_result () -   { -      return result; -   } - -   protected ComputationCompiler new_computation_compiler -   ( -      final boolean expect_ref -   ) -   { -      return -         new ComputationCompiler -         ( -            macro_manager, -            anonymous_variables, -            wyrd_world, -            expect_ref -         ); -   } - -   protected InstructionCompiler new_instruction_compiler () -   { -      return -         new InstructionCompiler -         ( -            macro_manager, -            anonymous_variables, -            wyrd_world -         ); -   } - -   protected InstructionCompiler new_instruction_compiler -   ( -      final List<Instruction> result -   ) +   protected Instruction get_result ()     { -      return -         new InstructionCompiler -         ( -            macro_manager, -            anonymous_variables, -            wyrd_world, -            result -         ); +      return compiler.assembler().merge(result);     }     protected void add_element_to_set @@ -145,174 +94,76 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor         *    <insert_at ...>         * )         */ -      /* -      final Ref element_as_ref, collection_as_ref, collection_size_as_ref; -      final Ref element_found, element_index, collection_size, next_index;        final ComputationCompiler element_compiler, reference_compiler; +      final Ref element, collection, collection_size; +      final Ref element_found, element_index;        final Type element_type; -      final List<Instruction> while_body, else_branch; - -      element_compiler = new_computation_compiler(false); +      final Computation value_of_element, value_of_collection_size; +      element_compiler = new ComputationCompiler(compiler, false);        ae.get_element().get_visited_by(element_compiler); - -      result.addAll(element_compiler.get_pre_instructions()); - +      result.add(element_compiler.get_init());        element_type = element_compiler.get_computation().get_type(); +      element = compiler.anonymous_variables().reserve(element_type); +      result.add(new SetValue(element, element_compiler.get_computation())); +      element_compiler.release_variables(); -      element_as_ref = anonymous_variable.reserve(element_type); - -      result.add -      ( -         new SetValue(element_as_ref, element_compiler.get_computation()) -      ); - -      reference_compiler = new_computation_compiler(true); - +      reference_compiler = new ComputationCompiler(compiler, true);        ae.get_collection().get_visited_by(reference_compiler); +      result.add(reference_compiler.get_init()); +      collection = reference_compiler.get_ref(); -      result.addAll(reference_compiler.get_pre_instructions()); +      element_found = compiler.anonymous_variables().reserve(Type.BOOLEAN); +      element_index = compiler.anonymous_variables().reserve(Type.INT); +      collection_size = compiler.anonymous_variables().reserve(Type.INT); -      collection_as_ref = reference_compiler.get_ref(); - -      element_found = anonymous_variable.reserve(Type.BOOLEAN); -      element_index = anonymous_variable.reserve(Type.INT); -      collection_size = anonymous_variable.reserve(Type.INT); -      next_index = anonymous_variable.reserve(Type.INT); +      value_of_element = new ValueOf(element); +      value_of_collection_size = new ValueOf(collection_size);        result.add        ( -         new SetValue(collection_size, new Size(collection_as_ref)) +         new SetValue(collection_size, new Size(collection))        ); -      result.addAll +      result.add        (           BinarySearch.generate           ( -            anonymous_variables, -            element_as_ref, -            collection_as_ref, -            collection_size, +            compiler.anonymous_variables(), +            compiler.assembler(), +            value_of_element, +            value_of_collection_size, +            collection,              element_found,              element_index           )        ); -      while_body = new ArrayList<Instruction>(); - -      while_body.add -      ( -         new SetValue -         ( -            next_index, -            Operation.minus -            ( -               new ValueOf(collection_size), -               new Constant(Type.INT, "1") -            ) -         ) -      ); - -      while_body.add +      result.add        ( -         new SetValue +         If.generate           ( -            new RelativeRef -            ( -               collection_as_ref, -               Collections.singletonList -               ( -                  new Cast -                  ( -                     new ValueOf(collection_size), -                     Type.STRING -                  ) -               ), -               element_type -            ), -            new ValueOf +            compiler.anonymous_variables(), +            compiler.assembler(), +            Operation.not(new ValueOf(element_found)), +            InsertAt.generate              ( -               new RelativeRef -               ( -                  collection_as_ref, -                  Collections.singletonList -                  ( -                     new Cast -                     ( -                        new ValueOf(next), -                        Type.STRING -                     ) -                  ), -                  element_type -               ) +               compiler.anonymous_variables(), +               compiler.assembler(), +               element_index, +               value_of_element, +               value_of_collection_size, +               collection              )           )        ); -      while_body.add -      ( -         new SetValue -         ( -            collection_size, -            new ValueOf(next) -         ) -      ); - -      else_branch = new ArrayList<Instruction>(); - -      else_branch.add -      ( -         new While -         ( -            Operation.less_than -            ( -               new ValueOf(element_index), -               new ValueOf(collection_size) -            ), -            while_body -         ) -      ); - -      else_branch.add -      ( -         new SetValue -         ( -            new RelativeRef -            ( -               collection_as_ref, -               Collections.singletonList -               ( -                  new Cast -                  ( -                     new ValueOf(element_index), -                     Type.STRING -                  ) -               ), -               element_type -            ), -            new ValueOf(element_as_ref) -         ) -      ); - -      result.add -      ( -         new IfElseInstruction -         ( -            new ValueOf(element_found), -            new NOP(), -            else_branch -         ) -      ); - -      anonymous_variables.release(element_as_ref); -      anonymous_variables.release(element_found); -      anonymous_variables.release(element_index); -      anonymous_variables.release(collection_size); -      anonymous_variables.release(next_index); +      compiler.anonymous_variables().release(element); +      compiler.anonymous_variables().release(element_found); +      compiler.anonymous_variables().release(element_index); +      compiler.anonymous_variables().release(collection_size); -      reference_compiler.free_anonymous_variables(); -      element_compiler.free_anonymous_variables(); -      */ +      reference_compiler.release_variables();     }     protected void add_element_to_list @@ -334,18 +185,18 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor        final Ref collection_as_ref;        final ComputationCompiler element_compiler, reference_compiler; -      element_compiler = new_computation_compiler(false); +      element_compiler = new ComputationCompiler(compiler, false);        ae.get_element().get_visited_by(element_compiler); -      reference_compiler = new_computation_compiler(true); +      reference_compiler = new ComputationCompiler(compiler, true);        ae.get_collection().get_visited_by(reference_compiler);        collection_as_ref = reference_compiler.get_ref(); -      result.addAll(reference_compiler.get_pre_instructions()); -      result.addAll(element_compiler.get_pre_instructions()); +      result.add(reference_compiler.get_init()); +      result.add(element_compiler.get_init());        result.add        (           new SetValue @@ -367,8 +218,8 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor           )        ); -      reference_compiler.free_anonymous_variables(); -      element_compiler.free_anonymous_variables(); +      reference_compiler.release_variables(); +      element_compiler.release_variables();     }     @Override @@ -415,14 +266,14 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor         */        final ComputationCompiler cc; -      cc = new_computation_compiler(false); +      cc = new ComputationCompiler(compiler, false);        a.get_condition().get_visited_by(cc); -      result.addAll(cc.get_pre_instructions()); +      result.add(cc.get_init());        result.add(new Assert(cc.get_computation())); -      cc.free_anonymous_variables(); +      cc.release_variables();     }     @Override @@ -432,64 +283,30 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor        /*         * Fate: (clear collection)         * -       * Wyrd: -       *    <clear collection> +       * Wyrd: <clear collection>         */ -      /*        final ComputationCompiler reference_compiler; -      final Ref iterator, collection_ref; -      final List<Instruction> while_body; +      final Ref collection_ref; -      reference_compiler = new_computation_compiler(true); +      reference_compiler = new ComputationCompiler(compiler, true);        c.get_collection().get_visited_by(reference_compiler); -      result.addAll(reference_compiler.get_pre_instructions()); - -      iterator = anonymous_variables.reserve(Type.INT); -        collection_ref = reference_compiler.get_ref(); -      while_body.add -      ( -         new Remove -         ( -            new RelativeRef -            ( -               collection_ref, -               Collections.singletonList -               ( -                  new Cast(new ValueOf(iterator), Type.STRING) -               ) -            ) -         ) -      ); - -      while_body.add -      ( -         new SetValue -         ( -            iterator, -            Operation.minus(new ValueOf(iterator), new Constant(Type.INT, "1")) -         ) -      ); - +      result.add(reference_compiler.get_init());        result.add        ( -         new While +         Clear.generate           ( -            Operation.greater_equal_than -            ( -               new ValueOf(iterator), -               new Constant(Type.INT, "0") -            ), -            while_body +            compiler.anonymous_variables(), +            compiler.assembler(), +            new Size(collection_ref), +            collection_ref           )        ); -      anonymous_variables.release(iterator); -      reference_compiler.free_anonymous_variables(); -      */ +      reference_compiler.release_variables();     }     @Override @@ -508,16 +325,16 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor         *    )         *         * Wyrd: -       *    (ifelse c0 +       *    <ifelse c0         *       i0 -       *       (ifelse ... +       *       <ifelse ...         *          ... -       *          (ifelse cn +       *          <ifelse cn         *             in -       *             (nop) -       *          ) -       *       ) -       *    ) +       *             <nop> +       *          > +       *       > +       *    >         */        InstructionCompiler ic;        ComputationCompiler cc; @@ -540,28 +357,37 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor        {           current_else_branch = new ArrayList<Instruction>(); -         ic = new_instruction_compiler(); -         cc = new_computation_compiler(false); +         ic = new InstructionCompiler(compiler); +         cc = new ComputationCompiler(compiler, false);           branch.get_car().get_visited_by(cc); -         previous_else_branch.addAll(cc.get_pre_instructions()); +         previous_else_branch.add(cc.get_init());           previous_else_branch.add           ( -            new IfElseInstruction +            IfElse.generate              ( +               compiler.anonymous_variables(), +               compiler.assembler(),                 cc.get_computation(),                 ic.get_result(), -               current_else_branch +               compiler.assembler().merge(current_else_branch)              )           );           previous_else_branch = current_else_branch; -         cc.free_anonymous_variables(); +         cc.release_variables();        } -      previous_else_branch.add(new NOP()); +      previous_else_branch.add +      ( +         NOP.generate +         ( +            compiler.anonymous_variables(), +            compiler.assembler() +         ) +      );     }     @Override @@ -575,14 +401,14 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor         */        final ComputationCompiler cc; -      cc = new_computation_compiler(false); +      cc = new ComputationCompiler(compiler, false);        n.get_content().get_visited_by(cc); -      result.addAll(cc.get_pre_instructions()); +      result.add(cc.get_init());        result.add(new Display(Collections.singletonList(cc.get_computation()))); -      cc.free_anonymous_variables(); +      cc.release_variables();     }     @Override @@ -611,11 +437,11 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor        {           final ComputationCompiler cc; -         cc = new_computation_compiler(false); +         cc = new ComputationCompiler(compiler, false);           fate_computation.get_visited_by(cc); -         result.addAll(cc.get_pre_instructions()); +         result.add(cc.get_init());           cc_list.add(cc);           parameters.add(cc.get_computation()); @@ -625,7 +451,7 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor        for (final ComputationCompiler cc: cc_list)        { -         cc.free_anonymous_variables(); +         cc.release_variables();        }     } @@ -645,26 +471,28 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor        final InstructionCompiler if_true_ic;        final InstructionCompiler if_false_ic; -      cc = new_computation_compiler(false); -      if_true_ic = new_instruction_compiler(); -      if_false_ic = new_instruction_compiler(); +      cc = new ComputationCompiler(compiler, false); +      if_true_ic = new InstructionCompiler(compiler); +      if_false_ic = new InstructionCompiler(compiler);        n.get_condition().get_visited_by(cc);        n.get_if_true().get_visited_by(if_true_ic);        n.get_if_false().get_visited_by(if_false_ic); -      result.addAll(cc.get_pre_instructions()); +      result.add(cc.get_init());        result.add        ( -         new IfElseInstruction +         IfElse.generate           ( +            compiler.anonymous_variables(), +            compiler.assembler(),              cc.get_computation(),              if_true_ic.get_result(),              if_false_ic.get_result()           )        ); -      cc.free_anonymous_variables(); +      cc.release_variables();     }     @Override @@ -682,24 +510,25 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor        final ComputationCompiler cc;        final InstructionCompiler if_true_ic; -      cc = new_computation_compiler(false); -      if_true_ic = new_instruction_compiler(); +      cc = new ComputationCompiler(compiler, false); +      if_true_ic = new InstructionCompiler(compiler);        n.get_condition().get_visited_by(cc);        n.get_if_true().get_visited_by(if_true_ic); -      result.addAll(cc.get_pre_instructions()); +      result.add(cc.get_init());        result.add        ( -         new IfElseInstruction +         If.generate           ( +            compiler.anonymous_variables(), +            compiler.assembler(),              cc.get_computation(), -            if_true_ic.get_result(), -            Collections.singletonList(new NOP()) +            if_true_ic.get_result()           )        ); -      cc.free_anonymous_variables(); +      cc.release_variables();     }     @Override @@ -722,11 +551,11 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor        {           final InstructionCompiler ic; -         ic = new_instruction_compiler(); +         ic = new InstructionCompiler(compiler);           fate_instruction.get_visited_by(ic); -         result.addAll(ic.get_result()); +         result.add(ic.get_result());        }     } @@ -756,23 +585,23 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor        {           final ComputationCompiler cc; -         cc = new_computation_compiler(true); +         cc = new ComputationCompiler(compiler, true);           fate_computation.get_visited_by(cc); -         result.addAll(cc.get_pre_instructions()); +         result.add(cc.get_init());           cc_list.add(cc);           parameters.add(cc.get_ref());        } -      macro_manager.push(n.get_macro(), parameters); +      compiler.macros().push(n.get_macro(), parameters);        n.get_macro().get_root().get_visited_by(this); -      macro_manager.pop(); +      compiler.macros().pop();        for (final ComputationCompiler cc: cc_list)        { -         cc.free_anonymous_variables(); +         cc.release_variables();        }     } @@ -791,12 +620,12 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor        final ComputationCompiler cc;        final InstructionCompiler ic; -      cc = new_computation_compiler(false); -      ic = new_instruction_compiler(); +      cc = new ComputationCompiler(compiler, false); +      ic = new InstructionCompiler(compiler);        n.get_text().get_visited_by(cc); -      result.addAll(cc.get_pre_instructions()); +      result.add(cc.get_init());        for        ( @@ -809,7 +638,7 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor        result.add(new AddChoice(cc.get_computation(), ic.get_result())); -      cc.free_anonymous_variables(); +      cc.release_variables();     }     @Override @@ -881,24 +710,29 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor        final ComputationCompiler elem_cc, collection_cc;        final Ref elem, collection_size, collection; -      elem_cc = new_computation_compiler(false); -      collection_cc = new_computation_compiler(true); +      elem_cc = new ComputationCompiler(compiler, false); +      collection_cc = new ComputationCompiler(compiler, true); + +      elem = +         compiler.anonymous_variables().reserve +         ( +            elem_cc.get_computation().get_type() +         ); -      elem = anonymous_variables.reserve(elem_cc.get_computation().get_type()); -      collection_size = anonymous_variables.reserve(Type.INT); +      collection_size = compiler.anonymous_variables().reserve(Type.INT);        n.get_element().get_visited_by(elem_cc);        n.get_collection().get_visited_by(collection_cc); -      result.addAll(elem_cc.get_pre_instructions()); -      result.addAll(collection_cc.get_pre_instructions()); +      result.add(elem_cc.get_init()); +      result.add(collection_cc.get_init());        collection = collection_cc.get_ref();        result.add(new SetValue(elem, elem_cc.get_computation()));        result.add(new SetValue(collection_size, new Size(collection))); -      elem_cc.free_anonymous_variables(); +      elem_cc.release_variables();        if        ( @@ -911,17 +745,18 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor           final Computation value_of_elem, value_of_collection_size;           final Ref index, found; -         index = anonymous_variables.reserve(Type.INT); -         found = anonymous_variables.reserve(Type.BOOLEAN); +         index = compiler.anonymous_variables().reserve(Type.INT); +         found = compiler.anonymous_variables().reserve(Type.BOOLEAN);           value_of_elem = new ValueOf(elem);           value_of_collection_size = new ValueOf(collection_size); -         result.addAll +         result.add           (              BinarySearch.generate              ( -               anonymous_variables, +               compiler.anonymous_variables(), +               compiler.assembler(),                 value_of_elem,                 value_of_collection_size,                 collection, @@ -932,30 +767,33 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor           result.add           ( -            new IfElseInstruction +            If.generate              ( +               compiler.anonymous_variables(), +               compiler.assembler(),                 new ValueOf(found),                 RemoveAt.generate                 ( -                  anonymous_variables, +                  compiler.anonymous_variables(), +                  compiler.assembler(),                    index,                    value_of_collection_size,                    collection -               ), -               Collections.singletonList(new NOP()) +               )              )           ); -         anonymous_variables.release(index); -         anonymous_variables.release(found); +         compiler.anonymous_variables().release(index); +         compiler.anonymous_variables().release(found);        }        else        { -         result.addAll +         result.add           (              RemoveAllOf.generate              ( -               anonymous_variables, +               compiler.anonymous_variables(), +               compiler.assembler(),                 new ValueOf(elem),                 new ValueOf(collection_size),                 collection @@ -963,10 +801,10 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor           );        } -      collection_cc.free_anonymous_variables(); +      collection_cc.release_variables(); -      anonymous_variables.release(elem); -      anonymous_variables.release(collection_size); +      compiler.anonymous_variables().release(elem); +      compiler.anonymous_variables().release(collection_size);     }     @Override @@ -1018,19 +856,24 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor        final Ref collection;        final Computation value_of_collection_size; -      elem_cc = new_computation_compiler(false); -      collection_cc = new_computation_compiler(true); +      elem_cc = new ComputationCompiler(compiler, false); +      collection_cc = new ComputationCompiler(compiler, true); -      elem = anonymous_variables.reserve(elem_cc.get_computation().get_type()); -      collection_size = anonymous_variables.reserve(Type.INT); -      found = anonymous_variables.reserve(Type.BOOLEAN); -      index = anonymous_variables.reserve(Type.INT); +      elem = +         compiler.anonymous_variables().reserve +         ( +            elem_cc.get_computation().get_type() +         ); + +      collection_size = compiler.anonymous_variables().reserve(Type.INT); +      found = compiler.anonymous_variables().reserve(Type.BOOLEAN); +      index = compiler.anonymous_variables().reserve(Type.INT);        n.get_element().get_visited_by(elem_cc);        n.get_collection().get_visited_by(collection_cc); -      result.addAll(elem_cc.get_pre_instructions()); -      result.addAll(collection_cc.get_pre_instructions()); +      result.add(elem_cc.get_init()); +      result.add(collection_cc.get_init());        collection = collection_cc.get_ref(); @@ -1039,7 +882,7 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor        result.add(new SetValue(elem, elem_cc.get_computation()));        result.add(new SetValue(collection_size, new Size(collection))); -      elem_cc.free_anonymous_variables(); +      elem_cc.release_variables();        if        ( @@ -1049,11 +892,12 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor           ).is_set()        )        { -         result.addAll +         result.add           (              BinarySearch.generate              ( -               anonymous_variables, +               compiler.anonymous_variables(), +               compiler.assembler(),                 new ValueOf(elem),                 value_of_collection_size,                 collection, @@ -1064,11 +908,12 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor        }        else        { -         result.addAll +         result.add           (              IterativeSearch.generate              ( -               anonymous_variables, +               compiler.anonymous_variables(), +               compiler.assembler(),                 new ValueOf(elem),                 value_of_collection_size,                 collection, @@ -1078,29 +923,31 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor           );        } -      anonymous_variables.release(elem); +      compiler.anonymous_variables().release(elem);        result.add        ( -         new IfElseInstruction +         If.generate           ( +            compiler.anonymous_variables(), +            compiler.assembler(),              new ValueOf(found),              RemoveAt.generate              ( -               anonymous_variables, +               compiler.anonymous_variables(), +               compiler.assembler(),                 index,                 value_of_collection_size,                 collection -            ), -            Collections.singletonList(new NOP()) +            )           )        ); -      anonymous_variables.release(index); -      anonymous_variables.release(found); -      anonymous_variables.release(collection_size); +      compiler.anonymous_variables().release(index); +      compiler.anonymous_variables().release(found); +      compiler.anonymous_variables().release(collection_size); -      collection_cc.free_anonymous_variables(); +      collection_cc.release_variables();     }     @Override @@ -1111,10 +958,18 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor     throws Throwable     {        /* -       * Fate: (sequence_call <string>) -       * Wyrd: (sequence_call <string>) +       * Fate: (sequence_call string) +       * Wyrd: (set_pc <label string>)         */ -      result.add(new SequenceCall(n.get_sequence_name())); +      compiler.assembler().add_fixed_name_label(n.get_sequence_name()); + +      result.add +      ( +         new SetPC +         ( +            compiler.assembler().get_label_constant(n.get_sequence_name()) +         ) +      );     }     @Override @@ -1130,21 +985,21 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor         */        final ComputationCompiler value_cc, ref_cc; -      value_cc = new_computation_compiler(false); -      ref_cc = new_computation_compiler(true); +      value_cc = new ComputationCompiler(compiler, false); +      ref_cc = new ComputationCompiler(compiler, true);        n.get_value().get_visited_by(value_cc); -      result.addAll(value_cc.get_pre_instructions()); +      result.add(value_cc.get_init());        n.get_reference().get_visited_by(ref_cc); -      result.addAll(ref_cc.get_pre_instructions()); +      result.add(ref_cc.get_init());        result.add        (           new SetValue(ref_cc.get_ref(), value_cc.get_computation())        ); -      value_cc.free_anonymous_variables(); -      ref_cc.free_anonymous_variables(); +      value_cc.release_variables(); +      ref_cc.release_variables();     }  } 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 29256e0..7cb839a 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 @@ -24,7 +24,7 @@ public class MacroManager     public void push     ( -      tonkadur.fate.v1.lang.Macro macro, +      final tonkadur.fate.v1.lang.Macro macro,        final List<Ref> parameter_refs     )     { diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/SequenceCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/SequenceCompiler.java index 242ec7e..8e12966 100644 --- a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/SequenceCompiler.java +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/SequenceCompiler.java @@ -2,10 +2,6 @@ package tonkadur.wyrd.v1.compiler.fate.v1;  import java.util.List; -import tonkadur.error.Error; - -import tonkadur.wyrd.v1.lang.Sequence; -  import tonkadur.wyrd.v1.lang.meta.Instruction;  import tonkadur.wyrd.v1.lang.World; @@ -15,38 +11,62 @@ public class SequenceCompiler     /* Utility Class */     private SequenceCompiler () { } -   public static Sequence compile +   public static void compile     ( -      tonkadur.fate.v1.lang.Sequence fate_sequence, -      final World wyrd_world +      final Compiler compiler, +      final tonkadur.fate.v1.lang.Sequence fate_sequence     ) -   throws Error +   throws Throwable     { -      Sequence result; +      final InstructionCompiler ic; -      result = wyrd_world.get_sequence(fate_sequence.get_name()); +      ic = new InstructionCompiler(compiler); -      if (result != null) -      { -         return result; -      } +      compiler.assembler().add_fixed_name_label(fate_sequence.get_name()); -      /* TODO */ -      result = null; +      fate_sequence.get_root().get_visited_by(ic); -      wyrd_world.add_sequence(result); +      compiler.world().add_sequence_label +      ( +         fate_sequence.get_name(), +         (compiler.world().get_current_line() + 1) +      ); -      return result; +      compiler.assembler().handle_adding_instruction +      ( +         compiler.assembler().mark +         ( +            fate_sequence.get_name(), +            ic.get_result() +         ), +         compiler.world() +      );     } -   public static List<Instruction> compile_main_sequence +   public static void compile_main_sequence     ( -      List<tonkadur.fate.v1.lang.meta.Instruction> fate_instruction_list, -      final World wyrd_world +      final Compiler compiler, +      final List<tonkadur.fate.v1.lang.meta.Instruction> fate_instruction_list     ) -   throws Error +   throws Throwable     { -      /* TODO */ -      return null; +      final InstructionCompiler ic; + +      ic = new InstructionCompiler(compiler); + +      for +      ( +         final tonkadur.fate.v1.lang.meta.Instruction fate_instruction: +            fate_instruction_list +      ) +      { +         fate_instruction.get_visited_by(ic); +      } + +      compiler.assembler().handle_adding_instruction +      ( +         ic.get_result(), +         compiler.world() +      );     }  } diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/TypeCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/TypeCompiler.java index e51e581..216b578 100644 --- a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/TypeCompiler.java +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/TypeCompiler.java @@ -17,8 +17,8 @@ public class TypeCompiler     public static Type compile     ( -      tonkadur.fate.v1.lang.type.Type fate_type, -      final World wyrd_world +      final Compiler compiler, +      tonkadur.fate.v1.lang.type.Type fate_type     )     throws Error     { @@ -27,8 +27,8 @@ public class TypeCompiler           return              compile_dict_type              ( -               (tonkadur.fate.v1.lang.type.DictType) fate_type, -               wyrd_world +               compiler, +               (tonkadur.fate.v1.lang.type.DictType) fate_type              );        } @@ -37,8 +37,8 @@ public class TypeCompiler           return              compile_collection_type              ( -               (tonkadur.fate.v1.lang.type.CollectionType) fate_type, -               wyrd_world +               compiler, +               (tonkadur.fate.v1.lang.type.CollectionType) fate_type              );        } @@ -80,15 +80,15 @@ public class TypeCompiler     protected static Type compile_dict_type     ( -      final tonkadur.fate.v1.lang.type.DictType fate_dict_type, -      final World wyrd_world +      final Compiler compiler, +      final tonkadur.fate.v1.lang.type.DictType fate_dict_type     )     throws Error     {        DictType result;        final Map<String, Type> fields; -      result = wyrd_world.get_dict_type(fate_dict_type.get_name()); +      result = compiler.world().get_dict_type(fate_dict_type.get_name());        if (result != null)        { @@ -103,20 +103,24 @@ public class TypeCompiler              fate_dict_type.get_fields()        )        { -         fields.put(field.getKey(), compile(field.getValue(), wyrd_world)); +         fields.put +         ( +            field.getKey(), +            compile(compiler, field.getValue()) +         );        }        result = new DictType(fate_dict_type.get_name(), fields); -      wyrd_world.add_dict_type(result); +      compiler.world().add_dict_type(result);        return result;     }     protected static Type compile_collection_type     ( -      final tonkadur.fate.v1.lang.type.CollectionType fate_collection_type, -      final World wyrd_world +      final Compiler compiler, +      final tonkadur.fate.v1.lang.type.CollectionType fate_collection_type     )     throws Error     { diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/VariableCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/VariableCompiler.java index 832827f..b24c78b 100644 --- a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/VariableCompiler.java +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/VariableCompiler.java @@ -11,32 +11,21 @@ public class VariableCompiler     /* Utility Class */     private VariableCompiler () { } -   public static Variable compile +   public static void compile     ( -      tonkadur.fate.v1.lang.Variable fate_variable, -      final World wyrd_world +      final Compiler compiler, +      final tonkadur.fate.v1.lang.Variable fate_variable     )     throws Error     { -      Variable result; - -      result = wyrd_world.get_variable(fate_variable.get_name()); - -      if (result != null) -      { -         return result; -      } - -      result = +      compiler.world().add_variable +      (           new Variable           (              fate_variable.get_name(),              fate_variable.get_scope().toString(), -            TypeCompiler.compile(fate_variable.get_type(), wyrd_world) -         ); - -      wyrd_world.add_variable(result); - -      return result; +            TypeCompiler.compile(compiler, fate_variable.get_type()) +         ) +      );     }  } diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/util/BinarySearch.java b/src/core/src/tonkadur/wyrd/v1/compiler/util/BinarySearch.java index 6857e58..57580e7 100644 --- a/src/core/src/tonkadur/wyrd/v1/compiler/util/BinarySearch.java +++ b/src/core/src/tonkadur/wyrd/v1/compiler/util/BinarySearch.java @@ -17,9 +17,7 @@ import tonkadur.wyrd.v1.lang.computation.RelativeRef;  import tonkadur.wyrd.v1.lang.computation.Size;  import tonkadur.wyrd.v1.lang.computation.ValueOf; -import tonkadur.wyrd.v1.lang.instruction.IfElseInstruction;  import tonkadur.wyrd.v1.lang.instruction.SetValue; -import tonkadur.wyrd.v1.lang.instruction.While;  public class BinarySearch  { @@ -42,7 +40,9 @@ public class BinarySearch      * (set .bot 0)      * (set .top (- collection_size 1))      * -    * (while (and (not (var result_found)) (<= (var .bot) (var .top))) +    * <while +    *    (and (not (var result_found)) (<= (var .bot) (var .top))) +    *      *    (set result_index      *       (+      *          (var .bot) @@ -57,18 +57,25 @@ public class BinarySearch      *       )      *    )      *    (set .midval (var collection[.result_index])) -    *    (ifelse (< (var .midval) element) +    *    <ifelse +    *       (< (var .midval) element) +    *      *       (set .bot (+ (var result_index) 1)) -    *       (ifelse (> (var .midval) element) +    * +    *       <ifelse +    *          (> (var .midval) element) +    *      *          (set .top (- (var result_index) 1)) +    *      *          (set result_found true) -    *       ) -    *    ) +    *       > +    *    >      * )      */ -   public static List<Instruction> generate +   public static Instruction generate     (        final AnonymousVariableManager anonymous_variables, +      final InstructionManager assembler,        final Computation target,        final Computation collection_size,        final Ref collection, @@ -174,67 +181,67 @@ public class BinarySearch        );        /* -       *    (ifelse (< (var .midval) element) +       *    <ifelse +       *       (< (var .midval) element) +       *         *       (set .bot (+ (var result_index) 1)) -       *       (ifelse (> (var .midval) element) +       * +       *       <ifelse +       *          (> (var .midval) element) +       *         *          (set .top (- (var result_index) 1)) +       *         *          (set result_found true) -       *       ) -       *    ) +       *       > +       *    >         */        while_body.add        ( -         new IfElseInstruction +         IfElse.generate           ( +            anonymous_variables, +            assembler,              Operation.less_than(value_of_midval, target), -            Collections.singletonList +            new SetValue              ( -               new SetValue +               bot, +               Operation.plus                 ( -                  bot, -                  Operation.plus -                  ( -                     value_of_result_index, -                     new Constant(Type.INT, "1") -                  ) +                  value_of_result_index, +                  new Constant(Type.INT, "1")                 )              ), -            Collections.singletonList +            IfElse.generate              ( -               new IfElseInstruction +               anonymous_variables, +               assembler, +               Operation.greater_than(value_of_midval, target), +               new SetValue                 ( -                  Operation.greater_than(value_of_midval, target), -                  Collections.singletonList +                  top, +                  Operation.minus                    ( -                     new SetValue -                     ( -                        top, -                        Operation.minus -                        ( -                           value_of_result_index, -                           new Constant(Type.INT, "1") -                        ) -                     ) -                  ), -                  Collections.singletonList -                  ( -                     new SetValue(result_was_found, Constant.TRUE) +                     value_of_result_index, +                     new Constant(Type.INT, "1")                    ) -               ) +               ), +               new SetValue(result_was_found, Constant.TRUE)              )           )        );        result.add        ( -         new While +         While.generate           ( +            anonymous_variables, +            assembler,              Operation.and              (                 Operation.not(new ValueOf(result_was_found)),                 Operation.less_equal_than(value_of_bot, value_of_top)              ), -            while_body +            assembler.merge(while_body)           )        ); @@ -242,6 +249,6 @@ public class BinarySearch        anonymous_variables.release(top);        anonymous_variables.release(midval); -      return result; +      return assembler.merge(result);     }  } diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/util/Clear.java b/src/core/src/tonkadur/wyrd/v1/compiler/util/Clear.java new file mode 100644 index 0000000..17965ae --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/util/Clear.java @@ -0,0 +1,113 @@ +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; +import tonkadur.wyrd.v1.lang.instruction.Remove; + +public class Clear +{ +   /* Utility Class */ +   private Clear () {} + +   /* +    * (Computation int collection_size) +    * (declare_variable global <List E> collection) +    * +    * (declare_variable int .iterator) +    * +    * (set .iterator collection_size) +    * +    * (while (> (var .iterator) 0) +    *    (set .iterator (- (val .iterator) 1)) +    *    (remove collection[.iterator]) +    * ) +    */ +   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 iterator; +      final Computation value_of_iterator; + +      result = new ArrayList<Instruction>(); +      while_body = new ArrayList<Instruction>(); + +      element_type = +         ((MapType) collection.get_target_type()).get_member_type(); + +      iterator = anonymous_variables.reserve(Type.INT); + +      value_of_iterator = new ValueOf(iterator); + +      /* (set .iterator collection_size) */ +      result.add(new SetValue(iterator, collection_size)); + +      /* (set .iterator (- (val .iterator) 1)) */ +      while_body.add +      ( +         new SetValue +         ( +            iterator, +            Operation.minus(value_of_iterator, new Constant(Type.INT, "1")) +         ) +      ); + +      /* (remove collection[.iterator]) */ +      while_body.add +      ( +         new Remove +         ( +            new RelativeRef +            ( +               collection, +               Collections.singletonList +               ( +                  new Cast(value_of_iterator, Type.STRING) +               ), +               element_type +            ) +         ) +      ); + +      result.add +      ( +         While.generate +         ( +            anonymous_variables, +            assembler, +            Operation.greater_than +            ( +               value_of_iterator, +               new Constant(Type.INT, "0") +            ), +            assembler.merge(while_body) +         ) +      ); + +      anonymous_variables.release(iterator); + +      return assembler.merge(result); +   } +} diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/util/If.java b/src/core/src/tonkadur/wyrd/v1/compiler/util/If.java new file mode 100644 index 0000000..9f20e30 --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/util/If.java @@ -0,0 +1,67 @@ +package tonkadur.wyrd.v1.compiler.util; + +import java.util.List; +import java.util.ArrayList; + +import tonkadur.wyrd.v1.lang.meta.Computation; +import tonkadur.wyrd.v1.lang.meta.Instruction; + +import tonkadur.wyrd.v1.lang.computation.IfElseComputation; + +import tonkadur.wyrd.v1.lang.instruction.SetPC; + +public class If +{ +   /* +    * Computation boolean condition +    * Instruction if_true +    * +    * (set .pc (ifelse condition (label if_true_label) (label end_if_label))) +    * (mark_after end_if_label (mark if_true_label instruction)) +    * +    */ +   public static Instruction generate +   ( +      final AnonymousVariableManager anonymous_variables, +      final InstructionManager assembler, +      final Computation condition, +      final Instruction if_true +   ) +   { +      final String if_true_label, end_label; +      final List<Instruction> result; + +      if_true_label = assembler.generate_label("<if#if_true_label>"); +      end_label = assembler.generate_label("<if#end_label>"); + +      result = new ArrayList<Instruction>(); + +      result.add +      ( +         new SetPC +         ( +            new IfElseComputation +            ( +               condition, +               assembler.get_label_constant(if_true_label), +               assembler.get_label_constant(end_label) +            ) +         ) +      ); + +      result.add +      ( +         assembler.mark_after +         ( +            assembler.mark +            ( +               if_true_label, +               if_true +            ), +            end_label +         ) +      ); + +      return assembler.merge(result); +   } +} diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/util/IfElse.java b/src/core/src/tonkadur/wyrd/v1/compiler/util/IfElse.java new file mode 100644 index 0000000..b52bffc --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/util/IfElse.java @@ -0,0 +1,78 @@ +package tonkadur.wyrd.v1.compiler.util; + +import java.util.List; +import java.util.ArrayList; + +import tonkadur.wyrd.v1.lang.meta.Computation; +import tonkadur.wyrd.v1.lang.meta.Instruction; + +import tonkadur.wyrd.v1.lang.computation.IfElseComputation; + +import tonkadur.wyrd.v1.lang.instruction.SetPC; + +public class IfElse +{ +   /* +    * Computation boolean condition +    * Instruction if_true +    * Instruction if_false +    * +    * (set .pc (ifelse condition (label if_true_label) (label if_false_label))) +    * (mark if_true_label if_true) +    * (mark_after if_false_label (set .pc (label end_label))) +    * (mark_after end_label if_false) +    * +    */ +   public static Instruction generate +   ( +      final AnonymousVariableManager anonymous_variables, +      final InstructionManager assembler, +      final Computation condition, +      final Instruction if_true, +      final Instruction if_false +   ) +   { +      final String if_true_label, if_false_label, end_label; +      final List<Instruction> result; + +      if_true_label = assembler.generate_label("<ifelse#if_true_label>"); +      if_false_label = assembler.generate_label("<ifelse#if_false_label>"); +      end_label = assembler.generate_label("<ifelse#end_label>"); + +      result = new ArrayList<Instruction>(); + +      result.add +      ( +         new SetPC +         ( +            new IfElseComputation +            ( +               condition, +               assembler.get_label_constant(if_true_label), +               assembler.get_label_constant(if_false_label) +            ) +         ) +      ); + +      result.add(assembler.mark(if_true_label, if_true)); +      result.add +      ( +         assembler.mark_after +         ( +            new SetPC(assembler.get_label_constant(end_label)), +            if_false_label +         ) +      ); +      result.add +      ( +         assembler.mark_after +         ( +            new SetPC(assembler.get_label_constant(end_label)), +            if_false_label +         ) +      ); +      result.add(assembler.mark_after(if_false, end_label)); + +      return assembler.merge(result); +   } +} diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/util/InsertAt.java b/src/core/src/tonkadur/wyrd/v1/compiler/util/InsertAt.java index 46369e2..dba37dc 100644 --- a/src/core/src/tonkadur/wyrd/v1/compiler/util/InsertAt.java +++ b/src/core/src/tonkadur/wyrd/v1/compiler/util/InsertAt.java @@ -16,9 +16,7 @@ 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.IfElseInstruction;  import tonkadur.wyrd.v1.lang.instruction.SetValue; -import tonkadur.wyrd.v1.lang.instruction.While;  public class InsertAt  { @@ -36,17 +34,20 @@ public class InsertAt      *      * (set .end collection_size)      * -    * (while (> .index .end) +    * <while +    *    (> .index .end) +    *      *    (set .prev (- (val .collection_size) 1))      *    (set collection[.end] (val collection[.prev]))      *    (set .end (val .prev)) -    * ) +    * >      *      * (set collection[.index] element)     */ -   public static List<Instruction> generate +   public static Instruction generate     (        final AnonymousVariableManager anonymous_variables, +      final InstructionManager assembler,        final Ref index,        final Computation element,        final Computation collection_size, @@ -124,7 +125,13 @@ public class InsertAt         */        result.add        ( -         new While(Operation.minus(value_of_index, value_of_end), while_body) +         While.generate +         ( +            anonymous_variables, +            assembler, +            Operation.minus(value_of_index, value_of_end), +            assembler.merge(while_body) +         )        );        /* (set collection[.index] element) */ @@ -148,6 +155,6 @@ public class InsertAt        anonymous_variables.release(end);        anonymous_variables.release(prev); -      return result; +      return assembler.merge(result);     }  } diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/util/InstructionManager.java b/src/core/src/tonkadur/wyrd/v1/compiler/util/InstructionManager.java new file mode 100644 index 0000000..b3ea40f --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/util/InstructionManager.java @@ -0,0 +1,255 @@ +package tonkadur.wyrd.v1.compiler.util; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import tonkadur.wyrd.v1.lang.World; + +import tonkadur.wyrd.v1.lang.computation.Constant; + +import tonkadur.wyrd.v1.lang.meta.Instruction; + +import tonkadur.wyrd.v1.lang.type.Type; + +public class InstructionManager +{ +   protected Map<String, Integer> label_locations; +   protected Map<String, List<Label>> unresolved_labels; +   protected int generated_labels; + +   public InstructionManager () +   { +      label_locations = new HashMap<String, Integer>(); +      unresolved_labels = new HashMap<String, List<Label>>(); +      generated_labels = 0; +   } + +   public void add_fixed_name_label (final String name) +   { +      if (!unresolved_labels.containsKey(name)) +      { +         unresolved_labels.put(name, new ArrayList<Label>()); +      } +   } + +   public String generate_label () +   { +      final String result; + +      result = "(label " + (generated_labels++) + ")"; + +      unresolved_labels.put(result, new ArrayList<Label>()); + +      return result; +   } + +   public String generate_label (final String extra) +   { +      final String result; + +      result = "(label " + (generated_labels++) + ": " + extra + ")"; + +      unresolved_labels.put(result, new ArrayList<Label>()); + +      return result; +   } + +   public Constant get_label_constant (final String name) +   { +      final Integer location; + +      location = label_locations.get(name); + +      if (location == null) +      { +         final Label result; + +         result = new Label(name); + +         unresolved_labels.get(name).add(result); + +         return result; +      } +      else +      { +         return new Constant(Type.INT, Integer.toString(location)); +      } +   } + +   public List<String> get_unresolved_labels () +   { +      final ArrayList<String> result; + +      result = new ArrayList<String>(); + +      for (final Map.Entry<String, Integer> entry: label_locations.entrySet()) +      { +         if (entry.getValue() == null) +         { +            result.add(entry.getKey()); +         } +      } + +      return result; +   } + +   public void handle_adding_instruction +   ( +      final Instruction i, +      final World wyrd_world +   ) +   { +      if (i instanceof InstructionList) +      { +         final List<Instruction> instructions; + +         instructions = ((InstructionList) i).get_list(); + +         for (final Instruction instruction: instructions) +         { +            handle_adding_instruction(instruction, wyrd_world); +         } +      } +      else if (i instanceof MarkLabel) +      { +         final MarkLabel mark_label; + +         mark_label = (MarkLabel) i; + +         if (mark_label.should_mark_after()) +         { +            handle_adding_instruction(mark_label.get_instruction(), wyrd_world); + +            register_label +            ( +               mark_label.get_label_name(), +               wyrd_world.get_current_line() +            ); +         } +         else +         { +            register_label +            ( +               mark_label.get_label_name(), +               wyrd_world.get_current_line() +            ); + +            handle_adding_instruction(mark_label.get_instruction(), wyrd_world); +         } +      } +      else +      { +         wyrd_world.add_instruction(i); +      } +   } + +   public Instruction mark (final String name, final Instruction i) +   { +      return new MarkLabel(name, false, i); +   } + +   public Instruction mark_after (final String name, final Instruction i) +   { +      return new MarkLabel(name, true, i); +   } + +   public Instruction mark_after (final Instruction i, final String name) +   { +      return new MarkLabel(name, true, i); +   } + +   public Instruction merge (final List<Instruction> instructions) +   { +      if (instructions.size() == 0) +      { +         /* Important in case of label on InstructionList */ +         return NOP.generate(this); +      } + +      return new InstructionList(instructions); +   } + +   protected void register_label (final String name, final Integer location) +   { +      label_locations.put(name, location); + +      for (final Label label: unresolved_labels.get(name)) +      { +         label.resolve_to(location); +      } + +      unresolved_labels.remove(name); +   } + +   protected static class InstructionList extends Instruction +   { +      protected final List<Instruction> content; + +      public InstructionList (final List<Instruction> content) +      { +         this.content = content; +      } + +      public List<Instruction> get_list () +      { +         return content; +      } +   } + +   protected static class MarkLabel extends Instruction +   { +      protected final String label_name; +      protected final boolean should_mark_after; +      protected final Instruction instruction; + +      public MarkLabel +      ( +         final String label_name, +         final boolean should_mark_after, +         final Instruction instruction +      ) +      { +         this.label_name = label_name; +         this.should_mark_after = should_mark_after; +         this.instruction = instruction; +      } + +      public String get_label_name () +      { +         return label_name; +      } + +      public boolean should_mark_after () +      { +         return should_mark_after; +      } + +      public Instruction get_instruction () +      { +         return instruction; +      } +   } + +   protected static class Label extends Constant +   { +      protected String changeable_value; + +      public Label (final String name) +      { +         super(Type.INT, name); +      } + +      @Override +      public String get_as_string () +      { +         return changeable_value; +      } + +      public void resolve_to (final Integer line) +      { +         changeable_value = line.toString(); +      } +   } +} diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/util/IterativeSearch.java b/src/core/src/tonkadur/wyrd/v1/compiler/util/IterativeSearch.java index 5ce409d..1d4c471 100644 --- a/src/core/src/tonkadur/wyrd/v1/compiler/util/IterativeSearch.java +++ b/src/core/src/tonkadur/wyrd/v1/compiler/util/IterativeSearch.java @@ -16,9 +16,7 @@ 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.IfElseInstruction;  import tonkadur.wyrd.v1.lang.instruction.SetValue; -import tonkadur.wyrd.v1.lang.instruction.While;  public class IterativeSearch  { @@ -36,20 +34,25 @@ public class IterativeSearch      * (set result_found false)      * (set result_index (- collection_size 1))      * -    * (while +    * <while      *    (and      *       (not (var result_found))      *       (>= (var result_index) 0)      *    ) -    *    (ifelse (= (var collection[result_index]) target) +    * +    *    <ifelse +    *       (= (var collection[result_index]) target) +    *      *       (set result_found true) +    *      *       (set result_index (- (var result_index) 1)) -    *    ) -    * ) +    *    > +    * >      */ -   public static List<Instruction> generate +   public static Instruction generate     (        final AnonymousVariableManager anonymous_variables, +      final InstructionManager assembler,        final Computation target,        final Computation collection_size,        final Ref collection, @@ -79,8 +82,10 @@ public class IterativeSearch        result.add        ( -         new While +         While.generate           ( +            anonymous_variables, +            assembler,              Operation.and              (                 Operation.not(new ValueOf(result_was_found)), @@ -90,53 +95,49 @@ public class IterativeSearch                    new Constant(Type.INT, "0")                 )              ), -            Collections.singletonList +            /* +             * <ifelse +             *    (= (var collection[result_index]) target) +             * +             *    (set result_found true) +             * +             *    (set result_index (- (var result_index) 1)) +             * > +             */ +            IfElse.generate              ( -               /* -                * (ifelse (= (var collection[result_index]) target) -                *    (set result_found true) -                *    (set result_index (- (var result_index) 1)) -                * ) -                */ -               new IfElseInstruction +               anonymous_variables, +               assembler, +               Operation.equals                 ( -                  Operation.equals +                  new ValueOf                    ( -                     new ValueOf +                     new RelativeRef                       ( -                        new RelativeRef +                        collection, +                        Collections.singletonList                          ( -                           collection, -                           Collections.singletonList -                           ( -                              new Cast(value_of_result_index, Type.STRING) -                           ), -                           target_type -                        ) -                     ), -                     target -                  ), -                  Collections.singletonList -                  ( -                     new SetValue(result_was_found, Constant.TRUE) +                           new Cast(value_of_result_index, Type.STRING) +                        ), +                        target_type +                     )                    ), -                  Collections.singletonList +                  target +               ), +               new SetValue(result_was_found, Constant.TRUE), +               new SetValue +               ( +                  result_index, +                  Operation.minus                    ( -                     new SetValue -                     ( -                        result_index, -                        Operation.minus -                        ( -                           value_of_result_index, -                           new Constant(Type.INT, "1") -                        ) -                     ) +                     value_of_result_index, +                     new Constant(Type.INT, "1")                    )                 )              )           )        ); -      return result; +      return assembler.merge(result);     }  } diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/util/NOP.java b/src/core/src/tonkadur/wyrd/v1/compiler/util/NOP.java new file mode 100644 index 0000000..6f26506 --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/util/NOP.java @@ -0,0 +1,37 @@ +package tonkadur.wyrd.v1.compiler.util; + +import tonkadur.wyrd.v1.lang.meta.Instruction; + +import tonkadur.wyrd.v1.lang.instruction.SetPC; + +public class NOP +{ +   /* +    * (mark_after (set .pc (label nop_label)) nop_label) +    */ +   public static Instruction generate +   ( +      final AnonymousVariableManager anonymous_variables, +      final InstructionManager assembler +   ) +   { +      return generate(assembler); +   } + +   public static Instruction generate +   ( +      final InstructionManager assembler +   ) +   { +      final String nop_label; + +      nop_label = assembler.generate_label("<nop_label>"); + +      return +         assembler.mark_after +         ( +            new SetPC(assembler.get_label_constant(nop_label)), +            nop_label +         ); +   } +} diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/util/RemoveAllOf.java b/src/core/src/tonkadur/wyrd/v1/compiler/util/RemoveAllOf.java index 78d81ad..cba50b5 100644 --- a/src/core/src/tonkadur/wyrd/v1/compiler/util/RemoveAllOf.java +++ b/src/core/src/tonkadur/wyrd/v1/compiler/util/RemoveAllOf.java @@ -16,11 +16,8 @@ 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.IfElseInstruction; -import tonkadur.wyrd.v1.lang.instruction.NOP;  import tonkadur.wyrd.v1.lang.instruction.Remove;  import tonkadur.wyrd.v1.lang.instruction.SetValue; -import tonkadur.wyrd.v1.lang.instruction.While;  public class RemoveAllOf  { @@ -40,27 +37,19 @@ public class RemoveAllOf      * (set .found 0)      * (set .end (- collection_size 1))      * -    * (while (< (var .index) (var .end)) +    * <while (< (var .index) (var .end))      *    ;; while_body0 -    *    (ifelse (= (var .found) 0) -    *       ( -    *          (nop) -    *       ) -    *       ( -    *          (ifelse (= element (var collection[.index])) -    *             ( +    *    <if (> (var .found) 0) +    *          <if (= element (var collection[.index]))      *                ;; if_false_true_body      *                (set .found (+ (var .found) 1))      *                (set .end (- (var .end) 1)) -    *             ) -    *             (nop) -    *          ) +    *          >      *          (set      *             collection[.index]      *             (var collection[(+ (var .index) (var .found))])      *          ) -    *       ) -    *    ) +    *    >      *    (set index ((val .index) + 1))      * )      * @@ -70,9 +59,10 @@ public class RemoveAllOf      *    (set .found (- (var .found) 1))      * )      */ -   public static List<Instruction> generate +   public static Instruction generate     (        final AnonymousVariableManager anonymous_variables, +      final InstructionManager assembler,        final Computation element,        final Computation collection_size,        final Ref collection @@ -81,11 +71,10 @@ public class RemoveAllOf        final List<Instruction> result;        final List<Instruction> while_body0, while_body1, if_false_body;        final List<Instruction> if_false_true_body; -      final List<Instruction> nop_branch;        final Type element_type;        final Ref index, found, end;        final Computation value_of_index, value_of_found, value_of_end; -      final Computation const_0, const_1; +      final Computation const_0, const_1, value_of_found_greater_than_0;        final Ref collection_at_index;        result = new ArrayList<Instruction>(); @@ -95,8 +84,6 @@ public class RemoveAllOf        if_false_body = new ArrayList<Instruction>();        if_false_true_body = new ArrayList<Instruction>(); -      nop_branch = Collections.singletonList(new NOP()); -        element_type = element.get_type();        index = anonymous_variables.reserve(Type.INT); @@ -110,6 +97,9 @@ public class RemoveAllOf        const_0 = new Constant(Type.INT, "0");        const_1 = new Constant(Type.INT, "1"); +      value_of_found_greater_than_0 = +         Operation.greater_than(value_of_found, const_0); +        collection_at_index =           new RelativeRef           ( @@ -149,11 +139,12 @@ public class RemoveAllOf        if_false_body.add        ( -         new IfElseInstruction +         If.generate           ( +            anonymous_variables, +            assembler,              Operation.equals(element, new ValueOf(collection_at_index)), -            if_false_true_body, -            nop_branch +            assembler.merge(if_false_true_body)           )        ); @@ -186,11 +177,12 @@ public class RemoveAllOf        while_body0.add        ( -         new IfElseInstruction +         If.generate           ( -            Operation.equals(value_of_found, const_0), -            nop_branch, -            if_false_body +            anonymous_variables, +            assembler, +            value_of_found_greater_than_0, +            assembler.merge(if_false_body)           )        ); @@ -201,10 +193,12 @@ public class RemoveAllOf        result.add        ( -         new While +         While.generate           ( +            anonymous_variables, +            assembler,              Operation.less_than(value_of_index, value_of_end), -            while_body0 +            assembler.merge(while_body0)           )        ); @@ -234,10 +228,12 @@ public class RemoveAllOf        result.add        ( -         new While +         While.generate           ( -            Operation.less_than(value_of_found, const_0), -            while_body1 +            anonymous_variables, +            assembler, +            value_of_found_greater_than_0, +            assembler.merge(while_body1)           )        ); @@ -245,6 +241,6 @@ public class RemoveAllOf        anonymous_variables.release(found);        anonymous_variables.release(end); -      return result; +      return assembler.merge(result);     }  } 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 2ec46cd..be5a859 100644 --- a/src/core/src/tonkadur/wyrd/v1/compiler/util/RemoveAt.java +++ b/src/core/src/tonkadur/wyrd/v1/compiler/util/RemoveAt.java @@ -17,9 +17,7 @@ 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.IfElseInstruction;  import tonkadur.wyrd.v1.lang.instruction.SetValue; -import tonkadur.wyrd.v1.lang.instruction.While;  import tonkadur.wyrd.v1.lang.instruction.Remove;  public class RemoveAt @@ -45,9 +43,10 @@ public class RemoveAt      *      * (remove collection[index])      */ -   public static List<Instruction> generate +   public static Instruction generate     (        final AnonymousVariableManager anonymous_variables, +      final InstructionManager assembler,        final Ref index,        final Computation collection_size,        final Ref collection @@ -133,10 +132,12 @@ public class RemoveAt         */        result.add        ( -         new While +         While.generate           ( +            anonymous_variables, +            assembler,              Operation.less_than(value_of_index, value_of_end), -            while_body +            assembler.merge(while_body)           )        ); @@ -146,6 +147,6 @@ public class RemoveAt        anonymous_variables.release(end);        anonymous_variables.release(next); -      return result; +      return assembler.merge(result);     }  } diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/util/While.java b/src/core/src/tonkadur/wyrd/v1/compiler/util/While.java new file mode 100644 index 0000000..1101a4c --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/util/While.java @@ -0,0 +1,71 @@ +package tonkadur.wyrd.v1.compiler.util; + +import java.util.List; +import java.util.ArrayList; + +import tonkadur.wyrd.v1.lang.meta.Computation; +import tonkadur.wyrd.v1.lang.meta.Instruction; + +import tonkadur.wyrd.v1.lang.computation.IfElseComputation; + +import tonkadur.wyrd.v1.lang.instruction.SetPC; + +public class While +{ +   /* +    * Computation boolean condition +    * Instruction while_body +    * +    * (mark start_label +    *    (set .pc (ifelse condition (label if_true_label) (label end_label))) +    * ) +    * (mark if_true_label while_body) +    * (mark_after end_label (set .pc (label start_label))) +    */ +   public static Instruction generate +   ( +      final AnonymousVariableManager anonymous_variables, +      final InstructionManager assembler, +      final Computation condition, +      final Instruction while_body +   ) +   { +      final String start_label, if_true_label, end_label; +      final List<Instruction> result; + +      start_label = assembler.generate_label("<while#start_label>"); +      if_true_label = assembler.generate_label("<while#if_true_label>"); +      end_label = assembler.generate_label("<while#end_label>"); + +      result = new ArrayList<Instruction>(); + +      result.add +      ( +         assembler.mark +         ( +            start_label, +            new SetPC +            ( +               new IfElseComputation +               ( +                  condition, +                  assembler.get_label_constant(if_true_label), +                  assembler.get_label_constant(end_label) +               ) +            ) +         ) +      ); + +      result.add(assembler.mark(if_true_label, while_body)); +      result.add +      ( +         assembler.mark_after +         ( +            new SetPC(assembler.get_label_constant(start_label)), +            end_label +         ) +      ); + +      return assembler.merge(result); +   } +} diff --git a/src/core/src/tonkadur/wyrd/v1/lang/Sequence.java b/src/core/src/tonkadur/wyrd/v1/lang/Sequence.java deleted file mode 100644 index 3cae8f9..0000000 --- a/src/core/src/tonkadur/wyrd/v1/lang/Sequence.java +++ /dev/null @@ -1,27 +0,0 @@ -package tonkadur.wyrd.v1.lang; - -import java.util.List; - -import tonkadur.wyrd.v1.lang.meta.Instruction; - -public class Sequence -{ -   protected final String name; -   protected final List<Instruction> content; - -   public Sequence (final String name, final List<Instruction> content) -   { -      this.name = name; -      this.content = content; -   } - -   public String get_name () -   { -      return name; -   } - -   public List<Instruction> get_content () -   { -      return content; -   } -} diff --git a/src/core/src/tonkadur/wyrd/v1/lang/World.java b/src/core/src/tonkadur/wyrd/v1/lang/World.java index e8e86ba..48edd73 100644 --- a/src/core/src/tonkadur/wyrd/v1/lang/World.java +++ b/src/core/src/tonkadur/wyrd/v1/lang/World.java @@ -8,7 +8,6 @@ import java.util.Map;  import java.util.Set;  import tonkadur.wyrd.v1.lang.Variable; -import tonkadur.wyrd.v1.lang.Sequence;  import tonkadur.wyrd.v1.lang.type.DictType; @@ -19,24 +18,24 @@ public class World     protected final Set<String> required_extensions;     protected final Map<String, Variable> variables; -   protected final Map<String, Sequence> sequences; +   protected final Map<String, Integer> sequence_labels;     protected final Map<String, DictType> dict_types;     /* This solves the issue of using other yet undefined dict types. */     protected final List<DictType> dict_types_in_order; -   protected final List<Instruction> global_instructions; +   protected final List<Instruction> code;     public World ()     {        required_extensions = new HashSet<String>();        variables = new HashMap<String, Variable>(); -      sequences = new HashMap<String, Sequence>(); +      sequence_labels = new HashMap<String, Integer>();        dict_types = new HashMap<String, DictType>();        dict_types_in_order = new ArrayList<DictType>(); -      global_instructions = new ArrayList<Instruction>(); +      code = new ArrayList<Instruction>();     }     public void add_required_extension (final String name) @@ -65,13 +64,18 @@ public class World        variables.put(variable.get_name(), variable);     } -   public Sequence get_sequence (final String name) +   public void add_sequence_label (final String name, final Integer line)     { -      return sequences.get(name); +      sequence_labels.put(name, line);     } -   public void add_sequence (final Sequence sequence) +   public void add_instruction (final Instruction i)     { -      sequences.put(sequence.get_name(), sequence); +      code.add(i); +   } + +   public Integer get_current_line () +   { +      return new Integer(code.size());     }  } diff --git a/src/core/src/tonkadur/wyrd/v1/lang/instruction/AddChoice.java b/src/core/src/tonkadur/wyrd/v1/lang/instruction/AddChoice.java index 2b93cab..8353b4e 100644 --- a/src/core/src/tonkadur/wyrd/v1/lang/instruction/AddChoice.java +++ b/src/core/src/tonkadur/wyrd/v1/lang/instruction/AddChoice.java @@ -1,7 +1,5 @@  package tonkadur.wyrd.v1.lang.instruction; -import java.util.List; -  import tonkadur.wyrd.v1.lang.meta.Computation;  import tonkadur.wyrd.v1.lang.meta.Instruction; @@ -11,13 +9,13 @@ public class AddChoice extends Instruction     /**** MEMBERS **************************************************************/     /***************************************************************************/     protected final Computation label; -   protected final List<Instruction> effect; +   protected final Instruction effect;     /***************************************************************************/     /**** PUBLIC ***************************************************************/     /***************************************************************************/     /**** Constructors *********************************************************/ -   public AddChoice (final Computation label, final List<Instruction> effect) +   public AddChoice (final Computation label, final Instruction effect)     {        this.label = label;        this.effect = effect; @@ -29,7 +27,7 @@ public class AddChoice extends Instruction        return label;     } -   public List<Instruction> get_effect () +   public Instruction get_effect ()     {        return effect;     } diff --git a/src/core/src/tonkadur/wyrd/v1/lang/instruction/IfElseInstruction.java b/src/core/src/tonkadur/wyrd/v1/lang/instruction/IfElseInstruction.java deleted file mode 100644 index 800eded..0000000 --- a/src/core/src/tonkadur/wyrd/v1/lang/instruction/IfElseInstruction.java +++ /dev/null @@ -1,50 +0,0 @@ -package tonkadur.wyrd.v1.lang.instruction; - -import java.util.List; - -import tonkadur.wyrd.v1.lang.type.Type; - -import tonkadur.wyrd.v1.lang.meta.Instruction; -import tonkadur.wyrd.v1.lang.meta.Computation; - -public class IfElseInstruction extends Instruction -{ -   /***************************************************************************/ -   /**** MEMBERS **************************************************************/ -   /***************************************************************************/ -   protected final Computation condition; -   protected final List<Instruction> if_true; -   protected final List<Instruction> if_false; - -   /***************************************************************************/ -   /**** PUBLIC ***************************************************************/ -   /***************************************************************************/ -   /**** Constructors *********************************************************/ -   public IfElseInstruction -   ( -      final Computation condition, -      final List<Instruction> if_true, -      final List<Instruction> if_false -   ) -   { -      this.condition = condition; -      this.if_true = if_true; -      this.if_false = if_false; -   } - -   /**** Accessors ************************************************************/ -   public Computation get_condition () -   { -      return condition; -   } - -   public List<Instruction> get_if_true () -   { -      return if_true; -   } - -   public List<Instruction> get_if_false () -   { -      return if_false; -   } -} diff --git a/src/core/src/tonkadur/wyrd/v1/lang/instruction/NOP.java b/src/core/src/tonkadur/wyrd/v1/lang/instruction/NOP.java deleted file mode 100644 index 46f7b3f..0000000 --- a/src/core/src/tonkadur/wyrd/v1/lang/instruction/NOP.java +++ /dev/null @@ -1,19 +0,0 @@ -package tonkadur.wyrd.v1.lang.instruction; - -import tonkadur.wyrd.v1.lang.meta.Instruction; - -public class NOP extends Instruction -{ -   /***************************************************************************/ -   /**** MEMBERS **************************************************************/ -   /***************************************************************************/ - -   /***************************************************************************/ -   /**** PUBLIC ***************************************************************/ -   /***************************************************************************/ -   /**** Constructors *********************************************************/ -   public NOP () -   { -   } - -} diff --git a/src/core/src/tonkadur/wyrd/v1/lang/instruction/SequenceCall.java b/src/core/src/tonkadur/wyrd/v1/lang/instruction/SetPC.java index 51a6265..1ed71c6 100644 --- a/src/core/src/tonkadur/wyrd/v1/lang/instruction/SequenceCall.java +++ b/src/core/src/tonkadur/wyrd/v1/lang/instruction/SetPC.java @@ -1,26 +1,27 @@  package tonkadur.wyrd.v1.lang.instruction;  import tonkadur.wyrd.v1.lang.meta.Instruction; +import tonkadur.wyrd.v1.lang.meta.Computation; -public class SequenceCall extends Instruction +public class SetPC extends Instruction  {     /***************************************************************************/     /**** MEMBERS **************************************************************/     /***************************************************************************/ -   protected final String name; +   protected final Computation value;     /***************************************************************************/     /**** PUBLIC ***************************************************************/     /***************************************************************************/     /**** Constructors *********************************************************/ -   public SequenceCall (final String name) +   public SetPC (final Computation value)     { -      this.name = name; +      this.value = value;     }     /**** Accessors ************************************************************/ -   public String get_name () +   public Computation get_value ()     { -      return name; +      return value;     }  } diff --git a/src/core/src/tonkadur/wyrd/v1/lang/instruction/While.java b/src/core/src/tonkadur/wyrd/v1/lang/instruction/While.java deleted file mode 100644 index baaa8ed..0000000 --- a/src/core/src/tonkadur/wyrd/v1/lang/instruction/While.java +++ /dev/null @@ -1,40 +0,0 @@ -package tonkadur.wyrd.v1.lang.instruction; - -import java.util.List; - -import tonkadur.wyrd.v1.lang.meta.Computation; -import tonkadur.wyrd.v1.lang.meta.Instruction; - -public class While extends Instruction -{ -   /***************************************************************************/ -   /**** MEMBERS **************************************************************/ -   /***************************************************************************/ -   protected final Computation condition; -   protected final List<Instruction> body; - -   /***************************************************************************/ -   /**** PUBLIC ***************************************************************/ -   /***************************************************************************/ -   /**** Constructors *********************************************************/ -   public While -   ( -      final Computation condition, -      final List<Instruction> body -   ) -   { -      this.condition = condition; -      this.body = body; -   } - -   /**** Accessors ************************************************************/ -   public Computation get_condition () -   { -      return condition; -   } - -   public List<Instruction> get_body () -   { -      return body; -   } -} | 


