AST — Abstract Syntax Tree

Node types, data structures, and construction helpers for the AST.

Abstract Syntax Tree definitions for the 42sh shell.

Author

jguillem pulgamecanica

Defines

REDIR(node)

Convenience accessor: get t_redir* from a t_list node.

Enums

enum t_node_type

AST node types.

Note

Important to don’t change the order

Values:

enumerator NODE_COMMAND
enumerator NODE_PIPE
enumerator NODE_AND
enumerator NODE_OR
enumerator NODE_SEQUENCE
enumerator NODE_SUBSHELL
enumerator NODE_BLOCK
enumerator NODE_BACKGROUND

Functions

t_ast *ast_new_command(t_cmd *cmd)

AST construction helpers.

AST construction helpers.

Parameters:

cmd – struct s_cmd

Returns:

pointer on struct s_ast

t_ast *ast_new_binary(t_node_type type, t_ast *left, t_ast *right)

create and allocate new binary ast node

Parameters:
  • type – enum e_node_type

  • left – struct s_ast pointer

  • right – struct s_ast pointer

Returns:

struct s_ast pointer

t_ast *ast_new_group(t_node_type type, t_ast *child, t_list *redirs)

helper function for the parse_subshell function

Parameters:
  • type – enum e_node_type

  • child – struct s_ast

  • redirs – struct s_list with a t_redir * as content

Returns:

pointer on struct s_ast

void ast_free(t_ast *node)

free ast recursively

Parameters:

nodet_ast struct

t_redir *redir_new(t_token_type type, int fd, char *target)

Redirection helpers.

void redir_free(t_redir *redir)

free t_redir helper

Parameters:

redirt_redir struct

struct t_redir
#include <ast.h>

Redirection data node.

Stored in t_cmd.redirs and t_group.redirs as t_list* (each node->content is a t_redir*). No *next field.

Param fd:

source fd (-1 = default for the operator type).

Param target:

raw filename or fd number string (unexpanded).

Param heredoc_delim:

the delimiter word for << (raw, may be quoted).

Param heredoc_stripped:

1 if leadling tab are stripped

Param heredoc_content:

collected heredoc content (filled after parse, before exec).

Param heredoc_quoted:

1 if delimiter was quoted (no expansion inside heredoc).

Public Members

t_token_type type
int fd
char *target
char *heredoc_delim
char *heredoc_content
int heredoc_quoted
struct t_cmd
#include <ast.h>

Simple command data.

Param argv:

NULL-terminated array of raw/unexpanded argument strings.

Param argc:

number of elements in argv (not counting the NULL terminator).

Param assignments:

t_list* of char* “NAME=value” strings (detected by parser, unexpanded). Each node->content is a char*.

Param redirs:

t_list* of t_redir* (in source order, applied left-to-right). Each node->content is a t_redir*.

Public Members

char **argv
int argc
t_list *assignments
t_list *redirs
struct t_binary
#include <ast.h>

Binary operation data (pipe, &&, ||, ;)

Public Members

struct s_ast *left
struct s_ast *right
struct t_group
#include <ast.h>

Group data (subshell, block, background).

Param redirs:

t_list* of t_redir* for redirections applied to the whole group, e.g. (cmd) > file or { cmd; } 2>&1.

Public Members

struct s_ast *child
t_list *redirs
struct t_ast
#include <ast.h>

AST node - union-based for zero-overhead dispatch.

Public Members

t_node_type type
t_cmd *cmd
t_binary *binary
t_group *group
union t_ast data