2026-01-10 19:16:36 +01:00
|
|
|
#include "parser.h"
|
|
|
|
|
|
2026-01-13 16:53:25 +01:00
|
|
|
#include <stdbool.h>
|
2026-01-10 19:16:36 +01:00
|
|
|
#include <stddef.h>
|
2026-01-13 16:53:25 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#include "lexer/lexer.h"
|
|
|
|
|
#include "utils/lists/lists.h"
|
|
|
|
|
|
|
|
|
|
// === 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
|
2026-01-10 19:16:36 +01:00
|
|
|
|
|
|
|
|
struct ast *get_ast()
|
|
|
|
|
{
|
2026-01-13 19:41:37 +01:00
|
|
|
struct list *result_list = NULL;
|
2026-01-13 16:53:25 +01:00
|
|
|
struct ast *current_node = NULL;
|
|
|
|
|
|
2026-01-13 19:41:37 +01:00
|
|
|
char *token = peek_token();
|
|
|
|
|
if (token != NULL)
|
|
|
|
|
{
|
|
|
|
|
puts("Internal error: cannot get the following token");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (token[0] != EOF)
|
|
|
|
|
{
|
|
|
|
|
struct ast *cmd = parse_simple_command();
|
|
|
|
|
result_list = list_append(result_list, cmd);
|
|
|
|
|
}
|
2026-01-13 16:53:25 +01:00
|
|
|
|
2026-01-13 19:41:37 +01:00
|
|
|
struct ast *result = ast_create_list(result_list);
|
2026-01-13 16:53:25 +01:00
|
|
|
return result;
|
2026-01-10 19:16:36 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-13 16:53:25 +01:00
|
|
|
// TODO
|
2026-01-10 19:16:36 +01:00
|
|
|
struct ast *get_ast_str(char *command)
|
|
|
|
|
{
|
2026-01-10 19:57:36 +01:00
|
|
|
(void)command;
|
2026-01-10 19:16:36 +01:00
|
|
|
return NULL;
|
|
|
|
|
}
|