From 2167f56be44848fb11e6025141b9f767a4049eaa Mon Sep 17 00:00:00 2001 From: Juerg Lang Date: Sun, 10 Sep 2023 22:01:51 +0200 Subject: [PATCH] refactoring Env --- .../github/jlangch/venice/impl/env/Env.java | 70 ++++++++++--------- .../SpecialForms_OtherFunctions.java | 25 ++++--- 2 files changed, 53 insertions(+), 42 deletions(-) diff --git a/src/main/java/com/github/jlangch/venice/impl/env/Env.java b/src/main/java/com/github/jlangch/venice/impl/env/Env.java index 282d689a7..c3ce74485 100644 --- a/src/main/java/com/github/jlangch/venice/impl/env/Env.java +++ b/src/main/java/com/github/jlangch/venice/impl/env/Env.java @@ -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))) { @@ -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; } /** @@ -222,7 +217,31 @@ public boolean isBound(final VncSymbol sym) { * @return the value or Nil 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 + * + *

Unqualified symbol resolution: + *

    + *
  1. try to resolve the symbol from the local namespace
  2. + *
  3. try to resolve the symbol from the global current namespace defined by *ns*
  4. + *
  5. try to resolve the symbol from the global 'core' namespace
  6. + *
+ * + *

Qualified symbol resolution: + *

    + *
  1. qualified symbols are resolved exclusively from the global symbols
  2. + *
+ * + * @param sym a symbol + * @return the value or Nil if not found + */ + public VncVal getSymbol(final VncSymbol sym) { + final Var v = getVar(sym); + return v != null ? v.getVal() : Nil; } /** @@ -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); } } } diff --git a/src/main/java/com/github/jlangch/venice/impl/specialforms/SpecialForms_OtherFunctions.java b/src/main/java/com/github/jlangch/venice/impl/specialforms/SpecialForms_OtherFunctions.java index ea575e42f..89a48b68c 100644 --- a/src/main/java/com/github/jlangch/venice/impl/specialforms/SpecialForms_OtherFunctions.java +++ b/src/main/java/com/github/jlangch/venice/impl/specialforms/SpecialForms_OtherFunctions.java @@ -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; /** @@ -410,7 +410,10 @@ public VncVal apply( .meta() .arglists("(inspect val)") .doc("Inspect a value") - .examples("(inspect '+)") + .examples( + "(inspect '+)", + "(inspect (symbol \"+\"))" + ) .build() ) { @Override @@ -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)); } @@ -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; @@ -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;