-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
G3Kappa
committed
May 21, 2024
1 parent
d366979
commit 089c64b
Showing
2 changed files
with
78 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,46 @@ | ||
using Ergo.Facade; | ||
using Ergo.Interpreter; | ||
using Ergo.Interpreter.Libraries; | ||
|
||
// The "Standard" Ergo Facade is the recommended pre-configured default environment. | ||
// You can extend it, modify it, or start from an empty facade. | ||
var facade = ErgoFacade.Standard; | ||
var shell = facade.BuildShell(); | ||
await foreach (var _ in shell.Repl()) | ||
|
||
var interpreter = facade.BuildInterpreter(InterpreterFlags.Default); | ||
var interpreterScope = interpreter.CreateScope(); | ||
|
||
var kb = interpreterScope.BuildKnowledgeBase(CompilerFlags.Default, beforeCompile: kb => | ||
{ | ||
if (interpreterScope.Parse<Predicate>("message_sent(M) :- write('User: ', M), nl.") | ||
.TryGetValue(out var pred)) | ||
kb.AssertA(pred); | ||
}); | ||
var vm = facade.BuildVM(kb); | ||
|
||
var eventTest = new EventTest(); | ||
using var hook = Hook.MarshallEvent(typeof(EventTest).GetEvent(nameof(EventTest.MessageSent)), eventTest, new Atom("message_sent"), WellKnown.Modules.User)(vm); | ||
eventTest.SendMessage("hello,"); | ||
eventTest.SendMessage("world!"); | ||
|
||
var del = Hook.MarshallDelegate<Action<string>>((s) => { }, new Atom("message_sent"), WellKnown.Modules.User)(vm); | ||
del("also hello,"); | ||
del("world!!!!!!"); | ||
|
||
// You can use the Ergo VM directly or through the Ergo Shell, which provides a | ||
// bunch of useful commands to interact with knowledge bases and module files. | ||
//var shell = facade.BuildShell(); | ||
|
||
//await foreach (var _ in shell.Repl()) | ||
//{ | ||
// ; | ||
//} | ||
|
||
|
||
public class EventTest | ||
{ | ||
; | ||
public event Action<string> MessageSent = _ => { }; | ||
public void SendMessage(string msg) | ||
{ | ||
MessageSent?.Invoke(msg); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters