Skip to content

Commit

Permalink
refactoring Env
Browse files Browse the repository at this point in the history
  • Loading branch information
jlangch committed Sep 10, 2023
1 parent 7ceddda commit 2167f56
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 42 deletions.
70 changes: 38 additions & 32 deletions src/main/java/com/github/jlangch/venice/impl/env/Env.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,10 @@ public int level() {
* @throws SymbolNotFoundException if the symbol does not exist.
*/
public VncVal get(final VncSymbol sym) {
final VncVal val = getOrElse(sym, null);
if (val != null) {
return val;
final Var v = getVar(sym);

if (v != null) {
return v.getVal();
}
else {
try (WithCallStack cs = new WithCallStack(CallFrame.from(sym))) {
Expand Down Expand Up @@ -193,14 +194,8 @@ public boolean isLocal(final VncSymbol sym) {
* @return returns true if a symbol is bound to a value else false
*/
public boolean isBound(final VncSymbol sym) {
if (sym.hasNamespace()) {
// if we got a namespace it must be a global var
return getGlobalVar(sym) != null;
}
else {
final Var v = findLocalVar(sym);
return v != null ? true : getGlobalVar(sym) != null;
}
final Var v = getVar(sym);
return v != null;
}

/**
Expand All @@ -222,7 +217,31 @@ public boolean isBound(final VncSymbol sym) {
* @return the value or <code>Nil</code> if not found
*/
public VncVal getOrNil(final VncSymbol sym) {
return getOrElse(sym, Nil);
final Var v = getVar(sym);
return v == null ? Nil : v.getVal();
}

/**
* Look up a symbol
*
* <p>Unqualified symbol resolution:
* <ol>
* <li>try to resolve the symbol from the local namespace</li>
* <li>try to resolve the symbol from the global current namespace defined by *ns*</li>
* <li>try to resolve the symbol from the global 'core' namespace</li>
* </ol>
*
* <p>Qualified symbol resolution:
* <ol>
* <li>qualified symbols are resolved exclusively from the global symbols</li>
* </ol>
*
* @param sym a symbol
* @return the value or <code>Nil</code> if not found
*/
public VncVal getSymbol(final VncSymbol sym) {
final Var v = getVar(sym);
return v != null ? v.getVal() : Nil;
}

/**
Expand Down Expand Up @@ -569,44 +588,31 @@ private DynamicVar findGlobalDynamicVar(final VncSymbol sym) {
return null;
}

private VncVal getOrElse(final VncSymbol sym, final VncVal defaultVal) {
private Var getVar(final VncSymbol sym) {
if (sym.hasNamespace()) {
// if we got a namespace it must be a global var
final Var glob = getGlobalVar(sym);
return glob == null ? defaultVal : glob.getVal();
return getGlobalVar(sym);
}
else {
final Var local = findLocalVar(sym);

if (globalVarLookupOptimization) {
if (local != null) {
if (local instanceof GlobalRefVar) {
final Var glob = getGlobalVar(sym);
return glob == null ? defaultVal : glob.getVal();
}
else {
return local.getVal();
}
}
return local instanceof GlobalRefVar ? getGlobalVar(sym) : local;
}
else {
final Var glob = getGlobalVar(sym);
if (glob != null) {
localSymbols.put(sym, new GlobalRefVar(sym));
return glob.getVal();
return glob;
}
else {
return defaultVal;
return null;
}
}
}
else {
if (local != null) {
return local.getVal();
}
else {
final Var glob = getGlobalVar(sym);
return glob == null ? defaultVal : glob.getVal();
}
return local != null ? local : getGlobalVar(sym);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@
import com.github.jlangch.venice.impl.types.util.Coerce;
import com.github.jlangch.venice.impl.types.util.Types;
import com.github.jlangch.venice.impl.util.ArityExceptions.FnType;
import com.github.jlangch.venice.impl.util.callstack.CallFrame;
import com.github.jlangch.venice.impl.util.callstack.CallStack;
import com.github.jlangch.venice.impl.util.callstack.WithCallStack;
import com.github.jlangch.venice.impl.util.Inspector;
import com.github.jlangch.venice.impl.util.MeterRegistry;
import com.github.jlangch.venice.impl.util.SymbolMapBuilder;
import com.github.jlangch.venice.impl.util.callstack.CallFrame;
import com.github.jlangch.venice.impl.util.callstack.CallStack;
import com.github.jlangch.venice.impl.util.callstack.WithCallStack;


/**
Expand Down Expand Up @@ -410,7 +410,10 @@ public VncVal apply(
.meta()
.arglists("(inspect val)")
.doc("Inspect a value")
.examples("(inspect '+)")
.examples(
"(inspect '+)",
"(inspect (symbol \"+\"))"
)
.build()
) {
@Override
Expand All @@ -422,6 +425,7 @@ public VncVal apply(
) {
specialFormCallValidation(ctx, "inspect");
assertArity("inspect", FnType.SpecialForm, args, 1);

final VncSymbol sym = Coerce.toVncSymbol(ctx.getEvaluator().evaluate(args.first(), env, false));
return Inspector.inspect(env.get(sym));
}
Expand Down Expand Up @@ -452,8 +456,9 @@ public VncVal apply(
) {
specialFormCallValidation(ctx, "resolve");
assertArity("resolve", FnType.SpecialForm, args, 1);
return env.getOrNil(Coerce.toVncSymbol(
ctx.getEvaluator().evaluate(args.first(), env, false)));

final VncSymbol sym = Coerce.toVncSymbol(ctx.getEvaluator().evaluate(args.first(), env, false));
return env.getOrNil(sym);
}

private static final long serialVersionUID = -1848883965231344442L;
Expand Down Expand Up @@ -483,10 +488,10 @@ public VncVal apply(
final Env env,
final SpecialFormsContext ctx
) {
return VncBoolean.of(
env.isBound(
Coerce.toVncSymbol(
ctx.getEvaluator().evaluate(args.first(), env, false))));
assertArity("bound?", FnType.SpecialForm, args, 1);

final VncSymbol sym = Coerce.toVncSymbol(ctx.getEvaluator().evaluate(args.first(), env, false));
return VncBoolean.of(env.isBound(sym));
}

private static final long serialVersionUID = -1848883965231344442L;
Expand Down

0 comments on commit 2167f56

Please sign in to comment.