Skip to content

Commit

Permalink
Base: Add interaction-environment and environment arg to eval
Browse files Browse the repository at this point in the history
I'm not 100% sure the `eval` part of this is implemented correctly just
yet. Validation of that will come in subsequent tests when there's more
than just `interaction-environment` available.
  • Loading branch information
paulirwin committed Aug 21, 2023
1 parent df29350 commit 4e5ce10
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
16 changes: 14 additions & 2 deletions Colibri.Core/Macros/CoreMacros.cs
Original file line number Diff line number Diff line change
Expand Up @@ -731,9 +731,21 @@ public static object DelayForce(ColibriRuntime runtime, Scope scope, object?[] a

public static object? Eval(ColibriRuntime runtime, Scope scope, object?[] args)
{
if (args.Length != 1)
switch (args.Length)
{
throw new ArgumentException("eval requires one argument");
case 0 or > 2:
throw new ArgumentException("eval requires one or two arguments");
case 2:
{
if (runtime.Evaluate(scope, args[1]) is not Scope argScope)
{
// ReSharper disable once StringLiteralTypo
throw new ArgumentException("eval's second argument must be a scope");
}

scope = argScope;
break;
}
}

return runtime.Evaluate(scope, runtime.Evaluate(scope, args[0]));
Expand Down
7 changes: 7 additions & 0 deletions Colibri.Core/Macros/EnvironmentMacros.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Colibri.Core.Macros;

public static class EnvironmentMacros
{
public static object InteractionEnvironment(ColibriRuntime runtime, Scope scope, object?[] args)
=> runtime.UserScope;
}
9 changes: 6 additions & 3 deletions Colibri.Core/StandardLibraries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ static StandardLibraries()
// TODO: (scheme load) library
(new LibraryName("scheme", "process-context"), ProcessContext),
(new LibraryName("scheme", "read"), Read),
// TODO: (scheme repl) library
(new LibraryName("scheme", "repl"), Repl),
(new LibraryName("scheme", "time"), Time),
(new LibraryName("scheme", "write"), Write),
// TODO: (scheme r5rs) library
Expand Down Expand Up @@ -437,8 +437,11 @@ static StandardLibraries()
{
["read"] = (MacroExpression)PortMacros.Read,
});

// TODO: (scheme repl) library

public static readonly Library Repl = new(new Dictionary<string, object?>
{
["interaction-environment"] = (MacroExpression)EnvironmentMacros.InteractionEnvironment,
});

public static readonly Library Time = new(new Dictionary<string, object?>
{
Expand Down
25 changes: 25 additions & 0 deletions Colibri.Tests/EnvironmentTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Colibri.Core;

namespace Colibri.Tests;

public class EnvironmentTests
{
[Fact]
public void InteractionEnvironmentTest()
{
const string program = @"
define x 42
eval 'x (interaction-environment)
";

var runtime = new ColibriRuntime();

var result = runtime.EvaluateProgram(program);

Assert.Equal(42, result);

var env = runtime.EvaluateProgram("(interaction-environment)");

Assert.Same(env, runtime.UserScope);
}
}

0 comments on commit 4e5ce10

Please sign in to comment.