42sh/src/parser/grammar_basic.c

397 lines
9.2 KiB
C
Raw Normal View History

#include "grammar_basic.h"
#include <stdio.h>
#include <string.h>
#include "../utils/lists/lists.h"
#include "grammar.h"
2026-01-24 15:34:10 +01:00
#include "grammar_advanced.h"
// === Static functions
2026-01-23 17:33:15 +01:00
static enum ast_and_or_type and_or_tok_to_ast(enum token_type tok_type)
2026-01-22 19:57:49 +01:00
{
switch (tok_type)
{
case TOKEN_AND:
return AST_AND_OR_TYPE_AND;
case TOKEN_OR:
return AST_AND_OR_TYPE_OR;
default:
fprintf(stderr, "and_or impossible to init, wrong token type");
return AST_AND_OR_NULL;
}
}
// === Functions
struct ast *parse_list(struct lexer_context *ctx)
2026-01-16 20:12:29 +01:00
{
struct list *result_list = NULL;
struct ast *current_node = NULL;
struct token *token = PEEK_TOKEN();
// and_or
2026-01-20 20:32:59 +01:00
current_node = parse_and_or(ctx);
if (current_node == NULL)
return NULL;
result_list = list_append(result_list, current_node);
// Following and_or commands
token = PEEK_TOKEN();
while (token->type == TOKEN_SEMICOLON)
2026-01-16 20:12:29 +01:00
{
token = POP_TOKEN();
token = PEEK_TOKEN();
if (is_first(*token, RULE_AND_OR))
{
current_node = parse_and_or(ctx);
if (current_node == NULL)
2026-01-24 16:48:21 +01:00
{
struct ast *tmp = ast_create_list(result_list);
ast_free(&tmp);
return NULL;
2026-01-24 16:48:21 +01:00
}
result_list = list_append(result_list, current_node);
token = PEEK_TOKEN();
}
2026-01-16 20:12:29 +01:00
}
return ast_create_list(result_list);
}
struct ast *parse_and_or(struct lexer_context *ctx)
{
2026-01-22 19:57:49 +01:00
struct ast *result = parse_pipeline(ctx);
2026-01-24 16:48:21 +01:00
if (result == NULL)
return NULL;
2026-01-22 19:57:49 +01:00
struct token *token = PEEK_TOKEN();
2026-01-24 16:48:21 +01:00
while (token->type == TOKEN_AND || token->type == TOKEN_OR)
2026-01-22 19:57:49 +01:00
{
2026-01-23 17:33:15 +01:00
// Set left part
2026-01-22 19:57:49 +01:00
struct ast *left = result;
// eat and_or token
token = POP_TOKEN();
2026-01-23 17:33:15 +01:00
// Set type
2026-01-22 19:57:49 +01:00
enum ast_and_or_type type = and_or_tok_to_ast(token->type);
token = PEEK_TOKEN();
2026-01-23 17:33:15 +01:00
// Skip newlines
2026-01-22 19:57:49 +01:00
while (token->type == TOKEN_NEWLINE)
{
token = POP_TOKEN();
token = PEEK_TOKEN();
}
2026-01-23 17:33:15 +01:00
// Right part
2026-01-22 19:57:49 +01:00
struct ast *right = parse_pipeline(ctx);
result = ast_create_and_or(left, right, type);
2026-01-23 17:33:15 +01:00
if (result == NULL)
{
ast_free(&left);
ast_free(&right);
return NULL;
}
2026-01-22 19:57:49 +01:00
}
return result;
}
struct ast *parse_pipeline(struct lexer_context *ctx)
{
2026-01-20 20:06:25 +01:00
return parse_command(ctx);
}
struct ast *parse_command(struct lexer_context *ctx)
{
struct token *token = PEEK_TOKEN();
if (token->type == TOKEN_WORD)
{
2026-01-20 20:06:25 +01:00
return parse_simple_command(ctx);
}
else if (token->type == TOKEN_IF)
{
2026-01-20 20:06:25 +01:00
return parse_shell_command(ctx);
}
else
{
2026-01-24 16:48:21 +01:00
puts("Syntax error: expected command");
return NULL;
}
}
struct ast *parse_simple_command(struct lexer_context *ctx)
{
2026-01-15 17:46:24 +01:00
struct list *command_elements = NULL;
2026-01-24 15:34:10 +01:00
// WORD
struct token *token = POP_TOKEN();
if (token->type != TOKEN_WORD)
{
puts("Expected a command but got a different token type");
return NULL;
}
2026-01-24 15:34:10 +01:00
char *command = strdup(token->data);
command_elements = list_append(command_elements, command);
2026-01-24 15:34:10 +01:00
token = PEEK_TOKEN();
// Eventual elements
while (is_first(*token, RULE_ELEMENT))
{
2026-01-24 15:34:10 +01:00
// Get element
struct ast *element = parse_element(ctx);
if (element == NULL)
{
2026-01-24 15:34:10 +01:00
list_deep_destroy(command_elements);
return NULL;
}
2026-01-24 15:34:10 +01:00
// Get element type
if (ast_is_word(element))
{
2026-01-24 15:34:10 +01:00
struct ast_word *element_word = ast_get_word(element);
command_elements =
list_append(command_elements, element_word->word);
}
else if (ast_is_redir(element))
{
// TODO
puts("NOT IMPLEMENTED");
return NULL;
}
2026-01-24 15:34:10 +01:00
else
{
2026-01-24 15:34:10 +01:00
puts("Internal error: unexpected return value from parse_element "
"in parse_simple_command");
list_deep_destroy(command_elements);
return NULL;
}
2026-01-24 15:34:10 +01:00
// Forward
token = PEEK_TOKEN();
}
2026-01-24 15:34:10 +01:00
// Result
2026-01-15 17:46:24 +01:00
struct ast *result = ast_create_command(command_elements);
if (result == NULL)
{
2026-01-24 15:34:10 +01:00
list_deep_destroy(command_elements);
return NULL;
}
return result;
}
2026-01-24 15:34:10 +01:00
struct ast *parse_element(struct lexer_context *ctx)
{
struct token *token = PEEK_TOKEN();
if (token->type == TOKEN_WORD)
{
token = POP_TOKEN();
return ast_create_word(token->data);
2026-01-24 15:34:10 +01:00
}
else if (token->type == TOKEN_IONUMBER || token->type == TOKEN_REDIRECTION)
{
return parse_redirection(ctx);
}
else
{
puts("Syntax error: unexpected token at parse_element");
return NULL;
}
}
struct ast *parse_shell_command(struct lexer_context *ctx)
2026-01-16 20:12:29 +01:00
{
2026-01-20 20:06:25 +01:00
return parse_if_rule(ctx);
2026-01-16 20:12:29 +01:00
}
struct ast *parse_if_rule(struct lexer_context *ctx)
{
// If keyword
struct token *token = POP_TOKEN();
if (token->type != TOKEN_IF)
{
puts("Internal error: expected a if rule but token has different "
"type");
return NULL;
}
// Condition content
2026-01-20 20:06:25 +01:00
struct ast *condition_content = parse_compound_list(ctx);
// Then keyword
token = POP_TOKEN();
if (token->type != TOKEN_THEN)
{
ast_free(&condition_content);
puts("Expected the 'then' keyword but token has different type");
return NULL;
}
// Then content
2026-01-20 20:06:25 +01:00
struct ast *then_content = parse_compound_list(ctx);
if (then_content == NULL)
{
ast_free(&condition_content);
ast_free(&then_content);
return NULL;
}
// Eventual else/elif clause(s)
2026-01-20 20:06:25 +01:00
struct ast *else_content = parse_else_clause(ctx);
if (else_content == NULL)
{
ast_free(&condition_content);
ast_free(&then_content);
return NULL;
}
2026-01-23 17:33:15 +01:00
// Fi keyword
token = POP_TOKEN();
if (token->type != TOKEN_FI)
{
ast_free(&condition_content);
ast_free(&then_content);
ast_free(&else_content);
puts("Expected the 'fi' keyword but token has different type");
return NULL;
}
2026-01-23 17:33:15 +01:00
// Result
struct ast *result =
ast_create_if(condition_content, then_content, else_content);
if (result == NULL)
{
ast_free(&condition_content);
ast_free(&then_content);
ast_free(&else_content);
puts("Internal error: could not create a new AST (AST_IF)");
return NULL;
}
return result;
}
struct ast *parse_compound_list(struct lexer_context *ctx)
{
struct list *result_list = NULL; // ast* list
struct ast *current_cmd = NULL;
struct token *token = PEEK_TOKEN();
// Skip newlines
while (token->type == TOKEN_NEWLINE)
{
token = POP_TOKEN();
2026-01-22 19:06:58 +01:00
token = PEEK_TOKEN();
}
2026-01-23 17:33:15 +01:00
// And/or
2026-01-20 20:06:25 +01:00
current_cmd = parse_and_or(ctx);
if (current_cmd == NULL)
return NULL;
2026-01-22 19:06:58 +01:00
result_list = list_append(result_list, current_cmd);
// Following commands
token = PEEK_TOKEN();
while (token->type == TOKEN_SEMICOLON || token->type == TOKEN_NEWLINE)
{
POP_TOKEN();
2026-01-22 19:06:58 +01:00
token = PEEK_TOKEN();
// Skip newlines
while (token->type == TOKEN_NEWLINE)
{
token = POP_TOKEN();
2026-01-22 19:06:58 +01:00
token = PEEK_TOKEN();
}
2026-01-23 17:33:15 +01:00
// And/or
if (is_first(*token, RULE_AND_OR))
{
current_cmd = parse_and_or(ctx);
if (current_cmd == NULL)
return NULL;
result_list = list_append(result_list, current_cmd);
}
token = PEEK_TOKEN();
}
// Eventual semicolons
if (token->type == TOKEN_SEMICOLON)
{
token = POP_TOKEN();
2026-01-22 19:06:58 +01:00
token = PEEK_TOKEN();
}
// Skip newlines
while (token->type == TOKEN_NEWLINE)
{
token = POP_TOKEN();
2026-01-22 19:06:58 +01:00
token = PEEK_TOKEN();
}
struct ast *result = ast_create_list(result_list);
return result;
}
struct ast *parse_else_clause(struct lexer_context *ctx)
{
struct token *token = PEEK_TOKEN();
// Eventual elif content
while (token->type == TOKEN_ELIF)
{
// Condition
token = POP_TOKEN();
2026-01-20 20:06:25 +01:00
struct ast *condition = parse_compound_list(ctx);
2026-01-23 17:33:15 +01:00
// Then keyword
token = POP_TOKEN();
if (token->type != TOKEN_THEN)
{
puts("Expected the 'then' keyword but got a different token type");
return NULL;
}
// Then clause
2026-01-20 20:06:25 +01:00
struct ast *then_content = parse_compound_list(ctx);
// Eventual else clause (recursive)
struct ast *else_content = NULL;
token = PEEK_TOKEN();
if (token->type == TOKEN_ELSE || token->type == TOKEN_ELIF)
{
2026-01-20 20:06:25 +01:00
else_content = parse_else_clause(ctx);
}
struct ast *result =
ast_create_if(condition, then_content, else_content);
return result;
}
// Eventual else content
struct ast *result = NULL;
if (token->type == TOKEN_ELSE)
{
2026-01-22 19:06:58 +01:00
token = POP_TOKEN(); // eat else
2026-01-20 20:06:25 +01:00
result = parse_compound_list(ctx);
}
if (result == NULL)
2026-01-20 20:32:59 +01:00
result = ast_create_void();
return result;
}