-
Notifications
You must be signed in to change notification settings - Fork 2
/
erplag
executable file
·189 lines (165 loc) · 5.46 KB
/
erplag
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#! /bin/bash
# erplag -a file.erp --> Generates file.asm
# erplag file.asm --> Generates file executable
# erplag -o testExec file.erp --> Generates executable with the name (Creates temp files)
# erplag file.erp --> Generates file executable directly (Creates temp files)
CYAN='\033[1;36m'
RED='\033[1;31m'
WHITE='\033[1;37m'
YELLOW='\033[1;33m'
BOLD='\033[1m'
MAGENTA='\033[1;35m'
GREEN='\033[1;32m'
RESET='\033[0m'
errHead () {
erplagHead
printf "${RED}FATAL ERROR ${RESET}${BOLD}--> ${RESET}"
}
erplagHead () {
printf "${BOLD}${CYAN}ERPLAG : "
}
erpHelp () {
printf "${BOLD}${YELLOW}The compiler can be used with the following options - ${RESET}\n"
printf "${BOLD}1. ${CYAN}erplag -a ${RESET}${BOLD}<filename>.erp ${RESET}generates a ${BOLD}.asm ${RESET}file with the same filename\n"
printf "${BOLD}2. ${CYAN}erplag ${RESET}${BOLD}<filename>.asm ${RESET}generates an executable with the same filename\n"
printf "${BOLD}3. ${CYAN}erplag -o ${RESET}${BOLD}myexec <filename>.erp ${RESET}generates an executable file named ${BOLD}myexec${RESET}\n"
printf "${BOLD}4. ${CYAN}erplag ${RESET}${BOLD}<filename>.erp ${RESET}directly generates an executable with the same filename\n\n"
printf "If you are making any changes to the ${BOLD}${GREEN}source code ${RESET}or the ${BOLD}${CYAN}erplag, erpclr ${RESET}scripts, make sure to run ${BOLD}${CYAN}./rebuild ${RESET} afterwards\n"
printf "A script named ${BOLD}${CYAN}erpclr${RESET} can be used to clean any ${BOLD}.asm, .o ${RESET}and ${BOLD}executable ${RESET}files recursively in a folder that contains only ERPLAG source files. ${BOLD}${RED}Use it with caution in other directories${RESET}\n\n"
printf "${BOLD}${YELLOW}Use ${CYAN}make debug ${YELLOW}to create the following executables useful for debugging - ${RESET}\n"
printf "${BOLD}1. ${CYAN}ast ${RESET}- To debug the abstract syntax tree (described in ${BOLD}astDriver.c${RESET})\n"
printf "${BOLD}2. ${CYAN}ST ${RESET}- To debug the symbol table (described in ${BOLD}symbolTableDriver.c${RESET})\n"
printf "${BOLD}3. ${CYAN}compiler ${RESET}- To debug the final compiler (described in ${BOLD}erplag.c${RESET})\n"
printf "Run ${BOLD}${CYAN}make clean ${RESET}to remove any ${BOLD}.o ${RESET}files or debugging ${BOLD}executables ${RESET}\n\n"
printf "${BOLD}${MAGENTA}The compiler can be uninstalled from the git repository using ${BOLD}${CYAN}./uninstall${RESET}\n"
}
successErpExe () {
erplagHead
printf "${BOLD}${GREEN}Compilation of ${RESET}${BOLD}$1 ${GREEN}successfull! Executable file ${RESET}${BOLD}$2 ${GREEN}generated${RESET}\n"
}
successErpAsm () {
erplagHead
printf "${BOLD}${GREEN}Compilation of ${RESET}${BOLD}$1 ${GREEN}successful! Assembly file ${RESET}${BOLD}$2 ${GREEN}generated${RESET}\n"
}
successAsmExe () {
erplagHead
printf "${BOLD}${GREEN}Assembly of ${RESET}${BOLD}$1 ${GREEN}successful! Executable file ${RESET}${BOLD}$2 ${GREEN}generated${RESET}\n"
}
successLines () {
erplagHead
printf "${BOLD}${GREEN}Lines of generated code ${RESET}${BOLD}--> ${YELLOW}$1${RESET}\n"
}
if [[ "$OSTYPE" == "linux"* ]]
then
NFLAG="-felf64"
elif [[ "$OSTYPE" == "darwin"* ]]
then
NFLAG="-fmacho64"
fi
optflag=0
while getopts ":a:o:h" opt; do
optflag=1
case ${opt} in
h) erpHelp
exit 0
;;
a) .compiler $OPTARG
status=$?
fname=${OPTARG:0:${#OPTARG}-4}
asmname=${fname}.asm
if [ $status -eq 0 ]
then
successErpAsm $OPTARG $asmname
# lines=$(wc -l $asmname | grep -o '[0-9]*[0-9]')
# successLines $lines
fi
exit $status
;;
o) target=$OPTARG
shift $(($OPTIND-1))
srcname=$1
if test "$srcname" = ""
then
errHead
echo "No source file"
exit 1
fi
asmname=${srcname}.asm
.compiler $srcname $asmname
status=$?
if [ $status -eq 0 ]
then
objname=${srcname}.o
nasm $NFLAG $asmname -o $objname
gcc -w -no-pie $objname -o $target
successErpExe $srcname $target
# lines=$(wc -l $asmname | grep -o '[0-9]*[0-9]')
# successLines $lines
fi
rm -f $asmname
rm -f $objname
exit $status
;;
\?) errHead
echo "Invalid option. Use ${BOLD}${CYAN}-a ${RESET}to generate x86 assembly or use ${BOLD}${CYAN}-o ${RESET}along with output name to generate executable. For more information, use ${BOLD}${CYAN}-h${RESET}"
exit 1
;;
:) errHead
if test "$1" = "-a"
then
echo "No source file"
else
echo "Please provide output file name"
fi
exit 1
esac
done
if [ $optflag -eq 0 ]
then
srcname=$1 ;
if [[ $srcname =~ \.asm$ ]]
then
# Need to check if file exists or not in bash
if [ -f $srcname ]
then
fname=${srcname:0:${#srcname}-4}
objname=${srcname}.o
nasm $NFLAG $srcname -o $objname
gcc -w -no-pie $objname -o $fname
rm -f $objname
successAsmExe $srcname $fname
exit 0
else
errHead
printf "Assembly file ${BOLD}$srcname ${RESET}does not exist\n"
exit 1
fi
elif [[ $srcname =~ \.erp$ ]]
then
fname=${srcname:0:${#srcname}-4}
asmname=${srcname}.asm
objname=${srcname}.o
.compiler $srcname $asmname
status=$?
if [ $status -eq 0 ]
then
nasm $NFLAG $asmname -o $objname
gcc -w -no-pie $objname -o $fname
successErpExe $srcname $fname
# lines=$(wc -l $asmname | grep -o '[0-9]*[0-9]')
# successLines $lines
fi
rm -f $asmname
rm -f $objname
exit $status
elif test "$srcname" = ""
then
errHead
echo "No source file"
exit 1
else
errHead
printf "Unsupported file format. Use ${BOLD}.erp ${RESET}for source files and ${BOLD}.asm ${RESET}for x86 assembly files\n"
exit 1
fi
fi