Skip to content

Commit

Permalink
jq: fix evaluation of vectors and maps in regression tests
Browse files Browse the repository at this point in the history
  • Loading branch information
asarhaddon committed Sep 29, 2024
1 parent 0262704 commit a9f7c88
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 357 deletions.
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};
. 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

0 comments on commit a9f7c88

Please sign in to comment.