4 import java.util.function.*;
6 public class Environment {
7 public static final Environment root = new Environment(null);
8 private static final ThreadLocal<Environment> current = new ThreadLocal<>();
9 public final Environment parent;
10 private Map<Variable<?>, Object> data = Collections.emptyMap();
12 public static class Variable<T> {
13 private final Supplier<? extends T> ival;
15 private boolean inited;
17 public Variable(Supplier<? extends T> ival) {
21 @SuppressWarnings("unchecked")
23 for(Environment env = current(); env != null; env = env.parent) {
24 Map<Variable<?>, Object> data = env.data;
25 if(data.containsKey(this))
26 return((T)data.get(this));
31 rval = (this.ival == null) ? null : this.ival.get();
40 public Environment(Environment parent) {
44 public Environment() {
48 public <T> void set(Variable<T> var, T val) {
50 Map<Variable<?>, Object> data = new IdentityHashMap<>(this.data);
56 public <T> void clear(Variable<T> var, T val) {
58 Map<Variable<?>, Object> data = new IdentityHashMap<>(this.data);
64 public static Environment current() {
65 Environment ret = current.get();
66 return((ret == null) ? root : ret);
69 public class Frame implements AutoCloseable {
70 private final Environment prev;
73 this.prev = current.get();
74 current.set(Environment.this);
82 public Frame frame() {