Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic parsing code #43

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 29 additions & 21 deletions src/codegen/x86/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,41 +21,49 @@ void code_gen_init() {

char *start_main() {
static char start[256] = "\
global _start\
section .text\
\
_start:";
global _start\n\
section .text\n\
\n\
_start:\n";

return start;
}

char *end_main() {
static char end[256] = "\
mov rax, 60\
mov rdi, 0\
syscall";
mov rax, 60\
mov rdi, 0\
syscall";

return end;
}

char *end_main_custom_return(int val) {
char *end;
end = (char *)malloc(256 * sizeof(char));
sprintf(end, " mov rax, 60\n mov rdi, %d\n syscall\n", val);

return end;
}

char *start_func() {
static char start[256] = "\
sub rsp, 32\
mov [rsp], r12\
mov [rsp+8], r13\
mov [rsp+16], r14\
mov [rsp+24], r15";
sub rsp, 32\
mov [rsp], r12\
mov [rsp+8], r13\
mov [rsp+16], r14\
mov [rsp+24], r15";

return start;
}

char *end_func() {
static char end[256] = "\
mov r12, [rsp]\
mov r13, [rsp+8]\
mov r14, [rsp+16]\
mov r15, [rsp+24]\
add rsp, 32";
mov r12, [rsp]\
mov r13, [rsp+8]\
mov r14, [rsp+16]\
mov r15, [rsp+24]\
add rsp, 32";

return end;
}
Expand All @@ -65,16 +73,16 @@ char *init_int_literal(int val) {

char *init;
init = (char *)malloc(256 * sizeof(char));
sprintf(init, "mov [rsp+%d], %d", GEN_STATE.rsp_offset, val);
sprintf(init, " mov [rsp+%d], %d", GEN_STATE.rsp_offset, val);

return init;
}

int test_init_int_literal() {
testing_func_setup();
testing_func_setup();
code_gen_init();

tassert(strcmp(init_int_literal(100), "mov [rsp+8], 100") == 0);
tassert(strcmp(init_int_literal(100), " mov [rsp+8], 100") == 0);

return 0;
return 0;
}
2 changes: 2 additions & 0 deletions src/codegen/x86/codegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ char *end_main();

char *start_func();

char *end_main_custom_return(int val);

char *end_func();

char *init_int_literal(int val);
Expand Down
37 changes: 22 additions & 15 deletions src/driver/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
#include <string.h> // strcmp

#include <lexer/lex.h>
#include <parser/parse.h>
#include <util/out.h>

int lexer_dump(const char* filename) {
int lexer_dump(const char *filename) {

// Initialization of everything
Lexer lexer;
FILE * fp = fopen(filename, "r");
FILE *fp = fopen(filename, "r");
if (!fp) {
PRINT_ERROR("File %s not found", filename);
return 1;
Expand All @@ -24,12 +25,15 @@ int lexer_dump(const char* filename) {
Token t;
do {
// Return if some non-zero (error) code is returned
if (lex(&lexer, &t)) return 1;
printf("Contents: %20s, type: %20s, position: %d/%d\n", t.contents, ttype_name(t.type), t.line, t.column);
if (lex(&lexer, &t))
return 1;
printf("Contents: %20s, type: %20s, position: %d/%d\n", t.contents,
ttype_name(t.type), t.line, t.column);
} while (t.type != TT_EOF);

return 0;
fclose(fp);

return 0;
}

int main(int argc, char **argv) {
Expand All @@ -38,14 +42,16 @@ int main(int argc, char **argv) {

// Skip the name of the executable.
--argc, ++argv;

if (argc == 0) {
PRINT_DEFAULT("Usage: --token-dump <filename> to see all tokens");
return 0;
}

if (argc == 1) {
PRINT_DEFAULT("default compilation not supported yet -- try 'jccc --token-dump %s' instead.", argv[0]);
PRINT_DEFAULT("default compilation not supported yet -- try 'jccc "
"--token-dump %s' instead.",
argv[0]);
return 1;
}

Expand All @@ -55,13 +61,14 @@ int main(int argc, char **argv) {
}

// Two arguments now.
if (strcmp(argv[0], "--token-dump")) {
PRINT_ERROR("option %s not recognized.", argv[1]);
return 1;
}

// Finally, we can do the lexer test properly!
return lexer_dump(argv[1]);
if (strcmp(argv[0], "--token-dump") == 0) {
// Finally, we can do the lexer test properly!
return lexer_dump(argv[1]);
} else if (strcmp(argv[0], "--test-parse") == 0) {
parse(argv[1]);
return 0;
}

return 0;
PRINT_ERROR("option %s not recognized.", argv[1]);
return 1;
}
97 changes: 97 additions & 0 deletions src/parser/parse.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/* Parser
*
*/

#include <codegen/x86/codegen.h>
#include <lexer/lex.h>
#include <lexer/token.h>
#include <stdlib.h> // calloc
#include <string.h> // strcmp
#include <ctype.h> // isdigit
#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 &&
isdigit(tokens[6].contents[0]) && 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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if I write the following code:

int main() {
    return "seven";
}


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");
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fclose(fp);?

fclose(fp);

return 0;
}

int parse_simple_main_func() {}
5 changes: 5 additions & 0 deletions src/parser/parse.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* Parser
*
*/

int parse(const char *filename);
3 changes: 3 additions & 0 deletions tests/simplemain.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
int main() {
return 5;
}
Loading