Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/07/13 08:49:34 (11 years ago)
Author:
jkarder
Message:

#2069:

  • merged IfStat and ElseStat into IfThenElseStat
  • changed the font of the suffixCode text box in the CodeNodeView
Location:
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Statements
Files:
1 deleted
1 moved

Legend:

Unmodified
Added
Removed
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Statements/IfThenElseStat.cs

    r10014 r10028  
    2828namespace HeuristicLab.Problems.Robocode {
    2929  [StorableClass]
    30   public class IfStat : CodeNode {
     30  public class IfThenElseStat : CodeNode {
    3131    public override int MinimumArity { get { return 2; } }
    3232    public override int MaximumArity { get { return 3; } }
     
    3939
    4040    [StorableConstructor]
    41     protected IfStat(bool deserializing) : base(deserializing) { }
    42     protected IfStat(IfStat original, Cloner cloner) : base(original, cloner) { }
     41    protected IfThenElseStat(bool deserializing) : base(deserializing) { }
     42    protected IfThenElseStat(IfThenElseStat original, Cloner cloner) : base(original, cloner) { }
    4343
    44     public IfStat()
    45       : base("IfStat", "An if statement.") {
    46       Prefix = "if (";
    47       Suffix = ") {";
    48     }
     44    public IfThenElseStat() : base("IfThenElseStat", "An if statement.") { }
    4945
    5046    public override IDeepCloneable Clone(Cloner cloner) {
    51       return new IfStat(this, cloner);
     47      return new IfThenElseStat(this, cloner);
    5248    }
    5349
    5450    public override string Interpret(ISymbolicExpressionTreeNode node, IEnumerable<ISymbolicExpressionTreeNode> children) {
    55       string condition = string.Empty, ifTrue = string.Empty, ifElse = string.Empty;
    56       foreach (var c in children) {
    57         if (c.Symbol is LogicalExpression)
    58           condition = ((CodeNode)c.Symbol).Interpret(c, c.Subtrees);
    59         else if (c.Symbol is Stat)
    60           ifTrue = ((CodeNode)c.Symbol).Interpret(c, c.Subtrees);
    61         else if (c.Symbol is ElseStat || c.Symbol is EmptyEvent || c.Symbol is DoNothing)
    62           ifElse = ((CodeNode)c.Symbol).Interpret(c, c.Subtrees);
    63         else throw new ArgumentException("Unexpected children.", "children");
     51      ISymbolicExpressionTreeNode condition = null, truePart = null, falsePart = null;
     52      var enumerator = children.GetEnumerator();
     53      int childCount = 0;
     54      while (enumerator.MoveNext()) {
     55        childCount++;
     56        switch (childCount) {
     57          case 1: condition = enumerator.Current; break;
     58          case 2: truePart = enumerator.Current; break;
     59          case 3: falsePart = enumerator.Current; break;
     60          default: throw new ArgumentException("Unexpected number of children. Expected a maximum of 3 children.");
     61        }
    6462      }
    65       string result = Prefix + condition + Suffix + Environment.NewLine + ifTrue + Environment.NewLine + "}";
    66       result += string.IsNullOrEmpty(ifElse) ? Environment.NewLine : ifElse;
     63      if (childCount < 2) throw new ArgumentException("Unexpected number of children. Expected at least 2 children.");
     64
     65      var parts = new[] {
     66        ((CodeNode)condition.Symbol).Interpret(condition, condition.Subtrees),
     67        ((CodeNode)truePart.Symbol).Interpret(truePart, truePart.Subtrees),
     68        ((CodeNode)falsePart.Symbol).Interpret(falsePart, falsePart.Subtrees)
     69      };
     70
     71      string result = "if (" + parts[0] + ") {" + parts[1] + "}";
     72      if (!string.IsNullOrEmpty(parts[2]))
     73        result += " else {" + result[2] + "}";
    6774      return result;
    6875    }
Note: See TracChangeset for help on using the changeset viewer.