Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jq: update interpreter, merge eval-ast, fix some env errors #685

Merged
merged 20 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 14 additions & 34 deletions impls/jq/step3_env.jq
Original file line number Diff line number Diff line change
Expand Up @@ -66,30 +66,6 @@ def interpret(arguments; env):
jqmal_error("Unsupported native function kind \(.kind)");

def EVAL(env):
def hmap_with_env:
.env as $env | .list as $list |
if $list|length == 0 then
empty
else
$list[0] as $elem |
$list[1:] as $rest |
$elem[1] | EVAL($env) as $resv |
{ value: [$elem[0], $resv.expr], env: env },
({env: $resv.env, list: $rest} | hmap_with_env)
end;
def map_with_env:
.env as $env | .list as $list |
if $list|length == 0 then
empty
else
$list[0] as $elem |
$list[1:] as $rest |
$elem | EVAL($env) as $resv |
{ value: $resv.expr, env: env },
({env: $resv.env, list: $rest} | map_with_env)
end;

# EVAL starts here.
if "DEBUG-EVAL" | env_find(env).environment["DEBUG-EVAL"] |
. != null and .kind != "false" and .kind != "nil"
then
Expand Down Expand Up @@ -126,19 +102,23 @@ def EVAL(env):
) //
(
select(.kind == "vector") |
[ { env: env, list: .value } | map_with_env ] as $res |
{
kind: "vector",
value: $res | map(.value)
} | addEnv($res | last.env)
.value |
reduce .[] as $x ({expr:[], env:env};
. as $acc |
$x | EVAL($acc.env) |
.expr |= $acc.expr + [.]
) |
.expr |= {kind:"vector", value:.}
) //
(
select(.kind == "hashmap") |
[ { env: env, list: .value | to_entries } | hmap_with_env ] as $res |
{
kind: "hashmap",
value: $res | map(.value) | from_entries
} | addEnv($res | last.env)
.value | to_entries |
reduce .[] as $x ({expr:[], env:env};
. as $acc |
$x.value.value | EVAL($acc.env) |
.expr |= (. as $e | $acc.expr + [$x | .value.value |= $e])
) |
.expr |= {kind:"hashmap", value:from_entries}
) //
(
select(.kind == "symbol") |
Expand Down
65 changes: 18 additions & 47 deletions impls/jq/step4_if_fn_do.jq
Original file line number Diff line number Diff line change
Expand Up @@ -150,29 +150,6 @@ def EVAL(env):
def _eval_here:
.env as $env | .expr | EVAL($env);

def hmap_with_env:
.env as $env | .list as $list |
if $list|length == 0 then
empty
else
$list[0] as $elem |
$list[1:] as $rest |
$elem[1] | EVAL($env) as $resv |
{ value: [$elem[0], $resv.expr], env: env },
({env: $resv.env, list: $rest} | hmap_with_env)
end;
def map_with_env:
.env as $env | .list as $list |
if $list|length == 0 then
empty
else
$list[0] as $elem |
$list[1:] as $rest |
$elem | EVAL($env) as $resv |
{ value: $resv.expr, env: env },
({env: $resv.env, list: $rest} | map_with_env)
end;

# EVAL starts here.
if "DEBUG-EVAL" | env_find(env).environment["DEBUG-EVAL"] |
. != null and .kind != "false" and .kind != "nil"
Expand Down Expand Up @@ -239,37 +216,31 @@ def EVAL(env):
)
)
) //
(select(.kind == "vector") |
if .value|length == 0 then
{
kind: "vector",
value: []
} | addEnv(env)
else
[ { env: env, list: .value } | map_with_env ] as $res |
{
kind: "vector",
value: $res | map(.value)
} | addEnv($res | last.env)
end
) //
(
select(.kind == "hashmap") |
[ { env: env, list: .value | to_entries } | hmap_with_env ] as $res |
{
kind: "hashmap",
value: $res | map(.value) | from_entries
} | addEnv($res | last.env)
select(.kind == "vector") |
.value |
reduce .[] as $x ({expr:[], env:env};
. as $acc |
$x | EVAL($acc.env) |
.expr |= $acc.expr + [.]
) |
.expr |= {kind:"vector", value:.}
) //
(
select(.kind == "function") |
. | addEnv(env) # return this unchanged, since it can only be applied to
select(.kind == "hashmap") |
.value | to_entries |
reduce .[] as $x ({expr:[], env:env};
. as $acc |
$x.value.value | EVAL($acc.env) |
.expr |= (. as $e | $acc.expr + [$x | .value.value |= $e])
) |
.expr |= {kind:"hashmap", value:from_entries}
) //
(
select(.kind == "symbol") |
.value | env_get(env) | addEnv(env)
)
// addEnv(env);
) //
addEnv(env);

def PRINT:
pr_str;
Expand Down
61 changes: 20 additions & 41 deletions impls/jq/step5_tco.jq
Original file line number Diff line number Diff line change
Expand Up @@ -162,29 +162,6 @@ def EVAL(env):
def _eval_here:
.env as $env | .expr | EVAL($env);

def hmap_with_env:
.env as $env | .list as $list |
if $list|length == 0 then
empty
else
$list[0] as $elem |
$list[1:] as $rest |
$elem[1] | EVAL($env) as $resv |
{ value: [$elem[0], $resv.expr], env: env },
({env: $resv.env, list: $rest} | hmap_with_env)
end;
def map_with_env:
.env as $env | .list as $list |
if $list|length == 0 then
empty
else
$list[0] as $elem |
$list[1:] as $rest |
$elem | EVAL($env) as $resv |
{ value: $resv.expr, env: env },
({env: $resv.env, list: $rest} | map_with_env)
end;

. as $ast
| { env: env, ast: ., cont: true, finish: false, ret_env: null }
| [ recurseflip(.cont;
Expand Down Expand Up @@ -261,27 +238,29 @@ def EVAL(env):
TCOWrap($_menv; $_orig_retenv; false)
)
) //
(select(.kind == "vector") |
if .value|length == 0 then
{
kind: "vector",
value: []
} | TCOWrap($_menv; $_orig_retenv; false)
else
[ { env: $_menv, list: .value } | map_with_env ] as $res |
{
kind: "vector",
value: $res | map(.value)
} | TCOWrap($res | last.env; $_orig_retenv; false)
end
(
select(.kind == "vector") |
.value |
reduce .[] as $x ({expr:[], env:env};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue with REGRESS is simple enough, this should be $_menv (env is the initial env, which is why you get "b not found" in the regression test), you use the right value for later steps.

. as $acc |
$x | EVAL($acc.env) |
.expr |= $acc.expr + [.]
) |
.env as $e |
{kind:"vector", value:.expr} |
TCOWrap($e; $_orig_retenv; false)
) //
(
select(.kind == "hashmap") |
[ { env: $_menv, list: .value | to_entries } | hmap_with_env ] as $res |
{
kind: "hashmap",
value: $res | map(.value) | from_entries
} | TCOWrap($res | last.env; $_orig_retenv; false)
.value | to_entries |
reduce .[] as $x ({expr:[], env:env};
. as $acc |
$x.value.value | EVAL($acc.env) |
.expr |= (. as $e | $acc.expr + [$x | .value.value |= $e])
) |
.env as $e |
{kind:"hashmap", value:.expr|from_entries} |
TCOWrap($e; $_orig_retenv; false)
) //
(
select(.kind == "function") |
Expand Down
67 changes: 20 additions & 47 deletions impls/jq/step6_file.jq
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,6 @@ def EVAL(env):
def _eval_here:
.env as $env | .expr | EVAL($env);

def hmap_with_env:
.env as $env | .list as $list |
if $list|length == 0 then
empty
else
$list[0] as $elem |
$list[1:] as $rest |
$elem.value.value | EVAL($env) as $resv |
{
value: {
key: $elem.key,
value: { kkind: $elem.value.kkind, value: $resv.expr }
},
env: env
},
({env: $resv.env, list: $rest} | hmap_with_env)
end;
def map_with_env:
.env as $env | .list as $list |
if $list|length == 0 then
empty
else
$list[0] as $elem |
$list[1:] as $rest |
$elem | EVAL($env) as $resv |
{ value: $resv.expr, env: env },
({env: $resv.env, list: $rest} | map_with_env)
end;

. as $ast
| { env: env, ast: ., cont: true, finish: false, ret_env: null }
| [ recurseflip(.cont;
Expand Down Expand Up @@ -144,27 +115,29 @@ def EVAL(env):
TCOWrap($_menv; $_orig_retenv; false)
)
) //
(select(.kind == "vector") |
if .value|length == 0 then
{
kind: "vector",
value: []
} | TCOWrap($_menv; $_orig_retenv; false)
else
[ { env: $currentEnv, list: .value } | map_with_env ] as $res |
{
kind: "vector",
value: $res | map(.value)
} | TCOWrap($res | last.env; $_orig_retenv; false)
end
(
select(.kind == "vector") |
.value |
reduce .[] as $x ({expr:[], env:$_menv};
. as $acc |
$x | EVAL($acc.env) |
.expr |= $acc.expr + [.]
) |
.env as $e |
{kind:"vector", value:.expr} |
TCOWrap($e; $_orig_retenv; false)
) //
(
select(.kind == "hashmap") |
[ { env: $currentEnv, list: (.value | to_entries) } | hmap_with_env ] as $res |
{
kind: "hashmap",
value: $res | map(.value) | from_entries
} | TCOWrap($res | last.env; $_orig_retenv; false)
.value | to_entries |
reduce .[] as $x ({expr:[], env:$_menv};
. as $acc |
$x.value.value | EVAL($acc.env) |
.expr |= (. as $e | $acc.expr + [$x | .value.value |= $e])
) |
.env as $e |
{kind:"hashmap", value:.expr|from_entries} |
TCOWrap($e; $_orig_retenv; false)
) //
(
select(.kind == "function") |
Expand Down
67 changes: 20 additions & 47 deletions impls/jq/step7_quote.jq
Original file line number Diff line number Diff line change
Expand Up @@ -59,35 +59,6 @@ def EVAL(env):
def _eval_here:
.env as $env | .expr | EVAL($env);

def hmap_with_env:
.env as $env | .list as $list |
if $list|length == 0 then
empty
else
$list[0] as $elem |
$list[1:] as $rest |
$elem.value.value | EVAL($env) as $resv |
{
value: {
key: $elem.key,
value: { kkind: $elem.value.kkind, value: $resv.expr }
},
env: env
},
({env: $resv.env, list: $rest} | hmap_with_env)
end;
def map_with_env:
.env as $env | .list as $list |
if $list|length == 0 then
empty
else
$list[0] as $elem |
$list[1:] as $rest |
$elem | EVAL($env) as $resv |
{ value: $resv.expr, env: env },
({env: $resv.env, list: $rest} | map_with_env)
end;

. as $ast
| { env: env, ast: ., cont: true, finish: false, ret_env: null }
| [ recurseflip(.cont;
Expand Down Expand Up @@ -187,27 +158,29 @@ def EVAL(env):
TCOWrap($_menv; $_orig_retenv; false)
)
) //
(select(.kind == "vector") |
if .value|length == 0 then
{
kind: "vector",
value: []
} | TCOWrap($_menv; $_orig_retenv; false)
else
[ { env: $_menv, list: .value } | map_with_env ] as $res |
{
kind: "vector",
value: $res | map(.value)
} | TCOWrap($res | last.env; $_orig_retenv; false)
end
(
select(.kind == "vector") |
.value |
reduce .[] as $x ({expr:[], env:$_menv};
. as $acc |
$x | EVAL($acc.env) |
.expr |= $acc.expr + [.]
) |
.env as $e |
{kind:"vector", value:.expr} |
TCOWrap($e; $_orig_retenv; false)
) //
(
select(.kind == "hashmap") |
[ { env: $_menv, list: (.value | to_entries) } | hmap_with_env ] as $res |
{
kind: "hashmap",
value: $res | map(.value) | from_entries
} | TCOWrap($res | last.env; $_orig_retenv; false)
.value | to_entries |
reduce .[] as $x ({expr:[], env:$_menv};
. as $acc |
$x.value.value | EVAL($acc.env) |
.expr |= (. as $e | $acc.expr + [$x | .value.value |= $e])
) |
.env as $e |
{kind:"hashmap", value:.expr|from_entries} |
TCOWrap($e; $_orig_retenv; false)
) //
(
select(.kind == "function") |
Expand Down
Loading