HEADERS = -I include -I $(LIB_DIR)/libft
VPATH := src
-SRC := main.c debug_tools.c init.c signal_handling.c repl.c
+SRC := main.c debug_tools.c init.c signal_handling.c repl.c new_token.c \
+ free_token.c new_node.c free_node.c
OBJ_DIR := _obj
OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o))
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ast.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/06/27 11:48:27 by dkaiser #+# #+# */
+/* Updated: 2024/06/28 14:56:55 by dkaiser ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "stdlib.h"
+#include "debug_tools.h"
+
+typedef struct s_sequence
+{
+ struct s_node **nodes;
+} t_sequence;
+
+enum e_node_type
+{
+ ASSIGN_NODE,
+ PIPE_NODE,
+ CMD_NODE,
+ STRING_NODE
+};
+
+typedef struct s_assign
+{
+ char *var;
+ char *value;
+} t_assign;
+
+typedef struct s_pipe
+{
+ struct s_node *left;
+ struct s_node *right;
+} t_pipe;
+
+enum e_redirection_type
+{
+ INPUT_FILE,
+ INPUT_LIMITER,
+ OUTPUT_OVERRIDE,
+ OUTPUT_APPEND
+};
+
+typedef struct s_redirection
+{
+ int type;
+ char *specifier;
+} t_redirection;
+
+typedef struct s_cmd
+{
+ char **args;
+ struct s_redirection redirs[2];
+} t_cmd;
+
+union u_node_content
+{
+ struct s_assign assign;
+ struct s_pipe pipe;
+ struct s_cmd cmd;
+ char *string;
+};
+
+typedef struct s_node
+{
+ int type;
+ union u_node_content content;
+} t_node;
+
+t_node *new_node(int type);
+t_node *new_assign_node(char *var, char *value);
+t_node *new_pipe_node(t_node *left, t_node *right);
+t_node *new_cmd_node(char **args, t_redirection redirs[2]);
+t_node *new_string_node(char *string);
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/24 18:34:37 by dkaiser #+# #+# */
-/* Updated: 2024/06/24 18:51:53 by dkaiser ### ########.fr */
+/* Updated: 2024/06/28 15:05:12 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
# ifndef DEBUG
# define DEBUG 0
# endif
+# define UNREACHABLE "Unreachable."
void dbg(char *str);
void panic(char *msg);
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/22 17:14:49 by dkaiser #+# #+# */
-/* Updated: 2024/06/25 15:02:38 by dkaiser ### ########.fr */
+/* Updated: 2024/06/27 18:47:31 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
# define MINISHELL_H
# include "debug_tools.h"
+# include "ast.h"
+# include "token.h"
# include "libft.h"
# include <stdio.h>
# include <readline/readline.h>
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* token.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/06/27 13:27:18 by dkaiser #+# #+# */
+/* Updated: 2024/06/28 14:59:19 by dkaiser ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#ifndef TOKEN_H
+# define TOKEN_H
+
+# include "stdlib.h"
+
+enum e_token_type
+{
+ STRING_TOKEN,
+ PIPE_TOKEN,
+ REDIR_TOKEN,
+ NEWLINE_TOKEN
+};
+
+union u_token_content
+{
+ char *string;
+ int redir_type;
+};
+
+typedef struct s_token
+{
+ int type;
+ union u_token_content content;
+ struct s_token *previous;
+ struct s_token *next;
+} t_token;
+
+t_token *new_token(int type, t_token *previous,
+ t_token *next);
+t_token *new_str_token(char *str, t_token *previous,
+ t_token *next);
+t_token *new_redir_token(int type, t_token *previous,
+ t_token *next);
+
+void free_token(t_token *token);
+
+#endif
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/24 15:34:14 by dkaiser #+# #+# */
-/* Updated: 2024/06/25 13:13:18 by dkaiser ### ########.fr */
+/* Updated: 2024/06/28 15:04:43 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* free_node.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/06/27 11:41:46 by dkaiser #+# #+# */
+/* Updated: 2024/06/28 14:55:50 by dkaiser ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ast.h"
+
+static void free_assign_node(t_node *node);
+static void free_pipe_node(t_node *node);
+static void free_cmd_node(t_node *node);
+
+void free_node(t_node *node)
+{
+ if (node->type == ASSIGN_NODE)
+ free_assign_node(node);
+ else if (node->type == PIPE_NODE)
+ free_pipe_node(node);
+ else if (node->type == CMD_NODE)
+ free_cmd_node(node);
+ else if (node->type == STRING_NODE)
+ free(node->content.string);
+ else
+ panic(UNREACHABLE);
+ free(node);
+}
+
+static void free_assign_node(t_node *node)
+{
+ free(node->content.assign.var);
+ free(node->content.assign.value);
+}
+
+static void free_pipe_node(t_node *node)
+{
+ free_node(node->content.pipe.left);
+ free_node(node->content.pipe.right);
+}
+
+static void free_cmd_node(t_node *node)
+{
+ int i;
+
+ i = 0;
+ while (node->content.cmd.args[i] != NULL)
+ {
+ free(node->content.cmd.args[i]);
+ i++;
+ }
+ free(node->content.cmd.args);
+ free(node->content.cmd.redirs[0].specifier);
+ free(node->content.cmd.redirs[1].specifier);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* free_token.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/06/27 14:38:57 by dkaiser #+# #+# */
+/* Updated: 2024/06/28 14:55:12 by dkaiser ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "token.h"
+
+void free_token(t_token *token)
+{
+ if (token->type == STRING_TOKEN)
+ free(token->content.string);
+ if (token->previous != NULL)
+ token->previous->next = NULL;
+ if (token->next != NULL)
+ token->next->previous = NULL;
+ free(token);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* new_node.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/06/27 11:21:03 by dkaiser #+# #+# */
+/* Updated: 2024/06/28 15:04:15 by dkaiser ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ast.h"
+
+t_node *new_node(int type)
+{
+ t_node *node;
+
+ node = malloc(sizeof(t_node));
+ if (node == NULL)
+ return (NULL);
+ node->type = type;
+ return (node);
+}
+
+t_node *new_assign_node(char *var, char *value)
+{
+ t_node *node;
+
+ node = new_node(ASSIGN_NODE);
+ if (node == NULL)
+ return (NULL);
+ node->content.assign.var = var;
+ node->content.assign.value = value;
+ return (node);
+}
+
+t_node *new_pipe_node(t_node *left, t_node *right)
+{
+ t_node *node;
+
+ node = new_node(PIPE_NODE);
+ if (node == NULL)
+ return (NULL);
+ node->content.pipe.left = left;
+ node->content.pipe.right = right;
+ return (node);
+}
+
+t_node *new_cmd_node(char **args, t_redirection redirs[2])
+{
+ t_node *node;
+
+ node = new_node(CMD_NODE);
+ if (node == NULL)
+ return (NULL);
+ node->content.cmd.args = args;
+ node->content.cmd.redirs[0] = redirs[0];
+ node->content.cmd.redirs[1] = redirs[1];
+ return (node);
+}
+
+t_node *new_string_node(char *string)
+{
+ t_node *node;
+
+ node = new_node(STRING_NODE);
+ if (node == NULL)
+ return (NULL);
+ node->content.string = string;
+ return (node);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* new_token.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/06/27 14:29:44 by dkaiser #+# #+# */
+/* Updated: 2024/06/28 14:59:34 by dkaiser ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "token.h"
+
+t_token *new_token(int type, t_token *previous, t_token *next)
+{
+ t_token *token;
+
+ token = malloc(sizeof(t_token));
+ if (token == NULL)
+ return (NULL);
+ token->type = type;
+ token->previous = previous;
+ token->next = next;
+ if (previous != NULL)
+ token->previous->next = token;
+ if (next != NULL)
+ token->next->previous = token;
+ return (token);
+}
+
+t_token *new_str_token(char *str, t_token *previous, t_token *next)
+{
+ t_token *token;
+
+ token = new_token(STRING_TOKEN, previous, next);
+ if (token == NULL)
+ return (NULL);
+ token->content.string = str;
+ return (token);
+}
+
+t_token *new_redir_token(int type, t_token *previous, t_token *next)
+{
+ t_token *token;
+
+ token = new_token(REDIR_TOKEN, previous, next);
+ if (token == NULL)
+ return (NULL);
+ token->content.redir_type = type;
+ return (token);
+}