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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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
#define AST_H
# define AST_H
#include "ast/ast_forward_def.h" // IWYU pragma: keep
#include "ast/ast_raw_structs.h" // IWYU pragma: keep
# include "ast/ast_forward_def.h" // IWYU pragma: keep
# include "ast/ast_raw_structs.h" // IWYU pragma: keep
enum e_ast_node_kind
{
S_AST_CATEGORY_MASK = 0xFFFF0000,
S_AST_NODETYPE_MASK = 0x0000FFFF,
S_AST_NONE = 0,
S_AST_COMPOUND_COMMAND = 1 << 16,
S_AST_COMMAND = 1 << 17,
S_AST_REDIRECT = 1 << 18,
S_AST_EXPANSION = 1 << 19,
AST_ARITHMETIC_EXPANSION = S_AST_EXPANSION | 0x0001,
AST_COMMAND_SUBSTITUTION = S_AST_EXPANSION | 0x0002,
AST_EXPANSION = S_AST_EXPANSION | 0x0003,
AST_COMMAND = S_AST_COMMAND | 0x0004,
AST_CASE_ITEM = S_AST_NONE | 0x0005,
AST_ELIF = S_AST_NONE | 0x0006,
AST_ELSE = S_AST_NONE | 0x0007,
@ -42,10 +38,8 @@ enum e_ast_node_kind
AST_VARIABLE_ASSIGNMENT = S_AST_NONE | 0x000E,
AST_EXTGLOB = S_AST_NONE | 0x001A,
AST_REGEX = S_AST_NONE | 0x001B,
AST_FILE_REDIRECTION = S_AST_REDIRECT | 0x000F,
AST_HEREDOC_REDIRECTION = S_AST_REDIRECT | 0x0010,
AST_FOR = S_AST_COMPOUND_COMMAND | 0x0011,
AST_CASE = S_AST_COMPOUND_COMMAND | 0x0012,
AST_COMPOUND_STATEMENT = S_AST_COMPOUND_COMMAND | 0x0013,
@ -59,47 +53,48 @@ enum e_ast_node_kind
AST_WHILE = S_AST_COMPOUND_COMMAND | 0x0019,
};
union u_ast_node_data {
t_ast_arithmetic_expansion arithmetic_expansion;
t_ast_case case_;
t_ast_case_item case_item;
t_ast_command command;
t_ast_command_substitution command_substitution;
t_ast_compound_statement compound_statement;
t_ast_elif elif;
t_ast_else else_;
t_ast_empty empty;
t_ast_expansion expansion;
t_ast_extglob extglob;
t_ast_file_redirection file_redirection;
t_ast_for for_;
t_ast_function_definition function_definition;
t_ast_heredoc_redirection heredoc_redirection;
t_ast_if if_;
t_ast_list list;
t_ast_pipeline pipeline;
t_ast_program program;
t_ast_raw_string raw_string;
t_ast_regex regex;
t_ast_subshell subshell;
t_ast_until until;
t_ast_variable_assignment variable_assignment;
t_ast_while while_;
t_ast_word word;
union u_ast_node_data
{
t_ast_arithmetic_expansion arithmetic_expansion;
t_ast_case case_;
t_ast_case_item case_item;
t_ast_command command;
t_ast_command_substitution command_substitution;
t_ast_compound_statement compound_statement;
t_ast_elif elif;
t_ast_else else_;
t_ast_empty empty;
t_ast_expansion expansion;
t_ast_extglob extglob;
t_ast_file_redirection file_redirection;
t_ast_for for_;
t_ast_function_definition function_definition;
t_ast_heredoc_redirection heredoc_redirection;
t_ast_if if_;
t_ast_list list;
t_ast_pipeline pipeline;
t_ast_program program;
t_ast_raw_string raw_string;
t_ast_regex regex;
t_ast_subshell subshell;
t_ast_until until;
t_ast_variable_assignment variable_assignment;
t_ast_while while_;
t_ast_word word;
};
struct s_ast_node
{
t_ast_node_kind kind;
t_ast_node_data data;
t_ast_node_kind kind;
t_ast_node_data data;
};
static inline bool ast_category(t_ast_node node)
static inline bool ast_category(t_ast_node node)
{
return (node->kind & S_AST_CATEGORY_MASK);
}
static inline bool ast_nodetype(t_ast_node node)
static inline bool ast_nodetype(t_ast_node node)
{
return (node->kind & S_AST_NODETYPE_MASK);
}

View file

@ -6,77 +6,76 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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
#define AST_FORWARD_DEF_H
# define AST_FORWARD_DEF_H
typedef enum e_ast_node_kind t_ast_node_kind;
typedef enum e_ast_word_kind t_ast_word_kind;
typedef enum e_ast_list_kind t_ast_list_kind;
typedef enum e_ast_expansion_operator t_ast_expansion_operator;
typedef enum e_ast_terminator_kind t_ast_terminator_kind;
typedef enum e_ast_redirection_kind t_ast_redirection_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_list_kind t_ast_list_kind;
typedef enum e_ast_expansion_operator t_ast_expansion_operator;
typedef enum e_ast_terminator_kind t_ast_terminator_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;
typedef struct s_ast_arithmetic_expansion t_ast_arithmetic_expansion;
typedef struct s_ast_case t_ast_case;
typedef struct s_ast_case_item t_ast_case_item;
typedef struct s_ast_command t_ast_command;
typedef struct s_ast_command_substitution t_ast_command_substitution;
typedef struct s_ast_compound_statement t_ast_compound_statement;
typedef struct s_ast_elif t_ast_elif;
typedef struct s_ast_else t_ast_else;
typedef struct s_ast_empty t_ast_empty;
typedef struct s_ast_expansion t_ast_expansion;
typedef struct s_ast_extglob t_ast_extglob;
typedef struct s_ast_file_redirection t_ast_file_redirection;
typedef struct s_ast_for t_ast_for;
typedef struct s_ast_function_definition t_ast_function_definition;
typedef struct s_ast_heredoc_redirection t_ast_heredoc_redirection;
typedef struct s_ast_if t_ast_if;
typedef struct s_ast_list t_ast_list;
typedef struct s_ast_pipeline t_ast_pipeline;
typedef struct s_ast_program t_ast_program;
typedef struct s_ast_raw_string t_ast_raw_string;
typedef struct s_ast_regex t_ast_regex;
typedef struct s_ast_subshell t_ast_subshell;
typedef struct s_ast_until t_ast_until;
typedef struct s_ast_variable_assignment t_ast_variable_assignment;
typedef struct s_ast_while t_ast_while;
typedef struct s_ast_word t_ast_word;
typedef struct s_ast_arithmetic_expansion t_ast_arithmetic_expansion;
typedef struct s_ast_case t_ast_case;
typedef struct s_ast_case_item t_ast_case_item;
typedef struct s_ast_command t_ast_command;
typedef struct s_ast_command_substitution t_ast_command_substitution;
typedef struct s_ast_compound_statement t_ast_compound_statement;
typedef struct s_ast_elif t_ast_elif;
typedef struct s_ast_else t_ast_else;
typedef struct s_ast_empty t_ast_empty;
typedef struct s_ast_expansion t_ast_expansion;
typedef struct s_ast_extglob t_ast_extglob;
typedef struct s_ast_file_redirection t_ast_file_redirection;
typedef struct s_ast_for t_ast_for;
typedef struct s_ast_function_definition t_ast_function_definition;
typedef struct s_ast_heredoc_redirection t_ast_heredoc_redirection;
typedef struct s_ast_if t_ast_if;
typedef struct s_ast_list t_ast_list;
typedef struct s_ast_pipeline t_ast_pipeline;
typedef struct s_ast_program t_ast_program;
typedef struct s_ast_raw_string t_ast_raw_string;
typedef struct s_ast_regex t_ast_regex;
typedef struct s_ast_subshell t_ast_subshell;
typedef struct s_ast_until t_ast_until;
typedef struct s_ast_variable_assignment t_ast_variable_assignment;
typedef struct s_ast_while t_ast_while;
typedef struct s_ast_word t_ast_word;
/*
t_ast_arithmetic_expansion arithmetic_expansion;
t_ast_case_item case_item;
t_ast_case case;
t_ast_command_substitution command_substitution;
t_ast_command command;
t_ast_compound_statement compound_statement;
t_ast_elif elif;
t_ast_else else;
t_ast_empty empty;
t_ast_expansion expansion;
t_ast_file_redirection file_redirection;
t_ast_for for;
t_ast_function_definition function_definition;
t_ast_heredoc_redirection heredoc_redirection;
t_ast_if if;
t_ast_list list;
t_ast_program program;
t_ast_pipeline pipeline;
t_ast_raw_string raw_string;
t_ast_string string;
t_ast_subshell subshell;
t_ast_until until;
t_ast_variable_assignment variable_assignment;
t_ast_while while;
t_ast_word word;
t_ast_arithmetic_expansion arithmetic_expansion;
t_ast_case_item case_item;
t_ast_case case;
t_ast_command_substitution command_substitution;
t_ast_command command;
t_ast_compound_statement compound_statement;
t_ast_elif elif;
t_ast_else else;
t_ast_empty empty;
t_ast_expansion expansion;
t_ast_file_redirection file_redirection;
t_ast_for for;
t_ast_function_definition function_definition;
t_ast_heredoc_redirection heredoc_redirection;
t_ast_if if;
t_ast_list list;
t_ast_program program;
t_ast_pipeline pipeline;
t_ast_raw_string raw_string;
t_ast_string string;
t_ast_subshell subshell;
t_ast_until until;
t_ast_variable_assignment variable_assignment;
t_ast_while while;
t_ast_word word;
*/
/*

View file

@ -6,16 +6,16 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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
#define AST_RAW_STRUCTS_H
# define AST_RAW_STRUCTS_H
#include "ast/ast_forward_def.h"
#include "me/types.h"
#include "me/vec/vec_ast.h"
# include "ast/ast_forward_def.h"
# include "me/types.h"
# include "me/vec/vec_ast.h"
enum e_ast_list_kind
{
@ -39,37 +39,66 @@ enum e_ast_terminator_kind
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
{
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}
E_OP_NONE = 0,
E_OP_DEFAULT,
E_OP_ASSIGN_DEFAULT,
E_OP_ERROR,
E_OP_ALTERNATE,
E_OP_DEFAULT_COLON,
E_OP_ASSIGN_DEFAULT_COLON,
E_OP_ERROR_COLON,
E_OP_ALTERNATE_COLON,
E_OP_SMALLEST_PREFIX,
E_OP_LARGEST_PREFIX,
E_OP_SMALLEST_SUFFIX,
E_OP_LARGEST_SUFFIX,
};
/*
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
{
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, // >&
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,
};
struct s_ast_empty
@ -79,18 +108,18 @@ struct s_ast_empty
struct s_ast_raw_string
{
t_str str;
t_usize len;
t_usize len;
};
struct s_ast_word
{
t_ast_word_kind kind;
t_ast_word_kind kind;
t_vec_ast inner;
};
struct s_ast_program
{
t_vec_ast body;
t_vec_ast body;
};
/// Pipeline Statemen
@ -99,13 +128,14 @@ struct s_ast_program
/// cmd1 && cmd2
/// cmd1 || cmd2 >outfile
/// ```
struct s_ast_list
{
t_ast_node left;
t_ast_list_kind op;
t_ast_node right;
t_ast_terminator_kind term;
t_vec_ast suffixes_redirections;
t_ast_node left;
t_ast_list_kind op;
t_ast_node right;
t_ast_terminator_kind term;
t_vec_ast suffixes_redirections;
};
/// Pipeline Statement
@ -113,12 +143,13 @@ struct s_ast_list
/// cat file | grep stuff | banane | truc
/// echo "$sutff" | if truc; then banane; fi | lololol
/// ```
struct s_ast_pipeline
{
bool bang;
t_vec_ast statements;
t_ast_terminator_kind term;
t_vec_ast suffixes_redirections;
bool bang;
t_vec_ast statements;
t_ast_terminator_kind term;
t_vec_ast suffixes_redirections;
};
/// Command Statement
@ -128,13 +159,14 @@ struct s_ast_pipeline
/// banane >output
/// VALUE=something echo $VALUE >&1 2>somewhere
/// ```
struct s_ast_command
{
t_vec_ast prefixes;
t_vec_ast cmd_word;
t_vec_ast suffixes_redirections;
t_ast_terminator_kind term;
bool bang;
t_vec_ast prefixes;
t_vec_ast cmd_word;
t_vec_ast suffixes_redirections;
t_ast_terminator_kind term;
bool bang;
};
/// If Statement
@ -144,14 +176,15 @@ struct s_ast_command
/// fi
/// ```
/// Closely related to `t_ast_elif` and `t_ast_else`
struct s_ast_if
{
t_vec_ast condition;
t_vec_ast then;
t_vec_ast elif_;
t_ast_node else_;
t_ast_terminator_kind term;
t_vec_ast suffixes_redirections;
t_vec_ast condition;
t_vec_ast then;
t_vec_ast elif_;
t_ast_node else_;
t_ast_terminator_kind term;
t_vec_ast suffixes_redirections;
};
/// Elif Statement
@ -161,10 +194,11 @@ struct s_ast_if
/// fi
/// ```
/// Closely related to `t_ast_if` and `t_ast_else`
struct s_ast_elif
{
t_vec_ast condition;
t_vec_ast then;
t_vec_ast condition;
t_vec_ast then;
};
/// Else Statement
@ -174,9 +208,10 @@ struct s_ast_elif
/// fi
/// ```
/// Closely related to `t_ast_if` and `t_ast_elif`
struct s_ast_else
{
t_vec_ast then;
t_vec_ast then;
};
/// While loop
@ -185,12 +220,13 @@ struct s_ast_else
/// cmd $varname;
/// done
/// ```
struct s_ast_while
{
t_vec_ast condition;
t_vec_ast do_;
t_ast_terminator_kind term;
t_vec_ast suffixes_redirections;
t_vec_ast condition;
t_vec_ast do_;
t_ast_terminator_kind term;
t_vec_ast suffixes_redirections;
};
/// For loop
@ -199,13 +235,14 @@ struct s_ast_while
/// cmd $varname;
/// done
/// ```
struct s_ast_for
{
t_str var_name;
t_vec_ast words;
t_vec_ast do_;
t_ast_terminator_kind term;
t_vec_ast suffixes_redirections;
t_str var_name;
t_vec_ast words;
t_vec_ast do_;
t_ast_terminator_kind term;
t_vec_ast suffixes_redirections;
};
/// Case Statement
@ -219,12 +256,13 @@ struct s_ast_for
/// fallback;
/// esac
/// ```
struct s_ast_case
{
t_ast_node word;
t_vec_ast cases;
t_ast_terminator_kind term;
t_vec_ast suffixes_redirections;
t_ast_node word;
t_vec_ast cases;
t_ast_terminator_kind term;
t_vec_ast suffixes_redirections;
};
/// Case Statement
@ -237,11 +275,12 @@ struct s_ast_case
/// fallback;
/// ```
/// Closely tied to `t_ast_case`
struct s_ast_case_item
{
t_vec_ast pattern;
t_vec_ast body;
t_ast_terminator_kind term;
t_vec_ast pattern;
t_vec_ast body;
t_ast_terminator_kind term;
};
/// Until loop
@ -252,12 +291,13 @@ struct s_ast_case_item
/// truc;
/// done
/// ```
struct s_ast_until
{
t_vec_ast condition;
t_vec_ast do_;
t_ast_terminator_kind term;
t_vec_ast suffixes_redirections;
t_vec_ast condition;
t_vec_ast do_;
t_ast_terminator_kind term;
t_vec_ast suffixes_redirections;
};
/// Function Definition
@ -265,45 +305,49 @@ struct s_ast_until
/// function_name() (comand1; command2 truc banane pomme;)
/// function_name() {comand1; command2 truc banane pomme;}
/// ```
struct s_ast_function_definition
{
t_str name;
t_vec_ast body;
t_str name;
t_vec_ast body;
};
/// Parenthesis block
/// ```shell
/// (comand1; command2 truc banane pomme;)
/// ```
struct s_ast_subshell
{
t_vec_ast body;
t_ast_terminator_kind term;
t_vec_ast suffixes_redirections;
bool bang;
t_vec_ast body;
t_ast_terminator_kind term;
t_vec_ast suffixes_redirections;
bool bang;
};
/// Brace block
/// ```shell
/// { command1; command2 truc banane pomme; }
/// ```
struct s_ast_compound_statement
{
t_vec_ast body;
t_ast_terminator_kind term;
t_vec_ast suffixes_redirections;
bool bang;
t_vec_ast body;
t_ast_terminator_kind term;
t_vec_ast suffixes_redirections;
bool bang;
};
/// Variable Assignment
/// ```shell
/// VARIABLE=something
/// ```
struct s_ast_variable_assignment
{
t_str name;
t_ast_node value;
bool bang;
t_str name;
t_ast_node value;
bool bang;
};
/// File Redirection
@ -312,11 +356,12 @@ struct s_ast_variable_assignment
/// >>outfile
/// 2>&1
/// ```
struct s_ast_file_redirection
{
t_ast_node output;
t_ast_redirection_kind op;
t_ast_node input;
t_ast_node output;
t_ast_redirection_kind op;
t_ast_node input;
};
/// File Redirection
@ -325,11 +370,12 @@ struct s_ast_file_redirection
/// TEXT blablabla
/// EOF
/// ```
struct s_ast_heredoc_redirection
{
t_ast_node output;
t_ast_redirection_kind op;
t_ast_node delimiter;
t_ast_node output;
t_ast_redirection_kind op;
t_ast_node delimiter;
};
/// Variable Expension
@ -340,12 +386,13 @@ struct s_ast_heredoc_redirection
/// ${VARNAME%%trucmuch pattern}
/// $@
/// ```
struct s_ast_expansion
{
t_str var_name;
bool len_operator;
t_ast_expansion_operator kind;
t_vec_ast args;
t_str var_name;
bool len_operator;
t_ast_expansion_operator kind;
t_vec_ast args;
};
/// Variable Expension
@ -355,18 +402,20 @@ struct s_ast_expansion
/// $((- 1))
/// $((1-1))
/// ```
struct s_ast_arithmetic_expansion
{
t_ast_node expr;
t_ast_node expr;
};
/// Command Substitution
/// ```shell
/// $(command)
/// ```
struct s_ast_command_substitution
{
t_vec_ast body;
t_vec_ast body;
};
/// Extended Globbing
@ -377,18 +426,20 @@ struct s_ast_command_substitution
/// +(pattern)
/// @(pattern)
/// ```
struct s_ast_extglob
{
t_str pattern;
t_str pattern;
};
/// Regex
/// ```shell
/// ~pattern
/// ```
struct s_ast_regex
{
t_str pattern;
t_str pattern;
};
#endif /* AST_RAW_STRUCTS_H */

View file

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */
@ -14,7 +14,7 @@
#include "me/types.h"
#include <stdio.h>
void ast_print_node(t_ast_node self);
void ast_print_node(t_ast_node self);
#define NOT_DONE \
{ \