Skip to content

Commit

Permalink
marshalling
Browse files Browse the repository at this point in the history
  • Loading branch information
gkevin-kappas committed Aug 15, 2024
1 parent cc86e32 commit 85b4cb4
Show file tree
Hide file tree
Showing 19 changed files with 161 additions and 128 deletions.
2 changes: 1 addition & 1 deletion Ergo/Interpreter/ErgoInterpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public virtual Maybe<Module> LoadDirectives(ref InterpreterScope scope, Atom mod
if (_libraries.TryGetValue(moduleName, out var linkedLib))
{
linkLibrary = linkedLib;
foreach (var dir in linkedLib.GetExportedDirectives())
foreach (var dir in linkedLib.ExportedDirectives)
{
if (visibleDirectives.ContainsKey(dir.Signature))
break; // This library was already added
Expand Down
4 changes: 2 additions & 2 deletions Ergo/Interpreter/InterpreterScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,15 @@ private static ImmutableHashSet<Library> GetVisibleLibraries(IEnumerable<Atom> v
private static ImmutableDictionary<Signature, InterpreterDirective> GetVisibleDirectives(IEnumerable<Atom> visibleModules, ImmutableDictionary<Atom, Module> modules)
=> visibleModules
.SelectMany(m => modules[m].LinkedLibrary
.Select(l => l.GetExportedDirectives())
.Select(l => l.ExportedDirectives)
.GetOr(Enumerable.Empty<InterpreterDirective>()))
.Where(d => visibleModules.Contains(d.Signature.Module.GetOr(WellKnown.Modules.Stdlib)))
.ToImmutableDictionary(x => x.Signature);

private static ImmutableDictionary<Signature, BuiltIn> GetVisibleBuiltIns(IEnumerable<Atom> visibleModules, ImmutableDictionary<Atom, Module> modules)
=> visibleModules
.SelectMany(m => modules[m].LinkedLibrary
.Select(l => l.GetExportedBuiltins())
.Select(l => l.ExportedBuiltins)
.GetOr(Enumerable.Empty<BuiltIn>()))
.Where(b => visibleModules.Contains(b.Signature.Module.GetOr(WellKnown.Modules.Stdlib)))
.ToImmutableDictionary(x => x.Signature);
Expand Down
12 changes: 7 additions & 5 deletions Ergo/Interpreter/Libraries/CSharp/CSharp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ namespace Ergo.Interpreter.Libraries.CSharp;
public class CSharp : Library
{
public override Atom Module => WellKnown.Modules.CSharp;
public override IEnumerable<BuiltIn> GetExportedBuiltins() => Enumerable.Empty<BuiltIn>()
.Append(new InvokeOp())
;
public override IEnumerable<InterpreterDirective> GetExportedDirectives() => Enumerable.Empty<InterpreterDirective>()
;
private readonly BuiltIn[] _exportedBuiltIns = [
new InvokeOp()
];
private readonly InterpreterDirective[] _interpreterDirectives = [
];
public override IEnumerable<BuiltIn> ExportedBuiltins => _exportedBuiltIns;
public override IEnumerable<InterpreterDirective> ExportedDirectives => _interpreterDirectives;
}
5 changes: 3 additions & 2 deletions Ergo/Interpreter/Libraries/Compiler/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ public Compiler()

protected readonly HashSet<Signature> InlinedPredicates = new();
public override Atom Module => WellKnown.Modules.Compiler;
public override IEnumerable<BuiltIn> GetExportedBuiltins() { yield break; }
public override IEnumerable<InterpreterDirective> GetExportedDirectives() { yield break; }

public override IEnumerable<BuiltIn> ExportedBuiltins => [];
public override IEnumerable<InterpreterDirective> ExportedDirectives => [];
public void AddInlinedPredicate(Signature sig)
{
InlinedPredicates.Add(sig);
Expand Down
14 changes: 8 additions & 6 deletions Ergo/Interpreter/Libraries/Dict/Dict.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ namespace Ergo.Interpreter.Libraries.Dict;
public class Dict : Library
{
public override Atom Module => WellKnown.Modules.Dict;
public override IEnumerable<BuiltIn> GetExportedBuiltins() => Enumerable.Empty<BuiltIn>()
.Append(new DictKeyValue())
.Append(new With())
;
public override IEnumerable<InterpreterDirective> GetExportedDirectives() => Enumerable.Empty<InterpreterDirective>()
;
private readonly BuiltIn[] _exportedBuiltIns = [
new DictKeyValue(),
new With()
];
private readonly InterpreterDirective[] _interpreterDirectives = [
];
public override IEnumerable<BuiltIn> ExportedBuiltins => _exportedBuiltIns;
public override IEnumerable<InterpreterDirective> ExportedDirectives => _interpreterDirectives;
}
12 changes: 7 additions & 5 deletions Ergo/Interpreter/Libraries/Expansions/Expansions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ public class Expansions : Library
public override Atom Module => WellKnown.Modules.Expansions;

protected readonly Dictionary<Signature, HashSet<Expansion>> Table = new();
public override IEnumerable<BuiltIn> GetExportedBuiltins() => Enumerable.Empty<BuiltIn>()
;
public override IEnumerable<InterpreterDirective> GetExportedDirectives() => Enumerable.Empty<InterpreterDirective>()
.Append(new DefineExpansion())
;
private readonly BuiltIn[] _exportedBuiltIns = [
];
private readonly InterpreterDirective[] _interpreterDirectives = [
new DefineExpansion()
];
public override IEnumerable<BuiltIn> ExportedBuiltins => _exportedBuiltIns;
public override IEnumerable<InterpreterDirective> ExportedDirectives => _interpreterDirectives;

public override void OnErgoEvent(ErgoEvent evt)
{
Expand Down
30 changes: 16 additions & 14 deletions Ergo/Interpreter/Libraries/IO/IO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@ namespace Ergo.Interpreter.Libraries.IO;
public class IO : Library
{
public override Atom Module => WellKnown.Modules.IO;
public override IEnumerable<BuiltIn> GetExportedBuiltins() => Enumerable.Empty<BuiltIn>()
.Append(new Write())
.Append(new WriteCanonical())
.Append(new WriteQuoted())
.Append(new WriteDict())
.Append(new WriteRaw())
.Append(new Read())
.Append(new ReadLine())
.Append(new GetChar())
.Append(new GetSingleChar())
.Append(new PeekChar())
;
public override IEnumerable<InterpreterDirective> GetExportedDirectives() => Enumerable.Empty<InterpreterDirective>()
;
private readonly BuiltIn[] _exportedBuiltIns = [
new Write(),
new WriteCanonical(),
new WriteQuoted(),
new WriteDict(),
new WriteRaw(),
new Read(),
new ReadLine(),
new GetChar(),
new GetSingleChar(),
new PeekChar()
];
private readonly InterpreterDirective[] _interpreterDirectives = [
];
public override IEnumerable<BuiltIn> ExportedBuiltins => _exportedBuiltIns;
public override IEnumerable<InterpreterDirective> ExportedDirectives => _interpreterDirectives;
}
11 changes: 5 additions & 6 deletions Ergo/Interpreter/Libraries/Lambda/Lambda.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ namespace Ergo.Interpreter.Libraries.Lambda;
public class Lambda : Library
{
public override Atom Module => WellKnown.Modules.Lambda;

public override IEnumerable<BuiltIn> GetExportedBuiltins() => Enumerable.Empty<BuiltIn>()
.Append(new Runtime.BuiltIns.Lambda())
;
public override IEnumerable<InterpreterDirective> GetExportedDirectives() => Enumerable.Empty<InterpreterDirective>()
;
private readonly BuiltIn[] _exportedBuiltIns = [
new Runtime.BuiltIns.Lambda(),
];
public override IEnumerable<BuiltIn> ExportedBuiltins => _exportedBuiltIns;
public override IEnumerable<InterpreterDirective> ExportedDirectives => [];
}
18 changes: 10 additions & 8 deletions Ergo/Interpreter/Libraries/List/List.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ namespace Ergo.Interpreter.Libraries.List;
public class List : Library
{
public override Atom Module => WellKnown.Modules.List;
public override IEnumerable<BuiltIn> GetExportedBuiltins() => Enumerable.Empty<BuiltIn>()
.Append(new Nth0())
.Append(new Nth1())
.Append(new Sort())
.Append(new ListSet())
;
public override IEnumerable<InterpreterDirective> GetExportedDirectives() => Enumerable.Empty<InterpreterDirective>()
;

private readonly BuiltIn[] _exportedBuiltIns = [
new Nth0(),
new Nth1(),
new Sort(),
new ListSet()
];

public override IEnumerable<BuiltIn> ExportedBuiltins => _exportedBuiltIns;
public override IEnumerable<InterpreterDirective> ExportedDirectives => [];
}
14 changes: 8 additions & 6 deletions Ergo/Interpreter/Libraries/Math/Math.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ namespace Ergo.Interpreter.Libraries.Math;
public class Math : Library
{
public override Atom Module => WellKnown.Modules.Math;
public override IEnumerable<BuiltIn> GetExportedBuiltins() => Enumerable.Empty<BuiltIn>()
.Append(new Eval())
.Append(new NumberString())
;
public override IEnumerable<InterpreterDirective> GetExportedDirectives() => Enumerable.Empty<InterpreterDirective>()
;

private readonly BuiltIn[] _exportedBuiltIns = [
new Eval(),
new NumberString()
];

public override IEnumerable<BuiltIn> ExportedBuiltins => _exportedBuiltIns;
public override IEnumerable<InterpreterDirective> ExportedDirectives => [];
}
22 changes: 11 additions & 11 deletions Ergo/Interpreter/Libraries/Meta/Meta.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ public class Meta : Library
{
public override Atom Module => WellKnown.Modules.Meta;

private readonly BuiltIn[] _exportedBuiltIns = [
new BagOf(),
new For(),
new Call(),
new FindAll(),
new SetOf(),
new SetupCallCleanup(),
new Choose()
];

public override IEnumerable<BuiltIn> GetExportedBuiltins() => Enumerable.Empty<BuiltIn>()
.Append(new BagOf())
.Append(new For())
.Append(new Call())
.Append(new FindAll())
.Append(new SetOf())
.Append(new SetupCallCleanup())
.Append(new Choose())
;
public override IEnumerable<InterpreterDirective> GetExportedDirectives() => Enumerable.Empty<InterpreterDirective>()
;
public override IEnumerable<BuiltIn> ExportedBuiltins => _exportedBuiltIns;
public override IEnumerable<InterpreterDirective> ExportedDirectives => [];
}
29 changes: 16 additions & 13 deletions Ergo/Interpreter/Libraries/Prologue/Prologue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ namespace Ergo.Interpreter.Libraries.Prologue;
public class Prologue : Library
{
public override Atom Module => WellKnown.Modules.Prologue;
public override IEnumerable<BuiltIn> GetExportedBuiltins() => Enumerable.Empty<BuiltIn>()
.Append(new AssertA())
.Append(new AssertZ())
.Append(new Cut())
.Append(new Not())
.Append(new Retract())
.Append(new RetractAll())
.Append(new Unifiable())
.Append(new Unify())
;
public override IEnumerable<InterpreterDirective> GetExportedDirectives() => Enumerable.Empty<InterpreterDirective>()
.Append(new DeclareMetaPredicate())
;

private readonly BuiltIn[] _exportedBuiltIns = [
new AssertA(),
new AssertZ(),
new Cut(),
new Not(),
new Retract(),
new RetractAll(),
new Unifiable(),
new Unify(),
];
private readonly InterpreterDirective[] _interpreterDirectives = [
new DeclareMetaPredicate()
];
public override IEnumerable<BuiltIn> ExportedBuiltins => _exportedBuiltIns;
public override IEnumerable<InterpreterDirective> ExportedDirectives => _interpreterDirectives;
}
40 changes: 22 additions & 18 deletions Ergo/Interpreter/Libraries/Reflection/Reflection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,26 @@ namespace Ergo.Interpreter.Libraries.Reflection;
public class Reflection : Library
{
public override Atom Module => WellKnown.Modules.Reflection;
public override IEnumerable<BuiltIn> GetExportedBuiltins() => Enumerable.Empty<BuiltIn>()
.Append(new AnonymousComplex())
.Append(new CommaToList())
.Append(new Compare())
.Append(new CopyTerm())
.Append(new Ground())
.Append(new CurrentModule())
.Append(new Nonvar())
.Append(new Number())
.Append(new NumberVars())
.Append(new SequenceType())
.Append(new Term())
.Append(new TermType())
.Append(new Variant())
.Append(new Explain())
;
public override IEnumerable<InterpreterDirective> GetExportedDirectives() => Enumerable.Empty<InterpreterDirective>()
;

private readonly BuiltIn[] _exportedBuiltIns = [
new AnonymousComplex(),
new CommaToList(),
new Compare(),
new CopyTerm(),
new Ground(),
new CurrentModule(),
new Nonvar(),
new Number(),
new NumberVars(),
new SequenceType(),
new Term(),
new TermType(),
new Variant(),
new Explain(),
];
private readonly InterpreterDirective[] _interpreterDirectives = [
];

public override IEnumerable<BuiltIn> ExportedBuiltins => _exportedBuiltIns;
public override IEnumerable<InterpreterDirective> ExportedDirectives => _interpreterDirectives;
}
16 changes: 10 additions & 6 deletions Ergo/Interpreter/Libraries/Set/Set.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ namespace Ergo.Interpreter.Libraries.List;
public class Set : Library
{
public override Atom Module => WellKnown.Modules.Set;
public override IEnumerable<BuiltIn> GetExportedBuiltins() => Enumerable.Empty<BuiltIn>()
.Append(new Union())
.Append(new IsSet())
;
public override IEnumerable<InterpreterDirective> GetExportedDirectives() => Enumerable.Empty<InterpreterDirective>()
;

private readonly BuiltIn[] _exportedBuiltIns = [
new Union(),
new IsSet(),
];
private readonly InterpreterDirective[] _interpreterDirectives = [
];

public override IEnumerable<BuiltIn> ExportedBuiltins => _exportedBuiltIns;
public override IEnumerable<InterpreterDirective> ExportedDirectives => _interpreterDirectives;
}
14 changes: 9 additions & 5 deletions Ergo/Interpreter/Libraries/String/String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ namespace Ergo.Interpreter.Libraries.String;
public class String : Library
{
public override Atom Module => WellKnown.Modules.String;
public override IEnumerable<BuiltIn> GetExportedBuiltins() => Enumerable.Empty<BuiltIn>()
.Append(new FormatString())
;
public override IEnumerable<InterpreterDirective> GetExportedDirectives() => Enumerable.Empty<InterpreterDirective>()
;

private readonly BuiltIn[] _exportedBuiltIns = [
new FormatString(),
];
private readonly InterpreterDirective[] _interpreterDirectives = [
];

public override IEnumerable<BuiltIn> ExportedBuiltins => _exportedBuiltIns;
public override IEnumerable<InterpreterDirective> ExportedDirectives => _interpreterDirectives;
}
17 changes: 11 additions & 6 deletions Ergo/Interpreter/Libraries/Tabling/Tabling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@ public class Tabling : Library

protected readonly Dictionary<ErgoVM, MemoizationContext> MemoizationContextTable = new();
protected readonly Dictionary<Atom, HashSet<Signature>> TabledPredicates = new();
public override IEnumerable<BuiltIn> GetExportedBuiltins() => Enumerable.Empty<BuiltIn>()
.Append(new Tabled())
;
public override IEnumerable<InterpreterDirective> GetExportedDirectives() => Enumerable.Empty<InterpreterDirective>()
.Append(new DeclareTabledPredicate())
;

private readonly BuiltIn[] _exportedBuiltIns = [
new Tabled(),
];
private readonly InterpreterDirective[] _interpreterDirectives = [
new DeclareTabledPredicate(),
];

public override IEnumerable<BuiltIn> ExportedBuiltins => _exportedBuiltIns;
public override IEnumerable<InterpreterDirective> ExportedDirectives => _interpreterDirectives;

public void AddTabledPredicate(Atom module, Signature sig)
{
if (!TabledPredicates.TryGetValue(module, out var sigs))
Expand Down
4 changes: 2 additions & 2 deletions Ergo/Interpreter/Libraries/_Shared/Library.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public abstract class Library
{
public virtual int LoadOrder => 0;
public abstract Atom Module { get; }
public abstract IEnumerable<InterpreterDirective> GetExportedDirectives();
public abstract IEnumerable<BuiltIn> GetExportedBuiltins();
public abstract IEnumerable<InterpreterDirective> ExportedDirectives { get; }
public abstract IEnumerable<BuiltIn> ExportedBuiltins { get; }
public virtual void OnErgoEvent(ErgoEvent evt) { }

}
23 changes: 12 additions & 11 deletions Ergo/Interpreter/Libraries/_Stdlib/Stdlib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ namespace Ergo.Interpreter.Libraries._Stdlib;
public class Stdlib : Library
{
public override int LoadOrder => 0;

public override Atom Module => WellKnown.Modules.Stdlib;
public override IEnumerable<BuiltIn> GetExportedBuiltins() => Enumerable.Empty<BuiltIn>()
;
public override IEnumerable<InterpreterDirective> GetExportedDirectives() => Enumerable.Empty<InterpreterDirective>()
.Append(new DeclareInlinedPredicate())
.Append(new DeclareDynamicPredicate())
.Append(new DeclareModule())
.Append(new DeclareOperator())
.Append(new SetModule())
.Append(new UseModule())
;

private readonly InterpreterDirective[] _exportedDirectives = [
new DeclareInlinedPredicate(),
new DeclareDynamicPredicate(),
new DeclareModule(),
new DeclareOperator(),
new SetModule(),
new UseModule()
];

public override IEnumerable<BuiltIn> ExportedBuiltins => [];
public override IEnumerable<InterpreterDirective> ExportedDirectives => _exportedDirectives;
}
Loading

0 comments on commit 85b4cb4

Please sign in to comment.