-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b8fe835
commit 7fd0973
Showing
6 changed files
with
153 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* Parser | ||
* | ||
*/ | ||
|
||
#include <codegen/x86/codegen.h> | ||
#include <lexer/lex.h> | ||
#include <lexer/token.h> | ||
#include <stdlib.h> // calloc | ||
#include <string.h> // strcmp | ||
#include <util/out.h> | ||
|
||
int parse(const char *filename) { | ||
|
||
Lexer lexer; | ||
|
||
FILE *fp = fopen(filename, "r"); | ||
|
||
if (!fp) { | ||
PRINT_ERROR("File %s not found", filename); | ||
return 1; | ||
} | ||
|
||
lexer.fp = fp; | ||
lexer.unlexed_count = 0; | ||
lexer.column = lexer.line = 1; | ||
|
||
Token t; | ||
|
||
int i = 0; | ||
int buffer_size = 16; | ||
Token *tokens = calloc(buffer_size, sizeof(Token)); | ||
|
||
do { | ||
if (lex(&lexer, &t)) { | ||
return 1; | ||
} | ||
|
||
if (buffer_size <= i) { | ||
buffer_size *= 2; | ||
tokens = calloc(buffer_size, sizeof(Token)); | ||
} | ||
|
||
tokens[i] = t; | ||
|
||
printf("Contents: %20s, type: %20s, position: %d/%d\n", t.contents, | ||
ttype_name(t.type), t.line, t.column); | ||
|
||
i++; | ||
} while (t.type != TT_EOF); | ||
|
||
// Main function | ||
if (tokens[0].type == TT_INT && tokens[1].type == TT_IDENTIFIER && | ||
(strcmp(tokens[1].contents, "main") == 0)) { | ||
|
||
// Correct empty function body | ||
if (tokens[2].type == TT_OPAREN && tokens[3].type == TT_CPAREN && | ||
tokens[4].type == TT_OBRACE) { | ||
|
||
// Return value | ||
if (tokens[5].type == TT_RETURN && tokens[6].type == TT_LITERAL && | ||
tokens[7].type == TT_SEMI) { | ||
|
||
// Correct matched closed brace | ||
if (tokens[8].type == TT_CBRACE) { | ||
printf("\n"); | ||
|
||
// Generate preamble main code | ||
char *code_start = start_main(); | ||
|
||
printf(code_start); | ||
|
||
// Add custom return code | ||
char *code_end = | ||
end_main_custom_return(atoi(tokens[6].contents)); | ||
|
||
printf(code_end); | ||
|
||
} else { | ||
PRINT_ERROR("Wrong closing brace.\n"); | ||
} | ||
} else { | ||
PRINT_ERROR("Return value is wrong.\n"); | ||
} | ||
} else { | ||
PRINT_ERROR("Wrong main function body.\n"); | ||
} | ||
} else { | ||
PRINT_ERROR("Not correct main function.\n"); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
int parse_simple_main_func() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/* Parser | ||
* | ||
*/ | ||
|
||
int parse(const char *filename); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
int main() { | ||
return 5; | ||
} |