Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] Parser - Generate command table list to replace AST #50

Merged
merged 12 commits into from
Dec 30, 2023
6 changes: 5 additions & 1 deletion include/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@
/* By: lyeh <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/08 18:02:03 by lyeh #+# #+# */
/* Updated: 2023/12/23 20:10:01 by lyeh ### ########.fr */
/* Updated: 2023/12/29 21:11:11 by lyeh ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef DEBUG_H
# define DEBUG_H

# include "defines.h"

void ft_show_env_list(t_shell *shell);
void ft_show_token_list(t_shell *shell);
void print_ast_bfs(t_ast *root);
void print_cmd_table_list(t_list_d *cmd_table_list);
void print_cmd_table(t_cmd_table *cmd_table);

#endif
40 changes: 31 additions & 9 deletions include/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: lyeh <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/08 15:56:26 by lyeh #+# #+# */
/* Updated: 2023/12/23 18:41:18 by lyeh ### ########.fr */
/* Updated: 2023/12/29 20:17:33 by lyeh ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -107,6 +107,17 @@ typedef enum e_token_type
T_R_BRACKET
} t_token_type;

typedef enum e_cmdtable_type
{
C_NONE = -1,
C_SIMPLE_CMD = 0,
C_PIPE,
C_AND,
C_OR,
C_SUBSHELL_START,
C_SUBSHELL_END
} t_cmdtable_type;

typedef struct s_env
{
char *key;
Expand Down Expand Up @@ -144,26 +155,37 @@ typedef struct s_pt_node
int num_reduced;
} t_pt_node;

typedef struct s_io_redirect
typedef struct s_io_red
{
char *redir_in;
char *redir_out;
} t_io_redirect;
int type;
char *in_file;
char *out_file;
char *here_end;
int red_in;
int red_out;
} t_io_red;

// TODO: Need to record each io_redirect in a list, need to touch the filename
// case: echo 1 | > out1 > out2 cat
// - if cmd_table with no cmd_name, just touch the filename in the io_red_list
// - in other cases, need execute the cmd_table with io redirect

typedef struct s_cmd_table
{
int type;
char *cmd_name;
char *cmd_args;
t_io_redirect *io_redirect;
t_list *cmd_args;
LeaYeh marked this conversation as resolved.
Show resolved Hide resolved
t_list *assignment_list;
t_list *io_red_list;
} t_cmd_table;

typedef struct s_shell
{
int exit_code;
t_list *env_list;
t_list *token_list;
t_list *final_cmd_table;
t_ast *ast;
t_list_d *cmd_table_list;
// t_ast *ast;
char *input_line;
} t_shell;

Expand Down
7 changes: 6 additions & 1 deletion include/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: lyeh <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/08 19:57:56 by lyeh #+# #+# */
/* Updated: 2023/12/22 21:25:26 by lyeh ### ########.fr */
/* Updated: 2023/12/29 21:12:48 by lyeh ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -28,4 +28,9 @@ bool init_parse(t_list **state_stack, t_list **parse_stack);
void free_parse(t_list **state_stack, t_list **parse_stack);
bool ft_parse(t_shell *shell);

/* Command table */
t_list_d *build_cmd_table_list(t_list *token_list);
bool handle_symbol_token(t_list **token_list, t_list_d **cmd_table_list);
bool handle_word_token(t_list **token_list, t_list_d **cmd_table_list);

#endif
70 changes: 46 additions & 24 deletions include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,60 @@
/* ::: :::::::: */
/* utils.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ldulling <ldulling@student.42.fr> +#+ +:+ +#+ */
/* By: lyeh <lyeh@student.42vienna.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/17 13:38:17 by lyeh #+# #+# */
/* Updated: 2023/12/26 19:01:02 by ldulling ### ########.fr */
/* Updated: 2023/12/29 20:19:15 by lyeh ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef UTILS_H
# define UTILS_H
# include "defines.h"

bool drop_num_stack(t_list **stack, int num, void (*del)(void *));
t_list *pop_num_stack(t_list **stack, int num);

t_token *init_token_node(int type, char *data);
void free_token_node(t_token *token);

t_ast *init_ast_node(int type, char *data, t_list *children);
void free_ast_node(t_ast *ast);
void free_ast_data(t_ast *ast);

char *ft_get_token_type_str(int type);

int get_state_from_stack(t_list *node);
t_token *get_token_from_stack(t_list *node);
t_ast *get_ast_from_stack(t_list *node);

int get_token_type_from_list(t_list *token_list);
char *get_token_data_from_list(t_list *token_list);
void print_state_stack(t_list *stack);
void print_parse_stack(t_list *stack);

void print_token_list(t_list *token_list);
/* Token list utils */
t_token *init_token_node(int type, char *data);
void free_token_node(t_token *token);
t_token *get_token_from_list(t_list *token_list);
int get_token_type_from_list(t_list *token_list);
char *get_token_data_from_list(t_list *token_list);
void print_token_list(t_list *token_list);
t_token *dup_token_node(t_token *token);
t_list *dup_token_list(t_list *token_list);

/* AST utils */
t_ast *init_ast_node(int type, char *data, t_list *children);
void free_ast_node(t_ast *ast);
void free_ast_data(t_ast *ast);

/* Redirect utils */
t_io_red *init_io_red(void);
void free_io_red(t_io_red *io_red);

/* Cmd table utils */
t_cmd_table *init_cmd_table(void);
void free_cmd_table(t_cmd_table *cmd_table);
t_cmd_table *get_last_simple_cmd_table(t_list_d *cmd_table_list);
bool append_empty_cmd_table(t_list_d **cmd_table_list);
bool append_cmd_table_by_scenario(
int token_type, t_list_d **cmd_table_list);

/* Type utils */
char *ft_get_token_type_str(int type);
bool is_control_op(int token_type);
bool is_io_red_op(int token_type);
bool is_word(int token_type);
bool is_subshell_symbol(int token_type);
// bool is_identifier(int token_type);
// bool is_rule(int token_type);

/* Stack utils */
int get_state_from_stack(t_list *node);
t_token *get_token_from_stack(t_list *node);
t_ast *get_ast_from_stack(t_list *node);
void print_state_stack(t_list *stack);
void print_parse_stack(t_list *stack);
bool drop_num_stack(t_list **stack, int num, void (*del)(void *));
t_list *pop_num_stack(t_list **stack, int num);

#endif
94 changes: 94 additions & 0 deletions source/debug/print_cmd_table.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_cmd_table.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lyeh <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/26 19:46:53 by lyeh #+# #+# */
/* Updated: 2023/12/29 21:12:08 by lyeh ### ########.fr */
/* */
/* ************************************************************************** */

#include "defines.h"
#include "utils.h"

void print_io_red_list(t_list *io_red_list)
{
t_list *node;
t_io_red *io_red;

printf("io_red_list:\n");
printf("-----------------\n");
node = io_red_list;
while (node)
{
io_red = (t_io_red *)node->content;
printf("\tin_file: %s,\n", io_red->in_file);
printf("\tout_file: %s,\n", io_red->out_file);
printf("\there_end: %s,\n", io_red->here_end);
printf("\tred_in: %d,\n", io_red->red_in);
printf("\tred_out: %d,\n", io_red->red_out);
printf("-----------------\n");
node = node->next;
}
printf("(NULL)\n");
}

void print_cmd_args_list(t_list *cmd_args)
{
t_list *node;

printf("cmd_args: ");
node = cmd_args;
while (node && node->content)
{
printf("%s -> ", (char *)node->content);
node = node->next;
}
printf("(NULL)\n");
}

void print_assignment_list(t_list *assignment_list)
{
t_list *node;

printf("assignment_list: ");
node = assignment_list;
while (node)
{
printf("%s -> ", (char *)node->content);
node = node->next;
}
printf("(NULL)\n");
}

void print_cmd_table(t_cmd_table *cmd_table)
{
printf("type: %d\n", cmd_table->type);
printf("cmd_name: %s\n", cmd_table->cmd_name);
print_cmd_args_list(cmd_table->cmd_args);
print_assignment_list(cmd_table->assignment_list);
print_io_red_list(cmd_table->io_red_list);
}

void print_cmd_table_list(t_list_d *cmd_table_list)
{
t_list_d *node;
t_cmd_table *cmd_table;
int i;

printf("\n\n========= cmd_table_list =========\n");
node = cmd_table_list;
i = 0;
while (node && node->content)
{
printf("========= %d =========\n", i);
cmd_table = (t_cmd_table *)node->content;
print_cmd_table(cmd_table);
node = node->next;
printf("=====================\n");
i++;
}
printf("===================================\n\n");
}
53 changes: 53 additions & 0 deletions source/frontend/parser/cmd_table_list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cmd_table_list.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lyeh <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/28 20:25:20 by lyeh #+# #+# */
/* Updated: 2023/12/29 20:20:34 by lyeh ### ########.fr */
/* */
/* ************************************************************************** */

#include "parser.h"
#include "utils.h"

bool handle_current_token(t_list **token_list, t_list_d **cmd_table_list)
{
int token_type;

token_type = get_token_type_from_list(*token_list);
if (!append_cmd_table_by_scenario(token_type, cmd_table_list))
return (false);
if (is_word(token_type))
{
if (handle_word_token(token_list, cmd_table_list))
return (true);
}
else if (is_control_op(token_type) || is_io_red_op(token_type) || \
is_subshell_symbol(token_type))
{
if (handle_symbol_token(token_list, cmd_table_list))
return (true);
}
else if (token_type == T_END)
return (true);
return (false);
}

t_list_d *build_cmd_table_list(t_list *token_list)
{
t_list_d *cmd_table_list;

cmd_table_list = NULL;
print_token_list(token_list);
while (token_list)
{
if (!handle_current_token(&token_list, &cmd_table_list))
return (
ft_lstclear_d(&cmd_table_list, (void *)free_cmd_table), NULL);
token_list = token_list->next;
}
return (cmd_table_list);
}
Loading