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
*/
public abstract class OwnLangRuntimeException extends RuntimeException {
public class OwnLangRuntimeException extends RuntimeException {
public OwnLangRuntimeException() {
super();
@ -12,4 +12,8 @@ public abstract class OwnLangRuntimeException extends RuntimeException {
public OwnLangRuntimeException(String 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) {
calls.push(new CallInfo(name, function));
calls.push(new CallInfo(name, function.toString()));
}
public static synchronized void exit() {
@ -25,10 +25,10 @@ public final class CallStack {
return calls;
}
public record CallInfo(String name, Function function) {
public record CallInfo(String name, String function) {
@Override
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;
import com.annimon.ownlang.exceptions.ArgumentsMismatchException;
import com.annimon.ownlang.exceptions.TypeException;
import com.annimon.ownlang.exceptions.VariableDoesNotExistsException;
import com.annimon.ownlang.exceptions.UnknownFunctionException;
import com.annimon.ownlang.exceptions.*;
import com.annimon.ownlang.lib.*;
import java.util.ArrayList;
import java.util.Iterator;
@ -42,13 +39,9 @@ public final class FunctionalExpression extends InterruptableNode implements Exp
}
final Function f = consumeFunction(functionExpr);
CallStack.enter(functionExpr.toString(), f);
try {
final Value result = f.execute(values);
CallStack.exit();
return result;
} catch (ArgumentsMismatchException | TypeException | VariableDoesNotExistsException ex) {
throw new RuntimeException(ex.getMessage() + " in function " + functionExpr, ex);
}
}
private Function consumeFunction(Expression expr) {