function definition now borrows variables from the defined context, not used context - so much more useful.

This commit is contained in:
gnembon committed 2019-11-04 01:16:24 -04:00
1 parent efb87c8497
commit ecf3f24c28
1 file changed
+16 -21
+16 -21
View File
@@ -710,6 +710,21 @@ public class Expression implements Cloneable
throw new ExpressionException(expr, token, "Problems in allocating global function "+name); throw new ExpressionException(expr, token, "Problems in allocating global function "+name);
} }
Map<String, LazyValue> 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) context.host.globalFunctions.put(name, new UserDefinedFunction(arguments, function_context, token)
{ {
@Override @Override
@@ -724,19 +739,7 @@ public class Expression implements Cloneable
} }
Context newFrame = c.recreate(); Context newFrame = c.recreate();
for (String global : globals) contextValues.forEach(newFrame::setVariable);
{
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);
}
}
for (int i=0; i<arguments.size(); i++) for (int i=0; i<arguments.size(); i++)
{ {
String arg = arguments.get(i); String arg = arguments.get(i);
@@ -774,14 +777,6 @@ public class Expression implements Cloneable
{ {
throw new ExpressionException(e, t, "Error while evaluating expression: "+exc.getMessage()); throw new ExpressionException(e, t, "Error while evaluating expression: "+exc.getMessage());
} }
for (String global: globals)
{
LazyValue lv = newFrame.getVariable(global);
if (lv != null)
{
c.setVariable(global, lv);
}
}
if (rethrow) if (rethrow)
{ {
throw new ThrowStatement(retVal); throw new ThrowStatement(retVal);