| summaryrefslogtreecommitdiff | 
diff options
| author | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2020-07-05 20:01:20 +0200 | 
|---|---|---|
| committer | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2020-07-05 20:01:20 +0200 | 
| commit | a433b570a340d5c0bf70e6452f68f60d2b241032 (patch) | |
| tree | c8493d19165dd3a89d51e408c64ec40126b8c94b /src/core | |
| parent | 3803f236f97b9149ddead5218c3585097245c600 (diff) | |
Adds if/else value node.
Diffstat (limited to 'src/core')
| -rw-r--r-- | src/core/src/tonkadur/fate/v1/lang/IfElseValue.java | 141 | ||||
| -rw-r--r-- | src/core/src/tonkadur/fate/v1/parser/FateParser.g4 | 16 | 
2 files changed, 154 insertions, 3 deletions
| diff --git a/src/core/src/tonkadur/fate/v1/lang/IfElseValue.java b/src/core/src/tonkadur/fate/v1/lang/IfElseValue.java new file mode 100644 index 0000000..960af57 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/IfElseValue.java @@ -0,0 +1,141 @@ +package tonkadur.fate.v1.lang; + +import java.util.Set; +import java.util.Map; +import java.util.HashMap; +import java.util.Collections; + +import tonkadur.error.ErrorManager; + +import tonkadur.parser.Origin; + +import tonkadur.fate.v1.lang.meta.ValueNode; + +import tonkadur.fate.v1.error.ConflictingTypeException; +import tonkadur.fate.v1.error.IncomparableTypeException; +import tonkadur.fate.v1.error.InvalidTypeException; + +public class IfElseValue extends ValueNode +{ +   /***************************************************************************/ +   /**** MEMBERS **************************************************************/ +   /***************************************************************************/ +   protected final ValueNode condition; +   protected final ValueNode if_true; +   protected final ValueNode if_false; + +   /***************************************************************************/ +   /**** PROTECTED ************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   protected IfElseValue +   ( +      final Origin origin, +      final Type return_type, +      final ValueNode condition, +      final ValueNode if_true, +      final ValueNode if_false +   ) +   { +      super(origin, return_type); + +      this.condition = condition; +      this.if_true = if_true; +      this.if_false = if_false; +   } + +   /***************************************************************************/ +   /**** PUBLIC ***************************************************************/ +   /***************************************************************************/ +   /**** Constructors *********************************************************/ +   public static IfElseValue build +   ( +      final Origin origin, +      final ValueNode condition, +      final ValueNode if_true, +      final ValueNode if_false +   ) +   throws +      InvalidTypeException, +      ConflictingTypeException, +      IncomparableTypeException +   { +      final Type hint; +      final Type if_true_type; +      final Type if_false_type; + +      if (!condition.get_type().can_be_used_as(Type.BOOLEAN)) +      { +         ErrorManager.handle +         ( +            new InvalidTypeException +            ( +               origin, +               condition.get_type(), +               Collections.singleton(Type.BOOLEAN) +            ) +         ); +      } + +      if_true_type = if_true.get_type(); +      if_false_type = if_false.get_type(); + +      if (if_true_type.equals(if_false_type)) +      { +         return +            new IfElseValue(origin, if_true_type, condition, if_true, if_false); +      } + +      ErrorManager.handle +      ( +         new ConflictingTypeException(origin, if_false_type, if_true_type) +      ); + +      hint = +         (Type) if_false_type.generate_comparable_to(if_true_type); + +      if (hint.equals(Type.ANY)) +      { +         ErrorManager.handle +         ( +            new IncomparableTypeException(origin, if_false_type, if_true_type) +         ); +      } + +      return new IfElseValue(origin, hint, condition, if_true, if_false); +   } + +   /**** Accessors ************************************************************/ + +   /**** Misc. ****************************************************************/ +   @Override +   public String toString () +   { +      final StringBuilder sb = new StringBuilder(); + +      sb.append(origin.toString()); +      sb.append("(IfElseValue"); +      sb.append(System.lineSeparator()); +      sb.append(System.lineSeparator()); + +      sb.append("Condition:"); +      sb.append(System.lineSeparator()); +      sb.append(condition.toString()); + +      sb.append(System.lineSeparator()); +      sb.append(System.lineSeparator()); +      sb.append("If true:"); +      sb.append(System.lineSeparator()); +      sb.append(if_true.toString()); + +      sb.append(System.lineSeparator()); +      sb.append(System.lineSeparator()); +      sb.append("If false:"); +      sb.append(System.lineSeparator()); +      sb.append(if_false.toString()); + +      sb.append(")"); + +      return sb.toString(); +   } +} diff --git a/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 b/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 index 2585948..27ff773 100644 --- a/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 +++ b/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 @@ -654,10 +654,20 @@ returns [ValueNode result]  non_text_value  returns [ValueNode result]  : -   IF_ELSE_KW value WS+ value WS+ value R_PAREN +   IF_ELSE_KW cond=value WS+ if_true=value WS+ if_false=value R_PAREN     { -      /* TODO */ -      $result = null; +      $result = +         IfElseValue.build +         ( +            CONTEXT.get_origin_at +            ( +               ($IF_ELSE_KW.getLine()), +               ($IF_ELSE_KW.getCharPositionInLine()) +            ), +            ($cond.result), +            ($if_true.result), +            ($if_false.result) +         );     }     | COND_KW value_cond_list R_PAREN | 


