Compare commits

..

28 Commits

Author SHA1 Message Date
omicron 297ad863c3 Slightly change the valid test input file
Validate the build / validate-build (push) Successful in 27s
2025-04-02 11:29:09 +02:00
omicron 28dffa4cb5 Fix parse_directive grammar rule 2025-04-02 11:29:09 +02:00
omicron 0169455fc2 Add registers and fix section primitive parser 2025-04-02 11:29:09 +02:00
omicron 0ec3d0fa35 Fix parser loops in parse_any and parse_consecutive 2025-04-02 11:29:09 +02:00
omicron 69571b344b Modify main to use the new print ast functionality 2025-04-02 11:29:09 +02:00
omicron ae2556ac9e TODO: REVIEW THIS FUNCTION Add function to print AST 2025-04-02 11:29:09 +02:00
omicron 4dc3ef8967 Add more grammar rules to the parser 2025-04-02 11:29:09 +02:00
omicron 449cffe79f Add a parser combinator to parse a delimited list 2025-04-02 11:29:09 +02:00
omicron 6794799b5f make parse_success always skip past trivia in the tokenlist 2025-04-02 11:29:09 +02:00
omicron a22cfb436e TODO: REVIEW ME AND WRITE PROPER MESSAGE
Fix lexer issue where consuming n tokens always fails if there are n
tokens and always succeeds if they aren't n tokens
2025-04-02 11:29:09 +02:00
omicron f3894211e0 Match ast nodes to new grammar 2025-04-02 11:29:09 +02:00
omicron 836a0550d9 Use new validator function for parse_token calls on all primitives
Also adds new validated primitives for NODE_SECTION and NODE_REGISTER
2025-04-02 11:29:09 +02:00
omicron 249f0190d8 Fix incorrect error returned in parse_consecutive 2025-04-02 11:29:09 +02:00
omicron a09d591e86 Fix memory leak in ast.
If a node has children the array of children was never freed.
2025-04-02 11:29:09 +02:00
omicron f4638d43b6 Extend parse_token to accept an optional validator function 2025-04-02 11:29:09 +02:00
omicron fcbf96af48 Expose err_parse_no_match in parser_util.h 2025-04-02 11:29:09 +02:00
omicron 94ac826cde Add basic parser combinators 2025-04-02 11:29:09 +02:00
omicron 5d28c51cf1 Add "primitive" parsers for all the semantic tokens in the lexer grammar 2025-04-02 11:29:09 +02:00
omicron 89c7bb5253 Add basic parser utilities 2025-04-02 11:29:09 +02:00
omicron 69e1cb840c Add functions to skip over trivia in a tokenlist 2025-04-02 11:29:09 +02:00
omicron ea5df2d129 Expand AST node ids to support the lexer tokens and grammar rules 2025-04-02 11:29:09 +02:00
omicron 81be56de33 Fix parse_token to add the correct information to a parse node 2025-04-02 11:29:09 +02:00
omicron ec7d06b135 Fix ast nodes now containing token entry instead of token 2025-04-02 11:29:09 +02:00
omicron 3ff53f923d Partial parser implementation 2025-04-02 11:29:09 +02:00
omicron 4c7555dfd8 Add invalid ast node id 2025-04-02 11:29:09 +02:00
omicron 68a41d035d FIXME REORDER COMMIT -- Change main so it can parse the ast
FIXME THIS COMMIT NEEDS TO BE REORDERED
FIXME THIS COMMIT NEEDS TO BE REORDERED
FIXME THIS COMMIT NEEDS TO BE REORDERED
FIXME THIS COMMIT NEEDS TO BE REORDERED
2025-04-02 11:29:09 +02:00
omicron 4e1186a305 Add basic AST functionality 2025-04-02 11:29:09 +02:00
omicron 34ace36920 Add a parser grammar 2025-04-02 11:28:58 +02:00
4 changed files with 25 additions and 7 deletions
+1 -1
View File
@@ -34,7 +34,7 @@ parse_result_t parse_section_directive(tokenlist_entry_t *current) {
parse_result_t parse_directive(tokenlist_entry_t *current) {
parser_t parsers[] = {parse_dot, parse_section_directive, nullptr};
return parse_consecutive(current, NODE_LABEL, parsers);
return parse_consecutive(current, NODE_DIRECTIVE, parsers);
}
parse_result_t parse_instruction(tokenlist_entry_t *current) {
+4 -2
View File
@@ -50,7 +50,8 @@ parse_result_t parse_list(tokenlist_entry_t *current, node_id_t id,
}
parse_result_t parse_any(tokenlist_entry_t *current, parser_t parsers[]) {
for (parser_t parser = *parsers; parser; parser = *parsers++) {
parser_t parser;
while ((parser = *parsers++)) {
parse_result_t result = parser(current);
if (result.err == nullptr)
return result;
@@ -106,7 +107,8 @@ parse_result_t parse_consecutive(tokenlist_entry_t *current, node_id_t id,
all->id = id;
for (parser_t parser = *parsers; parser && current; parser = *parsers++) {
parser_t parser;
while ((parser = *parsers++) && current) {
result = parser(current);
if (result.err) {
ast_node_free(all);
+17 -4
View File
@@ -62,9 +62,19 @@ parse_result_t parse_dot(tokenlist_entry_t *current) {
return parse_token(current, TOKEN_DOT, NODE_DOT, nullptr);
}
const char *registers[] = {"rax", "rcx", "rdx", "rbx", "rsp", "rbp",
"rsi", "rdi", "r8", "r9", "r10", "r11",
"r12", "r13", "r14", "r15", nullptr};
const char *registers[] = {
// 64-bit registers
"rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi", "r8", "r9", "r10",
"r11", "r12", "r13", "r14", "r15",
// 32-bit registers
"eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi", "r8d", "r9d",
"r10d", "r11d", "r12d", "r13d", "r14d", "r15d",
// 16-bit registers
"ax", "cx", "dx", "bx", "sp", "bp", "si", "di", "r8w", "r9w", "r10w",
"r11w", "r12w", "r13w", "r14w", "r15w",
// 8-bit low registers
"al", "cl", "dl", "bl", "spl", "bpl", "sil", "dil", "r8b", "r9b", "r10b",
"r11b", "r12b", "r13b", "r14b", "r15b", nullptr};
bool is_register_token(lexer_token_t *token) {
for (size_t i = 0; registers[i] != nullptr; ++i)
if (strcmp(token->value, registers[i]) == 0)
@@ -81,4 +91,7 @@ bool is_section_token(lexer_token_t *token) {
return strcmp(token->value, "section") == 0;
}
parse_result_t parse_section(tokenlist_entry_t *current) {}
parse_result_t parse_section(tokenlist_entry_t *current) {
return parse_token(current, TOKEN_IDENTIFIER, NODE_SECTION,
is_section_token);
}
+3
View File
@@ -1,4 +1,7 @@
.section text
_start:
mov eax, ebx
mov eax, 555 ; move 555 into eax
push 0o777
xor eax, 0xDEADBEEF