aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2020-08-27 19:13:28 +0200
committerCharles Cabergs <me@cacharle.xyz>2020-08-27 21:13:39 +0200
commit95a16d2d88c8628ab0ae76f3ae04dfebee566950 (patch)
treefb8ca171c2ce4fa91a294526764f579f1a7343f3 /src
parenta680cf09a3fa4b7c6adc38e4297ee5535172826b (diff)
downloadminishell-95a16d2d88c8628ab0ae76f3ae04dfebee566950.tar.gz
minishell-95a16d2d88c8628ab0ae76f3ae04dfebee566950.tar.bz2
minishell-95a16d2d88c8628ab0ae76f3ae04dfebee566950.zip
Fising tok_lst_new uninitialized next, Added tok_lst_debug, Fixing parse_cmd
Diffstat (limited to 'src')
-rw-r--r--src/ast.c4
-rw-r--r--src/debug.c10
-rw-r--r--src/lexer/lexer.c6
-rw-r--r--src/lexer/tok_lst.c15
-rw-r--r--src/main.c15
-rw-r--r--src/parser/parsed.c40
-rw-r--r--src/parser/parser.c (renamed from src/parse/parse.c)70
-rw-r--r--src/preprocess.c4
8 files changed, 100 insertions, 64 deletions
diff --git a/src/ast.c b/src/ast.c
index e2c4017..2bb90d9 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -6,7 +6,7 @@
/* By: charles <charles@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/01 17:05:42 by charles #+# #+# */
-/* Updated: 2020/08/27 18:39:31 by charles ### ########.fr */
+/* Updated: 2020/08/27 20:31:48 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,7 +15,7 @@
** \brief AST functions
*/
-#include "ast.h"
+#include "parser.h"
/*
** \brief Create a new AST node according to `tag`
diff --git a/src/debug.c b/src/debug.c
index 8255016..8759252 100644
--- a/src/debug.c
+++ b/src/debug.c
@@ -1,7 +1,15 @@
#include <stdio.h>
#include "lexer.h"
-#include "ast.h"
+
+void tok_lst_debug(t_tok_lst *tokens)
+{
+ while (tokens != NULL)
+ {
+ printf("[%#06x] |%s|%s\n", tokens->tag, tokens->content, tokens->tag & TAG_STICK ? " STICK" : "");
+ tokens = tokens->next;
+ }
+}
/* void token_debug(void *v) */
/* { */
diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c
index e6aeb80..7f49431 100644
--- a/src/lexer/lexer.c
+++ b/src/lexer/lexer.c
@@ -6,7 +6,7 @@
/* By: nahaddac <nahaddac@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/07/16 08:18:25 by nahaddac #+# #+# */
-/* Updated: 2020/08/27 17:34:53 by charles ### ########.fr */
+/* Updated: 2020/08/27 20:45:59 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -58,6 +58,10 @@ int check_input(char *input)
}
if (lexer_sep(input[i]))
{
+ /* src/lexer/lexer.c:62:12: warning: Although the value stored to 'i' is used in the enclosing expression, the value is never actually read from 'i' */
+ /* return (i += lexer_space(&input[i + 1]) + 1); */
+ /* ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
+
if (input[i] == ';')
return (i += lexer_space(&input[i + 1]) + 1);
while(input[i] == input[i + 1] && op < 2)
diff --git a/src/lexer/tok_lst.c b/src/lexer/tok_lst.c
index 8d29bb5..a620aa5 100644
--- a/src/lexer/tok_lst.c
+++ b/src/lexer/tok_lst.c
@@ -6,7 +6,7 @@
/* By: charles <me@cacharle.xyz> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/08/27 09:32:58 by charles #+# #+# */
-/* Updated: 2020/08/27 18:40:05 by charles ### ########.fr */
+/* Updated: 2020/08/27 20:54:35 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -31,6 +31,7 @@ t_tok_lst *tok_lst_new_until(enum e_tok tag, char *content, size_t n)
return (NULL);
}
ret->tag = tag;
+ ret->next = NULL;
return (ret);
}
@@ -57,3 +58,15 @@ t_tok_lst *tok_lst_last(t_tok_lst *tokens)
{
return ((t_tok_lst*)ft_lstlast((t_ftlst*)tokens));
}
+
+t_tok_lst *tok_lst_pop_front(t_tok_lst **tokens)
+{
+ t_tok_lst *poped;
+
+ if (tokens == NULL || *tokens == NULL)
+ return (NULL);
+ poped = *tokens;
+ *tokens = poped->next;
+ poped->next = NULL;
+ return (poped);
+}
diff --git a/src/main.c b/src/main.c
index b02c68b..41a4967 100644
--- a/src/main.c
+++ b/src/main.c
@@ -6,7 +6,7 @@
/* By: cacharle <cacharle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/28 11:45:44 by cacharle #+# #+# */
-/* Updated: 2020/08/27 18:41:53 by charles ### ########.fr */
+/* Updated: 2020/08/27 20:44:44 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -16,15 +16,15 @@
*/
#include "minishell.h"
-#include "ast.h"
#include "lexer.h"
#include "parser.h"
#include "eval.h"
-void token_debug(void *v);
-void token_put(void *v);
-void print_level(int level);
-void ast_print(int level, t_ast *ast);
+/* void token_debug(void *v); */
+/* void token_put(void *v); */
+/* void print_level(int level); */
+/* void ast_print(int level, t_ast *ast); */
+void tok_lst_debug(t_tok_lst *tokens);
/*
** TODO
@@ -90,6 +90,7 @@ int main(int argc, char **argv, char **envp)
t_tok_lst *lex_out = lexer(ft_strdup(argv[2]));
if (lex_out == NULL)
return (1);
+ tok_lst_debug(lex_out);
/* ft_lstiter((t_ftlst*)lex_out, token_debug); */
}
else if (argc == 3 && ft_strcmp(argv[1], "-c") == 0)
@@ -145,6 +146,8 @@ int main(int argc, char **argv, char **envp)
(void)eval_out;
print_prompt();
}
+ if (ret != FTGL_EOF)
+ return (1);
}
ft_htdestroy(path, free);
diff --git a/src/parser/parsed.c b/src/parser/parsed.c
new file mode 100644
index 0000000..0c8eba6
--- /dev/null
+++ b/src/parser/parsed.c
@@ -0,0 +1,40 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* parsed.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: charles <me@cacharle.xyz> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/08/27 20:27:42 by charles #+# #+# */
+/* Updated: 2020/08/27 20:31:11 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "parser.h"
+
+t_parsed *parsed_new(t_ast *ast, t_tok_lst *rest)
+{
+ t_parsed *ret;
+
+ if ((ret = malloc(sizeof(t_parsed))) == NULL)
+ return (NULL);
+ ret->syntax_error = false;
+ ret->rest = rest;
+ ret->ast = ast;
+ return ret;
+}
+
+t_parsed *parsed_error(const char *format, ...)
+{
+ t_parsed *err;
+ va_list ap;
+
+ if ((err = parsed_new(NULL, NULL)) == NULL)
+ return (NULL);
+ err->syntax_error = true;
+ va_start(ap, format);
+ verrorf(format, ap);
+ va_end(ap);
+ return (err);
+}
+
diff --git a/src/parse/parse.c b/src/parser/parser.c
index df39cd0..ae484ac 100644
--- a/src/parse/parse.c
+++ b/src/parser/parser.c
@@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
-/* parse.c :+: :+: :+: */
+/* parser.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nahaddac <nahaddac@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/17 18:09:04 by nahaddac #+# #+# */
-/* Updated: 2020/08/27 11:02:33 by charles ### ########.fr */
+/* Updated: 2020/08/27 21:12:21 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -16,38 +16,11 @@
*/
#include "parser.h"
-#include "lexer.h"
-
-t_parsed *parsed_new(t_ast *ast, t_tok_lst *rest)
-{
- t_parsed *ret;
-
- if ((ret = malloc(sizeof(t_parsed))) == NULL)
- return (NULL);
- ret->syntax_error = false;
- ret->rest = rest;
- ret->ast = ast;
- return ret;
-}
-
-t_parsed *parsed_error(const char *format, ...)
-{
- t_parsed *err;
- va_list ap;
-
- if ((err = parsed_new(NULL, NULL)) == NULL)
- return (NULL);
- err->syntax_error = true;
- va_start(ap, format);
- verrorf(format, ap);
- va_end(ap);
- return (err);
-}
t_parsed *parse_redir(t_tok_lst *input, t_tok_lst **redirs)
{
- tok_lst_push_back(redirs, input);
- input = input->next;
+ tok_lst_push_back(redirs, tok_lst_pop_front(&input));
+ /* input = input->next; */
if (input == NULL)
return (parsed_error("syntax error near unexpected token `newline'\n"));
while(input != NULL
@@ -55,42 +28,37 @@ t_parsed *parse_redir(t_tok_lst *input, t_tok_lst **redirs)
&& input->next->tag & TAG_IS_STR
&& input->tag & TAG_IS_STR && input->tag & TAG_STICK)
{
- tok_lst_push_back(redirs, input);
- input = input->next;
+ tok_lst_push_back(redirs, tok_lst_pop_front(&input));
+ /* input = input->next; */
}
- if (!(input->tag & TAG_IS_STR))
+ if (input != NULL && !(input->tag & TAG_IS_STR))
return (parsed_error("syntax error near unexpected token `%s'\n", input->content));
- tok_lst_push_back(redirs, input);
+ tok_lst_push_back(redirs, tok_lst_pop_front(&input));
return (parsed_new(NULL, input));
}
t_parsed *parse_cmd(t_tok_lst *input)
{
- enum e_tok tag;
- t_ast *ast;
- t_parsed *tmp;
+ t_ast *ast;
+ t_parsed *tmp;
- tag = input->tag;
- if (tag & TAG_IS_SEP)
+ if (input->tag & TAG_IS_SEP)
return (parsed_error("syntax error near unexpected token `%s'\n", input->content));
- ast = ast_new(AST_CMD);
+ if ((ast = ast_new(AST_CMD)) == NULL)
+ return (NULL);
while (input != NULL)
{
- tag = input->tag;
- if (tag & TAG_IS_STR)
- tok_lst_push_back(&ast->cmd_argv, input);
- else if (tag & TAG_IS_REDIR)
+ if (input->tag & TAG_IS_STR)
+ tok_lst_push_back(&ast->cmd_argv, tok_lst_pop_front(&input));
+ else if (input->tag & TAG_IS_REDIR)
{
tmp = parse_redir(input, &ast->redirs);
if (tmp == NULL || tmp->syntax_error)
- return tmp;
+ return (tmp);
input = tmp->rest;
}
else
break;
- if (input == NULL)
- break;
- input = input->next;
}
return parsed_new(ast, input);
}
@@ -159,7 +127,7 @@ t_parsed *parse_expr(t_tok_lst *input)
while(input != NULL)
{
tag = input->tag;
- tok_lst_push_back(&tmp->ast->redirs, input);
+ tok_lst_push_back(&tmp->ast->redirs, tok_lst_pop_front(&input));
if (tag & TAG_IS_STR && tag & TAG_STICK)
input = input->next;
else if (tag & TAG_IS_REDIR)
@@ -167,9 +135,9 @@ t_parsed *parse_expr(t_tok_lst *input)
else
break;
}
- input = input->next;
if (input == NULL)
break;
+ input = input->next;
tag = input->tag;
}
tmp->rest = input;
diff --git a/src/preprocess.c b/src/preprocess.c
index 2e477d0..f4eb104 100644
--- a/src/preprocess.c
+++ b/src/preprocess.c
@@ -6,7 +6,7 @@
/* By: charles <charles@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/03 08:58:49 by charles #+# #+# */
-/* Updated: 2020/08/27 17:33:55 by charles ### ########.fr */
+/* Updated: 2020/08/27 20:43:22 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -48,7 +48,7 @@ t_tok_lst *st_stick_tokens(t_tok_lst *tokens)
while (curr != NULL)
{
// curr->next shouldn't be null
- if (curr->tag & TAG_STICK)
+ if (curr->tag & TAG_STICK && curr->next != NULL)
{
curr->content = ft_strjoinf_fst(curr->content, curr->next->content);
t_tok_lst *tmp = curr->next->next;