forked from rui314/chibicc
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
6 changed files
with
84 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ $(OBJS): chibicc.h | |
|
||
test: chibicc | ||
./test.sh | ||
./test-driver.sh | ||
|
||
clean: | ||
rm -f chibicc *.o *~ tmp* | ||
|
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 |
---|---|---|
|
@@ -186,4 +186,4 @@ void add_type(Node *node); | |
// codegen.c | ||
// | ||
|
||
void codegen(Obj *prog); | ||
void codegen(Obj *prog, FILE *out); |
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 |
---|---|---|
@@ -1,15 +1,60 @@ | ||
#include "chibicc.h" | ||
|
||
static char *opt_o; | ||
|
||
static char *input_path; | ||
|
||
static void usage(int status) { | ||
fprintf(stderr, "chibicc [ -o <path> ] <file>\n"); | ||
exit(status); | ||
} | ||
|
||
static void parse_args(int argc, char **argv) { | ||
for (int i = 1; i < argc; i++) { | ||
if (!strcmp(argv[i], "--help")) | ||
usage(0); | ||
|
||
if (!strcmp(argv[i], "-o")) { | ||
if (!argv[++i]) | ||
usage(1); | ||
opt_o = argv[i]; | ||
continue; | ||
} | ||
|
||
if (!strncmp(argv[i], "-o", 2)) { | ||
opt_o = argv[i] + 2; | ||
continue; | ||
} | ||
|
||
if (argv[i][0] == '-' && argv[i][1] != '\0') | ||
error("unknown argument: %s", argv[i]); | ||
|
||
input_path = argv[i]; | ||
} | ||
|
||
if (!input_path) | ||
error("no input files"); | ||
} | ||
|
||
static FILE *open_file(char *path) { | ||
if (!path || strcmp(path, "-") == 0) | ||
return stdout; | ||
|
||
FILE *out = fopen(path, "w"); | ||
if (!out) | ||
error("cannot open output file: %s: %s", path, strerror(errno)); | ||
return out; | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
if (argc != 2) | ||
error("%s: invalid number of arguments", argv[0]); | ||
parse_args(argc, argv); | ||
|
||
// Tokenize and parse. | ||
Token *tok = tokenize_file(argv[1]); | ||
Token *tok = tokenize_file(input_path); | ||
Obj *prog = parse(tok); | ||
|
||
// Traverse the AST to emit assembly. | ||
codegen(prog); | ||
|
||
FILE *out = open_file(opt_o); | ||
codegen(prog, out); | ||
return 0; | ||
} |
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,25 @@ | ||
#!/bin/sh | ||
tmp=`mktemp -d /tmp/chibicc-test-XXXXXX` | ||
trap 'rm -rf $tmp' INT TERM HUP EXIT | ||
echo > $tmp/empty.c | ||
|
||
check() { | ||
if [ $? -eq 0 ]; then | ||
echo "testing $1 ... passed" | ||
else | ||
echo "testing $1 ... failed" | ||
exit 1 | ||
fi | ||
} | ||
|
||
# -o | ||
rm -f $tmp/out | ||
./chibicc -o $tmp/out $tmp/empty.c | ||
[ -f $tmp/out ] | ||
check -o | ||
|
||
# --help | ||
./chibicc --help 2>&1 | grep -q chibicc | ||
check --help | ||
|
||
echo OK |
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