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
|
|
|
|
|
*/
|
|
|
|
|
static struct ast *parse_simple_command(void)
|
|
|
|
|
{
|
|
|
|
|
struct list *cmd_elements = NULL;
|
|
|
|
|
char *token = get_token();
|
|
|
|
|
if (token == NULL) // just in case ?
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
while (token != NULL && !isterminator(token[0]))
|
|
|
|
|
{
|
|
|
|
|
cmd_elements = list_append(cmd_elements, token);
|
|
|
|
|
token = get_token();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (token == NULL)
|
|
|
|
|
return NULL; // TODO handle error
|
|
|
|
|
|
|
|
|
|
struct ast *result = ast_create_cmd(cmd_elements);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// === Functions
|
2026-01-10 19:16:36 +01:00
|
|
|
|
|
|
|
|
struct ast *get_ast()
|
|
|
|
|
{
|
2026-01-13 16:53:25 +01:00
|
|
|
struct ast *result = NULL;
|
|
|
|
|
struct ast *current_node = NULL;
|
|
|
|
|
|
|
|
|
|
// char *token = get_token();
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|