Skip to content

Commit

Permalink
feat: Add tests for compiler outputs in other modes
Browse files Browse the repository at this point in the history
  • Loading branch information
keyvank committed Oct 14, 2024
1 parent fe14d55 commit 7f84ee7
Show file tree
Hide file tree
Showing 15 changed files with 2,249 additions and 46 deletions.
60 changes: 36 additions & 24 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ char *read_source_file(FILE *fp);

int main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
if (argc != 3)
{
fprintf(stderr, "Usage: %s <filename> <mode> (<mode>: --lex, --asm or --tree)\n", argv[0]);
return 1;
}

Expand All @@ -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;
}
Expand All @@ -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);
Expand All @@ -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:
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion parser/type.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
43 changes: 22 additions & 21 deletions scripts/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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")

Expand Down
65 changes: 65 additions & 0 deletions tests/output/inp.c_asm_output.txt
Original file line number Diff line number Diff line change
@@ -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
59 changes: 59 additions & 0 deletions tests/output/inp.c_lex_output.txt
Original file line number Diff line number Diff line change
@@ -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
56 changes: 56 additions & 0 deletions tests/output/inp.c_tree_output.txt
Original file line number Diff line number Diff line change
@@ -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)
)
)
Loading

0 comments on commit 7f84ee7

Please sign in to comment.