Skip to content

Commit

Permalink
[doc/ref/chap-type-method] Update topics for Command and Expr types
Browse files Browse the repository at this point in the history
Based on lobste.rs feedback
  • Loading branch information
Andy C committed Jan 5, 2025
1 parent 097f0d1 commit 2883cec
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 9 deletions.
12 changes: 11 additions & 1 deletion doc/ref/chap-cmd-lang.md
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,10 @@ Redirects can also appear after the lazy typed args:

assert [42 === x] >out.txt

- Related: [Expr][] type

[Expr]: chap-type-method.html#Expr

### block-arg

Blocks can be passed to simple commands, either literally:
Expand Down Expand Up @@ -522,7 +526,13 @@ Redirects can appear after the block arg:
} >out.txt


- Related: [sh-block](#sh-block) in OSH.
Related:

- [sh-block](#sh-block) in OSH.
- [Command][] and [CommandFrag][] types.

[Command]: chap-type-method.html#Command
[CommandFrag]: chap-type-method.html#CommandFrag

## YSH Cond

Expand Down
62 changes: 54 additions & 8 deletions doc/ref/chap-type-method.md
Original file line number Diff line number Diff line change
Expand Up @@ -628,28 +628,74 @@ TODO

### Command

An unevaluated command. You can create a `Command` with a "block expression"
([block-expr][]):
A value of type `Command` represents an unevaluated command. There are **two**
syntaxes for such values:

var block = ^(echo $PWD; ls *.txt)
1. In [expression mode][command-vs-expression-mode], a [block
expression][block-expr] looks like this:

The Command is bound to a stack frame. This frame will be pushed as an
var block = ^(echo $PWD; ls *.txt)

This is similar to `$(echo $PWD)` in shell.

2. In [command mode][command-vs-expression-mode], an argument to a user-defined
proc is also of type `Command`:

myproc {
echo $PWD
}

This is similar to `{ echo $PWD; }` in shell. This syntax is a YSH
[block-arg][].

---

The `Command` value is bound to a stack frame. This frame will be pushed as an
"enclosed frame" when the command is evaluated.

[block-expr]: chap-expr-lang.html#block-expr

[command-vs-expression-mode]: ../command-vs-expression-mode.html

[block-arg]: chap-cmd-lang.html#block-arg

### CommandFrag

A command that's not bound to a stack frame.

### Expr

An unevaluated expression. You can create an `Expr` with an expression literal
([expr-literal][]):
A value of type `Expr` represents an unevaluated expression. There are **three**
syntaxes for such values:

1. In [expression mode][command-vs-expression-mode], an
[expression literal][expr-literal] looks like this:

var expr = ^[42 + a[i]]

var expr = ^[42 + a[i]]
1. There's also a shortcut for string literals:

var s = "foo".replace('o', ^"-$0-")
echo $s # => f-o--o-

The syntax `^"-$0-"` is short for `^["-$0-"]`. You can omit the brackets.

1. In [command mode][command-vs-expression-mode], an argument to a user-defined
proc is also of type `Expr`:

ls | my-where [size > 42]

This is a shortcut for:

ls | my-where (^[size > 42]) # same as syntax 1

This syntax is a YSH [lazy-expr-arg][].

[lazy-expr-arg]: chap-cmd-lang.html#lazy-expr-arg

---

The Command is bound to a stack frame. This frame will be pushed as an
The `Expr` value is bound to a stack frame. This frame will be pushed as an
"enclosed frame" when the expression is evaluated.

[expr-literal]: chap-expr-lang.html#expr-lit
Expand Down

0 comments on commit 2883cec

Please sign in to comment.