Ast

Enum Ast 

Source
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

Variable assignment in the global scope.

§Examples

  • VAR=value
  • PATH=/usr/bin:$PATH

Fields

§var: String

Variable name

§value: String

Value to assign

§

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=value
  • local COUNT=0

Fields

§var: String

Variable name

§value: String

Value to assign

§

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; fi
  • if [ $x -eq 0 ]; then echo zero; elif [ $x -gt 0 ]; then echo positive; else echo negative; fi

Fields

§branches: Vec<(Box<Ast>, Box<Ast>)>

List of (condition, then-body) pairs for if/elif branches

§else_branch: Option<Box<Ast>>

Optional else branch

§

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

§word: String

Word to match against patterns

§cases: Vec<(Vec<String>, Ast)>

List of (patterns, commands) pairs

§default: Option<Box<Ast>>

Optional default case (pattern: *)

§

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; done
  • for file in *.txt; do cat "$file"; done

Fields

§variable: String

Loop variable name

§items: Vec<String>

List of items to iterate over

§body: Box<Ast>

Loop body

§

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; done
  • while [ $count -lt 10 ]; do count=$((count + 1)); done

Fields

§condition: Box<Ast>

Condition to evaluate

§body: Box<Ast>

Loop body

§

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; done
  • until [ -f ready.txt ]; do sleep 1; done

Fields

§condition: Box<Ast>

Condition to evaluate

§body: Box<Ast>

Loop body

§

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"; }

Fields

§name: String

Function name

§body: Box<Ast>

Function body

§

FunctionCall

Function call with arguments.

Invokes a previously defined function with the given arguments. Arguments are accessible as positional parameters ($1, $2, etc.).

§Examples

  • myfunc
  • greet Alice

Fields

§name: String

Function name

§args: Vec<String>

Arguments to pass

§

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

  • return
  • return 0
  • return 1

Fields

§value: Option<String>

Optional exit code (defaults to last command’s exit code)

§

And

Logical AND operator (short-circuit evaluation).

Executes the right side only if the left side succeeds (exit code 0).

§Examples

  • cmd1 && cmd2
  • test -f file && cat file

Fields

§left: Box<Ast>

Left operand

§right: Box<Ast>

Right operand (executed only if left succeeds)

§

Or

Logical OR operator (short-circuit evaluation).

Executes the right side only if the left side fails (non-zero exit code).

§Examples

  • cmd1 || cmd2
  • test -f file || touch file

Fields

§left: Box<Ast>

Left operand

§right: Box<Ast>

Right operand (executed only if left fails)

§

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)

Fields

§body: Box<Ast>

Commands to execute in subshell

§

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; }

Fields

§body: Box<Ast>

Commands to execute in current shell

§

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

Fields

§command: Box<Ast>

Command to negate

§

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 &

Fields

§command: Box<Ast>

Command to execute asynchronously

Trait Implementations§

Source§

impl Clone for Ast

Source§

fn clone(&self) -> Ast

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Ast

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Ast

Source§

fn eq(&self, other: &Ast) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Ast

Source§

impl StructuralPartialEq for Ast

Auto Trait Implementations§

§

impl Freeze for Ast

§

impl RefUnwindSafe for Ast

§

impl Send for Ast

§

impl Sync for Ast

§

impl Unpin for Ast

§

impl UnwindSafe for Ast

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.