| summaryrefslogtreecommitdiff | 
diff options
Diffstat (limited to 'src')
10 files changed, 841 insertions, 149 deletions
| diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/Paragraph.java b/src/core/src/tonkadur/fate/v1/lang/computation/Paragraph.java index db8e2cb..36515d1 100644 --- a/src/core/src/tonkadur/fate/v1/lang/computation/Paragraph.java +++ b/src/core/src/tonkadur/fate/v1/lang/computation/Paragraph.java @@ -24,7 +24,22 @@ public class Paragraph extends Computation     /**** PUBLIC ***************************************************************/     /***************************************************************************/     /**** Constructors *********************************************************/ -   public Paragraph +   public static Paragraph build +   ( +      final Origin origin, +      final List<Computation> content +   ) +   throws Throwable +   { +      for (final Computation c: content) +      { +         c.expect_string(); +      } + +      return new Paragraph(origin, content); +   } + +   protected Paragraph     (        final Origin origin,        final List<Computation> content diff --git a/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 b/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 index e21134a..3934170 100644 --- a/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 +++ b/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 @@ -61,7 +61,6 @@ DECLARE_LOCAL_VARIABLE_KW: L_PAREN 'local' SEP+;  DICT_KW: L_PAREN 'dict'('ionary'?) SEP+;  LIST_KW: L_PAREN 'list' SEP+; -SET_KW: L_PAREN 'set' SEP+;  ENABLE_TEXT_EFFECT_KW: L_PAREN 'text'US'effect' SEP+; @@ -150,9 +149,11 @@ CONTINUE_AS_KW:  END_KW: L_PAREN 'end'('!'?) SEP* R_PAREN; +fragment IDENTIFIER_FRAG: ~([ \t\r\n()]|'!'); +IDENTIFIER_KW: IDENTIFIER_FRAG+; -WORD: ((~([ \t\r\n()]))|'(lp)'|'(rp)'|'(sp)')+ +WORD: (IDENTIFIER_FRAG|'!'|'(lp)'|'(rp)'|'(sp)')+     {        setText        ( @@ -172,4 +173,5 @@ WORD: ((~([ \t\r\n()]))|'(lp)'|'(rp)'|'(sp)')+        );     }; +  COMMENT: WS* ';;' .*? '\n' -> channel(HIDDEN); diff --git a/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 b/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 index adcae9b..9a6fbdf 100644 --- a/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 +++ b/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 @@ -60,7 +60,7 @@ fate_file [ParserData parser]     PARSER.increase_local_variables_hierarchy();  }  : -   WS* FATE_VERSION_KW WORD WS* R_PAREN WS* +   WS* FATE_VERSION_KW word WS* R_PAREN WS*     (        (           first_level_instruction @@ -206,7 +206,7 @@ first_level_instruction        PARSER.get_world().extra_computations().add(extra_computation);     } -   | DECLARE_EXTRA_TYPE_KW identifier WS+ argc=WORD WS+ comp=WORD WS* R_PAREN +   | DECLARE_EXTRA_TYPE_KW identifier WS+ argc=word WS+ comp=word WS* R_PAREN     {        final Type new_type;        final Origin start_origin; @@ -413,24 +413,24 @@ first_level_instruction     } -   | IMP_IGNORE_ERROR_KW WORD WS+ first_level_instruction WS* R_PAREN +   | IMP_IGNORE_ERROR_KW word WS+ first_level_instruction WS* R_PAREN     {        /* TODO: temporarily disable an compiler error category */     } -   | REQUIRE_EXTENSION_KW WORD WS* R_PAREN +   | REQUIRE_EXTENSION_KW word WS* R_PAREN     { -      PARSER.get_world().add_required_extension(($WORD.text)); +      PARSER.get_world().add_required_extension(($word.result));        /* TODO: error report if extension not explicitly enabled. */     } -   | REQUIRE_KW WORD WS* R_PAREN +   | REQUIRE_KW word WS* R_PAREN     {        final String filename; -      filename = Files.resolve_filename(PARSER.get_context(), ($WORD.text)); +      filename = Files.resolve_filename(PARSER.get_context(), ($word.result));        if (!PARSER.get_world().has_loaded_file(filename))        { @@ -446,11 +446,11 @@ first_level_instruction        }     } -   | INCLUDE_KW WORD WS* R_PAREN +   | INCLUDE_KW word WS* R_PAREN     {        final String filename; -      filename = Files.resolve_filename(PARSER.get_context(), ($WORD.text)); +      filename = Files.resolve_filename(PARSER.get_context(), ($word.result));        PARSER.add_file_content        ( @@ -464,29 +464,24 @@ first_level_instruction     }     /* -   | EXTENSION_FIRST_LEVEL_KW WORD WS+ instruction_list WS* R_PAREN +   | EXTENSION_FIRST_LEVEL_KW word WS+ instruction_list WS* R_PAREN     {        final Origin origin;        final ExtensionInstruction instr; -      origin = -         CONTEXT.get_origin_at -         ( -            ($WORD.getLine()), -            ($WORD.getCharPositionInLine()) -         ); +      origin = ($word.origin);        instr =           PARSER.get_world().extension_first_level_instructions().get           ( -            ($WORD.text) +            ($word.result)           );        if (instr == null)        {           ErrorManager.handle           ( -            new UnknownExtensionContentException(origin, ($WORD.text)) +            new UnknownExtensionContentException(origin, ($word.result))           );        }        else @@ -505,7 +500,7 @@ catch [final Throwable e]  instruction  returns [Instruction result]  : -   L_PAREN WS* maybe_instruction_list WS* R_PAREN +   L_PAREN WS+ maybe_instruction_list WS* R_PAREN     {        /*         * Don't define a local variable hierachy just for that group, as it would @@ -818,7 +813,7 @@ returns [Instruction result]  /**** ERRORS ******************************************************************/  /******************************************************************************/ -   | IMP_IGNORE_ERROR_KW WORD WS+ instruction WS* R_PAREN +   | IMP_IGNORE_ERROR_KW word WS+ instruction WS* R_PAREN     {        /* TODO: temporarily disable an compiler error category */        $result = ($instruction.result); @@ -926,7 +921,7 @@ returns [Instruction result]  /******************************************************************************/  /**** GENERIC INSTRUCTIONS ****************************************************/  /******************************************************************************/ -   | L_PAREN identifier IMP_MARKER maybe_computation_list WS* R_PAREN +   | L_PAREN WORD maybe_computation_list WS* R_PAREN     {        $result =           GenericInstruction.build @@ -936,7 +931,7 @@ returns [Instruction result]                 ($L_PAREN.getLine()),                 ($L_PAREN.getCharPositionInLine())              ), -            ($identifier.result), +            ($WORD.text),              ($maybe_computation_list.result)           );     } @@ -1119,7 +1114,7 @@ returns [Instruction result]        {           PARSER.enable_restricted_variable_stack_of(pcd);        } -      L_PAREN WS* WORD maybe_computation_list WS* R_PAREN WS* +      L_PAREN WS* word maybe_computation_list WS* R_PAREN WS*        {           PARSER.disable_restricted_stack_of(pcd);           PARSER.increase_local_variables_hierarchy(); @@ -1139,7 +1134,7 @@ returns [Instruction result]              ($L_PAREN.getCharPositionInLine())           ); -      event = PARSER.get_world().events().get(origin, ($WORD.text)); +      event = PARSER.get_world().events().get(origin, ($word.result));        $result =           EventOption.build @@ -1490,7 +1485,7 @@ returns [Computation result]        // convert all computations to text.        // return text node.        $result = -         new Paragraph +         Paragraph.build           (              $computation_list.result.get(0).get_origin(),              $computation_list.result @@ -1509,26 +1504,22 @@ returns [Computation result]     final StringBuilder string_builder = new StringBuilder();  }  : -   first_word=WORD +   first_word=word     { -      string_builder.append(($first_word.text)); +      string_builder.append(($first_word.result));     }     ( -      WS+ next_word=WORD +      WS+ next_word=word        {           string_builder.append(" "); -         string_builder.append(($next_word.text)); +         string_builder.append(($next_word.result));        }     )*     {        $result =           Constant.build_string           ( -            PARSER.get_origin_at -            ( -               ($first_word.getLine()), -               ($first_word.getCharPositionInLine()) -            ), +            ($first_word.origin),              string_builder.toString()           );     } @@ -1541,18 +1532,9 @@ catch [final Throwable e]  type  returns [Type result]  : -   WORD +   word     { -      $result = -         PARSER.get_world().types().get -         ( -            PARSER.get_origin_at -            ( -               ($WORD.getLine()), -               ($WORD.getCharPositionInLine()) -            ), -            ($WORD.text) -         ); +      $result = PARSER.get_world().types().get(($word.origin), ($word.result));        if ($result.get_parameters().size() != 0)        { @@ -1956,9 +1938,9 @@ returns [List<Cons<Origin, Cons<String, Computation>>> result]        WS*        (           ( -            L_PAREN WS* WORD WS+ +            L_PAREN WS* word WS+              { -               field_name = ($WORD.text); +               field_name = ($word.result);              }           )           | @@ -1992,25 +1974,17 @@ returns [List<Cons<Origin, Cons<String, Computation>>> result]  identifier  returns [String result]  : -   WORD +   word     { -      if (($WORD.text).indexOf('.') != -1) +      if (($word.result).indexOf('.') != -1)        {           ErrorManager.handle           ( -            new IllegalReferenceNameException -            ( -               PARSER.get_origin_at -               ( -                  ($WORD.getLine()), -                  ($WORD.getCharPositionInLine()) -               ), -               ($WORD.text) -            ) +            new IllegalReferenceNameException(($word.origin), ($word.result))           );        } -      $result = ($WORD.text); +      $result = ($word.result);     }  ;  catch [final Throwable e] @@ -2029,53 +2003,25 @@ returns [Computation result]     ParserData.LocalVariables previous_local_variables_stack;  }  : -   WORD +   word     { -      if ($WORD.text.matches("[0-9]+(\\.[0-9]+)?")) +      if ($word.result.matches("(-?)[0-9]+(\\.[0-9]+)?"))        { -         $result = -            Constant.build -            ( -               PARSER.get_origin_at -               ( -                  ($WORD.getLine()), -                  ($WORD.getCharPositionInLine()) -               ), -               ($WORD.text) -            ); +         $result = Constant.build(($word.origin), ($word.result));        }        else        { -         $result = -            new AmbiguousWord -            ( -               PARSER, -               PARSER.get_origin_at -               ( -                  ($WORD.getLine()), -                  ($WORD.getCharPositionInLine()) -               ), -               ($WORD.text) -            ); +         $result = new AmbiguousWord(PARSER, ($word.origin), ($word.result));        }     } -   | VARIABLE_KW WORD WS* R_PAREN +   | VARIABLE_KW word WS* R_PAREN     {        $result = -         VariableFromWord.generate -         ( -            PARSER, -            PARSER.get_origin_at -            ( -               ($WORD.getLine()), -               ($WORD.getCharPositionInLine()) -            ), -            ($WORD.text) -         ); +         VariableFromWord.generate(PARSER, ($word.origin), ($word.result));     } -   | IGNORE_ERROR_KW WORD WS+ computation WS* R_PAREN +   | IGNORE_ERROR_KW word WS+ computation WS* R_PAREN     {        $result = ($computation.result);        /* TODO: temporarily disable an compiler error category */ @@ -2091,7 +2037,7 @@ returns [Computation result]        $result = ($sentence.result);     } -   | FIELD_ACCESS_KW WORD WS+ computation WS* R_PAREN +   | FIELD_ACCESS_KW word WS+ computation WS* R_PAREN     {        $result =           FieldAccess.build @@ -2102,7 +2048,7 @@ returns [Computation result]                 ($FIELD_ACCESS_KW.getCharPositionInLine())              ),              ($computation.result), -            ($WORD.text) +            ($word.result)           );     } @@ -2244,29 +2190,17 @@ returns [Computation result]           );     } -   | ENABLE_TEXT_EFFECT_KW WORD WS+ paragraph WS* R_PAREN +   | ENABLE_TEXT_EFFECT_KW word WS+ paragraph WS* R_PAREN     {        final TextEffect effect;        effect = -         PARSER.get_world().text_effects().get -         ( -            PARSER.get_origin_at -            ( -               ($WORD.getLine()), -               ($WORD.getCharPositionInLine()) -            ), -            ($WORD.text) -         ); +         PARSER.get_world().text_effects().get(($word.origin), ($word.result));        $result =           TextWithEffect.build           ( -            PARSER.get_origin_at -            ( -               ($WORD.getLine()), -               ($WORD.getCharPositionInLine()) -            ), +            ($word.origin),              effect,              new ArrayList<Computation>(),              ($paragraph.result) @@ -2275,7 +2209,7 @@ returns [Computation result]     | ENABLE_TEXT_EFFECT_KW        L_PAREN -         WORD WS+ +         word WS+           computation_list WS*        R_PAREN WS+        paragraph WS* @@ -2284,15 +2218,7 @@ returns [Computation result]        final TextEffect effect;        effect = -         PARSER.get_world().text_effects().get -         ( -            PARSER.get_origin_at -            ( -               ($WORD.getLine()), -               ($WORD.getCharPositionInLine()) -            ), -            ($WORD.text) -         ); +         PARSER.get_world().text_effects().get(($word.origin), ($word.result));        $result =           TextWithEffect.build @@ -2308,7 +2234,7 @@ returns [Computation result]           );     } -   | L_PAREN identifier maybe_computation_list WS* R_PAREN +   | L_PAREN IDENTIFIER_KW maybe_computation_list WS* R_PAREN     {        $result =           GenericComputation.build @@ -2318,7 +2244,7 @@ returns [Computation result]                 ($L_PAREN.getLine()),                 ($L_PAREN.getCharPositionInLine())              ), -            ($identifier.result), +            ($IDENTIFIER_KW.text),              ($maybe_computation_list.result)           );     } @@ -2446,3 +2372,27 @@ catch [final Throwable e]  {     PARSER.handle_error(e);  } + +word returns [String result, Origin origin]: +   WORD +   { +      $result = $WORD.text; +      $origin = +         PARSER.get_origin_at +         ( +            ($WORD.getLine()), +            ($WORD.getCharPositionInLine()) +         ); +   } + +   | IDENTIFIER_KW +   { +      $result = $IDENTIFIER_KW.text; +      $origin = +         PARSER.get_origin_at +         ( +            ($IDENTIFIER_KW.getLine()), +            ($IDENTIFIER_KW.getCharPositionInLine()) +         ); +   } +; diff --git a/src/core/src/tonkadur/wyrd/v1/Base.java b/src/core/src/tonkadur/wyrd/v1/Base.java index 2d3538b..739cf3a 100644 --- a/src/core/src/tonkadur/wyrd/v1/Base.java +++ b/src/core/src/tonkadur/wyrd/v1/Base.java @@ -1,9 +1,16 @@  package tonkadur.wyrd.v1; +import tonkadur.TonkadurPlugin; + +import tonkadur.wyrd.v1.compiler.fate.v1.computation.GenericComputationCompiler; +  public class Base  {     public static void initialize ()     { -      /* Nothing to do. */ +      TonkadurPlugin.register_as_loadable_superclass +      ( +         GenericComputationCompiler.class +      );     }  } 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 6319be6..7fee7f6 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 @@ -39,7 +39,7 @@ public class Compiler        );        compiler.compile_extensions(fate_world); -      compiler.compile_types(fate_world); +//      compiler.compile_types(fate_world);        compiler.compile_variables(fate_world);        compiler.compile_sequences(fate_world); @@ -80,7 +80,10 @@ public class Compiler              fate_world.types().get_all()        )        { -         TypeCompiler.compile(this, type); +         if (!type.equals(tonkadur.fate.v1.lang.type.Type.ANY)) +         { +            TypeCompiler.compile(this, type); +         }        }     } 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 1e475eb..84b0ebc 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 @@ -57,6 +57,8 @@ import tonkadur.wyrd.v1.lang.computation.*;  import tonkadur.wyrd.v1.lang.World;  import tonkadur.wyrd.v1.lang.Register; +import tonkadur.wyrd.v1.compiler.fate.v1.computation.GenericComputationCompiler; +  public class ComputationCompiler  implements tonkadur.fate.v1.lang.meta.ComputationVisitor  { @@ -1796,25 +1798,7 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor        result_as_computation = new Text(content);     } -/* -   @Override -   public void visit_address_operator -   ( -      final tonkadur.fate.v1.lang.computation.AddressOperator n -   ) -   throws Throwable -   { -      final ComputationCompiler n_cc; - -      n_cc = new ComputationCompiler(compiler); - -      n.get_target().get_visited_by(n_cc); -      assimilate(n_cc); - -      result_as_computation = n_cc.get_address(); -   } -*/     @Override     public void visit_text_with_effect     ( @@ -2065,7 +2049,15 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor     )     throws Throwable     { -      // TODO: implement. +      final ComputationCompiler handler; + +      handler = GenericComputationCompiler.handle(compiler, n); + +      assimilate(handler); + +      this.instructions_were_generated = handler.instructions_were_generated; +      this.result_as_computation = handler.result_as_computation; +      this.result_as_address = handler.result_as_address;     }     @Override diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/GenericComputationCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/GenericComputationCompiler.java index a5ae047..27e8fe4 100644 --- a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/GenericComputationCompiler.java +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/GenericComputationCompiler.java @@ -58,12 +58,26 @@ public abstract class GenericComputationCompiler extends ComputationCompiler        {           System.err.println           ( -            "[F] No Wyrd compilation process registered for generic Fate " +            "[F] No Wyrd compilation process registered for generic Fate"              + " computation \""              + computation.getClass().getName()              + "\"."           ); +         System.err.println("Registered compilers:"); + +         for (final Class c: COMPILERS.keySet()) +         { +            System.err.println +            ( +               "- " +               + COMPILERS.get(c).getName() +               + " for " +               + c.getName() +               + "." +            ); +         } +           System.exit(-1);        } diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/AddressOperatorCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/AddressOperatorCompiler.java new file mode 100644 index 0000000..a3a8e91 --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/AddressOperatorCompiler.java @@ -0,0 +1,41 @@ +package tonkadur.wyrd.v1.compiler.fate.v1.computation.generic; + +import tonkadur.fate.v1.lang.computation.generic.AddressOperator; + +import tonkadur.wyrd.v1.compiler.fate.v1.Compiler; +import tonkadur.wyrd.v1.compiler.fate.v1.ComputationCompiler; + +import tonkadur.wyrd.v1.compiler.fate.v1.computation.GenericComputationCompiler; + +public class AddressOperatorCompiler extends GenericComputationCompiler +{ +   public static Class get_target_class () +   { +      return AddressOperator.class; +   } + +   public AddressOperatorCompiler (final Compiler compiler) +   { +      super(compiler); +   } + +   public void compile +   ( +      final tonkadur.fate.v1.lang.computation.GenericComputation computation +   ) +   throws Throwable +   { +      final AddressOperator source; +      final ComputationCompiler target_cc; + +      source = (AddressOperator) computation; + +      target_cc = new ComputationCompiler(compiler); + +      source.get_target().get_visited_by(target_cc); + +      assimilate(target_cc); + +      result_as_computation = target_cc.get_address(); +   } +} diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/AtReferenceCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/AtReferenceCompiler.java new file mode 100644 index 0000000..f840dbd --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/AtReferenceCompiler.java @@ -0,0 +1,57 @@ +package tonkadur.wyrd.v1.compiler.fate.v1.computation.generic; + +import tonkadur.fate.v1.lang.computation.generic.AtReference; + +import tonkadur.wyrd.v1.lang.computation.Address; + +import tonkadur.wyrd.v1.compiler.fate.v1.Compiler; +import tonkadur.wyrd.v1.compiler.fate.v1.TypeCompiler; +import tonkadur.wyrd.v1.compiler.fate.v1.ComputationCompiler; + +import tonkadur.wyrd.v1.compiler.fate.v1.computation.GenericComputationCompiler; + + +public class AtReferenceCompiler extends GenericComputationCompiler +{ +   public static Class get_target_class () +   { +      return AtReference.class; +   } + +   public AtReferenceCompiler (final Compiler compiler) +   { +      super(compiler); +   } + +   public void compile +   ( +      final tonkadur.fate.v1.lang.computation.GenericComputation computation +   ) +   throws Throwable +   { +      final AtReference source; +      final ComputationCompiler parent_cc; + +      source = (AtReference) computation; + +      parent_cc = new ComputationCompiler(compiler); + +      source.get_parent().get_visited_by(parent_cc); + +      assimilate(parent_cc); + +      result_as_address = +         new Address +         ( +            parent_cc.get_computation(), +            TypeCompiler.compile +            ( +               compiler, +               ( +                  (tonkadur.fate.v1.lang.type.PointerType) +                     source.get_parent().get_type() +               ).get_referenced_type() +            ) +         ); +   } +} diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/OperationCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/OperationCompiler.java new file mode 100644 index 0000000..3f98979 --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/computation/generic/OperationCompiler.java @@ -0,0 +1,611 @@ +package tonkadur.wyrd.v1.compiler.fate.v1.computation.generic; + +import java.util.List; +import java.util.ArrayList; +import java.util.Iterator; + +import tonkadur.wyrd.v1.lang.Register; + +import tonkadur.wyrd.v1.lang.computation.Cast; +import tonkadur.wyrd.v1.lang.computation.Constant; +import tonkadur.wyrd.v1.lang.computation.IfElseComputation; +import tonkadur.wyrd.v1.lang.computation.Operation; +import tonkadur.wyrd.v1.lang.computation.Size; +import tonkadur.wyrd.v1.lang.computation.ValueOf; +import tonkadur.wyrd.v1.lang.computation.RelativeAddress; + +import tonkadur.wyrd.v1.lang.instruction.SetValue; + +import tonkadur.wyrd.v1.lang.meta.Computation; +import tonkadur.wyrd.v1.lang.meta.Instruction; + +import tonkadur.wyrd.v1.lang.type.Type; + +import tonkadur.wyrd.v1.compiler.fate.v1.Compiler; +import tonkadur.wyrd.v1.compiler.fate.v1.TypeCompiler; +import tonkadur.wyrd.v1.compiler.fate.v1.ComputationCompiler; + +import tonkadur.wyrd.v1.compiler.fate.v1.computation.GenericComputationCompiler; + +public class OperationCompiler extends GenericComputationCompiler +{ +   public static Class get_target_class () +   { +      return tonkadur.fate.v1.lang.computation.generic.Operation.class; +   } + +   public OperationCompiler (final Compiler compiler) +   { +      super(compiler); +   } + +   public void compile +   ( +      final tonkadur.fate.v1.lang.computation.GenericComputation computation +   ) +   throws Throwable +   { +      final tonkadur.fate.v1.lang.computation.generic.Operation source; +      final String fate_op_name; +      final List<Computation> operands; + +      source = +         (tonkadur.fate.v1.lang.computation.generic.Operation) computation; + +      operands = new ArrayList<Computation>(); + +      for +      ( +         final tonkadur.fate.v1.lang.meta.Computation x: +            source.get_operands() +      ) +      { +         final ComputationCompiler cc; + +         cc = new ComputationCompiler(compiler); + +         x.get_visited_by(cc); + +         assimilate(cc); + +         operands.add(cc.get_computation()); +      } + +      fate_op_name = source.get_operator().get_name(); + +      if +      ( +         fate_op_name.equals +         ( +            tonkadur.fate.v1.lang.computation.Operator.PLUS.get_name() +         ) +      ) +      { +         final Iterator<Computation> operands_it; + +         operands_it = operands.iterator(); + +         result_as_computation = operands_it.next(); + +         while (operands_it.hasNext()) +         { +            result_as_computation = +               Operation.plus(operands_it.next(), result_as_computation); +         } +      } +      else if +      ( +         fate_op_name.equals +         ( +            tonkadur.fate.v1.lang.computation.Operator.MINUS.get_name() +         ) +      ) +      { +         final Iterator<Computation> operands_it; +         Computation sum; + +         operands_it = operands.iterator(); + +         result_as_computation = operands_it.next(); +         sum = operands_it.next(); + +         while (operands_it.hasNext()) +         { +            sum = Operation.plus(operands_it.next(), sum); +         } + +         result_as_computation = Operation.minus(result_as_computation, sum); +      } +      else if +      ( +         fate_op_name.equals(tonkadur.fate.v1.lang.computation.Operator.TIMES.get_name()) +      ) +      { +         final Iterator<Computation> operands_it; + +         operands_it = operands.iterator(); + +         result_as_computation = operands_it.next(); + +         while (operands_it.hasNext()) +         { +            result_as_computation = +               Operation.times(operands_it.next(), result_as_computation); +         } +      } +      else if +      ( +         fate_op_name.equals +         ( +            tonkadur.fate.v1.lang.computation.Operator.DIVIDE.get_name() +         ) +      ) +      { +         result_as_computation = +            Operation.divide(operands.get(0), operands.get(1)); +      } +      else if +      ( +         fate_op_name.equals +         ( +            tonkadur.fate.v1.lang.computation.Operator.MIN.get_name() +         ) +      ) +      { +         final Register candidate; + +         candidate = reserve(operands.get(0).get_type()); +         result_as_address = candidate.get_address(); +         result_as_computation = candidate.get_value(); + +         init_instructions.add +         ( +            new SetValue(result_as_address, operands.get(0)) +         ); + +         for (final Computation operand: operands) +         { +            init_instructions.add +            ( +               new SetValue +               ( +                  result_as_address, +                  new IfElseComputation +                  ( +                     Operation.less_than +                     ( +                        result_as_computation, +                        operand +                     ), +                     result_as_computation, +                     operand +                  ) +               ) +            ); +         } +      } +      else if +      ( +         fate_op_name.equals +         ( +            tonkadur.fate.v1.lang.computation.Operator.MAX.get_name() +         ) +      ) +      { +         final Register candidate; + +         candidate = reserve(operands.get(0).get_type()); +         result_as_address = candidate.get_address(); +         result_as_computation = candidate.get_value(); + +         init_instructions.add +         ( +            new SetValue(result_as_address, operands.get(0)) +         ); + +         for (final Computation operand: operands) +         { +            init_instructions.add +            ( +               new SetValue +               ( +                  result_as_address, +                  new IfElseComputation +                  ( +                     Operation.greater_than(result_as_computation, operand), +                     result_as_computation, +                     operand +                  ) +               ) +            ); +         } +      } +      else if +      ( +         fate_op_name.equals +         ( +            tonkadur.fate.v1.lang.computation.Operator.ABS.get_name() +         ) +      ) +      { +         final Computation zero, minus_one; + +         if (operands.get(0).get_type().equals(Type.INT)) +         { +            zero = Constant.ZERO; +            minus_one = new Constant(Type.INT, "-1"); +         } +         else +         { +            zero = new Constant(Type.FLOAT, "0.0"); +            minus_one = new Constant(Type.FLOAT, "-1.0"); +         } + +         result_as_computation = +            new IfElseComputation +            ( +               Operation.greater_than(zero, operands.get(0)), +               Operation.times(minus_one, operands.get(0)), +               operands.get(0) +            ); +      } +      else if +      ( +         fate_op_name.equals +         ( +            tonkadur.fate.v1.lang.computation.Operator.CLAMP.get_name() +         ) +      ) +      { +         final Type t; +         final Register candidate; +         final Computation result_as_computation; + +         t = operands.get(2).get_type(); +         candidate = reserve(t); +         result_as_address = candidate.get_address(); +         result_as_computation = candidate.get_value(); + +         init_instructions.add +         ( +            new SetValue +            ( +               result_as_address, +               new IfElseComputation +               ( +                  Operation.greater_than(operands.get(2), operands.get(1)), +                  operands.get(1), +                  operands.get(2) +               ) +            ) +         ); +         init_instructions.add +         ( +            new SetValue +            ( +               result_as_address, +               new IfElseComputation +               ( +                  Operation.less_than(result_as_computation, operands.get(0)), +                  operands.get(0), +                  result_as_computation +               ) +            ) +         ); +      } +      else if +      ( +         fate_op_name.equals +         ( +            tonkadur.fate.v1.lang.computation.Operator.MODULO.get_name() +         ) +      ) +      { +         result_as_computation = +            Operation.modulo(operands.get(0), operands.get(1)); +      } +      else if +      ( +         fate_op_name.equals +         ( +            tonkadur.fate.v1.lang.computation.Operator.POWER.get_name() +         ) +      ) +      { +         result_as_computation = +            Operation.power(operands.get(0), operands.get(1)); +      } +      else if +      ( +         fate_op_name.equals +         ( +            tonkadur.fate.v1.lang.computation.Operator.RANDOM.get_name() +         ) +      ) +      { +         final List<Instruction> push_rand, pop_rand; +         final Register result, zero_holder; + +         push_rand = new ArrayList<Instruction>(); +         pop_rand = new ArrayList<Instruction>(); + +         result = reserve(Type.INT); +         zero_holder = reserve(Type.INT); + +         result_as_computation = result.get_value(); +         result_as_address = result.get_address(); + +         push_rand.add +         ( +            new SetValue +            ( +               result_as_address, +               Operation.rand(operands.get(0), operands.get(1)) +            ) +         ); + +         push_rand.add +         ( +            new SetValue +            ( +               new RelativeAddress +               ( +                  compiler.registers().get_rand_value_holder().get_address(), +                  new Cast +                  ( +                     new Size +                     ( +                        compiler.registers().get_rand_value_holder +                        ( +                        ).get_address() +                     ), +                     Type.STRING +                  ), +                  Type.INT +               ), +               result_as_computation +            ) +         ); + +         pop_rand.add +         ( +            new SetValue +            ( +               result_as_address, +               new ValueOf +               ( +                  new RelativeAddress +                  ( +                     compiler.registers().get_rand_value_holder().get_address(), +                     new Cast(Constant.ZERO, Type.STRING), +                     Type.INT +                  ) +               ) +            ) +         ); + +         pop_rand.add(new SetValue(zero_holder.get_address(), Constant.ZERO)); + +         pop_rand.add +         ( +            tonkadur.wyrd.v1.compiler.util.RemoveAt.generate +            ( +               compiler.registers(), +               compiler.assembler(), +               zero_holder.get_address(), +               new Size +               ( +                  compiler.registers().get_rand_value_holder().get_address() +               ), +               compiler.registers().get_rand_value_holder().get_address() +            ) +         ); + +         init_instructions.add +         ( +            tonkadur.wyrd.v1.compiler.util.IfElse.generate +            ( +               compiler.registers(), +               compiler.assembler(), +               Operation.equals +               ( +                  compiler.registers().get_rand_mode_holder().get_value(), +                  Constant.ZERO +               ), +               new SetValue +               ( +                  result_as_address, +                  Operation.rand(operands.get(0), operands.get(1)) +               ), +               tonkadur.wyrd.v1.compiler.util.IfElse.generate +               ( +                  compiler.registers(), +                  compiler.assembler(), +                  Operation.equals +                  ( +                     compiler.registers().get_rand_mode_holder().get_value(), +                     Constant.ONE +                  ), +                  compiler.assembler().merge(push_rand), +                  compiler.assembler().merge(pop_rand) +               ) +            ) +         ); +      } +      else if +      ( +         fate_op_name.equals +         ( +            tonkadur.fate.v1.lang.computation.Operator.AND.get_name() +         ) +      ) +      { +         final Iterator<Computation> operands_it; + +         operands_it = operands.iterator(); + +         result_as_computation = operands_it.next(); + +         while (operands_it.hasNext()) +         { +            result_as_computation = +               Operation.and(operands_it.next(), result_as_computation); +         } +      } +      else if +      ( +         fate_op_name.equals +         ( +            tonkadur.fate.v1.lang.computation.Operator.OR.get_name() +         ) +      ) +      { +         final Iterator<Computation> operands_it; + +         operands_it = operands.iterator(); + +         result_as_computation = operands_it.next(); + +         while (operands_it.hasNext()) +         { +            result_as_computation = +               Operation.or(operands_it.next(), result_as_computation); +         } +      } +      else if +      ( +         fate_op_name.equals +         ( +            tonkadur.fate.v1.lang.computation.Operator.NOT.get_name() +         ) +      ) +      { +         result_as_computation = Operation.not(operands.get(0)); +      } +      else if +      ( +         fate_op_name.equals +         ( +            tonkadur.fate.v1.lang.computation.Operator.IMPLIES.get_name() +         ) +      ) +      { +         result_as_computation = +            Operation.implies(operands.get(0), operands.get(1)); +      } +      else if +      ( +         fate_op_name.equals +         ( +            tonkadur.fate.v1.lang.computation.Operator.ONE_IN.get_name() +         ) +      ) +      { +         final Iterator<Computation> operand_it; + +         operand_it = operands.iterator(); + +         result_as_computation = +            new IfElseComputation +            ( +               operand_it.next(), +               Constant.ONE, +               Constant.ZERO +            ); + +         while (operand_it.hasNext()) +         { +            result_as_computation = +               Operation.plus +               ( +                  new IfElseComputation +                  ( +                     operand_it.next(), +                     Constant.ONE, +                     Constant.ZERO +                  ), +                  result_as_computation +               ); +         } + +         result_as_computation = +            Operation.equals(result_as_computation, Constant.ONE); +      } +      else if +      ( +         fate_op_name.equals +         ( +            tonkadur.fate.v1.lang.computation.Operator.EQUALS.get_name() +         ) +      ) +      { +         final Iterator<Computation> operands_it; +         final Computation first_elem; + +         operands_it = operands.iterator(); + +         first_elem = operands_it.next(); + +         result_as_computation = +            Operation.equals(first_elem, operands_it.next()); + +         while (operands_it.hasNext()) +         { +            result_as_computation = +               Operation.and +               ( +                  result_as_computation, +                  Operation.equals(first_elem, operands_it.next()) +               ); +         } +      } +      else if +      ( +         fate_op_name.equals +         ( +            tonkadur.fate.v1.lang.computation.Operator.LOWER_THAN.get_name() +         ) +      ) +      { +         result_as_computation = +            Operation.less_than(operands.get(0), operands.get(1)); +      } +      else if +      ( +         fate_op_name.equals +         ( +            tonkadur.fate.v1.lang.computation.Operator.LOWER_EQUAL_THAN.get_name() +         ) +      ) +      { +         result_as_computation = +            Operation.less_equal_than(operands.get(0), operands.get(1)); +      } +      else if +      ( +         fate_op_name.equals +         ( +            tonkadur.fate.v1.lang.computation.Operator.GREATER_EQUAL_THAN.get_name() +         ) +      ) +      { +         result_as_computation = +            Operation.greater_equal_than(operands.get(0), operands.get(1)); +      } +      else if +      ( +         fate_op_name.equals +         ( +            tonkadur.fate.v1.lang.computation.Operator.GREATER_THAN.get_name() +         ) +      ) +      { +         result_as_computation = +            Operation.greater_than(operands.get(0), operands.get(1)); +      } +      else +      { +         System.err.println("[P] Unknown Fate operator '" + fate_op_name+ "'."); +      } +   } +} | 


