Change the file structure of the project
authorLukas Jiriste <ljiriste@student.42prague.com>
Fri, 21 Jun 2024 15:02:05 +0000 (17:02 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Fri, 21 Jun 2024 15:02:05 +0000 (17:02 +0200)
Because the parsing was implemented inside libft the parsingh was not
needed here. The informative contents were moved to about_grammar.

The variable expansion and tokenization were given their own files.

Makefile
about_grammar [moved from inc/parsing.h with 55% similarity]
inc/minishell.h
src/execution.c
src/expansion.c [moved from src/tree.c with 61% similarity]
src/helpers.c [new file with mode: 0644]
src/input_handling.c
src/parsing.c
src/tokenization.c [new file with mode: 0644]

index 04306101407913067adda1ff68620c4edf974d34..43bd56464c70afdf18820ee740713b90372ebac5 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -17,9 +17,11 @@ SRCDIR := src
 SOURCES :=     main.c                          \
                        vars.c                          \
                        input_handling.c        \
-                       tree.c                          \
+                       tokenization.c          \
                        parsing.c                       \
+                       expansion.c                     \
                        execution.c                     \
+                       helpers.c                       \
 
 SOURCES := $(addprefix $(SRCDIR)/, $(SOURCES))
 
similarity index 55%
rename from inc/parsing.h
rename to about_grammar
index 53c11815f429f489b47b45ebd9f75bf127943c66..bd34367dfb74dddcccc70253a3fea5010ce53b66 100644 (file)
@@ -1,29 +1,10 @@
-/* ************************************************************************** */
-/*                                                                            */
-/*                                                        :::      ::::::::   */
-/*   parsing.h                                          :+:      :+:    :+:   */
-/*                                                    +:+ +:+         +:+     */
-/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
-/*                                                +#+#+#+#+#+   +#+           */
-/*   Created: 2024/05/03 15:24:31 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/06/14 10:30:40 by ljiriste         ###   ########.fr       */
-/*                                                                            */
-/* ************************************************************************** */
-
-#ifndef PARSING_H
-# define PARSING_H
-
-# include "minishell.h"
-# include "libft.h"
-
-/*
-// As this is a minishell I used the
-// The Open Group Base Specifications Issue 7, 2018 edition
-// chapter 2. Shell Command Language as a main source.
-// which can be found at the address
-// https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
-// Because minishell is mini, I trim the grammar quite a bit,
-// so I think it appropriate to include the grammar here:
+As this is a minishell I used the
+The Open Group Base Specifications Issue 7, 2018 edition
+chapter 2. Shell Command Language as a main source.
+which can be found at the address
+https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
+Because minishell is mini, I trim the grammar quite a bit,
+so I think it appropriate to include the grammar here:
 
 Grammar Rules:
        2. Redirection to or from file
@@ -150,50 +131,3 @@ io_file -> < filename
 io_file -> > filename
 io_file -> >> filename
 filename -> WORD
-
-*/
-
-// Common type for WORD, ASSIGNMENT_WORD and IO_NUMBER is TOKEN.
-// The TOKEN type is identifies as WORD, ASSIGNMENT_WORD or IO_NUMBER
-// after tokenization itself
-typedef enum e_token_type
-{
-       TOKEN,
-       WORD,
-       ASSIGNMENT_WORD,
-       IO_NUMBER,
-       AND_IF,
-       OR_IF,
-       LESS,
-       GREAT,
-       DLESS,
-       DGREAT,
-       PIPE,
-       LPARA,
-       RPARA,
-}      t_token_type;
-
-// Only TOKEN (WORD, ASSIGNMENT_WORD, IO_NUMBER) type tokens
-// have the str member non-empty
-typedef struct s_token
-{
-       t_token_type    type;
-       char                    *str;
-}                                      t_token;
-
-typedef struct s_tree_node
-{
-       t_token                         token;
-       struct s_tree_node      *children[3];
-}                                              t_tree_node;
-
-typedef t_tree_node    t_tree;
-
-void   handle_input(const char *input, t_vars *vars);
-int            parse(t_vec *tokens, t_tree_node *parse_tree, t_vars *vars);
-int            execute(t_tree *parse_tree, t_vars *vars);
-
-int            init_tree(t_tree *tree);
-void   free_tree(t_tree *tree);
-
-#endif // PARSING_H
index 68727cf9c2ec2b1165ab382f695f43641b301a93..100011ca68e5754e3d19cba6efe75340f575f66d 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/02 13:22:57 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/05/03 13:01:59 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/06/21 16:35:59 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -21,6 +21,8 @@ typedef struct s_vars
        t_vec   other;
 }                      t_vars;
 
+typedef t_parse_tree_node      t_tree;
+
 int            init_vars(t_vars *vars, char **envp);
 int            add_var_line(t_vec *vec, const char *line);
 int            add_var(t_vec *vec, const char *key, const char *value);
@@ -28,4 +30,13 @@ void clean_vars(t_vars *vars);
 
 void   handle_input(const char *line, t_vars *vars);
 
+int            tokenize(const char *line, t_vec *tokens);
+int            parse(t_vec *tokens, t_tree *parse_tree);
+int            expand(t_tree *parse_tree, t_vars *vars);
+int            execute(t_tree *parse_tree, t_vars *vars);
+
+int            init_tree(t_tree *tree);
+void   free_tree(t_tree *tree);
+void   free_token(void *token);
+
 #endif // MINISHELL_H
index d350ec05156abc0a90a5d1b29e3dd407715d9531..238734c043da06df519564ba0cfeff4330de25b0 100644 (file)
@@ -6,11 +6,11 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/03 15:59:58 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/05/03 16:21:02 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/06/21 17:00:46 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
-#include "parsing.h"
+#include "minishell.h"
 
 int    execute(__attribute__((unused))t_tree *parse_tree, __attribute__((unused))t_vars *vars)
 {
similarity index 61%
rename from src/tree.c
rename to src/expansion.c
index c06af96c4e696de4f40b48fe46d3b83e67381a28..697241e4a5b66d5043d6d49591c2e971a5b741ef 100644 (file)
@@ -1,25 +1,19 @@
 /* ************************************************************************** */
 /*                                                                            */
 /*                                                        :::      ::::::::   */
-/*   tree.c                                             :+:      :+:    :+:   */
+/*   expansion.c                                        :+:      :+:    :+:   */
 /*                                                    +:+ +:+         +:+     */
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
-/*   Created: 2024/05/03 15:56:55 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/05/03 16:03:37 by ljiriste         ###   ########.fr       */
+/*   Created: 2024/06/21 16:29:48 by ljiriste          #+#    #+#             */
+/*   Updated: 2024/06/21 16:31:57 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
-#include "parsing.h"
+#include "minishell.h"
 
-int    init_tree(__attribute__((unused))t_tree *tree)
+int    expand(__attribute__((unused))t_tree *parse_tree, __attribute__((unused))t_vars *vars)
 {
-       ft_printf("Function init_tree has to be implemented.\n");
+       ft_printf("Variable expansion has yet to be implemented.\n");
        return (0);
 }
-
-void   free_tree(__attribute__((unused))t_tree *tree)
-{
-       ft_printf("Function free_tree has to by implemented.\n");
-       return ;
-}
diff --git a/src/helpers.c b/src/helpers.c
new file mode 100644 (file)
index 0000000..3f193e1
--- /dev/null
@@ -0,0 +1,20 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   helpers.c                                          :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/06/21 16:35:16 by ljiriste          #+#    #+#             */
+/*   Updated: 2024/06/21 16:40:21 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "minishell.h"
+#include <stdlib.h>
+
+void   free_token(void *token)
+{
+       free(((t_token *)token)->str);
+       return ;
+}
index b580fa914bb40ed3671831459513f3bf485cdaa7..1c1a763320aa7e58fc97ef31a4756b7b8507cea4 100644 (file)
@@ -6,56 +6,28 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/03 09:00:00 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/06/14 11:00:16 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/06/21 16:39:27 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
-#include "parsing.h"
 #include "minishell.h"
 #include "libft.h"
 #include <stdlib.h>
 
-static void    free_token(void *token)
-{
-       free(((t_token *)token)->str);
-       return ;
-}
-
-static int     delimit(__attribute__((unused))const char *line, __attribute__((unused))t_vec *tokens)
-{
-       ft_printf("Function delimit has to be implemented.\n");
-       return (0);
-}
-
-static int     identify(__attribute__((unused))t_vec *tokens)
-{
-       ft_printf("Function identify has to be implemented.\n");
-       return (0);
-}
-
-//     This function turns the input char string into a string of tokens
-static int     tokenize(const char *line, t_vec *tokens)
-{
-       if (delimit(line, tokens))
-               return (1);
-       if (identify(tokens))
-               return (1);
-       return (0);
-}
-
 void   handle_input(const char *input, t_vars *vars)
 {
        int             res;
        t_vec   tokens;
-       t_tree  parse_tree;
+       t_tree  *parse_tree;
 
        ft_vec_init(&tokens, sizeof(t_token));
-       init_tree(&parse_tree);
+       parse_tree = NULL;
        res = tokenize(input, &tokens);
-       res = res || parse(&tokens, &parse_tree, vars);
-       res = res || execute(&parse_tree, vars);
+       res = res || parse(&tokens, parse_tree);
+       res = res || expand(parse_tree, vars);
+       res = res || execute(parse_tree, vars);
        ft_vec_free(&tokens, free_token);
-       free_tree(&parse_tree);
+       ft_parse_tree_free(parse_tree);
        if (res != 0)
                ft_printf("An error has occured.\n");
        return ;
index 9fa21288484400abd5f8e4e96fdcd49bbac434bf..c28aa37a69f0bca25baf9f8559fc4dde733fd7e1 100644 (file)
@@ -6,14 +6,22 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/03 15:58:55 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/05/03 16:20:45 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/06/21 16:32:50 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
-#include "parsing.h"
+#include "minishell.h"
+#include "libft.h"
 
-int    parse(__attribute__((unused))t_vec *tokens, __attribute__((unused))t_tree_node *parse_tree, __attribute__((unused))t_vars *vars)
+//     table should be static or loaded at start and passed in as argument
+int    parse(t_vec *tokens, t_tree *parse_tree)
 {
-       ft_printf("Function parse has to be implemented.\n");
+       t_parsing_table table;
+
+       ft_parsing_table_init(&table);
+       ft_parsing_table_load(&table, "shell_parsing_table", "shell_rules");
+       parse_tree = ft_parse(tokens, &table);
+       ft_parse_tree_print(parse_tree);
+       ft_parsing_table_free(&table);
        return (0);
 }
diff --git a/src/tokenization.c b/src/tokenization.c
new file mode 100644 (file)
index 0000000..6324d31
--- /dev/null
@@ -0,0 +1,35 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   tokenize.c                                         :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/06/21 16:34:43 by ljiriste          #+#    #+#             */
+/*   Updated: 2024/06/21 16:35:59 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "minishell.h"
+
+static int     delimit(__attribute__((unused))const char *line, __attribute__((unused))t_vec *tokens)
+{
+       ft_printf("Function delimit has to be implemented.\n");
+       return (0);
+}
+
+static int     identify(__attribute__((unused))t_vec *tokens)
+{
+       ft_printf("Function identify has to be implemented.\n");
+       return (0);
+}
+
+//     This function turns the input char string into a string of tokens
+int    tokenize(const char *line, t_vec *tokens)
+{
+       if (delimit(line, tokens))
+               return (1);
+       if (identify(tokens))
+               return (1);
+       return (0);
+}