diff --git a/impls/jq/core.jq b/impls/jq/core.jq index 87d6385c7a..caf8ffd202 100644 --- a/impls/jq/core.jq +++ b/impls/jq/core.jq @@ -382,7 +382,6 @@ def core_interp(arguments; env): select(.function == "vec") | {kind:"vector", value:arguments[0].value} ) // ( select(.function == "nth") - | _debug(arguments) | arguments[0].value as $lst | arguments[1].value as $idx | if ($lst|length < $idx) or ($idx < 0) then diff --git a/impls/jq/utils.jq b/impls/jq/utils.jq index 7b0876b7cb..a4b3055ad9 100644 --- a/impls/jq/utils.jq +++ b/impls/jq/utils.jq @@ -45,30 +45,34 @@ def find_free_references(keys): | if .kind == "symbol" then if keys | contains([$dot.value]) then [] else [$dot.value] end else if "list" == $dot.kind then - # if - scan args - # def! - scan body - # let* - add keys sequentially, scan body - # fn* - add keys, scan body - # quote - [] - # quasiquote - ??? - $dot.value[0] as $head - | if $head.kind == "symbol" then - ( - select($head.value == "if") | $dot.value[1:] | map(_refs) | reduce .[] as $x ([]; . + $x) - ) // ( - select($head.value == "def!") | $dot.value[2] | _refs - ) // ( - select($head.value == "let*") | $dot.value[2] | find_free_references(($dot.value[1].value as $value | ([ range(0; $value|length; 2) ] | map(select(. % 2 == 0) | $value[.].value))) + keys) - ) // ( - select($head.value == "fn*") | $dot.value[2] | find_free_references(($dot.value[1].value | map(.value)) + keys) - ) // ( - select($head.value == "quote") | [] - ) // ( - select($head.value == "quasiquote") | [] - ) // ($dot.value | map(_refs) | reduce .[] as $x ([]; . + $x)) - else - [ $dot.values[1:][] | _refs ] - end + if $dot.value|length == 0 then + [] + else + # if - scan args + # def! - scan body + # let* - add keys sequentially, scan body + # fn* - add keys, scan body + # quote - [] + # quasiquote - ??? + $dot.value[0] as $head + | if $head.kind == "symbol" then + ( + select($head.value == "if") | $dot.value[1:] | map(_refs) | reduce .[] as $x ([]; . + $x) + ) // ( + select($head.value == "def!") | $dot.value[2] | _refs + ) // ( + select($head.value == "let*") | $dot.value[2] | find_free_references(($dot.value[1].value as $value | ([ range(0; $value|length; 2) ] | map(select(. % 2 == 0) | $value[.].value))) + keys) + ) // ( + select($head.value == "fn*") | $dot.value[2] | find_free_references(($dot.value[1].value | map(.value)) + keys) + ) // ( + select($head.value == "quote") | [] + ) // ( + select($head.value == "quasiquote") | [] + ) // ($dot.value | map(_refs) | reduce .[] as $x ([]; . + $x)) + else + [ $dot.values[1:][] | _refs ] + end + end else if "vector" == $dot.kind then ($dot.value | map(_refs) | reduce .[] as $x ([]; . + $x)) else if "hashmap" == $dot.kind then diff --git a/impls/tests/step4_if_fn_do.mal b/impls/tests/step4_if_fn_do.mal index c4dd1dc1b0..b98f2dae2a 100644 --- a/impls/tests/step4_if_fn_do.mal +++ b/impls/tests/step4_if_fn_do.mal @@ -181,6 +181,8 @@ ;=>7 ( (fn* () 4) ) ;=>4 +( (fn* () ()) ) +;=>() ( (fn* (f x) (f x)) (fn* (a) (+ 1 a)) 7) ;=>8