From ecf3f24c28104d862d91cae720498e50c8c53082 Mon Sep 17 00:00:00 2001 From: gnembon Date: Mon, 4 Nov 2019 01:16:24 -0400 Subject: [PATCH] function definition now borrows variables from the defined context, not used context - so much more useful. --- src/main/java/carpet/script/Expression.java | 37 +++++++++------------ 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/src/main/java/carpet/script/Expression.java b/src/main/java/carpet/script/Expression.java index e9201c4e..1fdbe740 100644 --- a/src/main/java/carpet/script/Expression.java +++ b/src/main/java/carpet/script/Expression.java @@ -710,6 +710,21 @@ public class Expression implements Cloneable throw new ExpressionException(expr, token, "Problems in allocating global function "+name); } + Map contextValues = new HashMap<>(); + for (String global : globals) + { + LazyValue lv = context.getVariable(global); + if (lv == null) + { + Value zero = Value.ZERO.reboundedTo(global); + contextValues.put(global, (cc, tt) -> zero); + } + else + { + contextValues.put(global, lv); + } + } + context.host.globalFunctions.put(name, new UserDefinedFunction(arguments, function_context, token) { @Override @@ -724,19 +739,7 @@ public class Expression implements Cloneable } Context newFrame = c.recreate(); - for (String global : globals) - { - LazyValue lv = c.getVariable(global); - if (lv == null) - { - Value zero = Value.ZERO.reboundedTo(global); - newFrame.setVariable(global, (cc, tt) -> zero); - } - else - { - newFrame.setVariable(global, lv); - } - } + contextValues.forEach(newFrame::setVariable); for (int i=0; i