.NET binding to GF by Bjørnar Luteberget

This commit is contained in:
krasimir
2017-05-31 13:48:36 +00:00
parent 2a8d2806e4
commit e1cec06f74
15 changed files with 1509 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
using System;
using System.Runtime.InteropServices;
namespace PGFSharp
{
/*public class Abs : Expression
{
public Abs ()
{
}
}
*/
}

View File

@@ -0,0 +1,57 @@
using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Linq;
namespace PGFSharp
{
public class ApplicationExpr : Expr
{
public override R Accept<R> (IVisitor<R> visitor)
{
var args = new List<Expr> ();
var expr = this;
while (expr.Function is ApplicationExpr) {
args.Add (expr.Argument);
expr = expr.Function as ApplicationExpr;
}
args.Add (expr.Argument);
if (!(expr.Function is FunctionExpr))
throw new ArgumentException ();
args.Reverse ();
return visitor.VisitApplication ((expr.Function as FunctionExpr).Name, args.ToArray());
}
[StructLayout(LayoutKind.Sequential)]
private struct PgfExprApp {
public IntPtr Function;
public IntPtr Argument;
}
private PgfExprApp Data => Marshal.PtrToStructure<PgfExprApp>(DataPtr);
public Expr Function => Expr.FromPtr(Data.Function, _pool);
public Expr Argument => Expr.FromPtr(Data.Argument, _pool);
internal ApplicationExpr(IntPtr ptr, NativeGU.NativeMemoryPool pool) : base(ptr, pool) { }
public ApplicationExpr(string fname, IEnumerable<Expr> args)
{
_pool = new NativeGU.NativeMemoryPool();
MkStringVariant((byte)PgfExprTag.PGF_EXPR_FUN, fname, ref _ptr);
foreach (var arg in args) {
var fun = _ptr;
var exprApp = NativeGU.gu_alloc_variant((byte)PgfExprTag.PGF_EXPR_APP,
(UIntPtr)Marshal.SizeOf<PgfExprApp>(), UIntPtr.Zero, ref _ptr, _pool.Ptr);
Native.EditStruct<PgfExprApp> (exprApp, (ref PgfExprApp app) => {
app.Function = fun;
app.Argument = arg.Ptr;
});
}
}
}
}

View File

@@ -0,0 +1,18 @@
using System;
using System.Linq;
using System.Collections.Generic;
namespace PGFSharp
{
public class FunctionExpr : Expr
{
public override R Accept<R> (IVisitor<R> visitor)
{
return visitor.VisitApplication (Name, new Expr[] {});
}
internal FunctionExpr (IntPtr expr, NativeGU.NativeMemoryPool pool) : base(expr,pool) {}
public string Name => Native.NativeString.StringFromNativeUtf8(DataPtr);
}
}

View File

@@ -0,0 +1,127 @@
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace PGFSharp
{
public class LiteralStringExpr : LiteralExpr
{
internal LiteralStringExpr(IntPtr expr, NativeGU.NativeMemoryPool pool) : base(expr, pool) { }
public LiteralStringExpr(string s) : base()
{
_pool = new NativeGU.NativeMemoryPool();
var exprTag = (byte)(int)PgfExprTag.PGF_EXPR_LIT;
IntPtr litPtr = NativeGU.gu_alloc_variant(exprTag,
(UIntPtr)Marshal.SizeOf<NativePgfExprLit>(), UIntPtr.Zero, ref _ptr, _pool.Ptr);
Native.EditStruct<NativePgfExprLit>(litPtr, (ref NativePgfExprLit lit) => {
MkStringVariant((byte)PgfLiteralTag.PGF_LITERAL_STR, s, ref lit.lit);
});
}
public override R Accept<R>(IVisitor<R> visitor)
{
return visitor.VisitLiteralString(Value);
}
public string Value => Native.NativeString.StringFromNativeUtf8(LitDataPtr);
}
public class LiteralIntExpr : LiteralExpr
{
internal LiteralIntExpr(IntPtr expr, NativeGU.NativeMemoryPool pool) : base(expr, pool) { }
public LiteralIntExpr(int val) : base()
{
Initialize<NativePgfLiteralInt>(PgfLiteralTag.PGF_LITERAL_INT,
(ref NativePgfLiteralInt ilit) => ilit.val = val);
}
public override R Accept<R>(IVisitor<R> visitor)
{
return visitor.VisitLiteralInt(Value);
}
public int Value => Marshal.PtrToStructure<NativePgfLiteralInt>(LitDataPtr).val;
[StructLayout(LayoutKind.Sequential)]
private struct NativePgfLiteralInt { public int val; }
}
public class LiteralFloatExpr : LiteralExpr
{
internal LiteralFloatExpr(IntPtr expr, NativeGU.NativeMemoryPool pool) : base(expr, pool) { }
public LiteralFloatExpr(double val) : base()
{
Initialize<NativePgfLiteralFlt>(PgfLiteralTag.PGF_LITERAL_FLT,
(ref NativePgfLiteralFlt flit) => flit.val = val);
}
public override R Accept<R>(IVisitor<R> visitor)
{
return visitor.VisitLiteralFloat(Value);
}
public double Value => Marshal.PtrToStructure<NativePgfLiteralFlt>(LitDataPtr).val;
[StructLayout(LayoutKind.Sequential)]
private struct NativePgfLiteralFlt { public double val; }
}
public abstract class LiteralExpr : Expr
{
internal LiteralExpr(IntPtr expr, NativeGU.NativeMemoryPool pool) : base(expr, pool) { }
internal LiteralExpr() { }
internal new static Expr FromPtr(IntPtr expr, NativeGU.NativeMemoryPool pool)
{
var dataPtr = NativeGU.gu_variant_open(expr).Data; // PgfExprLit*
var data = Marshal.PtrToStructure<NativePgfExprLit>(dataPtr);
var literalTag = (PgfLiteralTag)NativeGU.gu_variant_open(data.lit).Tag;
switch(literalTag)
{
case PgfLiteralTag.PGF_LITERAL_STR:
return new LiteralStringExpr(expr, pool);
case PgfLiteralTag.PGF_LITERAL_INT:
return new LiteralIntExpr(expr, pool);
case PgfLiteralTag.PGF_LITERAL_FLT:
return new LiteralFloatExpr(expr, pool);
default:
throw new ArgumentException();
}
}
internal void Initialize<TNative>(PgfLiteralTag litTag, Native.StructAction<TNative> setValue, UIntPtr? size = null) {
_pool = new NativeGU.NativeMemoryPool();
var exprTag = (byte)(int)PgfExprTag.PGF_EXPR_LIT;
IntPtr litPtr = NativeGU.gu_alloc_variant ( exprTag,
(UIntPtr)Marshal.SizeOf<NativePgfExprLit>(), UIntPtr.Zero, ref _ptr, _pool.Ptr);
Native.EditStruct<NativePgfExprLit> (litPtr, (ref NativePgfExprLit lit) => {
IntPtr ilitPtr = NativeGU.gu_alloc_variant ((byte)litTag,
(UIntPtr)Marshal.SizeOf<TNative> (), UIntPtr.Zero, ref lit.lit, _pool.Ptr);
Native.EditStruct<TNative>(ilitPtr, setValue);
});
}
// Deref DatPtr to det PgfExprLit.
private NativePgfExprLit Data => Marshal.PtrToStructure<NativePgfExprLit>(DataPtr);
private PgfLiteralTag LiteralTag => (PgfLiteralTag) NativeGU.gu_variant_open(Data.lit).Tag;
internal IntPtr LitDataPtr => NativeGU.gu_variant_open(Data.lit).Data;
internal enum PgfLiteralTag {
PGF_LITERAL_STR,
PGF_LITERAL_INT,
PGF_LITERAL_FLT,
PGF_LITERAL_NUM_TAGS
}
[StructLayout(LayoutKind.Sequential)]
internal struct NativePgfExprLit { public IntPtr lit; }
}
}

View File

@@ -0,0 +1,34 @@
using System;
using System.Runtime.InteropServices;
namespace PGFSharp
{
public class MetaVariableExpr : Expr {
internal MetaVariableExpr() {
_pool = new NativeGU.NativeMemoryPool();
IntPtr exprMetaPtr = NativeGU.gu_alloc_variant ((byte)PgfExprTag.PGF_EXPR_META,
(UIntPtr)Marshal.SizeOf <NativePgfExprMeta>(), UIntPtr.Zero, ref _ptr, _pool.Ptr);
Native.EditStruct<NativePgfExprMeta> (exprMetaPtr, (ref NativePgfExprMeta m) => m.Id = 0);
}
internal MetaVariableExpr(IntPtr ptr, NativeGU.NativeMemoryPool pool) : base(ptr, pool) { }
public int Id => Data.Id;
private NativePgfExprMeta Data => Marshal.PtrToStructure<NativePgfExprMeta>(DataPtr);
public override R Accept<R> (IVisitor<R> visitor)
{
// return visitor.VisitMetaVariable (Id);
// Not supported yet.
throw new NotImplementedException();
}
[StructLayout(LayoutKind.Sequential)]
private struct NativePgfExprMeta { public int Id; }
}
}