3

Is there a way to "reset" the IronScheme engine? I'm essentially aiming to ensure that successive calls to string.Eval() are executed independently.

For example, I want to be execute

"(define x 1.0) (+ x 5.0)".Eval()

then reset, and have the call

"(+ x 3.0)".Eval()

fail as it would if it were executed by itself.

Even better would be a way to give each of n concurrent threads its own independent space in which to execute.

4

1 回答 1

2

您有 2 个 REPL 选项(对于库,它甚至不允许您编写此类代码;p)。

1:重置交互环境。

这可以通过(interaction-environment (new-interaction-environment)).

样本:

> (define x 5) (+ x 5)
10
> (interaction-environment (new-interaction-environment))
> (+ x 5)
Unhandled exception during evaluation:
&undefined
&message: "attempted to use undefined symbol"
&irritants: (x)

2:在 C# 中创建一个新的交互环境。

如果你想要并发环境,这可能是最好的选择。我将这种方法用于IronScheme 在线评估器

样本:

"(define x 1.0) (+ x 5.0)".Eval();
var env = "(new-interaction-environment)".Eval();
"(+ x 3.0)".EvalWithEnvironment(env);

可能的解决方案:

Func<object> reset = 
   "(lambda () (interaction-environment (new-interaction-environment)))".
   Eval<Callable>().Call;

"(define x 1.0) (+ x 5.0)".Eval();

reset();

"(+ x 5.0)".Eval();
于 2012-10-11T05:19:10.640 回答