Add x64 emitter tests
This commit is contained in:
24
build.sh
24
build.sh
@@ -1,20 +1,4 @@
|
||||
# set -euo pipefail
|
||||
|
||||
assert_eq() {
|
||||
expected="$1"
|
||||
actual="$2"
|
||||
if [ "$expected" != "$actual" ]; then
|
||||
echo "assert failed: expected '$expected', got '$actual'"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
verify_expr() {
|
||||
./main "$1"
|
||||
clang out.s -o test
|
||||
./test
|
||||
assert_eq $? $2
|
||||
}
|
||||
set -euo pipefail
|
||||
|
||||
if [[ ! -e build ]]; then
|
||||
mkdir build
|
||||
@@ -23,10 +7,4 @@ cd build
|
||||
clang -o meta $(realpath ../meta.c) -g -Wall -Wextra -Wshadow -fdiagnostics-absolute-paths
|
||||
./meta > ../meta_gen.c
|
||||
clang -o main $(realpath ../main.c) -g -Wall -Wextra -Wshadow -fdiagnostics-absolute-paths
|
||||
|
||||
./main
|
||||
verify_expr "2+1-1" 2
|
||||
verify_expr "(2+1-1)*3" 6
|
||||
|
||||
|
||||
echo done
|
||||
@@ -32,3 +32,21 @@ void emit_program(FILE *file, Ast *n) {
|
||||
emit_expr(file, n);
|
||||
fprintf(file, " ret\n");
|
||||
}
|
||||
|
||||
void emit_expr_test(char *expr, int value) {
|
||||
Token_Array tokens = lex_file("expr", expr, strlen(expr));
|
||||
Parser parser = {tokens.data, tokens.data + tokens.len};
|
||||
Ast *ast = parse_expr(&parser, 0);
|
||||
FILE *file = fopen("out.s", "w");
|
||||
emit_program(file, ast);
|
||||
fclose(file);
|
||||
int result = system("clang out.s -o out");
|
||||
assert(result == 0);
|
||||
result = system("./out");
|
||||
assert(WEXITSTATUS(result) == value);
|
||||
}
|
||||
|
||||
void emit_x64_test(void) {
|
||||
emit_expr_test("10+5*2-10", 10+5*2-10);
|
||||
printf("x64 tests passed\n");
|
||||
}
|
||||
19
lex.c
19
lex.c
@@ -44,24 +44,7 @@ void lex_advance(Lexer *lex) {
|
||||
lex->column++;
|
||||
}
|
||||
|
||||
if (*lex->at == '\\') {
|
||||
lex->at += 1;
|
||||
if ((lex->at < lex->end) && *lex->at == '\n') {
|
||||
lex->at += 1;
|
||||
lex->line += 1; lex->column = 0;
|
||||
} else if ((lex->at < lex->end) && *lex->at == '\r') {
|
||||
lex->at += 1;
|
||||
if ((lex->at < lex->end) && *lex->at == '\n') {
|
||||
lex->at += 1;
|
||||
lex->line += 1; lex->column = 0;
|
||||
} else {
|
||||
panicf("after \\r missing \\n");
|
||||
}
|
||||
} else {
|
||||
panicf("stray '\\' without follow up new line");
|
||||
}
|
||||
|
||||
} else if (*lex->at == '\n') {
|
||||
if (*lex->at == '\n') {
|
||||
lex->preproc = false;
|
||||
lex->at += 1;
|
||||
} else {
|
||||
|
||||
18
main.c
18
main.c
@@ -12,17 +12,9 @@
|
||||
#include "parser.c"
|
||||
#include "emit_asm_x64.c"
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc == 2) {
|
||||
Token_Array tokens = lex_file("expr", argv[1], strlen(argv[1]));
|
||||
Parser parser = {tokens.data, tokens.data + tokens.len};
|
||||
Ast *ast = parse_expr(&parser, 0);
|
||||
FILE *file = fopen("out.s", "w");
|
||||
emit_program(file, ast);
|
||||
fclose(file);
|
||||
} else {
|
||||
vec_test();
|
||||
lex_test();
|
||||
parser_test();
|
||||
}
|
||||
int main() {
|
||||
vec_test();
|
||||
lex_test();
|
||||
parser_test();
|
||||
emit_x64_test();
|
||||
}
|
||||
Reference in New Issue
Block a user