feat: suport for the new lexer and if/else structures
This commit is contained in:
parent
e9d702efb4
commit
80e4b6c2fd
3 changed files with 194 additions and 46 deletions
|
|
@ -10,25 +10,7 @@
|
||||||
#include "utils/lists/lists.h"
|
#include "utils/lists/lists.h"
|
||||||
|
|
||||||
// === Static functions
|
// === Static functions
|
||||||
|
// ...
|
||||||
/* Returns true if c is a command terminator, false otherwise
|
|
||||||
*/
|
|
||||||
static bool isterminator(char c)
|
|
||||||
{
|
|
||||||
switch (c)
|
|
||||||
{
|
|
||||||
case '\n':
|
|
||||||
case ';':
|
|
||||||
case EOF:
|
|
||||||
return true;
|
|
||||||
default:
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Parses a simple list of words (command and arguments)
|
|
||||||
* and returns the resulting ast
|
|
||||||
*/
|
|
||||||
|
|
||||||
// === Functions
|
// === Functions
|
||||||
|
|
||||||
|
|
@ -37,19 +19,29 @@ struct ast *get_ast()
|
||||||
struct list *result_list = NULL;
|
struct list *result_list = NULL;
|
||||||
struct ast *current_node = NULL;
|
struct ast *current_node = NULL;
|
||||||
|
|
||||||
char *token = peek_token();
|
struct token *token = peek_token();
|
||||||
if (token != NULL)
|
|
||||||
|
while (token != NULL && token->type != TOKEN_EOF)
|
||||||
|
{
|
||||||
|
switch (token->type)
|
||||||
|
{
|
||||||
|
case TOKEN_WORD:
|
||||||
|
struct ast *cmd = parse_simple_command();
|
||||||
|
result_list = list_append(result_list, cmd);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
// Forward
|
||||||
|
token = pop_token();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (token == NULL)
|
||||||
{
|
{
|
||||||
puts("Internal error: cannot get the following token");
|
puts("Internal error: cannot get the following token");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (token[0] != EOF)
|
|
||||||
{
|
|
||||||
struct ast *cmd = parse_simple_command();
|
|
||||||
result_list = list_append(result_list, cmd);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct ast *result = ast_create_list(result_list);
|
struct ast *result = ast_create_list(result_list);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,23 +1,65 @@
|
||||||
|
// === Includes
|
||||||
|
#include "parsing_utils.h"
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#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
|
||||||
|
*/
|
||||||
|
static bool isterminator(struct token *token)
|
||||||
|
{
|
||||||
|
if (token == NULL)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
switch (token->type)
|
||||||
|
{
|
||||||
|
case TOKEN_NEWLINE:
|
||||||
|
case TOKEN_SEMICOLON:
|
||||||
|
case TOKEN_EOF:
|
||||||
|
return true;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// === Functions
|
||||||
|
|
||||||
|
/* Parses a simple list of words (command and arguments)
|
||||||
|
* and returns the resulting ast
|
||||||
|
*/
|
||||||
struct ast *parse_simple_command(void)
|
struct ast *parse_simple_command(void)
|
||||||
{
|
{
|
||||||
struct list *cmd_elements = NULL;
|
struct list *cmd_elements = NULL;
|
||||||
char *token = pop_token();
|
struct token *token = POP_TOKEN();
|
||||||
if (token == NULL) // just in case ?
|
|
||||||
{
|
|
||||||
puts("Internal error: cannot get the following token");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (token != NULL && !isterminator(token[0]))
|
while (!isterminator(token))
|
||||||
{
|
{
|
||||||
cmd_elements = list_append(cmd_elements, token);
|
cmd_elements = list_append(cmd_elements, token->data);
|
||||||
token = pop_token();
|
token = POP_TOKEN();
|
||||||
}
|
|
||||||
|
|
||||||
if (token == NULL)
|
|
||||||
{
|
|
||||||
puts("Internal error: cannot get the following token");
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ast *result = ast_create_cmd(cmd_elements);
|
struct ast *result = ast_create_cmd(cmd_elements);
|
||||||
|
|
@ -26,10 +68,123 @@ struct ast *parse_simple_command(void)
|
||||||
|
|
||||||
struct ast *parse_if_rule(void)
|
struct ast *parse_if_rule(void)
|
||||||
{
|
{
|
||||||
|
// If condition
|
||||||
|
struct token *token = POP_TOKEN();
|
||||||
|
|
||||||
|
if (token->type != TOKEN_IF)
|
||||||
|
{
|
||||||
|
puts("Internal error: expected a if rule but token has different "
|
||||||
|
"type");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct ast *condition_content = parse_compound_list();
|
||||||
|
|
||||||
|
// Then content
|
||||||
|
token = POP_TOKEN();
|
||||||
|
|
||||||
|
if (token->type != TOKEN_THEN)
|
||||||
|
{
|
||||||
|
puts("Expected the 'then' keyword but token has different type");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ast *then_content = parse_compound_list();
|
||||||
|
|
||||||
|
// Eventual else/elif clause(s)
|
||||||
|
struct ast *else_content = parse_else_clause();
|
||||||
|
|
||||||
|
token = POP_TOKEN();
|
||||||
|
if (token->type != TOKEN_FI)
|
||||||
|
{
|
||||||
|
puts("Expected the 'fi' keyword but token has different type");
|
||||||
|
// TODO free previous asts
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ast *result =
|
||||||
|
ast_create_if(condition_content, then_content, else_content);
|
||||||
|
if (result == NULL)
|
||||||
|
{
|
||||||
|
puts("Internal error: could not create a new AST (AST_IF)");
|
||||||
|
// TODO free previous asts
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ast *parse_compound_list(void)
|
||||||
|
{
|
||||||
|
struct list *result_list = NULL; // ast* list
|
||||||
|
struct list *cmd_elements = NULL; // token* list
|
||||||
|
struct token *token = PEEK_TOKEN();
|
||||||
|
|
||||||
|
while (token->type != TOKEN_THEN || token->type != TOKEN_ELIF
|
||||||
|
|| TOKEN_TYPE != TOKEN_ELSE)
|
||||||
|
{
|
||||||
|
// Parse simple command
|
||||||
|
if (token->type == TOKEN_SEMICOLON || token->type == TOKEN_NEWLINE)
|
||||||
|
{
|
||||||
|
// Stage (-> next command)
|
||||||
|
struct ast *command = ast_create_cmd(cmd_elements);
|
||||||
|
result_list = list_append(result_list, command);
|
||||||
|
cmd_elements = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (token->type == TOKEN_EOF)
|
||||||
|
{
|
||||||
|
puts("Syntax error: Unexpected end of stream"); // TODO pas très
|
||||||
|
// bien dit
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd_elements = list_append(cmd_elements, token->data);
|
||||||
|
token = POP_TOKEN();
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ast *result = ast_create_list(result_list);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ast *parse_else_clause(void)
|
||||||
|
{
|
||||||
|
// Eventual elif content
|
||||||
|
token = PEEK_TOKEN();
|
||||||
|
|
||||||
|
struct ast *result = NULL;
|
||||||
|
|
||||||
|
// TODO handle ELIF
|
||||||
|
// while (token->type == TOKEN_ELIF)
|
||||||
|
// {
|
||||||
|
// puts("ABORTING ELIF: Not implemented ma gueule");
|
||||||
|
// token = POP_TOKEN(); // Forward
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Eventual else content
|
||||||
|
if (token->type == TOKEN_ELSE)
|
||||||
|
{
|
||||||
|
result = parse_compound_list();
|
||||||
|
token = POP_TOKEN(); // Forward
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result == NULL)
|
||||||
|
result == ast_create_void();
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
struct ast *parse_shell_command(void)
|
struct ast *parse_shell_command(void)
|
||||||
{
|
{
|
||||||
|
struct token *token = PEEK_TOKEN();
|
||||||
|
|
||||||
|
switch (token->type)
|
||||||
|
{
|
||||||
|
case TOKEN_IF:
|
||||||
return parse_if_rule();
|
return parse_if_rule();
|
||||||
|
|
||||||
|
default:
|
||||||
|
puts("I think it's not implemented yet");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,8 @@ struct ast *parse_if_rule(void);
|
||||||
*/
|
*/
|
||||||
struct ast *parse_shell_command(void);
|
struct ast *parse_shell_command(void);
|
||||||
|
|
||||||
/*
|
/* @brief parses commands inside if/else clauses and returns the corresponding
|
||||||
|
* AST list
|
||||||
*/
|
*/
|
||||||
struct ast* parse_compound_list(void);
|
struct ast* parse_compound_list(void);
|
||||||
|
|
||||||
|
|
@ -21,4 +22,4 @@ struct ast* parse_and_or(void);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
*/
|
*/
|
||||||
struct ast* else_clause(void);
|
struct ast* parse_else_clause(void);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue