private static ProductionRule CheckNonZeroValue(OneMaxGO go) { string value = go.ConvertToCode(new ProductionRule((NonTerminal)"value")); Tuple <int, bool> parsingResult = EvaluateExpression(value, go); int calculatedValue = parsingResult.Item1; bool canExecute = parsingResult.Item2; go.ConvertToCode(new ErrorCheck <OneMaxGO>(g => !canExecute || calculatedValue != 0)); return(new ProductionRule(new Terminal(value))); }
private static ProductionRule CreateForLoop(OneMaxGO go) { if (go["unusedVariables"].Count == 0) { return(new ProductionRule((NonTerminal)"loop")); } string chosenVariable = go.ConvertToCode(new ProductionRule((NonTerminal)"unusedVariables")); OneMaxGO newGO = go.CreateChild <OneMaxGO>(); ProductionRule variableProductionRule = newGO["unusedVariables"].Find(x => ((Terminal)x[0]).Code == chosenVariable); newGO["unusedVariables"].Remove(variableProductionRule); newGO["variable"].Add(variableProductionRule); return(new ProductionRule(new Terminal("for(int " + chosenVariable + " = 0; " + chosenVariable + " < "), (NonTerminal)"value", new Terminal("; " + chosenVariable + "++)\n{\n"), new NonTerminal("code", g => newGO), (Terminal)"\n}\n")); }
private static ProductionRule CheckIfAlwaysTrue(OneMaxGO go) { string code = go.ConvertToCode((NonTerminal)"condition"); bool canExecute = !code.Contains("a") && !code.Contains("b") && !code.Contains("c") && !code.Contains("d") && !code.Contains("e") && !code.Contains("f"); ExecutionParameters parameters = new ExecutionParameters(genericCodeWrapper.Replace("TYPE", "bool"), "CodeExecutor.Executor", "Execute"); if (canExecute) { bool result = (bool)Executor.Execute(parameters, "return !" + code + ";"); go.ConvertToCode(new ErrorCheck(g => result)); return(new ProductionRule(new Terminal(code))); //If the value is always false, there can't be an infine loop (although a loop that is never executed is useless) } else//If the code contains variables, by definition its value isn't constant { return(new ProductionRule(new Terminal(code))); } }
public override GrammaticalObject GetClone() { OneMaxGO go = new OneMaxGO(); return(go); }