-
Notifications
You must be signed in to change notification settings - Fork 0
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
【軽PR】x_malloc
の使用 & ファイル構成の変更
#112
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
#include "internal.h" | ||
|
||
//<pipeline> '&&' <newline> <command_line> | ||
//<pipeline> '||' <newline> <command_line> | ||
//<pipeline> | ||
t_ast_node *command_line(t_parser *p); | ||
|
||
//<command> '|' <newline> <pipeline> | ||
//<command> | ||
static t_ast_node *pipeline(t_parser *p); | ||
|
||
//<subshell> | ||
//<simple_command> | ||
static t_ast_node *command(t_parser *p); | ||
|
||
//'(' <compound_list> ')' <redirection_list> | ||
//'(' <compound_list> ')' | ||
static t_ast_node *subshell(t_parser *p); | ||
|
||
//<pipeline> '&&' <compound_list> | ||
//<pipeline> '||' <compound_list> | ||
//<pipeline> '\n' <compound_list> | ||
//<pipeline> | ||
static t_ast_node *compound_list(t_parser *p); | ||
|
||
t_ast_node *command_line(t_parser *p) | ||
{ | ||
t_ast_node *result; | ||
t_ast_node *pipeline_; | ||
t_ast_node *commandline_; | ||
|
||
if (!assign_ast_node(&pipeline_, pipeline(p))) | ||
return (NULL); | ||
new_ast_node(&result); | ||
if (consume_token(p, AND_IF, NULL)) | ||
result->type = AND_IF_NODE; | ||
else if (consume_token(p, OR_IF, NULL)) | ||
result->type = OR_IF_NODE; | ||
else if (p->token->type == EOL) | ||
{ | ||
free(result); | ||
return (pipeline_); | ||
} | ||
else | ||
{ | ||
p->err = ERR_UNEXPECTED_TOKEN; | ||
return (delete_ast_nodes(pipeline_, result)); | ||
} | ||
if (!assign_ast_node(&commandline_, command_line(p))) | ||
{ | ||
p->err = ERR_UNEXPECTED_EOF; | ||
return (delete_ast_nodes(pipeline_, result)); | ||
} | ||
set_ast_nodes(result, pipeline_, commandline_); | ||
return (result); | ||
} | ||
|
||
t_ast_node *pipeline(t_parser *p) | ||
{ | ||
t_ast_node *result; | ||
t_ast_node *command_; | ||
t_ast_node *pipeline_; | ||
|
||
if (!assign_ast_node(&command_, command(p))) | ||
return (NULL); | ||
if (!consume_token(p, PIPE, NULL)) | ||
return (command_); | ||
if (!assign_ast_node(&pipeline_, pipeline(p))) | ||
{ | ||
if (!p->err) | ||
p->err = ERR_UNEXPECTED_EOF; | ||
return (delete_ast_nodes(command_, NULL)); | ||
} | ||
new_ast_node(&result); | ||
result->type = PIPE_NODE; | ||
set_ast_nodes(result, command_, pipeline_); | ||
return (result); | ||
} | ||
|
||
t_ast_node *command(t_parser *p) | ||
{ | ||
const t_token *tmp = p->token; | ||
t_ast_node *node; | ||
|
||
if (assign_ast_node(&node, subshell(p))) | ||
return (node); | ||
if (p->err) | ||
return (NULL); | ||
p->token = (t_token *)tmp; | ||
if (assign_ast_node(&node, simple_command(p))) | ||
return (node); | ||
return (NULL); | ||
} | ||
|
||
t_ast_node *subshell(t_parser *p) | ||
{ | ||
t_ast_node *result; | ||
t_ast_node *compound_list_; | ||
|
||
if (!consume_token(p, LPAREN, NULL)) | ||
return (NULL); | ||
p->is_subshell = true; | ||
consume_token(p, SUBSHELL_NEWLINE_MS, NULL); | ||
if (!assign_ast_node(&compound_list_, compound_list(p))) | ||
{ | ||
if (!p->err) | ||
p->err = ERR_UNEXPECTED_TOKEN; | ||
return (NULL); | ||
} | ||
if (!consume_token(p, RPAREN, NULL)) | ||
{ | ||
p->err = ERR_UNEXPECTED_EOF; | ||
return (delete_ast_nodes(compound_list_, NULL)); | ||
} | ||
p->is_subshell = false; | ||
new_ast_node(&result); | ||
result->type = SUBSHELL_NODE; | ||
set_ast_nodes(result, compound_list_, NULL); | ||
return (result); | ||
} | ||
|
||
t_ast_node *compound_list(t_parser *p) | ||
{ | ||
t_ast_node *result; | ||
t_ast_node *pipeline_; | ||
t_ast_node *compound_list_; | ||
|
||
if (!assign_ast_node(&pipeline_, pipeline(p))) | ||
return (NULL); | ||
new_ast_node(&result); | ||
if (consume_token(p, AND_IF, NULL)) | ||
result->type = AND_IF_NODE; | ||
else if (consume_token(p, OR_IF, NULL)) | ||
result->type = OR_IF_NODE; | ||
else if (consume_token(p, SUBSHELL_NEWLINE_MS, NULL)) | ||
result->type = SUBSHELL_NEWLINE_MS_NODE; | ||
else | ||
{ | ||
free(result); | ||
return (pipeline_); | ||
} | ||
if (!assign_ast_node(&compound_list_, compound_list(p))) | ||
{ | ||
p->err = ERR_UNEXPECTED_EOF; | ||
return (delete_ast_nodes(pipeline_, result)); | ||
} | ||
set_ast_nodes(result, pipeline_, compound_list_); | ||
return (result); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#ifndef INTERNAL_H | ||
# define INTERNAL_H | ||
|
||
# include <stdbool.h> | ||
# include <unistd.h> | ||
|
||
# include "../../token/token.h" | ||
# include "../../lexer/lexer.h" | ||
# include "../../ast/ast.h" | ||
# include "../../libft/libft.h" | ||
# include "../../utils/utils.h" | ||
|
||
# define ERR_UNEXPECTED_TOKEN 1 | ||
# define ERR_UNEXPECTED_EOF 2 | ||
|
||
//# define TEST | ||
|
||
typedef struct t_parser { | ||
t_token *token; | ||
int err; | ||
bool is_subshell; | ||
} t_parser; | ||
|
||
// parser_utils.c | ||
t_parser *new_parser(t_token *token); | ||
bool consume_token(t_parser *p, t_token_type expected, t_ast_node *node); | ||
|
||
# ifdef TEST | ||
char *handle_err(t_parser *p); | ||
# else | ||
bool handle_err(t_parser *p); | ||
# endif | ||
|
||
t_ast_node *command_line(t_parser *p); | ||
t_ast_node *simple_command(t_parser *p); | ||
|
||
#endif //INTERNAL_H |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ディレクトリとヘッダ名は
[verb-er]/[verb]
の形で統一します?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parse.hで宣言する関数がparse()だけだったので、parse.hにしました!
それで大丈夫でそうですか?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
個人的には、ディレクトリ名とヘッダ名を同じにした方が見やすい感はありますね
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
関数名は動詞の方がいいですよね?
lex()``parse()
みたいな