]> git.dkaiser.de - 42/minishell.git/commitdiff
Add data structures for tokenizing and parsing
authorDominik Kaiser <dkaiser@student.42heilbronn.de>
Fri, 28 Jun 2024 13:09:32 +0000 (15:09 +0200)
committerGitHub <noreply@github.com>
Fri, 28 Jun 2024 13:09:32 +0000 (15:09 +0200)
* Add data structures and helper functions for ast

* Add data structures for tokenizing

* Add helper functions for token structures

* Include token.h in minishell.h

* Add new/free functions for nodes/tokens to Makefile

* Add UNREACHABLE macro to debug_tools.h

Makefile
include/ast.h [new file with mode: 0644]
include/debug_tools.h
include/minishell.h
include/token.h [new file with mode: 0644]
src/debug_tools.c
src/free_node.c [new file with mode: 0644]
src/free_token.c [new file with mode: 0644]
src/new_node.c [new file with mode: 0644]
src/new_token.c [new file with mode: 0644]

index dfe7a30da61cf30ce054b6b22731a4329472228b..41551ec68379b360ebbae7ec5aa48851d42c6fba 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -11,7 +11,8 @@ LIBS    =  -L $(LIB_DIR)/libft -lft -lreadline
 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))
diff --git a/include/ast.h b/include/ast.h
new file mode 100644 (file)
index 0000000..e6ad25d
--- /dev/null
@@ -0,0 +1,79 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   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);
index e831ecc8ae8e581e6edcc3c7cca0c7a956c56f0a..4a7ff109419ae6423b2750d2cf1e84e3393c6396 100644 (file)
@@ -6,7 +6,7 @@
 /*   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       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -18,6 +18,7 @@
 # ifndef DEBUG
 #  define DEBUG 0
 # endif
+# define UNREACHABLE "Unreachable."
 
 void   dbg(char *str);
 void   panic(char *msg);
index 8dab21add379d512be4d44cdce2334a21526b723..a528e4a533120a3e31fcb8003d22d96501f5d8fe 100644 (file)
@@ -6,7 +6,7 @@
 /*   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       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -14,6 +14,8 @@
 # define MINISHELL_H
 
 # include "debug_tools.h"
+# include "ast.h"
+# include "token.h"
 # include "libft.h"
 # include <stdio.h>
 # include <readline/readline.h>
diff --git a/include/token.h b/include/token.h
new file mode 100644 (file)
index 0000000..38e758f
--- /dev/null
@@ -0,0 +1,49 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   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
index ccf21642c9fc1f23143b9fbc0281a6ee4654b86b..de5970355c8efd53a59357683b87d0dbb04c446c 100644 (file)
@@ -6,7 +6,7 @@
 /*   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       */
 /*                                                                            */
 /* ************************************************************************** */
 
diff --git a/src/free_node.c b/src/free_node.c
new file mode 100644 (file)
index 0000000..8f32c12
--- /dev/null
@@ -0,0 +1,59 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   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);
+}
diff --git a/src/free_token.c b/src/free_token.c
new file mode 100644 (file)
index 0000000..9f5c4e9
--- /dev/null
@@ -0,0 +1,24 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   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);
+}
diff --git a/src/new_node.c b/src/new_node.c
new file mode 100644 (file)
index 0000000..4cdbf9a
--- /dev/null
@@ -0,0 +1,72 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   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);
+}
diff --git a/src/new_token.c b/src/new_token.c
new file mode 100644 (file)
index 0000000..92ff421
--- /dev/null
@@ -0,0 +1,52 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   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);
+}