pub enum Ast {
Show 18 variants
Pipeline(Vec<ShellCommand>),
Sequence(Vec<Ast>),
Assignment {
var: String,
value: String,
},
LocalAssignment {
var: String,
value: String,
},
If {
branches: Vec<(Box<Ast>, Box<Ast>)>,
else_branch: Option<Box<Ast>>,
},
Case {
word: String,
cases: Vec<(Vec<String>, Ast)>,
default: Option<Box<Ast>>,
},
For {
variable: String,
items: Vec<String>,
body: Box<Ast>,
},
While {
condition: Box<Ast>,
body: Box<Ast>,
},
Until {
condition: Box<Ast>,
body: Box<Ast>,
},
FunctionDefinition {
name: String,
body: Box<Ast>,
},
FunctionCall {
name: String,
args: Vec<String>,
},
Return {
value: Option<String>,
},
And {
left: Box<Ast>,
right: Box<Ast>,
},
Or {
left: Box<Ast>,
right: Box<Ast>,
},
Subshell {
body: Box<Ast>,
},
CommandGroup {
body: Box<Ast>,
},
Negation {
command: Box<Ast>,
},
AsyncCommand {
command: Box<Ast>,
},
}Expand description
Abstract Syntax Tree node representing a parsed shell construct.
Each variant represents a different type of shell command or control structure. The AST is designed to be executed by the executor module, which traverses the tree and performs the corresponding operations.
Variants§
Pipeline(Vec<ShellCommand>)
A pipeline of one or more commands connected by pipes.
Each command in the pipeline receives input from the previous command’s output (except the first) and sends output to the next command (except the last).
§Examples
- Single command:
ls - Pipeline:
ls | grep txt | sort
Sequence(Vec<Ast>)
A sequence of commands separated by semicolons or newlines.
Commands are executed in order, regardless of their exit status.
§Examples
cmd1; cmd2; cmd3- Multiple lines in a script
Assignment
LocalAssignment
Local variable assignment (function scope).
Only valid within function bodies. Creates a variable that is local to the function and its callees.
§Examples
local VAR=valuelocal COUNT=0
If
Conditional execution with optional elif and else branches.
Each branch consists of a condition and a body. Branches are evaluated in order until one condition succeeds, then its body is executed.
§Examples
if test -f file; then echo exists; fiif [ $x -eq 0 ]; then echo zero; elif [ $x -gt 0 ]; then echo positive; else echo negative; fi
Fields
Case
Pattern matching construct.
Matches a word against a series of patterns and executes the corresponding commands for the first match. Supports glob patterns.
§Examples
case $var in pattern1) cmd1 ;; pattern2|pattern3) cmd2 ;; esac
Fields
For
For loop iterating over a list of items.
Executes the body once for each item, with the loop variable set to the current item.
§Examples
for i in 1 2 3; do echo $i; donefor file in *.txt; do cat "$file"; done
Fields
While
While loop executing while condition is true.
Evaluates the condition before each iteration. Continues looping as long as the condition exits with status 0.
§Examples
while true; do echo loop; donewhile [ $count -lt 10 ]; do count=$((count + 1)); done
Until
Until loop executing until condition is true.
Evaluates the condition before each iteration. Continues looping as long as the condition exits with non-zero status.
§Examples
until false; do echo loop; doneuntil [ -f ready.txt ]; do sleep 1; done
FunctionDefinition
Function definition.
Defines a named function that can be called later. The function body is stored as an AST and executed when the function is invoked.
§Examples
myfunc() { echo hello; }greet() { echo "Hello, $1"; }
FunctionCall
Function call with arguments.
Invokes a previously defined function with the given arguments. Arguments are accessible as positional parameters ($1, $2, etc.).
§Examples
myfuncgreet Alice
Return
Return statement for early exit from function.
Exits the current function with an optional exit code. If no value is provided, returns the exit code of the last command.
§Examples
returnreturn 0return 1
And
Logical AND operator (short-circuit evaluation).
Executes the right side only if the left side succeeds (exit code 0).
§Examples
cmd1 && cmd2test -f file && cat file
Or
Logical OR operator (short-circuit evaluation).
Executes the right side only if the left side fails (non-zero exit code).
§Examples
cmd1 || cmd2test -f file || touch file
Subshell
Subshell execution: (commands).
Commands execute in an isolated copy of the shell state. Changes to variables, directory, etc. do not affect the parent shell.
§Examples
(cd /tmp; ls)(export VAR=value; cmd)
CommandGroup
Command group execution: { commands; }.
Commands execute in the current shell state. Changes to variables, directory, etc. affect the current shell.
§Examples
{ cmd1; cmd2; }{ echo start; cmd; echo end; }
Negation
Command negation: ! command.
Inverts the exit code of the command (0 becomes non-zero, non-zero becomes 0). Also exempts the command from errexit behavior.
§Examples
! false! grep pattern file
AsyncCommand
Asynchronous command execution: command &.
Executes the command in the background, allowing the shell to continue processing subsequent commands without waiting for completion.
§Examples
sleep 10 &long_running_task &