Files
compiler-the-translators/genx.sh
Scarlett 76f98b93ab 💎💎💎💎💎
2025-05-05 14:28:02 -04:00

122 lines
3.4 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'
if [ ! -f "./alpha" ]; then
echo -e "${RED}[GenX] ${YELLOW}File ./alpha not found! Generating with 'make'.${WHITE}"
make
fi
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>${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."
echo -e "${WHITE}-----------------------------------------\n"
exit 1
}
if [ $# -eq 0 ]; then
help
fi
if [ ! -d "binaries" ]; then
mkdir -p binaries
fi
appendStd() {
if [ ! -f "$1" ]; then
echo "File not found: $1"
return 1
fi
backup_file="${1}.bak"
cp "$1" "$backup_file"
temp_file=$(mktemp)
while IFS= read -r line || [ -n "$line" ]; do
echo "$line" >> "$temp_file"
if [[ $line =~ ^#include\ \"[^\"]+\" ]]; then
include_file=$(echo "$line" | sed -n 's/.*"\([^"]*\)".*/\1/p')
if [[ $include_file == "std.alpha" ]]; then
echo -e "${GREEN}[GenX] ${WHITE}Including ${BLUE}std.alpha${WHITE}..."
if [ -f "library/std.alpha" ]; then
cat library/std.alpha >> "$temp_file"
else
echo -e "${RED}[GenX] ${YELLOW}File library/std.alpha not found!${WHITE}"
rm "$temp_file"
return 1
fi
fi
fi
done < "$1"
mv "$temp_file" "$1"
}
ass() {
gcc "$1" library/alpha_lib_reg.s library/alpha_driver.s -no-pie -o binaries/${2}
if [ $? -eq 0 ]; then
echo -e "${GREEN}[GenX] ${WHITE}Executable file: binaries/${2}"
echo -e "${GREEN}[GenX] ${WHITE}Done!"
else
echo -e "${RED}[GenX] ${YELLOW}Error generating executable file!${WHITE}"
exit 1
fi
}
if [ $# -eq 1 ]; then
if [ -f "$1" ]; then
filename=$(basename -- "$1")
if [[ "$1" == *.alpha ]]; then
filename="${filename:0:${#filename}-6}"
s_name="out/${filename}.s"
appendStd "$1"
./alpha -ir -tc -asc -cg -st "$1"
backup_file="${1}.bak"
if [ -f "$backup_file" ]; then
mv "$backup_file" "$1"
fi
echo -e "${GREEN}[GenX] ${WHITE}Generated .s file."
ass "$s_name" "$filename"
elif [[ "$1" == *.s ]]; then
filename="${filename:0:${#filename}-2}"
s_name="out/${filename}.s"
echo -e "${GREEN}[GenX] ${WHITE}File $1 is a .s file! (Skipping compiler)${WHITE}"
ass "$s_name" "$filename"
exit 1
else
echo -e "${RED}[GenX] ${YELLOW}File $1 is not a .alpha file!${WHITE}"
exit 1
fi
else
echo -e "${RED}[GenX] ${YELLOW}File $1 not found!${WHITE}"
exit 1
fi
else
help
fi