using System.Collections.Generic; using System.Linq; namespace HeuristicLab.Problems.ProgramSynthesis.Push.Stack { using System.Globalization; using HeuristicLab.Problems.ProgramSynthesis.Push.Constants; using HeuristicLab.Problems.ProgramSynthesis.Push.Extensions; using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter; public static class InterpreterStackStringifier { public static IEnumerable StringifyStack(this IPushInterpreter interpreter, StackTypes type) { switch (type) { case StackTypes.Boolean: return StringifyLiteralStack(interpreter.BooleanStack); case StackTypes.Integer: return StringifyLiteralStack(interpreter.IntegerStack); case StackTypes.Float: return StringifyFloatStack(interpreter.FloatStack, interpreter.Configuration.FloatStringFormat); case StackTypes.Char: return StringifyCharStack(interpreter.CharStack); case StackTypes.String: return StringifyLiteralStack(interpreter.StringStack, PushEnvironment.StringSymbolStr, PushEnvironment.StringSymbolStr); case StackTypes.Name: return StringifyLiteralStack(interpreter.NameStack); case StackTypes.Code: return StringifyLiteralStack(interpreter.CodeStack); case StackTypes.Exec: return StringifyLiteralStack(interpreter.ExecStack); case StackTypes.Print: return StringifyLiteralStack(interpreter.PrintStack, PushEnvironment.StringSymbolStr, PushEnvironment.StringSymbolStr, false); case StackTypes.BooleanVector: return StringifyVectorStack(interpreter.BooleanVectorStack); case StackTypes.IntegerVector: return StringifyVectorStack(interpreter.IntegerVectorStack); case StackTypes.FloatVector: return StringifyFloatVectorStack(interpreter.FloatVectorStack, interpreter.Configuration.FloatStringFormat); case StackTypes.StringVector: return StringifyVectorStack(interpreter.StringVectorStack, PushEnvironment.StringSymbolStr, PushEnvironment.StringSymbolStr); default: return Enumerable.Empty(); } } private static IEnumerable StringifyCharStack(IPushStack stack) { var result = stack.AsEnumerable(); return result.Select(x => PushEnvironment.CharSymbol + x.Printify() + PushEnvironment.CharSymbol); } private static IEnumerable StringifyFloatStack(IPushStack stack, string format) { var result = stack.AsEnumerable(); return result.Select(x => x.ToString(format, CultureInfo.InvariantCulture)); } private static IEnumerable StringifyLiteralStack(IPushStack stack, string prefix = "", string postfix = "", bool reverse = true) { var result = stack.AsStrings(); if (reverse) result = result.Reverse(); return result.Select(x => prefix + x.ToString() + postfix); } private static IEnumerable StringifyFloatVectorStack(IPushStack> stack, string format) { var separator = PushEnvironment.VectorSeparatorSymbol.ToString(); return stack .Reverse() .Select(x => PushEnvironment.VectorStartSymbol + string.Join(separator, x.Select(_ => _.ToString(format, CultureInfo.InvariantCulture))) + PushEnvironment.VectorEndSymbol); } private static IEnumerable StringifyVectorStack(IPushStack> stack, string prefix = "", string postfix = "") { var separator = PushEnvironment.VectorSeparatorSymbol.ToString(); return stack .Reverse() .Select(x => PushEnvironment.VectorStartSymbol + string.Join(separator, x.Select(_ => prefix + _.ToString() + postfix)) + PushEnvironment.VectorEndSymbol); } } }