fix(parser + lexer): interaction -- WIP
This commit is contained in:
parent
04529f858c
commit
10ce140e37
5 changed files with 99 additions and 48 deletions
|
|
@ -17,33 +17,39 @@
|
|||
|
||||
struct ast *get_ast()
|
||||
{
|
||||
struct list *result_list = NULL;
|
||||
struct ast *current_node = NULL;
|
||||
// struct list *result_list = NULL;
|
||||
// struct ast *current_node = NULL;
|
||||
|
||||
struct token *token = peek_token();
|
||||
struct token *token = PEEK_TOKEN();
|
||||
|
||||
while (token != NULL && token->type != TOKEN_EOF)
|
||||
if (token->type == TOKEN_EOF)
|
||||
{
|
||||
switch (token->type)
|
||||
{
|
||||
case TOKEN_WORD:
|
||||
token = pop_token();
|
||||
// TODO
|
||||
// return ast END.
|
||||
}
|
||||
else if (token->type == TOKEN_NEWLINE)
|
||||
{
|
||||
token = pop_token();
|
||||
// TODO
|
||||
// return ast EMPTY.
|
||||
}
|
||||
else // TOKEN WORD
|
||||
{
|
||||
// TODO
|
||||
// call parse_list
|
||||
current_node = parse_simple_command();
|
||||
result_list = list_append(result_list, current_node);
|
||||
break;
|
||||
default:
|
||||
// Forward
|
||||
token = pop_token();
|
||||
break;
|
||||
}
|
||||
token = peek_token();
|
||||
}
|
||||
|
||||
/*
|
||||
if (token == NULL)
|
||||
{
|
||||
puts("Internal error: cannot get the following token");
|
||||
puts("Hint: EOF might be missing");
|
||||
return NULL;
|
||||
}
|
||||
*/
|
||||
|
||||
struct ast *result = ast_create_list(result_list);
|
||||
return result;
|
||||
|
|
|
|||
|
|
@ -3,6 +3,24 @@
|
|||
|
||||
#include "utils/ast/ast.h"
|
||||
|
||||
// === Macros
|
||||
|
||||
#define PEEK_TOKEN() \
|
||||
peek_token(); \
|
||||
if (token == NULL) \
|
||||
{ \
|
||||
puts("Internal error: cannot get the following token"); \
|
||||
return NULL; \
|
||||
}
|
||||
|
||||
#define POP_TOKEN() \
|
||||
pop_token(); \
|
||||
if (token == NULL) \
|
||||
{ \
|
||||
puts("Internal error: cannot get the following token"); \
|
||||
return NULL; \
|
||||
}
|
||||
|
||||
/* @brief Builds the AST representation of the next command to execute.
|
||||
*
|
||||
* @return Returns the AST representation of the next command to execute.
|
||||
|
|
|
|||
|
|
@ -8,24 +8,6 @@
|
|||
#include "lexer/lexer.h"
|
||||
#include "utils/ast/ast.h"
|
||||
|
||||
// === Macros
|
||||
|
||||
#define PEEK_TOKEN() \
|
||||
peek_token(); \
|
||||
if (token == NULL) \
|
||||
{ \
|
||||
puts("Internal error: cannot get the following token"); \
|
||||
return NULL; \
|
||||
}
|
||||
|
||||
#define POP_TOKEN() \
|
||||
pop_token(); \
|
||||
if (token == NULL) \
|
||||
{ \
|
||||
puts("Internal error: cannot get the following token"); \
|
||||
return NULL; \
|
||||
}
|
||||
|
||||
// === Static functions
|
||||
|
||||
/* Returns true if c is a command terminator, false otherwise
|
||||
|
|
@ -58,8 +40,9 @@ struct ast *parse_simple_command(void)
|
|||
|
||||
while (!isterminator(token))
|
||||
{
|
||||
command_elements = list_append(command_elements, token->data);
|
||||
token = POP_TOKEN();
|
||||
command_elements = list_append(command_elements, token->data);
|
||||
token = PEEK_TOKEN();
|
||||
}
|
||||
|
||||
struct ast *result = ast_create_command(command_elements);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue