Skip to content

Commit

Permalink
jq: backport last changes from stepA to step 3-9
Browse files Browse the repository at this point in the history
  • Loading branch information
asarhaddon authored and kanaka committed Oct 7, 2024
1 parent 55c9497 commit 4319594
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 79 deletions.
11 changes: 3 additions & 8 deletions impls/jq/step3_env.jq
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def EVAL(env):
select(.[0].value == "let*") |
(reduce (.[1].value | nwise(2)) as $xvalue (
# Initial accumulator
{parent: env, environment:{}, fallback:null};
{parent:env, environment:{}, fallback:null};
# Loop body
. as $env | $xvalue[1] | EVAL($env) |
env_set(.env; $xvalue[0].value; .expr)
Expand Down Expand Up @@ -100,20 +100,15 @@ def EVAL(env):
) //
(
select(.kind == "symbol") |
.value | env_get(env) // jqmal_error("'\(.)' not found ") |
.value |
env_get(env) // jqmal_error("'\(.)' not found") |
{expr:., env:env}
) //
{expr:., env:env};

def PRINT:
pr_str;

def childEnv(binds; value):
{
parent: .,
environment: [binds, value] | transpose | map({(.[0]): .[1]}) | from_entries
};

def repl:
# Infinite generator, interrupted by an exception or ./run.
. as $env | "user> " | __readline |
Expand Down
16 changes: 7 additions & 9 deletions impls/jq/step4_if_fn_do.jq
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def interpret(arguments; env; _eval):
(if $DEBUG then debug("INTERP: \(. | pr_str(env))") else . end) |
(select(.kind == "fn") |
arg_check(arguments) |
(core_interp(arguments; env) | {expr:., env:env})
core_interp(arguments; env) | {expr:., env:env}
) //
(select(.kind == "function") as $fn |
# todo: arg_check
Expand All @@ -100,8 +100,8 @@ def interpret(arguments; env; _eval):
. as $env | try env_set(
.;
$name;
$name | env_get(env) // jqmal_error("'\(.)' not found")
| . as $xvalue
$name | env_get(env) // jqmal_error("'\(.)' not found") |
. as $xvalue
| if $xvalue.kind == "function" then
setpath(["free_referencess"]; $fn.free_referencess)
else
Expand All @@ -124,7 +124,7 @@ def interpret(arguments; env; _eval):
env: env
}
# | . as $dot
# | debug("FNPOST \(.expr | pr_str) \(; $fn.binds[0] | env_get($dot.expr.env) | pr_str)")
# | debug("FNPOST \(.expr | pr_str) \($fn.binds[0] | env_get($dot.expr.env) | pr_str)")
# | debug("INTERP \($src) = \(.expr | pr_str)")
) //
jqmal_error("Unsupported function kind \(.kind)");
Expand All @@ -142,16 +142,15 @@ def EVAL(env):
|
(select(.kind == "list") |
.value | select(length != 0) as $value |
(
(
select(.[0].value == "def!") |
.[1].value as $key |
.[2] | EVAL(env) |
.expr as $value |
if .env.replEnv != null then
addToEnv(.; $key)
addToEnv($key)
else
.env |= env_set_(.; $key; $value)
.expr as $def_value |
.env |= env_set_(.; $key; $def_value)
end
) //
(
Expand Down Expand Up @@ -204,7 +203,6 @@ def EVAL(env):
| $ev.expr | first |
interpret($ev.expr[1:]; $ev.env; _eval_here)
)
)
) //
(
select(.kind == "vector") |
Expand Down
25 changes: 11 additions & 14 deletions impls/jq/step5_tco.jq
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def EVAL(env):
.env as $env | .expr | EVAL($env);

. as $ast
| { env: env, ast: ., cont: true, finish: false, ret_env: null }
| TCOWrap(env; null; true)
| [ recurseflip(.cont;
.env as $_menv
| if .finish then
Expand All @@ -164,18 +164,15 @@ def EVAL(env):
|
(select(.kind == "list") |
.value | select(length != 0) as $value |
(
(
select(.[0].value == "def!") |
($value[2] | EVAL($_menv)) as $evval |
$value[2] | EVAL($_menv) |
(
if $evval.env.replEnv != null then
addToEnv($evval; $value[1].value)
if .env.replEnv != null then
addToEnv($value[1].value)
else
{
expr: $evval.expr,
env: env_set_($evval.env; $value[1].value; $evval.expr)
}
.expr as $def_value |
.env |= env_set_(.; $value[1].value; $def_value)
end
) as $val |
$val.expr | TCOWrap($val.env; $_orig_retenv; false)
Expand All @@ -192,10 +189,11 @@ def EVAL(env):
) //
(
select(.[0].value == "do") |
(reduce ($value[1:][]) as $xvalue (
{ env: $_menv, expr: {kind:"nil"} };
.env as $env | $xvalue | EVAL($env)
)) | . as $ex | .expr | TCOWrap($ex.env; $_orig_retenv; false)
(reduce $value[1:-1][] as $xvalue (
$_menv;
. as $env | $xvalue | EVAL($env) | .env
)) as $env |
$value[-1] | TCOWrap($env; $_orig_retenv; true)
) //
(
select(.[0].value == "if") |
Expand Down Expand Up @@ -228,7 +226,6 @@ def EVAL(env):
interpret($expr[1:]; $_menv; _eval_here) as $exprenv |
$exprenv.expr | TCOWrap($exprenv.env; $_orig_retenv; false)
)
)
) //
(
select(.kind == "vector") |
Expand Down
17 changes: 8 additions & 9 deletions impls/jq/step6_file.jq
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def EVAL(env):
.env as $env | .expr | EVAL($env);

. as $ast
| { env: env, ast: ., cont: true, finish: false, ret_env: null }
| TCOWrap(env; null; true)
| [ recurseflip(.cont;
.env as $_menv
| if .finish then
Expand All @@ -47,11 +47,10 @@ def EVAL(env):
|
(select(.kind == "list") |
.value | select(length != 0) as $value |
(
(
select(.[0].value == "def!") |
($value[2] | EVAL($_menv)) as $evval |
addToEnv($evval; $value[1].value) as $val |
$value[2] | EVAL($_menv) |
addToEnv($value[1].value) as $val |
$val.expr | TCOWrap($val.env; $_orig_retenv; false)
) //
(
Expand All @@ -67,10 +66,11 @@ def EVAL(env):
) //
(
select(.[0].value == "do") |
(reduce ($value[1:][]) as $xvalue (
{ env: $_menv, expr: {kind:"nil"} };
.env as $env | $xvalue | EVAL($env)
)) | . as $ex | .expr | TCOWrap($ex.env; $_orig_retenv; false)
(reduce $value[1:-1][] as $xvalue (
$_menv;
. as $env | $xvalue | EVAL($env) | .env
)) as $env |
$value[-1] | TCOWrap($env; $_orig_retenv; true)
) //
(
select(.[0].value == "if") |
Expand Down Expand Up @@ -114,7 +114,6 @@ def EVAL(env):
interpret($expr.val; $expr.env; _eval_here) as $exprenv |
$exprenv.expr | TCOWrap($exprenv.env; $_orig_retenv; false)
)
)
) //
(
select(.kind == "vector") |
Expand Down
17 changes: 8 additions & 9 deletions impls/jq/step7_quote.jq
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def EVAL(env):
.env as $env | .expr | EVAL($env);

. as $ast
| { env: env, ast: ., cont: true, finish: false, ret_env: null }
| TCOWrap(env; null; true)
| [ recurseflip(.cont;
.env as $_menv
| if .finish then
Expand All @@ -82,11 +82,10 @@ def EVAL(env):
|
(select(.kind == "list") |
.value | select(length != 0) as $value |
(
(
select(.[0].value == "def!") |
($value[2] | EVAL($_menv)) as $evval |
addToEnv($evval; $value[1].value) as $val |
$value[2] | EVAL($_menv) |
addToEnv($value[1].value) as $val |
$val.expr | TCOWrap($val.env; $_orig_retenv; false)
) //
(
Expand All @@ -102,10 +101,11 @@ def EVAL(env):
) //
(
select(.[0].value == "do") |
(reduce ($value[1:][]) as $xvalue (
{ env: $_menv, expr: {kind:"nil"} };
.env as $env | $xvalue | EVAL($env)
)) | . as $ex | .expr | TCOWrap($ex.env; $_orig_retenv; false)
(reduce $value[1:-1][] as $xvalue (
$_menv;
. as $env | $xvalue | EVAL($env) | .env
)) as $env |
$value[-1] | TCOWrap($env; $_orig_retenv; true)
) //
(
select(.[0].value == "if") |
Expand Down Expand Up @@ -157,7 +157,6 @@ def EVAL(env):
interpret($expr.val; $expr.env; _eval_here) as $exprenv |
$exprenv.expr | TCOWrap($exprenv.env; $_orig_retenv; false)
)
)
) //
(
select(.kind == "vector") |
Expand Down
22 changes: 11 additions & 11 deletions impls/jq/step8_macros.jq
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def EVAL(env):
.env as $env | .expr | EVAL($env);

. as $ast
| { env: env, ast: ., cont: true, finish: false, ret_env: null }
| TCOWrap(env; null; true)
| [ recurseflip(.cont;
.env as $_menv
| if .finish then
Expand All @@ -89,17 +89,17 @@ def EVAL(env):
|
(select(.kind == "list") |
.value | select(length != 0) as $value |
(
(
select(.[0].value == "def!") |
($value[2] | EVAL($_menv)) as $evval |
addToEnv($evval; $value[1].value) as $val |
$value[2] | EVAL($_menv) |
addToEnv($value[1].value) as $val |
$val.expr | TCOWrap($val.env; $_orig_retenv; false)
) //
(
select(.[0].value == "defmacro!") |
($value[2] | EVAL($_menv) | (.expr |= set_macro_function)) as $evval |
addToEnv($evval; $value[1].value) as $val |
$value[2] | EVAL($_menv) |
.expr |= set_macro_function |
addToEnv($value[1].value) as $val |
$val.expr | TCOWrap($val.env; $_orig_retenv; false)
) //
(
Expand All @@ -115,10 +115,11 @@ def EVAL(env):
) //
(
select(.[0].value == "do") |
(reduce ($value[1:][]) as $xvalue (
{ env: $_menv, expr: {kind:"nil"} };
.env as $env | $xvalue | EVAL($env)
)) | . as $ex | .expr | TCOWrap($ex.env; $_orig_retenv; false)
(reduce $value[1:-1][] as $xvalue (
$_menv;
. as $env | $xvalue | EVAL($env) | .env
)) as $env |
$value[-1] | TCOWrap($env; $_orig_retenv; true)
) //
(
select(.[0].value == "if") |
Expand Down Expand Up @@ -176,7 +177,6 @@ def EVAL(env):
$exprenv.expr | TCOWrap($exprenv.env; $_orig_retenv; false)
end
)
)
) //
(
select(.kind == "vector") |
Expand Down
37 changes: 18 additions & 19 deletions impls/jq/step9_try.jq
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def EVAL(env):
.env as $env | .expr | EVAL($env);

. as $ast
| { env: env, ast: ., cont: true, finish: false, ret_env: null }
| TCOWrap(env; null; true)
| [ recurseflip(.cont;
.env as $_menv
| if .finish then
Expand All @@ -89,17 +89,17 @@ def EVAL(env):
|
(select(.kind == "list") |
.value | select(length != 0) as $value |
(
(
select(.[0].value == "def!") |
($value[2] | EVAL($_menv)) as $evval |
addToEnv($evval; $value[1].value) as $val |
$value[2] | EVAL($_menv) |
addToEnv($value[1].value) as $val |
$val.expr | TCOWrap($val.env; $_orig_retenv; false)
) //
(
select(.[0].value == "defmacro!") |
($value[2] | EVAL($_menv) | (.expr |= set_macro_function)) as $evval |
addToEnv($evval; $value[1].value) as $val |
$value[2] | EVAL($_menv) |
.expr |= set_macro_function |
addToEnv($value[1].value) as $val |
$val.expr | TCOWrap($val.env; $_orig_retenv; false)
) //
(
Expand All @@ -115,18 +115,20 @@ def EVAL(env):
) //
(
select(.[0].value == "do") |
(reduce ($value[1:][]) as $xvalue (
{ env: $_menv, expr: {kind:"nil"} };
.env as $env | $xvalue | EVAL($env)
)) | . as $ex | .expr | TCOWrap($ex.env; $_orig_retenv; false)
(reduce $value[1:-1][] as $xvalue (
$_menv;
. as $env | $xvalue | EVAL($env) | .env
)) as $env |
$value[-1] | TCOWrap($env; $_orig_retenv; true)
) //
(
select(.[0].value == "try*") |
if $value[2]
and ($value[2].value[0] | .kind == "symbol" and .value == "catch*")
then
try (
$value[1] | EVAL($_menv) as $exp | $exp.expr | TCOWrap($exp.env; $_orig_retenv; false)
) catch ( . as $exc |
if $value[2] then
if ($value[2].value[0] | .kind == "symbol" and .value == "catch*") then
(if ($exc | is_jqmal_error) then
$exc[19:] as $ex |
try (
Expand All @@ -141,13 +143,11 @@ def EVAL(env):
end) as $exc |
$value[2].value[2] | EVAL($currentEnv | childEnv([$value[2].value[1].value]; [$exc]) | wrapEnv($replEnv; $_menv.atoms)) as $ex |
$ex.expr | TCOWrap($ex.env; $_retenv; false)
else
error($exc)
end
else
error($exc)
end
)
else
$value[1] | EVAL($_menv) as $exp |
$exp.expr | TCOWrap($exp.env; $_orig_retenv; false)
end
) //
(
select(.[0].value == "if") |
Expand Down Expand Up @@ -205,7 +205,6 @@ def EVAL(env):
$exprenv.expr | TCOWrap($exprenv.env; $_orig_retenv; false)
end
)
)
) //
(
select(.kind == "vector") |
Expand Down

0 comments on commit 4319594

Please sign in to comment.