← Back to context

Comment by jolmg

3 months ago

You mean like a shell's while-do-done? It's just about allowing statements as the conditions, rather than just a single expression. Here's an example from a repl I wrote:

  repl_prompt="${repl_prompt-repl$ }"
  while
    printf "%s" "$repl_prompt"
    read -r line
  do
    eval "$line"
  done
  echo

The `printf` is your `prepare`.

This should also be doable in languages where statements are expressions, like Ruby, Lisp, etc.

Here's a similar Ruby repl:

  while (
    print "repl> "
    line = gets
  ) do
    result = eval line
    puts result.inspect
  end
  puts

Exactly, here you are basically keeping it as a while with a condition but allowing it to be any code that at the end returns a boolean, although you need to make sure that variables defined in that block can be used in the do part.

Sidenote: I wasn't aware that shell allows for multiple lines, good to know!