Changeset 10028 for branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Statements
- Timestamp:
- 10/07/13 08:49:34 (11 years ago)
- 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 28 28 namespace HeuristicLab.Problems.Robocode { 29 29 [StorableClass] 30 public class If Stat : CodeNode {30 public class IfThenElseStat : CodeNode { 31 31 public override int MinimumArity { get { return 2; } } 32 32 public override int MaximumArity { get { return 3; } } … … 39 39 40 40 [StorableConstructor] 41 protected If Stat(bool deserializing) : base(deserializing) { }42 protected If Stat(IfStat original, Cloner cloner) : base(original, cloner) { }41 protected IfThenElseStat(bool deserializing) : base(deserializing) { } 42 protected IfThenElseStat(IfThenElseStat original, Cloner cloner) : base(original, cloner) { } 43 43 44 public IfStat() 45 : base("IfStat", "An if statement.") { 46 Prefix = "if ("; 47 Suffix = ") {"; 48 } 44 public IfThenElseStat() : base("IfThenElseStat", "An if statement.") { } 49 45 50 46 public override IDeepCloneable Clone(Cloner cloner) { 51 return new If Stat(this, cloner);47 return new IfThenElseStat(this, cloner); 52 48 } 53 49 54 50 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 } 64 62 } 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] + "}"; 67 74 return result; 68 75 }
Note: See TracChangeset
for help on using the changeset viewer.