Skip to content

Commit

Permalink
add meta:choose/*
Browse files Browse the repository at this point in the history
  • Loading branch information
G3Kappa committed May 10, 2024
1 parent ac49a98 commit 1cc7dbf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions Ergo/Interpreter/Libraries/Meta/Meta.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public override IEnumerable<BuiltIn> GetExportedBuiltins() => Enumerable.Empty<B
.Append(new FindAll())
.Append(new SetOf())
.Append(new SetupCallCleanup())
.Append(new Choose())
;
public override IEnumerable<InterpreterDirective> GetExportedDirectives() => Enumerable.Empty<InterpreterDirective>()
;
Expand Down
20 changes: 20 additions & 0 deletions Ergo/Runtime/Built-Ins/Meta/Choose.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Ergo.Runtime.BuiltIns;

public sealed class Choose : BuiltIn
{
public Random Rng { get; set; } = new Random();
private readonly Call CallInst = new();

public Choose()
: base("", new("choose"), default, WellKnown.Modules.Meta)
{
}

public override ErgoVM.Op Compile() => vm =>
{
var arg = vm.Args[Rng.Next(vm.Arity)];
vm.Arity = 1;
vm.SetArg(0, arg);
CallInst.Compile()(vm);
};
}

0 comments on commit 1cc7dbf

Please sign in to comment.