-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathb.sh
More file actions
executable file
·87 lines (74 loc) · 2.1 KB
/
b.sh
File metadata and controls
executable file
·87 lines (74 loc) · 2.1 KB
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
#!/bin/bash
set -e
# clear
# ----------- Configuration -----------
# Choose: tcc or gcc
COMPILER="gcc"
SALAM_OUTPUT="s"
C_FILES="c_files.txt"
# CFLAGS_COMMON=""
CFLAGS_COMMON="-Wno-unused-parameter -g -Wall -Wextra -pedantic"
INCLUDE_DIRS="-I. -Isrc"
CFLAGS="$CFLAGS_COMMON $INCLUDE_DIRS"
LDFLAGS=""
OBJ_FILES=""
# ----------- Compilation -----------
if [[ "$COMPILER" == "tcc" ]]; then
while IFS= read -r file; do
echo "Compiling $file ..."
if ! $COMPILER -c $CFLAGS "$file" -o "${file%.*}.o" 2>compile_error.tmp; then
echo "Compilation failed for $file:"
cat compile_error.tmp
rm -f compile_error.tmp
exit 1
fi
rm -f compile_error.tmp
OBJ_FILES="$OBJ_FILES ${file%.*}.o"
done <"$C_FILES"
echo "Linking..."
if ! $COMPILER -o "$SALAM_OUTPUT" $OBJ_FILES $LDFLAGS; then
echo "Linking failed!"
exit 1
fi
elif [[ "$COMPILER" == "gcc" ]]; then
echo "Compiling and linking with GCC..."
if ! $COMPILER $CFLAGS @"$C_FILES" -o "$SALAM_OUTPUT" $LDFLAGS; then
echo "Compilation or linking failed!"
exit 1
fi
else
echo "Unknown compiler: $COMPILER"
exit 1
fi
echo "Compilation and linking successful."
# ----------- Run Program -----------
echo "Running program..."
if ! ./"$SALAM_OUTPUT" input.salam; then
echo "Program execution failed!"
exit 1
fi
echo "Program executed successfully."
# ────── Function to beautify JSON ──────
beautify_json_if_valid() {
local file="$1"
if [ -f "$file" ]; then
if command -v jq >/dev/null 2>&1; then
echo "Checking if $file is valid JSON..."
if jq empty "$file" >/dev/null 2>&1; then
echo "Beautifying $file using jq..."
jq . "$file" >"${file}.pretty" && mv "${file}.pretty" "$file"
echo "Beautification of $file complete."
else
echo "$file contains invalid JSON. Skipping beautification."
fi
else
echo "Warning: 'jq' is not installed. Skipping beautification of $file."
echo "To install: sudo apt install jq"
fi
else
echo "$file not found. Skipping beautification."
fi
}
# ────── Beautify JSON outputs ──────
beautify_json_if_valid "tokens.json"
beautify_json_if_valid "ast.json"