- Timestamp:
- 10/07/13 08:49:34 (11 years ago)
- Location:
- branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3
- Files:
-
- 1 deleted
- 2 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Grammar.cs
r10014 r10028 57 57 var block = new Block(); 58 58 var stat = new Stat(); 59 var ifStat = new IfStat(); 60 var elseStat = new ElseStat(); 59 var ifThenElseStat = new IfThenElseStat(); 61 60 var whileStat = new WhileStat(); 62 61 … … 122 121 123 122 #region Symbol Collections 124 var controlSymbols = new ISymbol[] { if Stat, whileStat };123 var controlSymbols = new ISymbol[] { ifThenElseStat, whileStat }; 125 124 var actionSymbols = new ISymbol[] { 126 125 ahead, back, fire, turnGunLeft, turnGunRight, turnLeft, turnRadarLeft, turnRadarRight, turnRight … … 132 131 133 132 var events = new GroupSymbol(EventsName, new ISymbol[] { onScannedRobot, onBulletHit, onBulletMissed, onHitByBullet, onHitRobot, onHitWall }); 134 var controlStatements = new GroupSymbol(ControlStatementsName, controlSymbols .Concat(new[] { elseStat }));133 var controlStatements = new GroupSymbol(ControlStatementsName, controlSymbols); 135 134 var expressions = new GroupSymbol(ExpressionsName, new ISymbol[] { logicalExpr, numericalExpr }); 136 135 var robocodeFunctions = new GroupSymbol(RobocodeFunctionsName, actionSymbols.Concat(functionSymbols)); … … 193 192 194 193 // IfStat 195 AddAllowedChildSymbol(ifStat, logicalExpr, 0); 196 AddAllowedChildSymbol(ifStat, stat, 1); 197 AddAllowedChildSymbol(ifStat, emptyEvent, 1); 198 AddAllowedChildSymbol(ifStat, doNothing, 1); 199 AddAllowedChildSymbol(ifStat, elseStat, 2); 200 AddAllowedChildSymbol(ifStat, emptyEvent, 2); 201 AddAllowedChildSymbol(ifStat, doNothing, 2); 202 203 // ElseStat 204 AddAllowedChildSymbol(elseStat, stat); 205 AddAllowedChildSymbol(elseStat, emptyEvent); 206 AddAllowedChildSymbol(elseStat, doNothing); 194 AddAllowedChildSymbol(ifThenElseStat, logicalExpr, 0); 195 AddAllowedChildSymbol(ifThenElseStat, stat, 1); 196 AddAllowedChildSymbol(ifThenElseStat, emptyEvent, 1); 197 AddAllowedChildSymbol(ifThenElseStat, doNothing, 1); 198 AddAllowedChildSymbol(ifThenElseStat, stat, 2); 199 AddAllowedChildSymbol(ifThenElseStat, emptyEvent, 2); 200 AddAllowedChildSymbol(ifThenElseStat, doNothing, 2); 207 201 208 202 // WhileStat -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/HeuristicLab.Problems.Robocode-3.3.csproj
r10011 r10028 116 116 <Compile Include="Symbols\Void Methods\Back.cs" /> 117 117 <Compile Include="Symbols\Block.cs" /> 118 <Compile Include="Symbols\Statements\ElseStat.cs" /> 119 <Compile Include="Symbols\Statements\IfStat.cs" /> 118 <Compile Include="Symbols\Statements\IfThenElseStat.cs" /> 120 119 <Compile Include="Symbols\Statements\WhileStat.cs" /> 121 120 <Compile Include="Symbols\Logical Expressions\BooleanTreeNode.cs" /> -
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.