Call stack memory optimization

This commit is contained in:
aNNiMON 2023-10-02 19:02:16 +03:00 committed by Victor Melnik
parent 3e01978f22
commit 8a4dcde2a1
3 changed files with 12 additions and 15 deletions

View File

@ -3,7 +3,7 @@ package com.annimon.ownlang.exceptions;
/** /**
* Base type for all runtime exceptions * Base type for all runtime exceptions
*/ */
public abstract class OwnLangRuntimeException extends RuntimeException { public class OwnLangRuntimeException extends RuntimeException {
public OwnLangRuntimeException() { public OwnLangRuntimeException() {
super(); super();
@ -12,4 +12,8 @@ public abstract class OwnLangRuntimeException extends RuntimeException {
public OwnLangRuntimeException(String message) { public OwnLangRuntimeException(String message) {
super(message); super(message);
} }
public OwnLangRuntimeException(String message, Throwable ex) {
super(message, ex);
}
} }

View File

@ -14,7 +14,7 @@ public final class CallStack {
} }
public static synchronized void enter(String name, Function function) { public static synchronized void enter(String name, Function function) {
calls.push(new CallInfo(name, function)); calls.push(new CallInfo(name, function.toString()));
} }
public static synchronized void exit() { public static synchronized void exit() {
@ -25,10 +25,10 @@ public final class CallStack {
return calls; return calls;
} }
public record CallInfo(String name, Function function) { public record CallInfo(String name, String function) {
@Override @Override
public String toString() { public String toString() {
return String.format("%s: %s", name, function.toString().trim()); return String.format("%s: %s", name, function);
} }
} }
} }

View File

@ -1,9 +1,6 @@
package com.annimon.ownlang.parser.ast; package com.annimon.ownlang.parser.ast;
import com.annimon.ownlang.exceptions.ArgumentsMismatchException; import com.annimon.ownlang.exceptions.*;
import com.annimon.ownlang.exceptions.TypeException;
import com.annimon.ownlang.exceptions.VariableDoesNotExistsException;
import com.annimon.ownlang.exceptions.UnknownFunctionException;
import com.annimon.ownlang.lib.*; import com.annimon.ownlang.lib.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
@ -42,13 +39,9 @@ public final class FunctionalExpression extends InterruptableNode implements Exp
} }
final Function f = consumeFunction(functionExpr); final Function f = consumeFunction(functionExpr);
CallStack.enter(functionExpr.toString(), f); CallStack.enter(functionExpr.toString(), f);
try { final Value result = f.execute(values);
final Value result = f.execute(values); CallStack.exit();
CallStack.exit(); return result;
return result;
} catch (ArgumentsMismatchException | TypeException | VariableDoesNotExistsException ex) {
throw new RuntimeException(ex.getMessage() + " in function " + functionExpr, ex);
}
} }
private Function consumeFunction(Expression expr) { private Function consumeFunction(Expression expr) {