-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler_driver.bash
executable file
·74 lines (66 loc) · 1.63 KB
/
compiler_driver.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env bash
set -euo pipefail
function parse_file_name {
if (( $# == 2 )); then
echo "${2}"
else
echo "${1}"
fi
}
function print_usage_message {
>&2 echo "error: wrong number of arguments"
>&2 echo " usage: compiler_driver.bash [-(-(lex|parse|codegen|tacky|validate)|S)] /path/to/program.c"
}
function exit_with_usage_message {
print_usage_message
exit 1
}
function parse_option {
if (( $# == 1)); then
echo ""
else
while getopts ":-:S" OPTION; do
case "${OPTION}" in
S ) echo "S";;
- )
case "${OPTARG}" in
lex ) echo --lex;;
parse ) echo --parse;;
codegen ) echo --codegen;;
tacky ) echo --tacky;;
validate ) echo --validate;;
* ) exit_with_usage_message;;
esac
;;
: ) exit_with_usage_message;;
* ) exit_with_usage_message;;
esac
done
fi
}
function main {
if (( $# > 2 )); then
exit_with_usage_message
fi
FILE_NAME="$(parse_file_name "$@")"
OPTION="$(parse_option "$@")"
FILE_NAME_WITHOUT_EXTENSION="${FILE_NAME%.c}"
PREPROCESSED_FILE="${FILE_NAME_WITHOUT_EXTENSION}.i"
ASSEMBLY_FILE="${FILE_NAME_WITHOUT_EXTENSION}.s"
gcc -E -P "${FILE_NAME}" -o "${PREPROCESSED_FILE}"
if [ -n "${OPTION}" ]; then
"${HOME}/Documents/simple-c-compiler/g++-build/sc2" "${OPTION}" "${PREPROCESSED_FILE}";
else
"${HOME}/Documents/simple-c-compiler/g++-build/sc2" "${PREPROCESSED_FILE}";
fi
EXIT_CODE="$?"
if [ -n "${OPTION}" ]; then
exit "${EXIT_CODE}"
fi
gcc "${ASSEMBLY_FILE}" -o "${FILE_NAME_WITHOUT_EXTENSION}"
EXIT_CODE="$?"
rm "${PREPROCESSED_FILE}"
rm "${ASSEMBLY_FILE}"
exit "${EXIT_CODE}"
}
main "$@"