fix(parser): rule if now works
This commit is contained in:
parent
2f2509f1c9
commit
5faa179b63
2 changed files with 18 additions and 13 deletions
|
|
@ -2,8 +2,8 @@
|
||||||
#define EXECUTION_H
|
#define EXECUTION_H
|
||||||
|
|
||||||
#include "../utils/ast/ast.h"
|
#include "../utils/ast/ast.h"
|
||||||
#include "../utils/lists/lists.h"
|
|
||||||
#include "../utils/hash_map/hash_map.h"
|
#include "../utils/hash_map/hash_map.h"
|
||||||
|
#include "../utils/lists/lists.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Execute the AST
|
* @brief Execute the AST
|
||||||
|
|
|
||||||
|
|
@ -78,16 +78,16 @@ struct ast *parse_list(struct lexer_context *ctx)
|
||||||
while (token->type == TOKEN_SEMICOLON)
|
while (token->type == TOKEN_SEMICOLON)
|
||||||
{
|
{
|
||||||
token = POP_TOKEN();
|
token = POP_TOKEN();
|
||||||
// if (!isterminator(token)) // Follow(list)
|
// if (!isterminator(token)) // Follow(list)
|
||||||
// {
|
// {
|
||||||
current_node = parse_and_or(ctx);
|
current_node = parse_and_or(ctx);
|
||||||
if (current_node == NULL)
|
if (current_node == NULL)
|
||||||
{
|
{
|
||||||
// TODO free list
|
// TODO free list
|
||||||
// There must be a function for that
|
// There must be a function for that
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
result_list = list_append(result_list, current_node);
|
result_list = list_append(result_list, current_node);
|
||||||
// }
|
// }
|
||||||
token = PEEK_TOKEN();
|
token = PEEK_TOKEN();
|
||||||
}
|
}
|
||||||
|
|
@ -247,31 +247,34 @@ struct ast *parse_compound_list(struct lexer_context *ctx)
|
||||||
while (token->type == TOKEN_NEWLINE)
|
while (token->type == TOKEN_NEWLINE)
|
||||||
{
|
{
|
||||||
token = POP_TOKEN();
|
token = POP_TOKEN();
|
||||||
|
token = PEEK_TOKEN();
|
||||||
}
|
}
|
||||||
|
|
||||||
// and_or
|
// and_or
|
||||||
current_cmd = parse_and_or(ctx);
|
current_cmd = parse_and_or(ctx);
|
||||||
if (current_cmd == NULL)
|
if (current_cmd == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
list_append(result_list, current_cmd);
|
result_list = list_append(result_list, current_cmd);
|
||||||
|
|
||||||
// Following commands
|
// Following commands
|
||||||
token = PEEK_TOKEN();
|
token = PEEK_TOKEN();
|
||||||
while (token->type == TOKEN_SEMICOLON || token->type == TOKEN_NEWLINE)
|
while (token->type == TOKEN_SEMICOLON || token->type == TOKEN_NEWLINE)
|
||||||
{
|
{
|
||||||
POP_TOKEN();
|
POP_TOKEN();
|
||||||
|
token = PEEK_TOKEN();
|
||||||
|
|
||||||
// Skip newlines
|
// Skip newlines
|
||||||
while (token->type == TOKEN_NEWLINE)
|
while (token->type == TOKEN_NEWLINE)
|
||||||
{
|
{
|
||||||
token = POP_TOKEN();
|
token = POP_TOKEN();
|
||||||
|
token = PEEK_TOKEN();
|
||||||
}
|
}
|
||||||
|
|
||||||
// and_or
|
// and_or
|
||||||
current_cmd = parse_and_or(ctx);
|
current_cmd = parse_and_or(ctx);
|
||||||
if (current_cmd == NULL)
|
if (current_cmd == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
list_append(result_list, current_cmd);
|
result_list = list_append(result_list, current_cmd);
|
||||||
|
|
||||||
token = PEEK_TOKEN();
|
token = PEEK_TOKEN();
|
||||||
}
|
}
|
||||||
|
|
@ -280,12 +283,14 @@ struct ast *parse_compound_list(struct lexer_context *ctx)
|
||||||
if (token->type == TOKEN_SEMICOLON)
|
if (token->type == TOKEN_SEMICOLON)
|
||||||
{
|
{
|
||||||
token = POP_TOKEN();
|
token = POP_TOKEN();
|
||||||
|
token = PEEK_TOKEN();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Skip newlines
|
// Skip newlines
|
||||||
while (token->type == TOKEN_NEWLINE)
|
while (token->type == TOKEN_NEWLINE)
|
||||||
{
|
{
|
||||||
token = POP_TOKEN();
|
token = POP_TOKEN();
|
||||||
|
token = PEEK_TOKEN();
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ast *result = ast_create_list(result_list);
|
struct ast *result = ast_create_list(result_list);
|
||||||
|
|
@ -333,8 +338,8 @@ struct ast *parse_else_clause(struct lexer_context *ctx)
|
||||||
|
|
||||||
if (token->type == TOKEN_ELSE)
|
if (token->type == TOKEN_ELSE)
|
||||||
{
|
{
|
||||||
|
token = POP_TOKEN(); // eat else
|
||||||
result = parse_compound_list(ctx);
|
result = parse_compound_list(ctx);
|
||||||
token = POP_TOKEN(); // Forward
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result == NULL)
|
if (result == NULL)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue