starting the norm of the ast

This commit is contained in:
EniumRaphael 2024-07-11 14:53:15 +02:00
parent 0e18e20181
commit 0da9510ec6
5 changed files with 253 additions and 208 deletions

View file

@ -6,33 +6,29 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/11 14:23:40 by maiboyer #+# #+# */ /* Created: 2024/06/11 14:23:40 by maiboyer #+# #+# */
/* Updated: 2024/07/03 22:33:51 by maiboyer ### ########.fr */ /* Updated: 2024/07/11 14:05:20 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef AST_H #ifndef AST_H
#define AST_H # define AST_H
#include "ast/ast_forward_def.h" // IWYU pragma: keep # include "ast/ast_forward_def.h" // IWYU pragma: keep
#include "ast/ast_raw_structs.h" // IWYU pragma: keep # include "ast/ast_raw_structs.h" // IWYU pragma: keep
enum e_ast_node_kind enum e_ast_node_kind
{ {
S_AST_CATEGORY_MASK = 0xFFFF0000, S_AST_CATEGORY_MASK = 0xFFFF0000,
S_AST_NODETYPE_MASK = 0x0000FFFF, S_AST_NODETYPE_MASK = 0x0000FFFF,
S_AST_NONE = 0, S_AST_NONE = 0,
S_AST_COMPOUND_COMMAND = 1 << 16, S_AST_COMPOUND_COMMAND = 1 << 16,
S_AST_COMMAND = 1 << 17, S_AST_COMMAND = 1 << 17,
S_AST_REDIRECT = 1 << 18, S_AST_REDIRECT = 1 << 18,
S_AST_EXPANSION = 1 << 19, S_AST_EXPANSION = 1 << 19,
AST_ARITHMETIC_EXPANSION = S_AST_EXPANSION | 0x0001, AST_ARITHMETIC_EXPANSION = S_AST_EXPANSION | 0x0001,
AST_COMMAND_SUBSTITUTION = S_AST_EXPANSION | 0x0002, AST_COMMAND_SUBSTITUTION = S_AST_EXPANSION | 0x0002,
AST_EXPANSION = S_AST_EXPANSION | 0x0003, AST_EXPANSION = S_AST_EXPANSION | 0x0003,
AST_COMMAND = S_AST_COMMAND | 0x0004, AST_COMMAND = S_AST_COMMAND | 0x0004,
AST_CASE_ITEM = S_AST_NONE | 0x0005, AST_CASE_ITEM = S_AST_NONE | 0x0005,
AST_ELIF = S_AST_NONE | 0x0006, AST_ELIF = S_AST_NONE | 0x0006,
AST_ELSE = S_AST_NONE | 0x0007, AST_ELSE = S_AST_NONE | 0x0007,
@ -42,10 +38,8 @@ enum e_ast_node_kind
AST_VARIABLE_ASSIGNMENT = S_AST_NONE | 0x000E, AST_VARIABLE_ASSIGNMENT = S_AST_NONE | 0x000E,
AST_EXTGLOB = S_AST_NONE | 0x001A, AST_EXTGLOB = S_AST_NONE | 0x001A,
AST_REGEX = S_AST_NONE | 0x001B, AST_REGEX = S_AST_NONE | 0x001B,
AST_FILE_REDIRECTION = S_AST_REDIRECT | 0x000F, AST_FILE_REDIRECTION = S_AST_REDIRECT | 0x000F,
AST_HEREDOC_REDIRECTION = S_AST_REDIRECT | 0x0010, AST_HEREDOC_REDIRECTION = S_AST_REDIRECT | 0x0010,
AST_FOR = S_AST_COMPOUND_COMMAND | 0x0011, AST_FOR = S_AST_COMPOUND_COMMAND | 0x0011,
AST_CASE = S_AST_COMPOUND_COMMAND | 0x0012, AST_CASE = S_AST_COMPOUND_COMMAND | 0x0012,
AST_COMPOUND_STATEMENT = S_AST_COMPOUND_COMMAND | 0x0013, AST_COMPOUND_STATEMENT = S_AST_COMPOUND_COMMAND | 0x0013,
@ -59,7 +53,8 @@ enum e_ast_node_kind
AST_WHILE = S_AST_COMPOUND_COMMAND | 0x0019, AST_WHILE = S_AST_COMPOUND_COMMAND | 0x0019,
}; };
union u_ast_node_data { union u_ast_node_data
{
t_ast_arithmetic_expansion arithmetic_expansion; t_ast_arithmetic_expansion arithmetic_expansion;
t_ast_case case_; t_ast_case case_;
t_ast_case_item case_item; t_ast_case_item case_item;

View file

@ -6,12 +6,12 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/11 14:24:24 by maiboyer #+# #+# */ /* Created: 2024/06/11 14:24:24 by maiboyer #+# #+# */
/* Updated: 2024/07/03 22:30:23 by maiboyer ### ########.fr */ /* Updated: 2024/07/11 14:04:34 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef AST_FORWARD_DEF_H #ifndef AST_FORWARD_DEF_H
#define AST_FORWARD_DEF_H # define AST_FORWARD_DEF_H
typedef enum e_ast_node_kind t_ast_node_kind; typedef enum e_ast_node_kind t_ast_node_kind;
typedef enum e_ast_word_kind t_ast_word_kind; typedef enum e_ast_word_kind t_ast_word_kind;
@ -19,7 +19,6 @@ typedef enum e_ast_list_kind t_ast_list_kind;
typedef enum e_ast_expansion_operator t_ast_expansion_operator; typedef enum e_ast_expansion_operator t_ast_expansion_operator;
typedef enum e_ast_terminator_kind t_ast_terminator_kind; typedef enum e_ast_terminator_kind t_ast_terminator_kind;
typedef enum e_ast_redirection_kind t_ast_redirection_kind; typedef enum e_ast_redirection_kind t_ast_redirection_kind;
typedef union u_ast_node_data t_ast_node_data; typedef union u_ast_node_data t_ast_node_data;
typedef struct s_ast_node *t_ast_node; typedef struct s_ast_node *t_ast_node;

View file

@ -6,16 +6,16 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/14 17:46:58 by maiboyer #+# #+# */ /* Created: 2024/06/14 17:46:58 by maiboyer #+# #+# */
/* Updated: 2024/07/03 22:30:01 by maiboyer ### ########.fr */ /* Updated: 2024/07/11 14:48:53 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef AST_RAW_STRUCTS_H #ifndef AST_RAW_STRUCTS_H
#define AST_RAW_STRUCTS_H # define AST_RAW_STRUCTS_H
#include "ast/ast_forward_def.h" # include "ast/ast_forward_def.h"
#include "me/types.h" # include "me/types.h"
#include "me/vec/vec_ast.h" # include "me/vec/vec_ast.h"
enum e_ast_list_kind enum e_ast_list_kind
{ {
@ -39,37 +39,66 @@ enum e_ast_terminator_kind
AST_TERM_FORK, AST_TERM_FORK,
}; };
/*
E_OP_NONE = 0,= ${var}
E_OP_DEFAULT, = ${var-word}
E_OP_ASSIGN_DEFAULT, = ${var=word}
E_OP_ERROR, = ${var?word}
E_OP_ALTERNATE, = ${var+word}
E_OP_DEFAULT_COLON, = ${var:-word}
E_OP_ASSIGN_DEFAULT_COLON, = ${var:=word}
E_OP_ERROR_COLON, = ${var:?word}
E_OP_ALTERNATE_COLON, = ${var:+word}
E_OP_SMALLEST_PREFIX, = ${var#pattern}
E_OP_LARGEST_PREFIX, = ${var##pattern}
E_OP_SMALLEST_SUFFIX, = ${var%pattern}
E_OP_LARGEST_SUFFIX, = ${var%%pattern}
*/
enum e_ast_expansion_operator enum e_ast_expansion_operator
{ {
E_OP_NONE = 0,
E_OP_NONE = 0, // ${var} E_OP_DEFAULT,
E_OP_DEFAULT, // ${var-word} E_OP_ASSIGN_DEFAULT,
E_OP_ASSIGN_DEFAULT, // ${var=word} E_OP_ERROR,
E_OP_ERROR, // ${var?word} E_OP_ALTERNATE,
E_OP_ALTERNATE, // ${var+word} E_OP_DEFAULT_COLON,
E_OP_DEFAULT_COLON, // ${var:-word} E_OP_ASSIGN_DEFAULT_COLON,
E_OP_ASSIGN_DEFAULT_COLON, // ${var:=word} E_OP_ERROR_COLON,
E_OP_ERROR_COLON, // ${var:?word} E_OP_ALTERNATE_COLON,
E_OP_ALTERNATE_COLON, // ${var:+word} E_OP_SMALLEST_PREFIX,
E_OP_SMALLEST_PREFIX, // ${var#pattern} E_OP_LARGEST_PREFIX,
E_OP_LARGEST_PREFIX, // ${var##pattern} E_OP_SMALLEST_SUFFIX,
E_OP_SMALLEST_SUFFIX, // ${var%pattern} E_OP_LARGEST_SUFFIX,
E_OP_LARGEST_SUFFIX, // ${var%%pattern}
}; };
/*
AST_REDIR_INPUT, = <
AST_REDIR_OUTPUT, = >
AST_REDIR_APPEND, = >>
AST_REDIR_HEREDOC, = <<
AST_REDIR_HEREDOC_INDENT, = <<-
AST_REDIR_DUP_INPUT, = <&
AST_REDIR_DUP_OUTPUT, = >&
AST_REDIR_OUTPUT_CLOBBER, = >|
AST_REDIR_INPUT_OUTPUT, = <>
AST_REDIR_CLOSE_INPUT, = <&
AST_REDIR_CLOSE_OUTPUT, = >&
*/
enum e_ast_redirection_kind enum e_ast_redirection_kind
{ {
AST_REDIR_INPUT, // < AST_REDIR_INPUT,
AST_REDIR_OUTPUT, // > AST_REDIR_OUTPUT,
AST_REDIR_APPEND, // >> AST_REDIR_APPEND,
AST_REDIR_HEREDOC, // << AST_REDIR_HEREDOC,
AST_REDIR_HEREDOC_INDENT, // <<- AST_REDIR_HEREDOC_INDENT,
AST_REDIR_DUP_INPUT, // <& AST_REDIR_DUP_INPUT,
AST_REDIR_DUP_OUTPUT, // >& AST_REDIR_DUP_OUTPUT,
AST_REDIR_OUTPUT_CLOBBER, // >| AST_REDIR_OUTPUT_CLOBBER,
AST_REDIR_INPUT_OUTPUT, // <> AST_REDIR_INPUT_OUTPUT,
AST_REDIR_CLOSE_INPUT, // <& AST_REDIR_CLOSE_INPUT,
AST_REDIR_CLOSE_OUTPUT, // >& AST_REDIR_CLOSE_OUTPUT,
}; };
struct s_ast_empty struct s_ast_empty
@ -99,6 +128,7 @@ struct s_ast_program
/// cmd1 && cmd2 /// cmd1 && cmd2
/// cmd1 || cmd2 >outfile /// cmd1 || cmd2 >outfile
/// ``` /// ```
struct s_ast_list struct s_ast_list
{ {
t_ast_node left; t_ast_node left;
@ -113,6 +143,7 @@ struct s_ast_list
/// cat file | grep stuff | banane | truc /// cat file | grep stuff | banane | truc
/// echo "$sutff" | if truc; then banane; fi | lololol /// echo "$sutff" | if truc; then banane; fi | lololol
/// ``` /// ```
struct s_ast_pipeline struct s_ast_pipeline
{ {
bool bang; bool bang;
@ -128,6 +159,7 @@ struct s_ast_pipeline
/// banane >output /// banane >output
/// VALUE=something echo $VALUE >&1 2>somewhere /// VALUE=something echo $VALUE >&1 2>somewhere
/// ``` /// ```
struct s_ast_command struct s_ast_command
{ {
t_vec_ast prefixes; t_vec_ast prefixes;
@ -144,6 +176,7 @@ struct s_ast_command
/// fi /// fi
/// ``` /// ```
/// Closely related to `t_ast_elif` and `t_ast_else` /// Closely related to `t_ast_elif` and `t_ast_else`
struct s_ast_if struct s_ast_if
{ {
t_vec_ast condition; t_vec_ast condition;
@ -161,6 +194,7 @@ struct s_ast_if
/// fi /// fi
/// ``` /// ```
/// Closely related to `t_ast_if` and `t_ast_else` /// Closely related to `t_ast_if` and `t_ast_else`
struct s_ast_elif struct s_ast_elif
{ {
t_vec_ast condition; t_vec_ast condition;
@ -174,6 +208,7 @@ struct s_ast_elif
/// fi /// fi
/// ``` /// ```
/// Closely related to `t_ast_if` and `t_ast_elif` /// Closely related to `t_ast_if` and `t_ast_elif`
struct s_ast_else struct s_ast_else
{ {
t_vec_ast then; t_vec_ast then;
@ -185,6 +220,7 @@ struct s_ast_else
/// cmd $varname; /// cmd $varname;
/// done /// done
/// ``` /// ```
struct s_ast_while struct s_ast_while
{ {
t_vec_ast condition; t_vec_ast condition;
@ -199,6 +235,7 @@ struct s_ast_while
/// cmd $varname; /// cmd $varname;
/// done /// done
/// ``` /// ```
struct s_ast_for struct s_ast_for
{ {
t_str var_name; t_str var_name;
@ -219,6 +256,7 @@ struct s_ast_for
/// fallback; /// fallback;
/// esac /// esac
/// ``` /// ```
struct s_ast_case struct s_ast_case
{ {
t_ast_node word; t_ast_node word;
@ -237,6 +275,7 @@ struct s_ast_case
/// fallback; /// fallback;
/// ``` /// ```
/// Closely tied to `t_ast_case` /// Closely tied to `t_ast_case`
struct s_ast_case_item struct s_ast_case_item
{ {
t_vec_ast pattern; t_vec_ast pattern;
@ -252,6 +291,7 @@ struct s_ast_case_item
/// truc; /// truc;
/// done /// done
/// ``` /// ```
struct s_ast_until struct s_ast_until
{ {
t_vec_ast condition; t_vec_ast condition;
@ -265,6 +305,7 @@ struct s_ast_until
/// function_name() (comand1; command2 truc banane pomme;) /// function_name() (comand1; command2 truc banane pomme;)
/// function_name() {comand1; command2 truc banane pomme;} /// function_name() {comand1; command2 truc banane pomme;}
/// ``` /// ```
struct s_ast_function_definition struct s_ast_function_definition
{ {
t_str name; t_str name;
@ -275,6 +316,7 @@ struct s_ast_function_definition
/// ```shell /// ```shell
/// (comand1; command2 truc banane pomme;) /// (comand1; command2 truc banane pomme;)
/// ``` /// ```
struct s_ast_subshell struct s_ast_subshell
{ {
t_vec_ast body; t_vec_ast body;
@ -287,6 +329,7 @@ struct s_ast_subshell
/// ```shell /// ```shell
/// { command1; command2 truc banane pomme; } /// { command1; command2 truc banane pomme; }
/// ``` /// ```
struct s_ast_compound_statement struct s_ast_compound_statement
{ {
t_vec_ast body; t_vec_ast body;
@ -299,6 +342,7 @@ struct s_ast_compound_statement
/// ```shell /// ```shell
/// VARIABLE=something /// VARIABLE=something
/// ``` /// ```
struct s_ast_variable_assignment struct s_ast_variable_assignment
{ {
t_str name; t_str name;
@ -312,6 +356,7 @@ struct s_ast_variable_assignment
/// >>outfile /// >>outfile
/// 2>&1 /// 2>&1
/// ``` /// ```
struct s_ast_file_redirection struct s_ast_file_redirection
{ {
t_ast_node output; t_ast_node output;
@ -325,6 +370,7 @@ struct s_ast_file_redirection
/// TEXT blablabla /// TEXT blablabla
/// EOF /// EOF
/// ``` /// ```
struct s_ast_heredoc_redirection struct s_ast_heredoc_redirection
{ {
t_ast_node output; t_ast_node output;
@ -340,6 +386,7 @@ struct s_ast_heredoc_redirection
/// ${VARNAME%%trucmuch pattern} /// ${VARNAME%%trucmuch pattern}
/// $@ /// $@
/// ``` /// ```
struct s_ast_expansion struct s_ast_expansion
{ {
t_str var_name; t_str var_name;
@ -355,6 +402,7 @@ struct s_ast_expansion
/// $((- 1)) /// $((- 1))
/// $((1-1)) /// $((1-1))
/// ``` /// ```
struct s_ast_arithmetic_expansion struct s_ast_arithmetic_expansion
{ {
t_ast_node expr; t_ast_node expr;
@ -364,6 +412,7 @@ struct s_ast_arithmetic_expansion
/// ```shell /// ```shell
/// $(command) /// $(command)
/// ``` /// ```
struct s_ast_command_substitution struct s_ast_command_substitution
{ {
t_vec_ast body; t_vec_ast body;
@ -377,6 +426,7 @@ struct s_ast_command_substitution
/// +(pattern) /// +(pattern)
/// @(pattern) /// @(pattern)
/// ``` /// ```
struct s_ast_extglob struct s_ast_extglob
{ {
t_str pattern; t_str pattern;
@ -386,6 +436,7 @@ struct s_ast_extglob
/// ```shell /// ```shell
/// ~pattern /// ~pattern
/// ``` /// ```
struct s_ast_regex struct s_ast_regex
{ {
t_str pattern; t_str pattern;

View file

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/03 20:38:29 by maiboyer #+# #+# */ /* Created: 2024/07/03 20:38:29 by maiboyer #+# #+# */
/* Updated: 2024/07/03 22:44:59 by maiboyer ### ########.fr */ /* Updated: 2024/07/11 14:50:03 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */