AST

An Abstract Syntax Tree.

Grammer

Parse tokens. Tokens are defined in shell.grammer.tokenizer

Parsing rules;

lines: a BREAK-separated sequence of line
line: statement with optional INDENT
statement: 
    - assignment        # a = 1
    - conditional       # if .. then ..
    - conjunction       # add 1 |> echo
    - definition        # f (x): ..
    - return_statement  # return 1
conjunction: a PIPE-separated sequence of expression
expression: a command

Lines with an assignment.

# a b   = 10 11  ;      print $a   ;
|-------------||-----||----------||-----|
 assignment     break  expression  break
|-----| |-----|
 terms   terms

A line with a conjunction.

# range 1 5  |>    print '# ' |>    print
|----------------------------------------------| (conjunction)
|----------||----||----------------------------|
 expression  pipe  conjunction
|----------|      |----------||----||----------|
 terms             expression  pipe  expression
                  |----------|      |----------|
                  terms              terms

Core classes

Nodes

A container class for Node’s. See shell.grammer.parser

# Tree
Lines
└── Nodes
    └── Node

Node

An “edge” of the AST.

Subclasses of Node:

Node
├── Condition
│   ├── ElseCondition
│      ├── Else
│      └── ElseIf
│   ├── If
│   ├── IfThen
│   ├── IfThenElse
│   └── Then
├── Infix
│   ├── Assign
│   ├── BashPipe
│   ├── BinaryExpression
│   ├── LogicExpression
│   ├── Map
│   └── Pipe
└── Term
    ├── Method
    ├── Quoted
    ├── Variable
    └── Word        

Term

# Classes
Term
├── Method # f()
├── Quoted # "abc"
├── Variable $x
└── Word

Conditions

# Classes
Condition
├── ElseCondition
│   ├── Else
│   └── ElseIf
├── If
├── IfThen
├── IfThenElse
└── Then

Example code:

# An inline if-then clause
if $a > 10 then print high
if $a > 10 then print high else print low

# A multi-line if-then-else clause
a = 10
if $a > 10 then
    print large
else if $a == 10
    print medium
else
    print small

Additional classes

class mash.shell.ast.function_definition.FunctionDefinition(f, args=None, body=None)[source]

A user-defined function.

f (x):
    return $x

# e.g.
f 2 # yield 2
class mash.shell.ast.function_definition.InlineFunctionDefinition(f, args=None, body=None)[source]

A user-defined inline (oneline) function.

f (x): $x

# e.g.
f 2 # yield 2
class mash.shell.ast.infix.Assign(lhs: Node, rhs: Node, op: str)[source]

Assign a value to a variable.

# static assignment
a = 10

# left- and right-hand evaluation
b <- range 10
range 10 -> c
class mash.shell.ast.infix.BashPipe(lhs: Node, rhs: Node, op: str)[source]

Pipe

echo abcde | egrep -o 'a??'
class mash.shell.ast.infix.BinaryExpression(lhs: Node, rhs: Node, op: str)[source]
class mash.shell.ast.infix.Infix(lhs: Node, rhs: Node, op: str)[source]
class mash.shell.ast.infix.LogicExpression(lhs: Node, rhs: Node, op: str)[source]
class mash.shell.ast.infix.Map(lhs: Node, rhs: Node)[source]

Apply a function to each element of a sequence.

range 10 >>= echo The value is $ .
static map(command, values: str, shell, delimiter='\n') Iterable[source]

Apply a function to every line. If $ is present, then each line from stdin is inserted there. Otherwise each line is appended.

Usage

println a b |> map echo
println a b |> map echo prefix $ suffix
class mash.shell.ast.infix.Pipe(lhs: Node, rhs: Node, op: str)[source]
range 10 |> flatten