From 4e923ed30b7a48b7e5aa6ce4eadff1ff96904c20 Mon Sep 17 00:00:00 2001 From: Patrick Ferris Date: Fri, 23 Jan 2026 13:20:57 +0000 Subject: [PATCH] Until compound commands --- src/lib/eval.ml | 15 +++++++++++---- test/while.t | 25 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/lib/eval.ml b/src/lib/eval.ml index 467a1c8..e81e3c7 100644 --- a/src/lib/eval.ml +++ b/src/lib/eval.ml @@ -778,6 +778,16 @@ module Make (S : Types.State) (E : Types.Exec) = struct in loop (Exit.zero ctx) + and handle_until_clause ctx + (Until ((term, sep), (term', sep')) : Ast.until_clause) = + let rec loop exit_so_far = + let running_ctx = Exit.value exit_so_far in + match exec running_ctx (term, Some sep) with + | Exit.Zero _ -> exit_so_far (* TODO: Context? *) + | Exit.Nonzero { value = ctx; _ } -> loop (exec ctx (term', Some sep')) + in + loop (Exit.zero ctx) + and handle_compound_command ctx v : ctx Exit.t = match v with | Ast.ForClause fc -> handle_for_clause ctx fc @@ -786,10 +796,7 @@ module Make (S : Types.State) (E : Types.Exec) = struct | Ast.Subshell s -> exec_subshell ctx s | Ast.CaseClause cases -> handle_case_clause ctx cases | Ast.WhileClause while_ -> handle_while_clause ctx while_ - | _ as c -> - Fmt.epr "Compound command not supported: %a\n%!" yojson_pp - (Ast.compound_command_to_yojson c); - exit 127 + | Ast.UntilClause until -> handle_until_clause ctx until and handle_function_application (ctx : ctx) ~name argv : ctx Exit.t option = match List.assoc_opt name ctx.functions with diff --git a/test/while.t b/test/while.t index 6271dbd..ba4fb72 100644 --- a/test/while.t +++ b/test/while.t @@ -23,3 +23,28 @@ While clauses. Iteration 3... Iteration 4... Iteration 5... + +Also until loops. + + $ cat > test.sh << EOF + > i=1 + > + > until [ "\$i" -ge 5 ] + > do + > echo "Iteration \$i..." + > i=\$((i + 1)) + > done + > + > EOF + + + $ sh test.sh + Iteration 1... + Iteration 2... + Iteration 3... + Iteration 4... + $ msh test.sh + Iteration 1... + Iteration 2... + Iteration 3... + Iteration 4... -- 2.51.2