diff --git a/src/main/java/com/github/jlangch/venice/impl/specialforms/SpecialForms_VarFunctions.java b/src/main/java/com/github/jlangch/venice/impl/specialforms/SpecialForms_VarFunctions.java index a97295f6b..018bdcda2 100644 --- a/src/main/java/com/github/jlangch/venice/impl/specialforms/SpecialForms_VarFunctions.java +++ b/src/main/java/com/github/jlangch/venice/impl/specialforms/SpecialForms_VarFunctions.java @@ -67,7 +67,7 @@ public class SpecialForms_VarFunctions { " (def x 10) \n" + " (var-get 'x))") .seeAlso( - "var-ns", "var-name", "var-local?", "var-global?", "var-thread-local?") + "var-name", "var-ns", "var-local?", "var-global?", "var-thread-local?") .build() ) { @Override @@ -79,6 +79,7 @@ public VncVal apply( ) { specialFormCallValidation(ctx, "var-get"); assertArity("var-get", FnType.SpecialForm, args, 1); + final VncSymbol sym = Types.isVncSymbol(args.first()) ? (VncSymbol)args.first() : Coerce.toVncSymbol( @@ -89,6 +90,62 @@ public VncVal apply( private static final long serialVersionUID = -1848883965231344442L; }; + public static VncSpecialForm var_name = + new VncSpecialForm( + "var-name", + VncSpecialForm + .meta() + .arglists("(var-name v)") + .doc("Returns the name of the var's symbol") + .examples( + "(var-name +)", + "(var-name '+)", + "(var-name (symbol \"+\"))", + ";; aliased function \n" + + "(do \n" + + " (ns foo) \n" + + " (def add +)\n" + + " (var-name add))", + "(do \n" + + " (def x 10) \n" + + " (var-name x))", + "(let [x 10] \n" + + " (var-name x))", + ";; compare with name \n" + + "(do \n" + + " (ns foo) \n" + + " (def add +)\n" + + " (name add))", + ";; compare aliased function with name \n" + + "(do \n" + + " (ns foo) \n" + + " (def add +)\n" + + " (name add))") + .seeAlso( + "name", "var-get", "var-ns", "var-local?", "var-global?", "var-thread-local?") + .build() + ) { + @Override + public VncVal apply( + final VncVal specialFormMeta, + final VncList args, + final Env env, + final SpecialFormsContext ctx + ) { + specialFormCallValidation(ctx, "var-name"); + assertArity("var-name", FnType.SpecialForm, args, 1); + + final VncSymbol sym = Types.isVncSymbol(args.first()) + ? (VncSymbol)args.first() + : Coerce.toVncSymbol( + ctx.getEvaluator().evaluate(args.first(), env, false)); + return new VncString(sym.getSimpleName()); + } + + private static final long serialVersionUID = -1848883965231344442L; + + }; + public static VncSpecialForm var_ns = new VncSpecialForm( "var-ns", @@ -159,39 +216,19 @@ else if (env.isLocal(sym)) { private static final long serialVersionUID = -1848883965231344442L; }; - public static VncSpecialForm var_name = + public static VncSpecialForm var_sym = new VncSpecialForm( - "var-name", + "var-sym", VncSpecialForm .meta() - .arglists("(var-name v)") - .doc("Returns the name of the var's symbol") + .arglists("(var-sym v)") + .doc("Returns the var's symbol") .examples( - "(var-name +)", - "(var-name '+)", - "(var-name (symbol \"+\"))", - ";; aliased function \n" + - "(do \n" + - " (ns foo) \n" + - " (def add +)\n" + - " (var-name add))", - "(do \n" + - " (def x 10) \n" + - " (var-name x))", - "(let [x 10] \n" + - " (var-name x))", - ";; compare with name \n" + - "(do \n" + - " (ns foo) \n" + - " (def add +)\n" + - " (name add))", - ";; compare aliased function with name \n" + - "(do \n" + - " (ns foo) \n" + - " (def add +)\n" + - " (name add))") + "(var-sym +)", + "(var-sym '+)", + "(var-sym (symbol \"+\"))") .seeAlso( - "name", "var-get", "var-ns", "var-local?", "var-global?", "var-thread-local?") + "name", "var-get", "var-name", "var-ns", "var-local?", "var-global?", "var-thread-local?") .build() ) { @Override @@ -201,13 +238,14 @@ public VncVal apply( final Env env, final SpecialFormsContext ctx ) { - specialFormCallValidation(ctx, "var-name"); - assertArity("var-name", FnType.SpecialForm, args, 1); + specialFormCallValidation(ctx, "var-sym"); + assertArity("var-sym", FnType.SpecialForm, args, 1); + final VncSymbol sym = Types.isVncSymbol(args.first()) ? (VncSymbol)args.first() : Coerce.toVncSymbol( ctx.getEvaluator().evaluate(args.first(), env, false)); - return new VncString(sym.getSimpleName()); + return env.getSymbol(sym); } private static final long serialVersionUID = -1848883965231344442L; @@ -225,13 +263,13 @@ public VncVal apply( "(var-local? +)", "(var-local? '+)", "(var-local? (symbol \"+\"))", + "(let [x 10] \n" + + " (var-local? x)) ", "(do \n" + " (def x 10) \n" + - " (var-local? x)) ", - "(let [x 10] \n" + " (var-local? x)) ") .seeAlso( - "var-get", "var-ns", "var-name", "var-global?", "var-thread-local?") + "var-get", "var-name", "var-ns", "var-global?", "var-thread-local?") .build() ) { @Override @@ -242,6 +280,7 @@ public VncVal apply( final SpecialFormsContext ctx ) { assertArity("var-local?", FnType.SpecialForm, args, 1); + final VncSymbol sym = Types.isVncSymbol(args.first()) ? (VncSymbol)args.first() : Coerce.toVncSymbol( @@ -261,9 +300,9 @@ public VncVal apply( .doc("Returns true if the var is thread-local else false") .examples( "(binding [x 100] \n" + - " (var-local? x))") + " (var-thread-local? x))") .seeAlso( - "var-get", "var-ns", "var-name", "var-local?", "var-global?") + "var-get", "var-name", "var-ns", "var-local?", "var-global?") .build() ) { @Override @@ -274,6 +313,7 @@ public VncVal apply( final SpecialFormsContext ctx ) { assertArity("var-thread-local?", FnType.SpecialForm, args, 1); + final VncSymbol sym = Types.isVncSymbol(args.first()) ? (VncSymbol)args.first() : Coerce.toVncSymbol( @@ -301,7 +341,7 @@ public VncVal apply( "(let [x 10] \n" + " (var-global? x)) ") .seeAlso( - "var-get", "var-ns", "var-name", "var-local?", "var-thread-local?") + "var-get", "var-name", "var-ns", "var-local?", "var-thread-local?") .build() ) { @Override @@ -312,6 +352,7 @@ public VncVal apply( final SpecialFormsContext ctx ) { assertArity("var-global?", FnType.SpecialForm, args, 1); + final VncSymbol sym = Types.isVncSymbol(args.first()) ? (VncSymbol)args.first() : Coerce.toVncSymbol( @@ -332,10 +373,11 @@ public VncVal apply( public static final Map ns = new SymbolMapBuilder() .add(var_get) - .add(var_globalQ) - .add(var_localQ) .add(var_name) + .add(var_sym) .add(var_ns) + .add(var_globalQ) + .add(var_localQ) .add(var_thread_localQ) .toMap(); } diff --git a/src/test/java/com/github/jlangch/venice/impl/specialforms/SpecialFormsTest_var.java b/src/test/java/com/github/jlangch/venice/impl/specialforms/SpecialFormsTest_var.java index 0ea30a89e..de472df9e 100644 --- a/src/test/java/com/github/jlangch/venice/impl/specialforms/SpecialFormsTest_var.java +++ b/src/test/java/com/github/jlangch/venice/impl/specialforms/SpecialFormsTest_var.java @@ -22,6 +22,8 @@ package com.github.jlangch.venice.impl.specialforms; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; @@ -45,4 +47,29 @@ public void test_var_name() { assertEquals("split", new Venice().eval("(var-name str/split)")); assertEquals("x", new Venice().eval("(let [x 100] (var-name x))")); } + + @Test + public void test_var_global() { + assertTrue( (Boolean)new Venice().eval("(do (def x 100) (var-global? x))")); + assertFalse((Boolean)new Venice().eval("(do (def x 100) (var-local? x))")); + assertFalse((Boolean)new Venice().eval("(do (def x 100) (var-thread-local? x))")); + } + + @Test + public void test_var_local() { + assertFalse((Boolean)new Venice().eval("(do (let [x 100] (var-global? x)))")); + assertTrue( (Boolean)new Venice().eval("(do (let [x 100] (var-local? x)))")); + assertFalse((Boolean)new Venice().eval("(do (let [x 100] (var-thread-local? x)))")); + + assertFalse((Boolean)new Venice().eval("(do (defn foo [x] (var-global? x)) (foo 0))")); + assertTrue( (Boolean)new Venice().eval("(do (defn foo [x] (var-local? x)) (foo 0))")); + assertFalse((Boolean)new Venice().eval("(do (defn foo [x] (var-thread-local? x)) (foo 0))")); + } + + @Test + public void test_var_threadlocal() { + assertFalse((Boolean)new Venice().eval("(do (binding [x 100] (var-global? x)))")); + assertFalse((Boolean)new Venice().eval("(do (binding [x 100] (var-local? x)))")); + assertTrue( (Boolean)new Venice().eval("(do (binding [x 100] (var-thread-local? x)))")); + } }