234 lines
6.6 KiB
Bash
Executable File
234 lines
6.6 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# GenX Tool #
|
|
# The Translators - Spring 2025 #
|
|
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
ORANGE='\033[0;33m'
|
|
BLUE='\033[0;34m'
|
|
PURPLE='\033[0;35m'
|
|
CYAN='\033[0;36m'
|
|
LIGHTGRAY='\033[0;37m'
|
|
DARKGRAY='\033[1;30m'
|
|
LIGHTRED='\033[1;31m'
|
|
LIGHTGREEN='\033[1;32m'
|
|
YELLOW='\033[1;33m'
|
|
LIGHTBLUE='\033[1;34m'
|
|
LIGHTPURPLE='\033[1;35m'
|
|
LIGHTCYAN='\033[1;36m'
|
|
WHITE='\033[1;37m'
|
|
|
|
alphaToInclude=()
|
|
cToInclude=()
|
|
sToInclude=()
|
|
cToBuild=()
|
|
sToInclude+=("library/alpha_lib_reg.s")
|
|
sToInclude+=("library/alpha_driver.s")
|
|
sToInclude+=("library/std.s")
|
|
|
|
filename=""
|
|
s_name=""
|
|
backup_file=""
|
|
|
|
# -{ FUNC }----------------------------------------------------#
|
|
|
|
help() {
|
|
echo -e "${WHITE}-----{ ${BLUE}GenX.sh Help ${WHITE}}-----\n"
|
|
echo -e "${YELLOW} Arguments: ${WHITE}"
|
|
echo -e "${GREEN} -help ${WHITE} Displays this message\n"
|
|
echo -e "${YELLOW} Usage: ${WHITE}"
|
|
echo -e "${GREEN} ./genx.sh <file.alpha || file.s>${WHITE} Generates executable file from <file.alpha>\n"
|
|
echo -e "${YELLOW} Notes: ${WHITE}"
|
|
echo -e "${ORANGE} Generates .s and links alpha driver and general library and other includes.\n"
|
|
echo -e "${WHITE}-----------------------------------------\n"
|
|
}
|
|
|
|
printLists() {
|
|
echo -e "${GREEN}[GenX] ${WHITE}Alpha files to include: ${BLUE}${alphaToInclude[@]}${WHITE}"
|
|
echo -e "${GREEN}[GenX] ${WHITE}C files to include: ${BLUE}${cToInclude[@]}${WHITE}"
|
|
echo -e "${GREEN}[GenX] ${WHITE}C files to build: ${BLUE}${cToBuild[@]}${WHITE}"
|
|
echo -e "${GREEN}[GenX] ${WHITE}S files to include: ${BLUE}${sToInclude[@]}${WHITE}"
|
|
}
|
|
|
|
getIncludes() {
|
|
while IFS= read -r line || [ -n "$line" ]; do
|
|
if [[ $line =~ ^#include\ \"[^\"]+\" ]]; then
|
|
between_quotes=$(echo "$line" | sed -n 's/.*"\([^"]*\)".*/\1/p')
|
|
extension="${between_quotes##*.}"
|
|
sed -i "\|$line|d" "$1"
|
|
|
|
if [[ $between_quotes == *".alpha" ]]; then
|
|
path=$(dirname "$1")
|
|
alpha_file=${path}"/"${between_quotes}
|
|
|
|
if [[ -f "library/$between_quotes" ]]; then
|
|
alphaToInclude+=("library/${between_quotes}")
|
|
|
|
elif [[ -f $alpha_file ]]; then
|
|
alphaToInclude+=("$alpha_file")
|
|
|
|
else
|
|
echo -e "${RED}[GenX] ${YELLOW}File $between_quotes not found!${WHITE}"
|
|
restoreFileFromBackup "$1"
|
|
exit 1
|
|
fi
|
|
|
|
elif [[ $between_quotes == *".c" ]]; then
|
|
path=$(dirname "$1")
|
|
c_file=${path}"/"${between_quotes}
|
|
if [[ -f $c_file ]]; then
|
|
cToBuild+=("$c_file")
|
|
else
|
|
echo -e "${RED}[GenX] ${YELLOW}File $between_quotes not found!${WHITE}"
|
|
restoreFileFromBackup "$1"
|
|
exit 1
|
|
fi
|
|
|
|
elif [[ $between_quotes == *".s" ]]; then
|
|
path=$(dirname "$1")
|
|
s_file=${path}"/"${between_quotes}
|
|
if [[ -f $s_file ]]; then
|
|
sToInclude+=("$s_file")
|
|
else
|
|
echo -e "${RED}[GenX] ${YELLOW}File $between_quotes not found!${WHITE}"
|
|
restoreFileFromBackup "$1"
|
|
exit 1
|
|
fi
|
|
|
|
else
|
|
echo -e "${RED}[GenX] ${YELLOW}Not able to include file type $extension!${WHITE}"
|
|
exit 1
|
|
fi
|
|
fi
|
|
|
|
done < "$1"
|
|
}
|
|
|
|
|
|
ass_c() {
|
|
for c in "${cToBuild[@]}"; do
|
|
if [ -f "$c" ]; then
|
|
c_file_ass=$(basename "$c" .c)
|
|
c_file_ass=out/$c_file_ass.s
|
|
cToInclude+=($c_file_ass)
|
|
gcc -S $c -o $c_file_ass
|
|
else
|
|
echo -e "${RED}[GenX] ${YELLOW}File $c not found!${WHITE}"
|
|
exit 1
|
|
fi
|
|
done
|
|
}
|
|
|
|
|
|
appendStr() {
|
|
for alpha_file in "${alphaToInclude[@]}"; do
|
|
if [ -f "$alpha_file" ]; then
|
|
temp_file=$(mktemp)
|
|
cat "$alpha_file" > "$temp_file"
|
|
cat "$1" >> "$temp_file"
|
|
mv "$temp_file" "$1"
|
|
else
|
|
echo -e "${RED}[GenX] ${YELLOW}File $alpha_file not found!${WHITE}"
|
|
fi
|
|
done
|
|
}
|
|
|
|
ass() {
|
|
sToInclude_string=$(printf "%s " "${sToInclude[@]}")
|
|
cToInclude_string=$(printf "%s " "${cToInclude[@]}")
|
|
gcc "$1" ${cToInclude_string} ${sToInclude_string} -no-pie -o binaries/${2}
|
|
if [ $? -eq 0 ]; then
|
|
echo -e "${GREEN}[GenX] ${WHITE}Done!"
|
|
else
|
|
echo -e "${RED}[GenX] ${YELLOW}Error generating executable file!${WHITE}"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
restoreFileFromBackup() {
|
|
if [[ -f "$backup_file" ]]; then
|
|
cp "$backup_file" "$1"
|
|
rm "$backup_file"
|
|
else
|
|
echo -e "${RED}[GenX] ${YELLOW}Backup file not found!${WHITE}"
|
|
fi
|
|
|
|
}
|
|
|
|
# ----------------------------------------------------{ FUNC }-#
|
|
|
|
|
|
|
|
# -{ INIT }----------------------------------------------------#
|
|
|
|
if [ ! -f "./alpha" ]; then
|
|
echo -e "${RED}[GenX] ${YELLOW}File ./alpha not found! Generating with 'make'.${WHITE}"
|
|
make
|
|
fi
|
|
|
|
if [ $# -eq 0 ]; then
|
|
help
|
|
fi
|
|
|
|
if [ ! -d "binaries" ]; then
|
|
mkdir -p binaries
|
|
fi
|
|
|
|
# ----------------------------------------------------{ INIT }-#
|
|
|
|
|
|
|
|
# -{ RUNNER }--------------------------------------------------#
|
|
|
|
if [ $# -eq 1 ]; then
|
|
if [ -f "$1" ]; then
|
|
filename=$(basename -- "$1")
|
|
backup_file="${1}.bak"
|
|
cp "$1" "$backup_file"
|
|
|
|
if [[ "$1" == *.alpha ]]; then
|
|
filename="${filename:0:${#filename}-6}"
|
|
s_name="out/${filename}.s"
|
|
|
|
getIncludes "$1"
|
|
appendStr "$1"
|
|
./alpha -ir -tc -asc -cg -st "$1"
|
|
|
|
# include a grep here of asc to see if there are any errors
|
|
|
|
echo -e "${GREEN}[GenX] ${WHITE}Generated .s file."
|
|
restoreFileFromBackup "$1"
|
|
ass_c
|
|
ass "$s_name" "$filename"
|
|
printLists
|
|
exit 1
|
|
|
|
elif [[ "$1" == *.s ]]; then
|
|
echo -e "${GREEN}[GenX] ${WHITE}File $1 is a .s file! (Skipping compiler)${WHITE}"
|
|
filename="${filename:0:${#filename}-2}"
|
|
s_name="out/${filename}.s"
|
|
ass "$s_name" "$filename"
|
|
restoreFileFromBackup "$1"
|
|
exit 1
|
|
|
|
else
|
|
echo -e "${RED}[GenX] ${YELLOW}File $1 is not a .alpha file!${WHITE}"
|
|
restoreFileFromBackup "$1"
|
|
exit 1
|
|
|
|
fi
|
|
|
|
else
|
|
echo -e "${RED}[GenX] ${YELLOW}File $1 not found!${WHITE}"
|
|
restoreFileFromBackup "$1"
|
|
exit 1
|
|
fi
|
|
|
|
else
|
|
help
|
|
exit 1
|
|
fi
|
|
|
|
# --------------------------------------------------{ RUNNER }-#
|