Added a way to retrieve the current expression instance.

This commit is contained in:
TomyLobo 2011-11-30 05:15:56 +01:00
parent 662e597f1a
commit c6518a9243

View File

@ -22,6 +22,7 @@ package com.sk89q.worldedit.expression;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import com.sk89q.worldedit.expression.lexer.Lexer;
import com.sk89q.worldedit.expression.lexer.tokens.Token;
@ -57,6 +58,8 @@ import com.sk89q.worldedit.expression.runtime.Variable;
* @author TomyLobo
*/
public class Expression {
private static final ThreadLocal<Stack<Expression>> instance = new ThreadLocal<Stack<Expression>>();
private final Map<String, RValue> variables = new HashMap<String, RValue>();
private final String[] variableNames;
private RValue root;
@ -98,10 +101,13 @@ public class Expression {
((Variable) invokable).value = values[i];
}
pushInstance();
try {
return root.getValue();
} catch (ReturnException e) {
return e.getValue();
} finally {
popInstance();
}
}
@ -122,4 +128,27 @@ public class Expression {
return variable;
}
public static Expression getInstance() {
return instance.get().peek();
}
private void pushInstance() {
Stack<Expression> foo = instance.get();
if (foo == null) {
instance.set(foo = new Stack<Expression>());
}
foo.push(this);
}
private void popInstance() {
Stack<Expression> foo = instance.get();
foo.pop();
if (foo.isEmpty()) {
instance.set(null);
}
}
}