Skip to content

Commit

Permalink
add LOAD function
Browse files Browse the repository at this point in the history
  • Loading branch information
brettfo committed Aug 31, 2024
1 parent f1a9c62 commit d42f332
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/IxMilia.Lisp.Test/ExecutionControlTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
Expand Down Expand Up @@ -660,5 +661,32 @@ public async Task HandlerCaseCanInterceptAnErrorAfterSkippingALevel()
Assert.NotNull(capturedError);
Assert.Equal("some error", capturedError.Message);
}

[Fact]
public async Task LoadAndExecuteFile()
{
using var temporaryDirectory = new TemporaryDirectory();
var previousWorkingDirectory = Environment.CurrentDirectory;
try
{
Environment.CurrentDirectory = temporaryDirectory.DirectoryPath;
await File.WriteAllTextAsync(Path.Combine(temporaryDirectory.DirectoryPath, "external-file.lisp"), """
(setf some-other-value 42)
""");
var host = await CreateHostAsync();
var executionState = host.CreateExecutionState();
var evalResult = await host.EvalAsync("test.lisp", """
(load "external-file.lisp")
(+ some-other-value 1)
""",
executionState);
EnsureNotError(evalResult.Value);
Assert.Equal(43, ((LispInteger)evalResult.Value).Value);
}
finally
{
Environment.CurrentDirectory = previousWorkingDirectory;
}
}
}
}
29 changes: 29 additions & 0 deletions src/IxMilia.Lisp.Test/TemporaryDirectory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.IO;

namespace IxMilia.Lisp.Test
{
public class TemporaryDirectory : IDisposable
{
public string DirectoryPath { get; }

public TemporaryDirectory()
{
var parentDir = Path.GetDirectoryName(GetType().Assembly.Location)!;
var tempDirName = $"ixmilia-lisp-{Guid.NewGuid():d}";
DirectoryPath = Path.Combine(parentDir, "test-data", tempDirName);
Directory.CreateDirectory(DirectoryPath);
}

public void Dispose()
{
try
{
Directory.Delete(DirectoryPath, true);
}
catch
{
}
}
}
}
23 changes: 23 additions & 0 deletions src/IxMilia.Lisp/LispDefaultContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,29 @@ public Task<LispObject> Labels(LispHost host, LispExecutionState executionState,
return Task.FromResult<LispObject>(result);
}

[LispMacro("LOAD", Signature = "FILESPEC", Documentation = "Loads the file named by _FILESPEC_ into the environment.")]
public async Task<LispObject> Load(LispHost host, LispExecutionState executionState, LispObject[] args, CancellationToken cancellationToken)
{
// TODO: handle full version, not just a single path
if (args.Length == 1 && args[0] is LispString path)
{
var filePath = path.Value;
var fullPath = Path.Combine(Environment.CurrentDirectory, filePath);
if (!File.Exists(fullPath))
{
executionState.ReportError(new LispError($"File \"{fullPath}\" does not exist"), insertPop: true);
return host.Nil;
}

var content = File.ReadAllText(fullPath);
var result = await host.EvalAsync(fullPath, content, executionState, cancellationToken);
return result.Value;
}

executionState.ReportError(new LispError("Expected a file path"), insertPop: true);
return host.Nil;
}

[LispMacro("DEFPACKAGE")]
public Task<LispObject> DefPackage(LispHost host, LispExecutionState executionState, LispObject[] args, CancellationToken cancellationToken)
{
Expand Down

0 comments on commit d42f332

Please sign in to comment.