Skip to content
This repository was archived by the owner on Jan 27, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ It has most features that a language would support:

Many Scheme features are not (yet) supported. Among those are:

* continuation (`call/cc`)
* use square brackets `[...]` in place of parenthesis `(...)`


Expand Down
1 change: 1 addition & 0 deletions src/schemy/Builtins.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public static IDictionary<Symbol, object> CreateBuiltins(Interpreter interpreter
builtins[Symbol.FromString("null?")] = NativeProcedure.Create<object, bool>(x => x is List<object> && ((List<object>)x).Count == 0, "null?");
builtins[Symbol.FromString("assert")] = new NativeProcedure(AssertImpl, "assert");
builtins[Symbol.FromString("load")] = NativeProcedure.Create<string, None>(filename => LoadImpl(interpreter, filename), "load");
builtins[Symbol.FromString("call/cc")] = NativeProcedure.Create<ICallable, object>(Continuation.CallWithCurrentContinuation, "call/cc");

return builtins;
}
Expand Down
51 changes: 51 additions & 0 deletions src/schemy/Continuation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
namespace Schemy
{
class Continuation : Exception
{
object Value { get; set; }
StackTrace Stack { get; set; }
Thread Thread { get; set; }

public static object CallWithCurrentContinuation(ICallable fc1)
{
var ccc = new Continuation { Stack = new StackTrace(), Thread = Thread.CurrentThread };
try
{
var exitproc = NativeProcedure.Create<object, object>(v =>
{
var f1 = new StackTrace().GetFrames();
var c1 = ccc.Stack.GetFrames();
var offset = f1.Length - c1.Length;
if (ccc.Thread == Thread.CurrentThread)
{
for (int i = c1.Length - 1; i >= 0; i--)
{
if (c1[i].GetMethod() != f1[i + offset].GetMethod())
{
throw new NotImplementedException("not supported, continuation called outside dynamic extent");
}
}
}
ccc.Value = v;
throw ccc;
});
return fc1.Call(new List<object> { exitproc });
}
catch (Continuation c)
{
if (ccc == c)
{
return c.Value;
}
else
{
throw;
}
}
}
}
}
1 change: 1 addition & 0 deletions src/schemy/schemy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<Compile Include="Symbol.cs" />
<Compile Include="Utils.cs" />
<EmbeddedResource Include="init.ss"><LogicalName>init.ss</LogicalName></EmbeddedResource>
<Compile Include="Continuation.cs" />
</ItemGroup>

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
21 changes: 20 additions & 1 deletion src/test/tests.ss
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,25 @@
(* a b)))
(assert (= 20 x)))

(define (test-call/cc)
; test call/cc
(assert
(= 20
(call/cc
(lambda (k)
(* 5 4)))))
(assert
(= 4
(call/cc
(lambda (k)
(* 5 (k 4))))))
(assert
(= 6
(+ 2 (call/cc
(lambda (k)
(* 5 (k 4))))))))



;; =========
;; RUN TESTS
Expand All @@ -132,7 +151,7 @@
(test-list)
(test-syntax)
(test-macro)

(test-call/cc)

;; =======================
;; Interpreter integration
Expand Down