diff --git a/src/AST/Expression.cs b/src/AST/Expression.cs new file mode 100644 index 00000000..24e89cd9 --- /dev/null +++ b/src/AST/Expression.cs @@ -0,0 +1,49 @@ +using System; + +namespace CppSharp.AST +{ + public abstract class Expression + { + public string DebugText; + + public abstract TV Visit(IExpressionVisitor visitor); + } + + public class BuiltinTypeExpression : Expression + { + public long Value { get; set; } + + public BuiltinType Type { get; set; } + + public bool IsHexadecimal + { + get + { + if (DebugText == null) + { + return false; + } + return DebugText.Contains("0x") || DebugText.Contains("0X"); + } + } + + public override string ToString() + { + var printAsHex = IsHexadecimal && Type.IsUnsigned; + var format = printAsHex ? "x" : string.Empty; + var value = Type.IsUnsigned ? Value.ToString(format) : + ((long)Value).ToString(format); + return printAsHex ? "0x" + value : value; + } + + public override T Visit(IExpressionVisitor visitor) + { + return visitor.VisitBuiltinExpression(this); + } + } + + public interface IExpressionVisitor + { + T VisitBuiltinExpression(BuiltinTypeExpression builtinType); + } +} \ No newline at end of file diff --git a/src/Generator/Generators/CSharp/CSharpExpressionPrinter.cs b/src/Generator/Generators/CSharp/CSharpExpressionPrinter.cs new file mode 100644 index 00000000..9b450e60 --- /dev/null +++ b/src/Generator/Generators/CSharp/CSharpExpressionPrinter.cs @@ -0,0 +1,41 @@ +using CppSharp.AST; +using CppSharp.Types; + +namespace CppSharp.Generators.CSharp +{ + public class CSharpExpressionPrinterResult + { + public string Value; + + public override string ToString() + { + return Value; + } + } + + public static class CSharpExpressionPrinterExtensions + { + public static CSharpExpressionPrinterResult CSharpValue(this Expression value, + CSharpExpressionPrinter printer) + { + return value.Visit(printer); + } + + } + public class CSharpExpressionPrinter : IExpressionPrinter, + IExpressionVisitor + { + public CSharpExpressionPrinterResult VisitBuiltinExpression(BuiltinTypeExpression builtinType) + { + return new CSharpExpressionPrinterResult() + { + Value = builtinType.ToString(), + }; + } + + public string ToString(Type type) + { + throw new System.NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/src/Generator/Types/IExpressionPrinter.cs b/src/Generator/Types/IExpressionPrinter.cs new file mode 100644 index 00000000..19edd17a --- /dev/null +++ b/src/Generator/Types/IExpressionPrinter.cs @@ -0,0 +1,13 @@ +using CppSharp.AST; + +namespace CppSharp.Types +{ + public interface IExpressionPrinter + { + string ToString(Type type); + } + + public interface IExpressionPrinter : IExpressionPrinter, IExpressionVisitor + { + } +} \ No newline at end of file