mirror of https://github.com/mono/CppSharp.git
Browse Source
Add support for a expression type in the AST and make it possible to specify expression/value for a field.pull/186/head
6 changed files with 125 additions and 8 deletions
@ -0,0 +1,49 @@ |
|||||||
|
using System; |
||||||
|
|
||||||
|
namespace CppSharp.AST |
||||||
|
{ |
||||||
|
public abstract class Expression |
||||||
|
{ |
||||||
|
public string DebugText; |
||||||
|
|
||||||
|
public abstract TV Visit<TV>(IExpressionVisitor<TV> 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<T>(IExpressionVisitor<T> visitor) |
||||||
|
{ |
||||||
|
return visitor.VisitBuiltinExpression(this); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public interface IExpressionVisitor<out T> |
||||||
|
{ |
||||||
|
T VisitBuiltinExpression(BuiltinTypeExpression builtinType); |
||||||
|
} |
||||||
|
} |
@ -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<CSharpExpressionPrinterResult>, |
||||||
|
IExpressionVisitor<CSharpExpressionPrinterResult> |
||||||
|
{ |
||||||
|
public CSharpExpressionPrinterResult VisitBuiltinExpression(BuiltinTypeExpression builtinType) |
||||||
|
{ |
||||||
|
return new CSharpExpressionPrinterResult() |
||||||
|
{ |
||||||
|
Value = builtinType.ToString(), |
||||||
|
}; |
||||||
|
} |
||||||
|
|
||||||
|
public string ToString(Type type) |
||||||
|
{ |
||||||
|
throw new System.NotImplementedException(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue