diff options
author | Llewellyn Pritchard <xacc.ide@gmail.com> | 2022-08-24 21:11:38 +0200 |
---|---|---|
committer | Llewellyn Pritchard <xacc.ide@gmail.com> | 2022-08-24 21:11:38 +0200 |
commit | 7bdcc8c7c10d5a9cf0ece4b48456cf4c42bcb25d (patch) | |
tree | 6f0d7a7f40e06cdf76d001faeed50f73879b1ec3 | |
parent | c5a26fdae2cf6511f1ee245d58cfa3a3828113b2 (diff) |
Add EvalWithEnvironmentInstance1.0.309
Fix instance substitution with immutable environments
Fixes #136
-rw-r--r-- | IronScheme/IronScheme/RuntimeExtensions.cs | 31 |
1 files changed, 27 insertions, 4 deletions
diff --git a/IronScheme/IronScheme/RuntimeExtensions.cs b/IronScheme/IronScheme/RuntimeExtensions.cs index f5b68e7e..52d78cc7 100644 --- a/IronScheme/IronScheme/RuntimeExtensions.cs +++ b/IronScheme/IronScheme/RuntimeExtensions.cs @@ -55,6 +55,23 @@ namespace IronScheme throw new ArgumentException("importspec cannot be null or empty");
}
+ if (importspec != INTERACTION_ENVIRONMENT)
+ {
+ importspec = importspec.Replace("(environment", "(environment '(only (ironscheme) define begin symbol-value)");
+ }
+
+ var env = se.Evaluate(importspec);
+
+ return EvalWithEnvironmentInstance(expr, env, args);
+ }
+
+ public static object EvalWithEnvironmentInstance(this string expr, object env, params object[] args)
+ {
+ var currentInteractionEnv = se.Evaluate("(interaction-environment)");
+ var envId = string.Format("env:{0}", Guid.NewGuid());
+ Builtins.SetSymbolValueFast(SymbolTable.StringToObject(envId), env);
+ var isInteractive = Builtins.IsTrue(se.Evaluate(string.Format("(interaction-environment? (symbol-value '{0}))", envId)));
+
Guid[] replacements = new Guid[args.Length];
string[] vars = new string[args.Length];
@@ -96,13 +113,12 @@ namespace IronScheme {
if (assigns.Length > 0)
{
- expr = string.Format("(begin {0} {1})", string.Join(" ", assigns), expr);
+ expr = string.Format("({2} {0} {1})", string.Join(" ", assigns), expr, isInteractive ? "begin" : "let ()");
}
- if (importspec != INTERACTION_ENVIRONMENT)
+ if (!isInteractive || env != currentInteractionEnv)
{
- importspec = importspec.Replace("(environment", "(environment '(only (ironscheme) define begin symbol-value)");
- expr = string.Format("(eval '{0} {1})", expr, importspec);
+ expr = string.Format("(eval '{0} (symbol-value '{1}))", expr, envId);
}
return se.Evaluate(expr);
@@ -113,6 +129,8 @@ namespace IronScheme {
Builtins.RemoveLocation(SymbolTable.StringToObject(vars[i]));
}
+
+ Builtins.RemoveLocation(SymbolTable.StringToObject(envId));
}
}
@@ -126,6 +144,11 @@ namespace IronScheme return (T)EvalWithEnvironment(expr, importspec, args);
}
+ public static T EvalWithEnvironmentInstance<T>(this string expr, object env, params object[] args)
+ {
+ return (T)EvalWithEnvironmentInstance(expr, env, args);
+ }
+
readonly static Type[] CallTargets =
{
typeof(CallTarget0),
|