private static string ValueAsString(object value, Type type = null) { if (type == null) { type = value?.GetType() ?? typeof(object); } if (value == null) { return("null"); } if (value is bool) { return(value.ToString().ToLower()); } if (value is IntPtr) { return($"0x{((IntPtr) value).ToInt64():X}"); } if (value is string) { return($"{AstStringUtils.ToLiteral(value as string)}"); } if (AstUtils.IsTypeSigned(type)) { return(value.ToString()); } if (Convert.ToInt64(value) > 9) { return($"0x{value:X}"); } return(value.ToString()); }
private String ValueAsString(object Value, Type Type = null) { if (Type == null) { Type = (Value != null) ? Value.GetType() : typeof(Object); } if (Value == null) { return("null"); } if (Value is bool) { return(Value.ToString().ToLower()); } else if (Value is IntPtr) { return(String.Format("0x{0:X}", ((IntPtr)Value).ToInt64())); } else if (Value is string) { return(String.Format("{0}", AstStringUtils.ToLiteral(Value as string))); } else if (!AstUtils.IsTypeSigned(Type)) { //StringValue = String.Format("0x{0:X8}", ItemValue); if (Convert.ToInt64(Value) > 9) { return(String.Format("0x{0:X}", Value)); } } return(Value.ToString()); }
public void TestWriteLineLoadString() { var ast = Ast.Statements( Ast.Statement(Ast.CallStatic((Action <string>)Console.WriteLine, Ast.Argument <string>(0))), Ast.Statement(Ast.CallStatic((Action <string>)Console.WriteLine, "Goodbye World!")), Ast.Return() ); var generatorIl = new GeneratorIl(); var method = generatorIl.GenerateDelegate <Action <string> >("TestWriteLine", ast); Console.WriteLine(new GeneratorCSharp().GenerateRoot(ast).ToString()); Console.WriteLine("{0}", new GeneratorIl().GenerateToString(method.Method, ast)); var output = AstStringUtils.CaptureOutput(() => { method("Hello World!"); }); Assert.Equal("Hello World!" + Environment.NewLine + "Goodbye World!" + Environment.NewLine, output); }
public void TestIndentation() { var Output = new IndentedStringBuilder(); Output.Write("{\n"); Output.Indent(() => { Output.Write("Hello World!\n"); Output.Write("Goodbye World!\n"); }); Output.Write("}\n"); Assert.AreEqual( @"{\n" + @" Hello World!\n" + @" Goodbye World!\n" + @"}\n", AstStringUtils.ToLiteralRaw(Output.ToString()) ); }
public void TestIndentation() { var output = new IndentedStringBuilder(); output.Write("{"); output.WriteNewLine(); output.Indent(() => { output.Write("Hello World!"); output.WriteNewLine(); output.Write("Goodbye World!"); output.WriteNewLine(); }); output.Write("}"); output.WriteNewLine(); Assert.Equal( @"{\n" + @" Hello World!\n" + @" Goodbye World!\n" + @"}\n", AstStringUtils.ToLiteralRaw(output.ToString()) ); }