rewrite basically
This commit is contained in:
218
genx.sh
218
genx.sh
@ -19,23 +19,153 @@ 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
|
||||
alphaToInclude=()
|
||||
cToInclude=()
|
||||
sToInclude=()
|
||||
cToBuild=()
|
||||
sToInclude+=("library/alpha_lib_reg.s")
|
||||
sToInclude+=("library/alpha_driver.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>${WHITE} Generates executable file from <file.alpha>\n"
|
||||
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."
|
||||
echo -e "${ORANGE} Generates .s and links alpha driver and general library and other includes.\n"
|
||||
echo -e "${WHITE}-----------------------------------------\n"
|
||||
exit 1
|
||||
}
|
||||
|
||||
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
|
||||
@ -44,79 +174,59 @@ if [ ! -d "binaries" ]; then
|
||||
mkdir -p binaries
|
||||
fi
|
||||
|
||||
# ----------------------------------------------------{ INIT }-#
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
# -{ 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"
|
||||
appendStd "$1"
|
||||
|
||||
getIncludes "$1"
|
||||
appendStr "$1"
|
||||
./alpha -ir -tc -asc -cg -st "$1"
|
||||
backup_file="${1}.bak"
|
||||
if [ -f "$backup_file" ]; then
|
||||
mv "$backup_file" "$1"
|
||||
fi
|
||||
|
||||
# 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"
|
||||
echo -e "${GREEN}[GenX] ${WHITE}File $1 is a .s file! (Skipping compiler)${WHITE}"
|
||||
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
|
||||
fi
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# --------------------------------------------------{ RUNNER }-#
|
||||
|
Reference in New Issue
Block a user