From 7f84ee7fffe4e34ad4f75d54bcb654663f0c9fab Mon Sep 17 00:00:00 2001 From: Keyvan Kambakhsh Date: Mon, 14 Oct 2024 14:22:22 +0330 Subject: [PATCH] feat: Add tests for compiler outputs in other modes --- main.c | 60 +-- parser/type.c | 2 +- scripts/test.py | 43 +- tests/output/inp.c_asm_output.txt | 65 +++ tests/output/inp.c_lex_output.txt | 59 +++ tests/output/inp.c_tree_output.txt | 56 +++ tests/output/inp2.c_asm_output.txt | 154 +++++++ tests/output/inp2.c_lex_output.txt | 145 +++++++ tests/output/inp2.c_tree_output.txt | 145 +++++++ tests/output/inp3.c_asm_output.txt | 617 ++++++++++++++++++++++++++++ tests/output/inp3.c_lex_output.txt | 518 +++++++++++++++++++++++ tests/output/inp3.c_tree_output.txt | 371 +++++++++++++++++ tests/output/inp4.c_asm_output.txt | 21 + tests/output/inp4.c_lex_output.txt | 22 + tests/output/inp4.c_tree_output.txt | 17 + 15 files changed, 2249 insertions(+), 46 deletions(-) create mode 100644 tests/output/inp.c_asm_output.txt create mode 100644 tests/output/inp.c_lex_output.txt create mode 100644 tests/output/inp.c_tree_output.txt create mode 100644 tests/output/inp2.c_asm_output.txt create mode 100644 tests/output/inp2.c_lex_output.txt create mode 100644 tests/output/inp2.c_tree_output.txt create mode 100644 tests/output/inp3.c_asm_output.txt create mode 100644 tests/output/inp3.c_lex_output.txt create mode 100644 tests/output/inp3.c_tree_output.txt create mode 100644 tests/output/inp4.c_asm_output.txt create mode 100644 tests/output/inp4.c_lex_output.txt create mode 100644 tests/output/inp4.c_tree_output.txt diff --git a/main.c b/main.c index 73db417..8035cd4 100644 --- a/main.c +++ b/main.c @@ -21,8 +21,9 @@ char *read_source_file(FILE *fp); int main(int argc, char *argv[]) { - if (argc != 2) { - fprintf(stderr, "Usage: %s \n", argv[0]); + if (argc != 3) + { + fprintf(stderr, "Usage: %s (: --lex, --asm or --tree)\n", argv[0]); return 1; } @@ -31,7 +32,8 @@ int main(int argc, char *argv[]) const char *filename = argv[1]; FILE *fp = fopen(filename, "rb"); - if (fp == NULL) { + if (fp == NULL) + { fprintf(stderr, "Error opening file: %s\n", filename); return 1; } @@ -46,11 +48,14 @@ int main(int argc, char *argv[]) typed_token *tkn = tokenize(content); - typed_token *t = tkn; - while (t) + if (strcmp(argv[2], "--lex") == 0) { - // t->debug(t); - t = t->next; + typed_token *t = tkn; + while (t) + { + t->debug(t); + t = t->next; + } } tkn = preprocess(tkn); @@ -59,29 +64,35 @@ int main(int argc, char *argv[]) parser_node *prog = parse_program(&tkn); if (prog) { - // prog->debug(0, prog); - // exit(0); + if (strcmp(argv[2], "--tree") == 0) + { + prog->debug(0, prog); + exit(0); + } } else { printf("Parse failed!\n"); } - context ctx = new_context(); - prog->apply(prog, &ctx); - list_node *curr = ctx.data.first; - printf("section .data\n"); - while (curr) + if (strcmp(argv[2], "--asm") == 0) { - printf("%s\n", (char*)curr->value); - curr = curr->next; - } - printf("section .text\n"); - curr = ctx.text.first; - while (curr) - { - printf("%s\n", (char*)curr->value); - curr = curr->next; + context ctx = new_context(); + prog->apply(prog, &ctx); + list_node *curr = ctx.data.first; + printf("section .data\n"); + while (curr) + { + printf("%s\n", (char *)curr->value); + curr = curr->next; + } + printf("section .text\n"); + curr = ctx.text.first; + while (curr) + { + printf("%s\n", (char *)curr->value); + curr = curr->next; + } } defer_exit: @@ -105,7 +116,8 @@ char *read_source_file(FILE *fp) goto ret; int rd = fread(data, sizeof(char), st.st_size, fp); - if(rd != st.st_size) { + if (rd != st.st_size) + { data = NULL; goto ret; } diff --git a/parser/type.c b/parser/type.c index be6750f..29d5add 100644 --- a/parser/type.c +++ b/parser/type.c @@ -27,7 +27,7 @@ void type_debug(int depth, parser_node *node) } parser_node *parse_type(typed_token **tkns_ptr) -{ +{ typed_token *tkn = *tkns_ptr; parser_node *node = NULL; if (tkn->type_id == TKN_INT || tkn->type_id == TKN_VOID || tkn->type_id == TKN_CHAR) diff --git a/scripts/test.py b/scripts/test.py index 935b2c0..e00a460 100644 --- a/scripts/test.py +++ b/scripts/test.py @@ -15,10 +15,10 @@ TEMP_FOLDER = "temp_snapshots" -def run(input_file): +def run(input_file, mode): try: result = subprocess.run( - [C_PROGRAM_NAME, input_file], capture_output=True, text=True, check=True + [C_PROGRAM_NAME, input_file, '--' + mode], capture_output=True, text=True, check=True ) return result.stdout except subprocess.CalledProcessError as e: @@ -97,25 +97,26 @@ def main(): diff_count = 0 for test_file in TEST_FILES: - output_file = os.path.join( - OUTPUT_FOLDER, f"{os.path.basename(test_file)}_output.txt" - ) - - if mode == "revert": - revert_snapshot(output_file) - continue - - output = run(test_file) - if output is None: - continue - - if os.path.exists(output_file): - if not compare_outputs(output_file, output): - diff_count += 1 - print(f"Alert: New output differs from {output_file}") - update_output(output_file, output, True) - else: - update_output(output_file, output) + for mode in ['lex', 'tree', 'asm']: + output_file = os.path.join( + OUTPUT_FOLDER, f"{os.path.basename(test_file)}_{mode}_output.txt" + ) + + if mode == "revert": + revert_snapshot(output_file) + continue + + output = run(test_file, mode) + if output is None: + continue + + if os.path.exists(output_file): + if not compare_outputs(output_file, output): + diff_count += 1 + print(f"Alert: New output differs from {output_file}") + update_output(output_file, output, True) + else: + update_output(output_file, output) print(f"found {diff_count} differences in snapshots") diff --git a/tests/output/inp.c_asm_output.txt b/tests/output/inp.c_asm_output.txt new file mode 100644 index 0000000..dc1fa92 --- /dev/null +++ b/tests/output/inp.c_asm_output.txt @@ -0,0 +1,65 @@ +section .data +__printf_size: equ 16 +__temp_str_1 db `%u `, 0 +__temp_str_2 db `%u `, 0 +__main_size: equ 96 +section .text +extern printf +global main +main: +push rbp +mov rbp, rsp +sub rsp, __main_size +mov rax, 10 +mov [rsp+0], rax +mov rax, 0 +mov [rsp+8], rax +__tmp_label_0: +mov rax, [rsp + 8] +mov rbx, 5 +cmp rax, rbx +jl __tmp_label_2 +mov rax, 0 +jmp __tmp_label_3 +__tmp_label_2: +mov rax, 1 +__tmp_label_3: +mov [rsp + 16], rax +mov rax, [rsp + 16] +cmp rax, 0 +je __tmp_label_1 +mov rax, 2 +mov [rsp+24], rax +mov rax, __temp_str_1 +mov [rsp + 32], rax +mov rax, [rsp + 24] +mov [rsp + 40], rax +mov rdi, [rsp + 32] +mov rsi, [rsp + 40] +call printf +mov [rsp + 48], rax +mov rax, [rsp + 8] +mov rbx, 1 +add rax, rbx +mov [rsp + 24], rax +mov rax, [rsp + 24] +mov [rsp+8], rax +jmp __tmp_label_0 +__tmp_label_1: +mov rax, __temp_str_2 +mov [rsp + 8], rax +mov rax, [rsp + 0] +mov [rsp + 16], rax +mov rdi, [rsp + 8] +mov rsi, [rsp + 16] +call printf +mov [rsp + 24], rax +mov rsp, rbp +pop rbp +ret +extern exit +global _start +_start: +call main +mov rdi, 0 +call exit diff --git a/tests/output/inp.c_lex_output.txt b/tests/output/inp.c_lex_output.txt new file mode 100644 index 0000000..f057058 --- /dev/null +++ b/tests/output/inp.c_lex_output.txt @@ -0,0 +1,59 @@ +TKN_VOID +TKN_ID(printf) +TKN_L_PAREN +TKN_CHAR +TKN_STAR +TKN_COMMA +TKN_DOTS +TKN_R_PAREN +TKN_SEMICOLON +TKN_INT +TKN_ID(main) +TKN_L_PAREN +TKN_R_PAREN +TKN_L_BRACE +TKN_INT +TKN_ID(i) +TKN_ASSIGN +TKN_LIT_INT(10) +TKN_SEMICOLON +TKN_FOR +TKN_L_PAREN +TKN_INT +TKN_ID(i) +TKN_ASSIGN +TKN_LIT_INT(0) +TKN_SEMICOLON +TKN_ID(i) +TKN_LT +TKN_LIT_INT(5) +TKN_SEMICOLON +TKN_ID(i) +TKN_ASSIGN +TKN_ID(i) +TKN_PLUS +TKN_LIT_INT(1) +TKN_R_PAREN +TKN_L_BRACE +TKN_INT +TKN_ID(i) +TKN_ASSIGN +TKN_LIT_INT(2) +TKN_SEMICOLON +TKN_ID(printf) +TKN_L_PAREN +TKN_LIT_STR(%u ) +TKN_COMMA +TKN_ID(i) +TKN_R_PAREN +TKN_SEMICOLON +TKN_R_BRACE +TKN_ID(printf) +TKN_L_PAREN +TKN_LIT_STR(%u ) +TKN_COMMA +TKN_ID(i) +TKN_R_PAREN +TKN_SEMICOLON +TKN_R_BRACE +TKN_EOF diff --git a/tests/output/inp.c_tree_output.txt b/tests/output/inp.c_tree_output.txt new file mode 100644 index 0000000..d53b200 --- /dev/null +++ b/tests/output/inp.c_tree_output.txt @@ -0,0 +1,56 @@ +Program( + FunctionDecl( + Name: + printf + Returns: + Type(TKN_VOID) + ParamTypes: + Type(TKN_CHAR*) + ) + Function( + Name: + main + Returns: + Type(TKN_INT) + Params: + Statements: + VarDecl(i): + Type(TKN_INT) + Value: + Literal(Type: 34, Value: 10) + For( + Init: + VarDecl(i): + Type(TKN_INT) + Value: + Literal(Type: 34, Value: 0) + Cond: + BinaryOp(Op: 66) + Left: + Variable(i) + Right: + Literal(Type: 34, Value: 5) + Act: + Assign(i): + Value: + BinaryOp(Op: 72) + Left: + Variable(i) + Right: + Literal(Type: 34, Value: 1) + Body: + CompoundStatement + VarDecl(i): + Type(TKN_INT) + Value: + Literal(Type: 34, Value: 2) + FunctionCall(Name: printf) + Args: + Literal(Type: 33, Value: %u ) + Variable(i) + FunctionCall(Name: printf) + Args: + Literal(Type: 33, Value: %u ) + Variable(i) + ) +) diff --git a/tests/output/inp2.c_asm_output.txt b/tests/output/inp2.c_asm_output.txt new file mode 100644 index 0000000..bd44c56 --- /dev/null +++ b/tests/output/inp2.c_asm_output.txt @@ -0,0 +1,154 @@ +section .data +__haha_size: equ 16 +__abc_size: equ 16 +__f_size: equ 32 +__temp_str_3 db `1`, 0 +__temp_str_4 db `2`, 0 +__temp_str_5 db `3`, 0 +__temp_str_6 db `4`, 0 +__temp_str_7 db `5`, 0 +__temp_str_8 db `6`, 0 +__temp_str_9 db `234`, 0 +__temp_str_10 db `2`, 0 +__temp_str_11 db `23`, 0 +__temp_str_12 db `1`, 0 +__temp_str_13 db `2`, 0 +__temp_str_14 db `3`, 0 +__temp_str_15 db `4`, 0 +__temp_str_16 db `5`, 0 +__temp_str_17 db `ab`, 0 +__temp_str_18 db `cd`, 0 +__temp_str_19 db `salam`, 0 +__temp_str_20 db `khubi`, 0 +__temp_str_21 db `chetori`, 0 +__temp_str_22 db `haha`, 0 +__temp_str_23 db `a`, 0 +__temp_str_24 db `b`, 0 +__temp_str_25 db `d`, 0 +__main_size: equ 240 +section .text +extern haha +extern abc +global f +f: +push rbp +mov rbp, rsp +sub rsp, __f_size +mov [rsp+0], rdi +mov rax, 234 +mov rbx, 345 +mul rbx +mov [rsp + 8], rax +mov rax, 123 +mov rbx, [rsp + 8] +add rax, rbx +mov [rsp + 16], rax +mov rsp, rbp +pop rbp +ret +global main +main: +push rbp +mov rbp, rsp +sub rsp, __main_size +mov [rsp+0], rdi +mov [rsp+8], rsi +mov rax, __temp_str_5 +mov rbx, __temp_str_6 +mul rbx +mov [rsp + 24], rax +mov rax, __temp_str_7 +mov rbx, __temp_str_8 +add rax, rbx +mov [rsp + 32], rax +mov rax, [rsp + 24] +mov rbx, [rsp + 32] +add rax, rbx +mov [rsp + 40], rax +mov rax, __temp_str_4 +mov rbx, [rsp + 40] +add rax, rbx +mov [rsp + 48], rax +mov rax, __temp_str_3 +mov rbx, [rsp + 48] +add rax, rbx +mov [rsp + 56], rax +mov rax, [rsp + 56] +mov [rsp+16], rax +mov rax, __temp_str_11 +mov rbx, __temp_str_12 +add rax, rbx +mov [rsp + 72], rax +mov rax, __temp_str_10 +mov rbx, [rsp + 72] +add rax, rbx +mov [rsp + 80], rax +mov rax, __temp_str_9 +mov rbx, [rsp + 80] +add rax, rbx +mov [rsp + 88], rax +mov rax, [rsp + 88] +mov [rsp+64], rax +mov rax, __temp_str_15 +mov rbx, __temp_str_16 +add rax, rbx +mov [rsp + 96], rax +mov rax, __temp_str_14 +mov rbx, [rsp + 96] +add rax, rbx +mov [rsp + 104], rax +mov rax, __temp_str_13 +mov rbx, [rsp + 104] +add rax, rbx +mov [rsp + 112], rax +mov rax, [rsp + 112] +mov [rsp+64], rax +call hey +mov [rsp + 120], rax +call func +mov [rsp + 128], rax +mov rax, __temp_str_18 +mov rbx, [rsp + 128] +add rax, rbx +mov [rsp + 136], rax +mov rax, __temp_str_17 +mov rbx, [rsp + 136] +add rax, rbx +mov [rsp + 144], rax +mov rax, [rsp + 120] +mov rbx, [rsp + 144] +add rax, rbx +mov [rsp + 152], rax +mov rax, [rsp + 152] +mov [rsp+16], rax +call salam +mov [rsp + 160], rax +mov rax, __temp_str_19 +mov [rsp+16], rax +mov rax, __temp_str_22 +mov [rsp+168], rax +mov rax, __temp_str_23 +mov [rsp + 176], rax +mov rax, __temp_str_24 +mov [rsp + 184], rax +mov rax, __temp_str_25 +mov [rsp + 192], rax +mov rdi, [rsp + 192] +call ggg +mov [rsp + 200], rax +mov rax, [rsp + 200] +mov [rsp + 208], rax +mov rdi, [rsp + 176] +mov rsi, [rsp + 184] +mov rdx, [rsp + 208] +call salam +mov [rsp + 216], rax +mov rsp, rbp +pop rbp +ret +extern exit +global _start +_start: +call main +mov rdi, 0 +call exit diff --git a/tests/output/inp2.c_lex_output.txt b/tests/output/inp2.c_lex_output.txt new file mode 100644 index 0000000..48ede5b --- /dev/null +++ b/tests/output/inp2.c_lex_output.txt @@ -0,0 +1,145 @@ +TKN_VOID +TKN_ID(haha) +TKN_L_PAREN +TKN_R_PAREN +TKN_SEMICOLON +TKN_VOID +TKN_ID(abc) +TKN_L_PAREN +TKN_INT +TKN_COMMA +TKN_INT +TKN_ID(a) +TKN_COMMA +TKN_INT +TKN_ID(b) +TKN_COMMA +TKN_INT +TKN_STAR +TKN_STAR +TKN_STAR +TKN_R_PAREN +TKN_SEMICOLON +TKN_VOID +TKN_ID(f) +TKN_L_PAREN +TKN_INT +TKN_ID(a) +TKN_R_PAREN +TKN_L_BRACE +TKN_LIT_INT(123) +TKN_PLUS +TKN_LIT_INT(234) +TKN_STAR +TKN_LIT_INT(345) +TKN_SEMICOLON +TKN_R_BRACE +TKN_VOID +TKN_STAR +TKN_ID(main) +TKN_L_PAREN +TKN_INT +TKN_STAR +TKN_ID(a) +TKN_COMMA +TKN_INT +TKN_STAR +TKN_STAR +TKN_ID(b) +TKN_R_PAREN +TKN_L_BRACE +TKN_INT +TKN_ID(a) +TKN_ASSIGN +TKN_LIT_STR(1) +TKN_PLUS +TKN_LIT_STR(2) +TKN_PLUS +TKN_LIT_STR(3) +TKN_STAR +TKN_LIT_STR(4) +TKN_PLUS +TKN_LIT_STR(5) +TKN_PLUS +TKN_LIT_STR(6) +TKN_SEMICOLON +TKN_INT +TKN_ID(b) +TKN_ASSIGN +TKN_LIT_STR(234) +TKN_PLUS +TKN_LIT_STR(2) +TKN_PLUS +TKN_L_PAREN +TKN_LIT_STR(23) +TKN_PLUS +TKN_LIT_STR(1) +TKN_R_PAREN +TKN_SEMICOLON +TKN_ID(b) +TKN_ASSIGN +TKN_LIT_STR(2) +TKN_PLUS +TKN_LIT_STR(3) +TKN_PLUS +TKN_L_PAREN +TKN_LIT_STR(4) +TKN_PLUS +TKN_LIT_STR(5) +TKN_R_PAREN +TKN_SEMICOLON +TKN_ID(a) +TKN_ASSIGN +TKN_ID(hey) +TKN_L_PAREN +TKN_R_PAREN +TKN_PLUS +TKN_LIT_STR(ab) +TKN_PLUS +TKN_L_PAREN +TKN_LIT_STR(cd) +TKN_PLUS +TKN_ID(func) +TKN_L_PAREN +TKN_R_PAREN +TKN_R_PAREN +TKN_SEMICOLON +TKN_ID(salam) +TKN_L_PAREN +TKN_R_PAREN +TKN_SEMICOLON +TKN_L_BRACE +TKN_ID(a) +TKN_ASSIGN +TKN_LIT_STR(salam) +TKN_SEMICOLON +TKN_LIT_STR(khubi) +TKN_SEMICOLON +TKN_L_BRACE +TKN_LIT_STR(chetori) +TKN_SEMICOLON +TKN_INT +TKN_ID(a) +TKN_ASSIGN +TKN_LIT_STR(haha) +TKN_SEMICOLON +TKN_L_BRACE +TKN_R_BRACE +TKN_L_BRACE +TKN_ID(salam) +TKN_L_PAREN +TKN_LIT_STR(a) +TKN_COMMA +TKN_LIT_STR(b) +TKN_COMMA +TKN_ID(ggg) +TKN_L_PAREN +TKN_LIT_STR(d) +TKN_R_PAREN +TKN_R_PAREN +TKN_SEMICOLON +TKN_R_BRACE +TKN_R_BRACE +TKN_R_BRACE +TKN_R_BRACE +TKN_EOF diff --git a/tests/output/inp2.c_tree_output.txt b/tests/output/inp2.c_tree_output.txt new file mode 100644 index 0000000..e1bcdfd --- /dev/null +++ b/tests/output/inp2.c_tree_output.txt @@ -0,0 +1,145 @@ +Program( + FunctionDecl( + Name: + haha + Returns: + Type(TKN_VOID) + ParamTypes: + ) + FunctionDecl( + Name: + abc + Returns: + Type(TKN_VOID) + ParamTypes: + Type(TKN_INT) + Type(TKN_INT) + Type(TKN_INT) + Type(TKN_INT***) + ) + Function( + Name: + f + Returns: + Type(TKN_VOID) + Params: + Param: a + Type(TKN_INT) + Statements: + BinaryOp(Op: 72) + Left: + Literal(Type: 34, Value: 123) + Right: + BinaryOp(Op: 78) + Left: + Literal(Type: 34, Value: 234) + Right: + Literal(Type: 34, Value: 345) + ) + Function( + Name: + main + Returns: + Type(TKN_VOID*) + Params: + Param: a + Type(TKN_INT*) + Param: b + Type(TKN_INT**) + Statements: + VarDecl(a): + Type(TKN_INT) + Value: + BinaryOp(Op: 72) + Left: + Literal(Type: 33, Value: 1) + Right: + BinaryOp(Op: 72) + Left: + Literal(Type: 33, Value: 2) + Right: + BinaryOp(Op: 72) + Left: + BinaryOp(Op: 78) + Left: + Literal(Type: 33, Value: 3) + Right: + Literal(Type: 33, Value: 4) + Right: + BinaryOp(Op: 72) + Left: + Literal(Type: 33, Value: 5) + Right: + Literal(Type: 33, Value: 6) + VarDecl(b): + Type(TKN_INT) + Value: + BinaryOp(Op: 72) + Left: + Literal(Type: 33, Value: 234) + Right: + BinaryOp(Op: 72) + Left: + Literal(Type: 33, Value: 2) + Right: + BinaryOp(Op: 72) + Left: + Literal(Type: 33, Value: 23) + Right: + Literal(Type: 33, Value: 1) + Assign(b): + Value: + BinaryOp(Op: 72) + Left: + Literal(Type: 33, Value: 2) + Right: + BinaryOp(Op: 72) + Left: + Literal(Type: 33, Value: 3) + Right: + BinaryOp(Op: 72) + Left: + Literal(Type: 33, Value: 4) + Right: + Literal(Type: 33, Value: 5) + Assign(a): + Value: + BinaryOp(Op: 72) + Left: + FunctionCall(Name: hey) + Args: + Right: + BinaryOp(Op: 72) + Left: + Literal(Type: 33, Value: ab) + Right: + BinaryOp(Op: 72) + Left: + Literal(Type: 33, Value: cd) + Right: + FunctionCall(Name: func) + Args: + FunctionCall(Name: salam) + Args: + CompoundStatement + Assign(a): + Value: + Literal(Type: 33, Value: salam) + Literal(Type: 33, Value: khubi) + CompoundStatement + Literal(Type: 33, Value: chetori) + VarDecl(a): + Type(TKN_INT) + Value: + Literal(Type: 33, Value: haha) + CompoundStatement + CompoundStatement + FunctionCall(Name: salam) + Args: + Literal(Type: 33, Value: a) + Literal(Type: 33, Value: b) + FunctionCall(Name: ggg) + Args: + Literal(Type: 33, Value: d) + ) +) diff --git a/tests/output/inp3.c_asm_output.txt b/tests/output/inp3.c_asm_output.txt new file mode 100644 index 0000000..8802ae7 --- /dev/null +++ b/tests/output/inp3.c_asm_output.txt @@ -0,0 +1,617 @@ +section .data +__printf_size: equ 16 +__doub_size: equ 32 +__temp_str_2 db `Fact: %u\n`, 0 +__fact_size: equ 80 +__fibo_size: equ 96 +__temp_str_5 db `I: %u\n`, 0 +__temp_str_6 db `%c %c %u`, 0 +__temp_str_7 db `\n`, 0 +__temp_str_8 db `Result: %u\n`, 0 +__temp_str_9 db `Addr of a: %u\n`, 0 +__temp_str_10 db `Value of b: %u\n`, 0 +__temp_str_11 db `Addr of c: %u\n`, 0 +__temp_str_12 db `Double of 123: %u\n`, 0 +__temp_str_13 db `Factorial 5: %u\n`, 0 +__temp_str_14 db `TRUE\n`, 0 +__temp_str_15 db `FALSE\n`, 0 +__temp_str_16 db `TRUE\n`, 0 +__temp_str_17 db `FALSE\n`, 0 +__temp_str_18 db `%u\n`, 0 +__temp_str_19 db `%u `, 0 +__temp_str_20 db `%u `, 0 +__temp_str_21 db `%u `, 0 +__temp_str_22 db `%u `, 0 +__temp_str_23 db `%u `, 0 +__temp_str_24 db `%u `, 0 +__temp_str_25 db `%u `, 0 +__temp_str_26 db `%u `, 0 +__temp_str_27 db `%u\n`, 0 +__temp_str_28 db `%u `, 0 +__temp_str_29 db `\nDONE!\n`, 0 +__temp_str_30 db `%u\n`, 0 +__main_size: equ 1040 +section .text +extern printf +global doub +doub: +push rbp +mov rbp, rsp +sub rsp, __doub_size +mov [rsp+0], rdi +mov rax, [rsp + 0] +mov rbx, 2 +mul rbx +mov [rsp + 8], rax +mov rax, [rsp + 8] +mov rsp, rbp +pop rbp +ret +mov rsp, rbp +pop rbp +ret +global fact +fact: +push rbp +mov rbp, rsp +sub rsp, __fact_size +mov [rsp+0], rdi +mov rax, __temp_str_2 +mov [rsp + 8], rax +mov rax, [rsp + 0] +mov [rsp + 16], rax +mov rdi, [rsp + 8] +mov rsi, [rsp + 16] +call printf +mov [rsp + 24], rax +mov rax, 0 +cmp rax, [rsp + 0] +je __tmp_label_0 +mov rax, [rsp + 0] +mov rbx, 1 +sub rax, rbx +mov [rsp + 32], rax +mov rax, [rsp + 32] +mov [rsp + 40], rax +mov rdi, [rsp + 40] +call fact +mov [rsp + 48], rax +mov rax, [rsp + 0] +mov rbx, [rsp + 48] +mul rbx +mov [rsp + 56], rax +mov rax, [rsp + 56] +mov rsp, rbp +pop rbp +ret +jmp __tmp_label_1 +__tmp_label_0: +mov rax, 1 +mov rsp, rbp +pop rbp +ret +__tmp_label_1: +mov rsp, rbp +pop rbp +ret +global fibo +fibo: +push rbp +mov rbp, rsp +sub rsp, __fibo_size +mov [rsp+0], rdi +mov rax, [rsp + 0] +mov rbx, 0 +cmp rax, rbx +je __tmp_label_2 +mov rax, 0 +jmp __tmp_label_3 +__tmp_label_2: +mov rax, 1 +__tmp_label_3: +mov [rsp + 8], rax +mov rax, 0 +cmp rax, [rsp + 8] +je __tmp_label_4 +mov rax, 1 +mov rsp, rbp +pop rbp +ret +jmp __tmp_label_5 +__tmp_label_4: +mov rax, [rsp + 0] +mov rbx, 1 +cmp rax, rbx +je __tmp_label_6 +mov rax, 0 +jmp __tmp_label_7 +__tmp_label_6: +mov rax, 1 +__tmp_label_7: +mov [rsp + 16], rax +mov rax, 0 +cmp rax, [rsp + 16] +je __tmp_label_8 +mov rax, 1 +mov rsp, rbp +pop rbp +ret +jmp __tmp_label_9 +__tmp_label_8: +mov rax, [rsp + 0] +mov rbx, 1 +sub rax, rbx +mov [rsp + 24], rax +mov rax, [rsp + 24] +mov [rsp + 32], rax +mov rdi, [rsp + 32] +call fibo +mov [rsp + 40], rax +mov rax, [rsp + 0] +mov rbx, 2 +sub rax, rbx +mov [rsp + 48], rax +mov rax, [rsp + 48] +mov [rsp + 56], rax +mov rdi, [rsp + 56] +call fibo +mov [rsp + 64], rax +mov rax, [rsp + 40] +mov rbx, [rsp + 64] +add rax, rbx +mov [rsp + 72], rax +mov rax, [rsp + 72] +mov rsp, rbp +pop rbp +ret +__tmp_label_9: +__tmp_label_5: +mov rsp, rbp +pop rbp +ret +global main +main: +push rbp +mov rbp, rsp +sub rsp, __main_size +mov rax, 0 +mov [rsp+0], rax +__tmp_label_10: +mov rax, [rsp + 0] +mov rbx, 10 +cmp rax, rbx +jl __tmp_label_12 +mov rax, 0 +jmp __tmp_label_13 +__tmp_label_12: +mov rax, 1 +__tmp_label_13: +mov [rsp + 8], rax +mov rax, [rsp + 8] +cmp rax, 0 +je __tmp_label_11 +mov rax, __temp_str_5 +mov [rsp + 16], rax +mov rax, [rsp + 0] +mov [rsp + 24], rax +mov rdi, [rsp + 16] +mov rsi, [rsp + 24] +call printf +mov [rsp + 32], rax +mov rax, [rsp + 0] +mov rbx, 1 +add rax, rbx +mov [rsp + 40], rax +mov rax, [rsp + 40] +mov [rsp+0], rax +jmp __tmp_label_10 +__tmp_label_11: +mov rax, __temp_str_6 +mov [rsp + 16], rax +mov rax, 97 +mov [rsp + 24], rax +mov rax, 110 +mov [rsp + 32], rax +mov rax, 99 +mov [rsp + 40], rax +mov rdi, [rsp + 16] +mov rsi, [rsp + 24] +mov rdx, [rsp + 32] +mov rcx, [rsp + 40] +call printf +mov [rsp + 48], rax +mov rax, __temp_str_7 +mov [rsp + 56], rax +mov rdi, [rsp + 56] +call printf +mov [rsp + 64], rax +mov rax, 10 +mov [rsp+72], rax +mov rax, 20 +mov [rsp+80], rax +mov rax, [rsp + 72] +mov rbx, [rsp + 80] +mul rbx +mov [rsp + 96], rax +mov rax, [rsp + 96] +mov [rsp+88], rax +mov rax, [rsp + 88] +mov rbx, [rsp + 72] +add rax, rbx +mov [rsp + 112], rax +mov rax, [rsp + 88] +mov rbx, [rsp + 112] +mul rbx +mov [rsp + 120], rax +mov rax, [rsp + 120] +mov [rsp+104], rax +mov rax, __temp_str_8 +mov [rsp + 128], rax +mov rax, [rsp + 80] +mov rbx, [rsp + 72] +add rax, rbx +mov [rsp + 136], rax +mov rax, [rsp + 136] +mov rbx, [rsp + 72] +mul rbx +mov [rsp + 144], rax +mov rax, [rsp + 104] +mov rbx, [rsp + 144] +add rax, rbx +mov [rsp + 152], rax +mov rax, [rsp + 88] +mov rbx, [rsp + 152] +add rax, rbx +mov [rsp + 160], rax +mov rax, [rsp + 88] +mov rbx, [rsp + 160] +add rax, rbx +mov [rsp + 168], rax +mov rax, [rsp + 168] +mov [rsp + 176], rax +mov rdi, [rsp + 128] +mov rsi, [rsp + 176] +call printf +mov [rsp + 184], rax +mov rax, rsp +add rax, 88 +mov rax, rax +mov [rsp+192], rax +mov rax, __temp_str_9 +mov [rsp + 200], rax +mov rax, rsp +add rax, 72 +mov rax, rax +mov [rsp + 208], rax +mov rdi, [rsp + 200] +mov rsi, [rsp + 208] +call printf +mov [rsp + 216], rax +mov rax, __temp_str_10 +mov [rsp + 224], rax +mov rax, rsp +add rax, 72 +mov rax, rax +mov rbx, 8 +add rax, rbx +mov [rsp + 232], rax +mov rax, [rsp + 232] +mov rax, [rax] +mov rax, rax +mov [rsp + 240], rax +mov rdi, [rsp + 224] +mov rsi, [rsp + 240] +call printf +mov [rsp + 248], rax +mov rax, __temp_str_11 +mov [rsp + 256], rax +mov rax, [rsp + 192] +mov [rsp + 264], rax +mov rdi, [rsp + 256] +mov rsi, [rsp + 264] +call printf +mov [rsp + 272], rax +mov rax, __temp_str_12 +mov [rsp + 280], rax +mov rax, 123 +mov [rsp + 288], rax +mov rdi, [rsp + 288] +call doub +mov [rsp + 296], rax +mov rax, 123 +mov rbx, [rsp + 296] +mul rbx +mov [rsp + 304], rax +mov rax, [rsp + 304] +mov [rsp + 312], rax +mov rdi, [rsp + 312] +call doub +mov [rsp + 320], rax +mov rax, [rsp + 320] +mov rbx, 2 +mul rbx +mov [rsp + 328], rax +mov rax, [rsp + 328] +mov [rsp + 336], rax +mov rdi, [rsp + 336] +call doub +mov [rsp + 344], rax +mov rax, [rsp + 344] +mov rbx, 10 +add rax, rbx +mov [rsp + 352], rax +mov rax, [rsp + 352] +mov [rsp + 360], rax +mov rdi, [rsp + 280] +mov rsi, [rsp + 360] +call printf +mov [rsp + 368], rax +mov rax, __temp_str_13 +mov [rsp + 376], rax +mov rax, 5 +mov [rsp + 384], rax +mov rdi, [rsp + 384] +call fact +mov [rsp + 392], rax +mov rax, [rsp + 392] +mov [rsp + 400], rax +mov rdi, [rsp + 376] +mov rsi, [rsp + 400] +call printf +mov [rsp + 408], rax +mov rax, 0 +cmp rax, 1 +je __tmp_label_14 +mov rax, __temp_str_14 +mov [rsp + 416], rax +mov rdi, [rsp + 416] +call printf +mov [rsp + 424], rax +jmp __tmp_label_15 +__tmp_label_14: +mov rax, __temp_str_15 +mov [rsp + 416], rax +mov rdi, [rsp + 416] +call printf +mov [rsp + 424], rax +__tmp_label_15: +mov rax, 0 +cmp rax, 1 +je __tmp_label_16 +mov rax, __temp_str_16 +mov [rsp + 416], rax +mov rdi, [rsp + 416] +call printf +mov [rsp + 424], rax +jmp __tmp_label_17 +__tmp_label_16: +mov rax, __temp_str_17 +mov [rsp + 416], rax +mov rdi, [rsp + 416] +call printf +mov [rsp + 424], rax +__tmp_label_17: +mov rax, __temp_str_18 +mov [rsp + 416], rax +mov rax, 5 +mov rbx, 1 +cmp rax, rbx +jne __tmp_label_18 +mov rax, 0 +jmp __tmp_label_19 +__tmp_label_18: +mov rax, 1 +__tmp_label_19: +mov [rsp + 424], rax +mov rax, [rsp + 424] +mov [rsp + 432], rax +mov rdi, [rsp + 416] +mov rsi, [rsp + 432] +call printf +mov [rsp + 440], rax +mov rax, __temp_str_19 +mov [rsp + 448], rax +mov rax, 0 +mov [rsp + 456], rax +mov rdi, [rsp + 456] +call fibo +mov [rsp + 464], rax +mov rax, [rsp + 464] +mov [rsp + 472], rax +mov rdi, [rsp + 448] +mov rsi, [rsp + 472] +call printf +mov [rsp + 480], rax +mov rax, __temp_str_20 +mov [rsp + 488], rax +mov rax, 1 +mov [rsp + 496], rax +mov rdi, [rsp + 496] +call fibo +mov [rsp + 504], rax +mov rax, [rsp + 504] +mov [rsp + 512], rax +mov rdi, [rsp + 488] +mov rsi, [rsp + 512] +call printf +mov [rsp + 520], rax +mov rax, __temp_str_21 +mov [rsp + 528], rax +mov rax, 2 +mov [rsp + 536], rax +mov rdi, [rsp + 536] +call fibo +mov [rsp + 544], rax +mov rax, [rsp + 544] +mov [rsp + 552], rax +mov rdi, [rsp + 528] +mov rsi, [rsp + 552] +call printf +mov [rsp + 560], rax +mov rax, __temp_str_22 +mov [rsp + 568], rax +mov rax, 3 +mov [rsp + 576], rax +mov rdi, [rsp + 576] +call fibo +mov [rsp + 584], rax +mov rax, [rsp + 584] +mov [rsp + 592], rax +mov rdi, [rsp + 568] +mov rsi, [rsp + 592] +call printf +mov [rsp + 600], rax +mov rax, __temp_str_23 +mov [rsp + 608], rax +mov rax, 4 +mov [rsp + 616], rax +mov rdi, [rsp + 616] +call fibo +mov [rsp + 624], rax +mov rax, [rsp + 624] +mov [rsp + 632], rax +mov rdi, [rsp + 608] +mov rsi, [rsp + 632] +call printf +mov [rsp + 640], rax +mov rax, __temp_str_24 +mov [rsp + 648], rax +mov rax, 5 +mov [rsp + 656], rax +mov rdi, [rsp + 656] +call fibo +mov [rsp + 664], rax +mov rax, [rsp + 664] +mov [rsp + 672], rax +mov rdi, [rsp + 648] +mov rsi, [rsp + 672] +call printf +mov [rsp + 680], rax +mov rax, __temp_str_25 +mov [rsp + 688], rax +mov rax, 6 +mov [rsp + 696], rax +mov rdi, [rsp + 696] +call fibo +mov [rsp + 704], rax +mov rax, [rsp + 704] +mov [rsp + 712], rax +mov rdi, [rsp + 688] +mov rsi, [rsp + 712] +call printf +mov [rsp + 720], rax +mov rax, __temp_str_26 +mov [rsp + 728], rax +mov rax, 7 +mov [rsp + 736], rax +mov rdi, [rsp + 736] +call fibo +mov [rsp + 744], rax +mov rax, [rsp + 744] +mov [rsp + 752], rax +mov rdi, [rsp + 728] +mov rsi, [rsp + 752] +call printf +mov [rsp + 760], rax +mov rax, __temp_str_27 +mov [rsp + 768], rax +mov rax, 8 +mov [rsp + 776], rax +mov rdi, [rsp + 776] +call fibo +mov [rsp + 784], rax +mov rax, [rsp + 784] +mov [rsp + 792], rax +mov rdi, [rsp + 768] +mov rsi, [rsp + 792] +call printf +mov [rsp + 800], rax +mov rax, 0 +mov [rsp+808], rax +__tmp_label_20: +mov rax, [rsp + 808] +mov rbx, 20 +cmp rax, rbx +jl __tmp_label_22 +mov rax, 0 +jmp __tmp_label_23 +__tmp_label_22: +mov rax, 1 +__tmp_label_23: +mov [rsp + 816], rax +mov rax, [rsp + 816] +cmp rax, 0 +je __tmp_label_21 +mov rax, __temp_str_28 +mov [rsp + 824], rax +mov rax, [rsp + 808] +mov [rsp + 832], rax +mov rdi, [rsp + 832] +call fibo +mov [rsp + 840], rax +mov rax, [rsp + 840] +mov [rsp + 848], rax +mov rdi, [rsp + 824] +mov rsi, [rsp + 848] +call printf +mov [rsp + 856], rax +mov rax, [rsp + 808] +mov rbx, 1 +add rax, rbx +mov [rsp + 824], rax +mov rax, [rsp + 824] +mov [rsp+808], rax +jmp __tmp_label_20 +__tmp_label_21: +mov rax, __temp_str_29 +mov [rsp + 808], rax +mov rdi, [rsp + 808] +call printf +mov [rsp + 816], rax +mov rax, __temp_str_30 +mov [rsp + 824], rax +mov rax, 1 +mov rbx, 1 +cmp rax, 0 +je __tmp_label_24 +mov rax, rbx +jmp __tmp_label_25 +__tmp_label_24: +mov rax, 0 +__tmp_label_25: +mov [rsp + 832], rax +mov rax, 1 +mov rbx, 1 +cmp rax, 0 +je __tmp_label_26 +mov rax, rbx +jmp __tmp_label_27 +__tmp_label_26: +mov rax, 0 +__tmp_label_27: +mov [rsp + 840], rax +mov rax, [rsp + 832] +mov rbx, [rsp + 840] +cmp rax, 0 +je __tmp_label_28 +mov rax, rbx +jmp __tmp_label_29 +__tmp_label_28: +mov rax, 0 +__tmp_label_29: +mov [rsp + 848], rax +mov rax, [rsp + 848] +mov [rsp + 856], rax +mov rdi, [rsp + 824] +mov rsi, [rsp + 856] +call printf +mov [rsp + 864], rax +mov rsp, rbp +pop rbp +ret +extern exit +global _start +_start: +call main +mov rdi, 0 +call exit diff --git a/tests/output/inp3.c_lex_output.txt b/tests/output/inp3.c_lex_output.txt new file mode 100644 index 0000000..4c42e69 --- /dev/null +++ b/tests/output/inp3.c_lex_output.txt @@ -0,0 +1,518 @@ +TKN_MACRO_DEFINE +TKN_ID(SALAM2) +TKN_LIT_INT(110) +TKN_CHAR +TKN_ID(c) +TKN_ASSIGN +TKN_LIT_CHAR( +) +TKN_SEMICOLON +TKN_VOID +TKN_ID(printf) +TKN_L_PAREN +TKN_CHAR +TKN_STAR +TKN_COMMA +TKN_DOTS +TKN_R_PAREN +TKN_SEMICOLON +TKN_LIT_STRUCT +TKN_ID(MyStruct) +TKN_L_BRACE +TKN_INT +TKN_STAR +TKN_STAR +TKN_ID(a) +TKN_L_BRACK +TKN_LIT_INT(10) +TKN_R_BRACK +TKN_L_BRACK +TKN_ID(SALAM2) +TKN_R_BRACK +TKN_L_BRACK +TKN_LIT_INT(30) +TKN_R_BRACK +TKN_SEMICOLON +TKN_CHAR +TKN_ID(b) +TKN_SEMICOLON +TKN_INT +TKN_ID(c) +TKN_SEMICOLON +TKN_R_BRACE +TKN_SEMICOLON +TKN_INT +TKN_ID(salam) +TKN_ASSIGN +TKN_LIT_INT(52) +TKN_SEMICOLON +TKN_INT +TKN_ID(doub) +TKN_L_PAREN +TKN_INT +TKN_ID(k) +TKN_R_PAREN +TKN_L_BRACE +TKN_RETURN +TKN_ID(k) +TKN_STAR +TKN_LIT_INT(2) +TKN_SEMICOLON +TKN_R_BRACE +TKN_INT +TKN_ID(fact) +TKN_L_PAREN +TKN_INT +TKN_ID(a) +TKN_R_PAREN +TKN_L_BRACE +TKN_ID(printf) +TKN_L_PAREN +TKN_LIT_STR(Fact: %u +) +TKN_COMMA +TKN_ID(a) +TKN_R_PAREN +TKN_SEMICOLON +TKN_IF +TKN_L_PAREN +TKN_ID(a) +TKN_R_PAREN +TKN_L_BRACE +TKN_RETURN +TKN_ID(a) +TKN_STAR +TKN_ID(fact) +TKN_L_PAREN +TKN_ID(a) +TKN_MIN +TKN_LIT_INT(1) +TKN_R_PAREN +TKN_SEMICOLON +TKN_R_BRACE +TKN_ELSE +TKN_L_BRACE +TKN_RETURN +TKN_LIT_INT(1) +TKN_SEMICOLON +TKN_R_BRACE +TKN_R_BRACE +TKN_INT +TKN_ID(fibo) +TKN_L_PAREN +TKN_INT +TKN_ID(n) +TKN_R_PAREN +TKN_L_BRACE +TKN_IF +TKN_L_PAREN +TKN_ID(n) +TKN_EQ +TKN_LIT_INT(0) +TKN_R_PAREN +TKN_L_BRACE +TKN_RETURN +TKN_LIT_INT(1) +TKN_SEMICOLON +TKN_R_BRACE +TKN_ELSE +TKN_IF +TKN_L_PAREN +TKN_ID(n) +TKN_EQ +TKN_LIT_INT(1) +TKN_R_PAREN +TKN_L_BRACE +TKN_RETURN +TKN_LIT_INT(1) +TKN_SEMICOLON +TKN_R_BRACE +TKN_ELSE +TKN_L_BRACE +TKN_RETURN +TKN_ID(fibo) +TKN_L_PAREN +TKN_ID(n) +TKN_MIN +TKN_LIT_INT(1) +TKN_R_PAREN +TKN_PLUS +TKN_ID(fibo) +TKN_L_PAREN +TKN_ID(n) +TKN_MIN +TKN_LIT_INT(2) +TKN_R_PAREN +TKN_SEMICOLON +TKN_R_BRACE +TKN_R_BRACE +TKN_INT +TKN_ID(main) +TKN_L_PAREN +TKN_R_PAREN +TKN_L_BRACE +TKN_INT +TKN_ID(i2) +TKN_ASSIGN +TKN_LIT_INT(0) +TKN_SEMICOLON +TKN_WHILE +TKN_L_PAREN +TKN_ID(i2) +TKN_LT +TKN_LIT_INT(10) +TKN_R_PAREN +TKN_L_BRACE +TKN_ID(printf) +TKN_L_PAREN +TKN_LIT_STR(I: %u +) +TKN_COMMA +TKN_ID(i2) +TKN_R_PAREN +TKN_SEMICOLON +TKN_ID(i2) +TKN_ASSIGN +TKN_ID(i2) +TKN_PLUS +TKN_LIT_INT(1) +TKN_SEMICOLON +TKN_R_BRACE +TKN_ID(printf) +TKN_L_PAREN +TKN_LIT_STR(%c %c %u) +TKN_COMMA +TKN_LIT_CHAR(a) +TKN_COMMA +TKN_ID(SALAM2) +TKN_COMMA +TKN_LIT_CHAR(c) +TKN_R_PAREN +TKN_SEMICOLON +TKN_ID(printf) +TKN_L_PAREN +TKN_LIT_STR( +) +TKN_R_PAREN +TKN_SEMICOLON +TKN_INT +TKN_ID(a) +TKN_ASSIGN +TKN_LIT_INT(10) +TKN_SEMICOLON +TKN_INT +TKN_ID(b) +TKN_ASSIGN +TKN_LIT_INT(20) +TKN_SEMICOLON +TKN_INT +TKN_ID(c) +TKN_ASSIGN +TKN_ID(a) +TKN_STAR +TKN_ID(b) +TKN_SEMICOLON +TKN_INT +TKN_ID(d) +TKN_ASSIGN +TKN_ID(c) +TKN_STAR +TKN_L_PAREN +TKN_ID(c) +TKN_PLUS +TKN_ID(a) +TKN_R_PAREN +TKN_SEMICOLON +TKN_ID(printf) +TKN_L_PAREN +TKN_LIT_STR(Result: %u +) +TKN_COMMA +TKN_ID(c) +TKN_PLUS +TKN_ID(c) +TKN_PLUS +TKN_ID(d) +TKN_PLUS +TKN_L_PAREN +TKN_L_PAREN +TKN_ID(b) +TKN_PLUS +TKN_ID(a) +TKN_R_PAREN +TKN_STAR +TKN_ID(a) +TKN_R_PAREN +TKN_R_PAREN +TKN_SEMICOLON +TKN_INT +TKN_STAR +TKN_ID(cc) +TKN_ASSIGN +TKN_AND +TKN_ID(c) +TKN_SEMICOLON +TKN_ID(printf) +TKN_L_PAREN +TKN_LIT_STR(Addr of a: %u +) +TKN_COMMA +TKN_AND +TKN_ID(a) +TKN_R_PAREN +TKN_SEMICOLON +TKN_ID(printf) +TKN_L_PAREN +TKN_LIT_STR(Value of b: %u +) +TKN_COMMA +TKN_STAR +TKN_L_PAREN +TKN_AND +TKN_ID(a) +TKN_PLUS +TKN_LIT_INT(8) +TKN_R_PAREN +TKN_R_PAREN +TKN_SEMICOLON +TKN_ID(printf) +TKN_L_PAREN +TKN_LIT_STR(Addr of c: %u +) +TKN_COMMA +TKN_ID(cc) +TKN_R_PAREN +TKN_SEMICOLON +TKN_ID(printf) +TKN_L_PAREN +TKN_LIT_STR(Double of 123: %u +) +TKN_COMMA +TKN_ID(doub) +TKN_L_PAREN +TKN_ID(doub) +TKN_L_PAREN +TKN_LIT_INT(123) +TKN_STAR +TKN_ID(doub) +TKN_L_PAREN +TKN_LIT_INT(123) +TKN_R_PAREN +TKN_R_PAREN +TKN_STAR +TKN_LIT_INT(2) +TKN_R_PAREN +TKN_PLUS +TKN_LIT_INT(10) +TKN_R_PAREN +TKN_SEMICOLON +TKN_ID(printf) +TKN_L_PAREN +TKN_LIT_STR(Factorial 5: %u +) +TKN_COMMA +TKN_ID(fact) +TKN_L_PAREN +TKN_LIT_INT(5) +TKN_R_PAREN +TKN_R_PAREN +TKN_SEMICOLON +TKN_IF +TKN_L_PAREN +TKN_LIT_INT(1) +TKN_R_PAREN +TKN_L_BRACE +TKN_ID(printf) +TKN_L_PAREN +TKN_LIT_STR(TRUE +) +TKN_R_PAREN +TKN_SEMICOLON +TKN_R_BRACE +TKN_ELSE +TKN_L_BRACE +TKN_ID(printf) +TKN_L_PAREN +TKN_LIT_STR(FALSE +) +TKN_R_PAREN +TKN_SEMICOLON +TKN_R_BRACE +TKN_IF +TKN_L_PAREN +TKN_LIT_INT(1) +TKN_R_PAREN +TKN_L_BRACE +TKN_ID(printf) +TKN_L_PAREN +TKN_LIT_STR(TRUE +) +TKN_R_PAREN +TKN_SEMICOLON +TKN_R_BRACE +TKN_ELSE +TKN_L_BRACE +TKN_ID(printf) +TKN_L_PAREN +TKN_LIT_STR(FALSE +) +TKN_R_PAREN +TKN_SEMICOLON +TKN_R_BRACE +TKN_ID(printf) +TKN_L_PAREN +TKN_LIT_STR(%u +) +TKN_COMMA +TKN_LIT_INT(5) +TKN_NEQ +TKN_LIT_INT(1) +TKN_R_PAREN +TKN_SEMICOLON +TKN_ID(printf) +TKN_L_PAREN +TKN_LIT_STR(%u ) +TKN_COMMA +TKN_ID(fibo) +TKN_L_PAREN +TKN_LIT_INT(0) +TKN_R_PAREN +TKN_R_PAREN +TKN_SEMICOLON +TKN_ID(printf) +TKN_L_PAREN +TKN_LIT_STR(%u ) +TKN_COMMA +TKN_ID(fibo) +TKN_L_PAREN +TKN_LIT_INT(1) +TKN_R_PAREN +TKN_R_PAREN +TKN_SEMICOLON +TKN_ID(printf) +TKN_L_PAREN +TKN_LIT_STR(%u ) +TKN_COMMA +TKN_ID(fibo) +TKN_L_PAREN +TKN_LIT_INT(2) +TKN_R_PAREN +TKN_R_PAREN +TKN_SEMICOLON +TKN_ID(printf) +TKN_L_PAREN +TKN_LIT_STR(%u ) +TKN_COMMA +TKN_ID(fibo) +TKN_L_PAREN +TKN_LIT_INT(3) +TKN_R_PAREN +TKN_R_PAREN +TKN_SEMICOLON +TKN_ID(printf) +TKN_L_PAREN +TKN_LIT_STR(%u ) +TKN_COMMA +TKN_ID(fibo) +TKN_L_PAREN +TKN_LIT_INT(4) +TKN_R_PAREN +TKN_R_PAREN +TKN_SEMICOLON +TKN_ID(printf) +TKN_L_PAREN +TKN_LIT_STR(%u ) +TKN_COMMA +TKN_ID(fibo) +TKN_L_PAREN +TKN_LIT_INT(5) +TKN_R_PAREN +TKN_R_PAREN +TKN_SEMICOLON +TKN_ID(printf) +TKN_L_PAREN +TKN_LIT_STR(%u ) +TKN_COMMA +TKN_ID(fibo) +TKN_L_PAREN +TKN_LIT_INT(6) +TKN_R_PAREN +TKN_R_PAREN +TKN_SEMICOLON +TKN_ID(printf) +TKN_L_PAREN +TKN_LIT_STR(%u ) +TKN_COMMA +TKN_ID(fibo) +TKN_L_PAREN +TKN_LIT_INT(7) +TKN_R_PAREN +TKN_R_PAREN +TKN_SEMICOLON +TKN_ID(printf) +TKN_L_PAREN +TKN_LIT_STR(%u +) +TKN_COMMA +TKN_ID(fibo) +TKN_L_PAREN +TKN_LIT_INT(8) +TKN_R_PAREN +TKN_R_PAREN +TKN_SEMICOLON +TKN_FOR +TKN_L_PAREN +TKN_INT +TKN_ID(i) +TKN_ASSIGN +TKN_LIT_INT(0) +TKN_SEMICOLON +TKN_ID(i) +TKN_LT +TKN_LIT_INT(20) +TKN_SEMICOLON +TKN_ID(i) +TKN_ASSIGN +TKN_ID(i) +TKN_PLUS +TKN_LIT_INT(1) +TKN_R_PAREN +TKN_L_BRACE +TKN_ID(printf) +TKN_L_PAREN +TKN_LIT_STR(%u ) +TKN_COMMA +TKN_ID(fibo) +TKN_L_PAREN +TKN_ID(i) +TKN_R_PAREN +TKN_R_PAREN +TKN_SEMICOLON +TKN_R_BRACE +TKN_ID(printf) +TKN_L_PAREN +TKN_LIT_STR( +DONE! +) +TKN_R_PAREN +TKN_SEMICOLON +TKN_ID(printf) +TKN_L_PAREN +TKN_LIT_STR(%u +) +TKN_COMMA +TKN_L_PAREN +TKN_LIT_INT(1) +TKN_ANDAND +TKN_LIT_INT(1) +TKN_R_PAREN +TKN_ANDAND +TKN_L_PAREN +TKN_LIT_INT(1) +TKN_ANDAND +TKN_LIT_INT(1) +TKN_R_PAREN +TKN_R_PAREN +TKN_SEMICOLON +TKN_R_BRACE +TKN_EOF diff --git a/tests/output/inp3.c_tree_output.txt b/tests/output/inp3.c_tree_output.txt new file mode 100644 index 0000000..b73ab03 --- /dev/null +++ b/tests/output/inp3.c_tree_output.txt @@ -0,0 +1,371 @@ +Program( + FunctionDecl( + Name: + printf + Returns: + Type(TKN_VOID) + ParamTypes: + Type(TKN_CHAR*) + ) + Function( + Name: + doub + Returns: + Type(TKN_INT) + Params: + Param: k + Type(TKN_INT) + Statements: + Return: + BinaryOp(Op: 78) + Left: + Variable(k) + Right: + Literal(Type: 34, Value: 2) + ) + Function( + Name: + fact + Returns: + Type(TKN_INT) + Params: + Param: a + Type(TKN_INT) + Statements: + FunctionCall(Name: printf) + Args: + Literal(Type: 33, Value: Fact: %u +) + Variable(a) + If( + Cond: + Variable(a) + Body: + CompoundStatement + Return: + BinaryOp(Op: 78) + Left: + Variable(a) + Right: + FunctionCall(Name: fact) + Args: + BinaryOp(Op: 75) + Left: + Variable(a) + Right: + Literal(Type: 34, Value: 1) + ) + Function( + Name: + fibo + Returns: + Type(TKN_INT) + Params: + Param: n + Type(TKN_INT) + Statements: + If( + Cond: + BinaryOp(Op: 65) + Left: + Variable(n) + Right: + Literal(Type: 34, Value: 0) + Body: + CompoundStatement + Return: + Literal(Type: 34, Value: 1) + ) + Function( + Name: + main + Returns: + Type(TKN_INT) + Params: + Statements: + VarDecl(i2): + Type(TKN_INT) + Value: + Literal(Type: 34, Value: 0) + While( + Cond: + BinaryOp(Op: 66) + Left: + Variable(i2) + Right: + Literal(Type: 34, Value: 10) + Body: + CompoundStatement + FunctionCall(Name: printf) + Args: + Literal(Type: 33, Value: I: %u +) + Variable(i2) + Assign(i2): + Value: + BinaryOp(Op: 72) + Left: + Variable(i2) + Right: + Literal(Type: 34, Value: 1) + FunctionCall(Name: printf) + Args: + Literal(Type: 33, Value: %c %c %u) + Literal(Type: 35) + Literal(Type: 34, Value: 110) + Literal(Type: 35) + FunctionCall(Name: printf) + Args: + Literal(Type: 33, Value: +) + VarDecl(a): + Type(TKN_INT) + Value: + Literal(Type: 34, Value: 10) + VarDecl(b): + Type(TKN_INT) + Value: + Literal(Type: 34, Value: 20) + VarDecl(c): + Type(TKN_INT) + Value: + BinaryOp(Op: 78) + Left: + Variable(a) + Right: + Variable(b) + VarDecl(d): + Type(TKN_INT) + Value: + BinaryOp(Op: 78) + Left: + Variable(c) + Right: + BinaryOp(Op: 72) + Left: + Variable(c) + Right: + Variable(a) + FunctionCall(Name: printf) + Args: + Literal(Type: 33, Value: Result: %u +) + BinaryOp(Op: 72) + Left: + Variable(c) + Right: + BinaryOp(Op: 72) + Left: + Variable(c) + Right: + BinaryOp(Op: 72) + Left: + Variable(d) + Right: + BinaryOp(Op: 78) + Left: + BinaryOp(Op: 72) + Left: + Variable(b) + Right: + Variable(a) + Right: + Variable(a) + VarDecl(cc): + Type(TKN_INT*) + Value: + Ref: + Variable(c) + FunctionCall(Name: printf) + Args: + Literal(Type: 33, Value: Addr of a: %u +) + Ref: + Variable(a) + FunctionCall(Name: printf) + Args: + Literal(Type: 33, Value: Value of b: %u +) + Deref: + BinaryOp(Op: 72) + Left: + Ref: + Variable(a) + Right: + Literal(Type: 34, Value: 8) + FunctionCall(Name: printf) + Args: + Literal(Type: 33, Value: Addr of c: %u +) + Variable(cc) + FunctionCall(Name: printf) + Args: + Literal(Type: 33, Value: Double of 123: %u +) + BinaryOp(Op: 72) + Left: + FunctionCall(Name: doub) + Args: + BinaryOp(Op: 78) + Left: + FunctionCall(Name: doub) + Args: + BinaryOp(Op: 78) + Left: + Literal(Type: 34, Value: 123) + Right: + FunctionCall(Name: doub) + Args: + Literal(Type: 34, Value: 123) + Right: + Literal(Type: 34, Value: 2) + Right: + Literal(Type: 34, Value: 10) + FunctionCall(Name: printf) + Args: + Literal(Type: 33, Value: Factorial 5: %u +) + FunctionCall(Name: fact) + Args: + Literal(Type: 34, Value: 5) + If( + Cond: + Literal(Type: 34, Value: 1) + Body: + CompoundStatement + FunctionCall(Name: printf) + Args: + Literal(Type: 33, Value: TRUE +) + If( + Cond: + Literal(Type: 34, Value: 1) + Body: + CompoundStatement + FunctionCall(Name: printf) + Args: + Literal(Type: 33, Value: TRUE +) + FunctionCall(Name: printf) + Args: + Literal(Type: 33, Value: %u +) + BinaryOp(Op: 71) + Left: + Literal(Type: 34, Value: 5) + Right: + Literal(Type: 34, Value: 1) + FunctionCall(Name: printf) + Args: + Literal(Type: 33, Value: %u ) + FunctionCall(Name: fibo) + Args: + Literal(Type: 34, Value: 0) + FunctionCall(Name: printf) + Args: + Literal(Type: 33, Value: %u ) + FunctionCall(Name: fibo) + Args: + Literal(Type: 34, Value: 1) + FunctionCall(Name: printf) + Args: + Literal(Type: 33, Value: %u ) + FunctionCall(Name: fibo) + Args: + Literal(Type: 34, Value: 2) + FunctionCall(Name: printf) + Args: + Literal(Type: 33, Value: %u ) + FunctionCall(Name: fibo) + Args: + Literal(Type: 34, Value: 3) + FunctionCall(Name: printf) + Args: + Literal(Type: 33, Value: %u ) + FunctionCall(Name: fibo) + Args: + Literal(Type: 34, Value: 4) + FunctionCall(Name: printf) + Args: + Literal(Type: 33, Value: %u ) + FunctionCall(Name: fibo) + Args: + Literal(Type: 34, Value: 5) + FunctionCall(Name: printf) + Args: + Literal(Type: 33, Value: %u ) + FunctionCall(Name: fibo) + Args: + Literal(Type: 34, Value: 6) + FunctionCall(Name: printf) + Args: + Literal(Type: 33, Value: %u ) + FunctionCall(Name: fibo) + Args: + Literal(Type: 34, Value: 7) + FunctionCall(Name: printf) + Args: + Literal(Type: 33, Value: %u +) + FunctionCall(Name: fibo) + Args: + Literal(Type: 34, Value: 8) + For( + Init: + VarDecl(i): + Type(TKN_INT) + Value: + Literal(Type: 34, Value: 0) + Cond: + BinaryOp(Op: 66) + Left: + Variable(i) + Right: + Literal(Type: 34, Value: 20) + Act: + Assign(i): + Value: + BinaryOp(Op: 72) + Left: + Variable(i) + Right: + Literal(Type: 34, Value: 1) + Body: + CompoundStatement + FunctionCall(Name: printf) + Args: + Literal(Type: 33, Value: %u ) + FunctionCall(Name: fibo) + Args: + Variable(i) + FunctionCall(Name: printf) + Args: + Literal(Type: 33, Value: +DONE! +) + FunctionCall(Name: printf) + Args: + Literal(Type: 33, Value: %u +) + BinaryOp(Op: 82) + Left: + BinaryOp(Op: 82) + Left: + Literal(Type: 34, Value: 1) + Right: + Literal(Type: 34, Value: 1) + Right: + BinaryOp(Op: 82) + Left: + Literal(Type: 34, Value: 1) + Right: + Literal(Type: 34, Value: 1) + ) + Struct(MyStruct) + VarDecl(a): + Type(TKN_INT**[10][110][30]) + VarDecl(b): + Type(TKN_CHAR) + VarDecl(c): + Type(TKN_INT) +) diff --git a/tests/output/inp4.c_asm_output.txt b/tests/output/inp4.c_asm_output.txt new file mode 100644 index 0000000..8b7a06f --- /dev/null +++ b/tests/output/inp4.c_asm_output.txt @@ -0,0 +1,21 @@ +section .data +__main_size: equ 16 +section .text +global main +main: +push rbp +mov rbp, rsp +sub rsp, __main_size +mov rax, 0 +mov rsp, rbp +pop rbp +ret +mov rsp, rbp +pop rbp +ret +extern exit +global _start +_start: +call main +mov rdi, 0 +call exit diff --git a/tests/output/inp4.c_lex_output.txt b/tests/output/inp4.c_lex_output.txt new file mode 100644 index 0000000..7de4b5c --- /dev/null +++ b/tests/output/inp4.c_lex_output.txt @@ -0,0 +1,22 @@ +TKN_LIT_STRUCT +TKN_ID(MyStruct) +TKN_L_BRACE +TKN_INT +TKN_ID(c) +TKN_SEMICOLON +TKN_R_BRACE +TKN_SEMICOLON +TKN_INT +TKN_ID(main) +TKN_L_PAREN +TKN_R_PAREN +TKN_L_BRACE +TKN_LIT_STRUCT +TKN_ID(MyStruct) +TKN_ID(m) +TKN_SEMICOLON +TKN_RETURN +TKN_LIT_INT(0) +TKN_SEMICOLON +TKN_R_BRACE +TKN_EOF diff --git a/tests/output/inp4.c_tree_output.txt b/tests/output/inp4.c_tree_output.txt new file mode 100644 index 0000000..44fc911 --- /dev/null +++ b/tests/output/inp4.c_tree_output.txt @@ -0,0 +1,17 @@ +Program( + Function( + Name: + main + Returns: + Type(TKN_INT) + Params: + Statements: + VarDecl(m): + Type(MyStruct)struct + Return: + Literal(Type: 34, Value: 0) + ) + Struct(MyStruct) + VarDecl(c): + Type(TKN_INT) +)