implemented parse_expression
This commit is contained in:
parent
256f9fe611
commit
0c9f97b41d
70
src/syntax.c
70
src/syntax.c
|
@ -11,7 +11,6 @@
|
|||
#include "sync/syntax.h"
|
||||
|
||||
typedef struct {
|
||||
const char* filename;
|
||||
TokenArray tokens;
|
||||
size_t pos;
|
||||
} Parser;
|
||||
|
@ -106,7 +105,45 @@ static SyntaxResult syntax_error(const char* message, Token token) {
|
|||
}};
|
||||
}
|
||||
|
||||
static SyntaxResult parse_expression(Parser* parser) {
|
||||
}
|
||||
|
||||
static SyntaxResult parse_statement(Parser* parser) {
|
||||
Child* children = NULL;
|
||||
|
||||
if (
|
||||
parser->tokens.tokens[parser->pos].type == TOKEN_EOF ||
|
||||
parser->tokens.tokens[parser->pos].type == TOKEN_RBRACE
|
||||
) return syntax_error("Expected statement", parser->tokens.tokens[parser->pos]);
|
||||
SyntaxResult result = parse_expression(parser);
|
||||
if (result.type == SYNC_ERROR) return result;
|
||||
if (result.result.type == SYNC_ERROR) return result;
|
||||
if (children == NULL) {
|
||||
children = (Child*)malloc(sizeof(Child));
|
||||
if (children == NULL)
|
||||
return (SyntaxResult){SYNC_ERROR, .error = (GeneralError){"Failed to allocate memory.", 1}};
|
||||
children->next = NULL;
|
||||
children->node = result.result.result;
|
||||
} else {
|
||||
GeneralError error = add_child(children, result.result.result);
|
||||
if (error.message != NULL) return (SyntaxResult){SYNC_ERROR, .error = error};
|
||||
}
|
||||
|
||||
if (parser->tokens.tokens[parser->pos].type != TOKEN_SEMICOLON)
|
||||
return syntax_error("Expected statement", parser->tokens.tokens[parser->pos]);
|
||||
|
||||
SyntaxNode node = {
|
||||
.type = NODE_SEMICOLON,
|
||||
.start = parser->tokens.tokens[parser->pos].start,
|
||||
.length = parser->tokens.tokens[parser->pos].length,
|
||||
.file_info = parser->tokens.tokens[parser->pos].file_info,
|
||||
.children = (Children){0, NULL}
|
||||
};
|
||||
GeneralError error = add_child(children, result.result.result);
|
||||
if (error.message != NULL) return (SyntaxResult){SYNC_ERROR, .error = error};
|
||||
parser->pos++;
|
||||
|
||||
return syntax_result(NODE_STATEMENT, children);
|
||||
}
|
||||
|
||||
static SyntaxResult parse_block(Parser* parser) {
|
||||
|
@ -120,10 +157,14 @@ static SyntaxResult parse_block(Parser* parser) {
|
|||
if (result.result.type == SYNC_ERROR) return result;
|
||||
if (children == NULL) {
|
||||
children = (Child*)malloc(sizeof(Child));
|
||||
if (children == NULL) return (SyntaxResult){SYNC_ERROR, .error = (GeneralError){"Failed to allocate memory.", 1}};
|
||||
if (children == NULL)
|
||||
return (SyntaxResult){SYNC_ERROR, .error = (GeneralError){"Failed to allocate memory.", 1}};
|
||||
children->next = NULL;
|
||||
children->node = result.result.result;
|
||||
} else add_child(children, result.result.result);
|
||||
} else {
|
||||
GeneralError error = add_child(children, result.result.result);
|
||||
if (error.message != NULL) return (SyntaxResult){SYNC_ERROR, .error = error};
|
||||
}
|
||||
if (parser->tokens.tokens[parser->pos].type == TOKEN_EOF)
|
||||
return syntax_error("Expected '}'", parser->tokens.tokens[parser->pos]);
|
||||
}
|
||||
|
@ -141,26 +182,25 @@ static SyntaxResult parse_block(Parser* parser) {
|
|||
return syntax_result(NODE_BLOCK, children);
|
||||
}
|
||||
|
||||
SyntaxResult syntactical_analysis(TokenArray tokens) {
|
||||
Parser* parser = (Parser*)malloc(sizeof(Parser));
|
||||
if (parser == NULL) return (SyntaxResult){SYNC_ERROR, .error = (GeneralError){"Failed to allocate memory.", 1}};
|
||||
SyntaxResult syntactical_analysis(const char* filename, TokenArray tokens) {
|
||||
Parser parser = { tokens, 0 };
|
||||
Child* children = NULL;
|
||||
|
||||
while (parser->tokens.tokens[parser->pos].type != TOKEN_EOF) {
|
||||
SyntaxResult result = parse_block(parser);
|
||||
if (result.type == SYNC_ERROR) { free(parser); return result; }
|
||||
if (result.result.type == SYNC_ERROR) { free(parser); return result; }
|
||||
while (parser.tokens.tokens[parser.pos].type != TOKEN_EOF) {
|
||||
SyntaxResult result = parse_block(&parser);
|
||||
if (result.type == SYNC_ERROR) return result;
|
||||
if (result.result.type == SYNC_ERROR) return result;
|
||||
if (children == NULL) {
|
||||
children = (Child*)malloc(sizeof(Child));
|
||||
if (children == NULL) {
|
||||
free(parser);
|
||||
if (children == NULL)
|
||||
return (SyntaxResult){SYNC_ERROR, .error = (GeneralError){"Failed to allocate memory.", 1}};
|
||||
}
|
||||
children->next = NULL;
|
||||
children->node = result.result.result;
|
||||
} else add_child(children, result.result.result);
|
||||
} else {
|
||||
GeneralError error = add_child(children, result.result.result);
|
||||
if (error.message != NULL) return (SyntaxResult){SYNC_ERROR, .error = error};
|
||||
}
|
||||
}
|
||||
|
||||
free(parser);
|
||||
return syntax_result(NODE_FILE, children);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue