-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.sh
More file actions
executable file
·294 lines (251 loc) · 7.21 KB
/
run.sh
File metadata and controls
executable file
·294 lines (251 loc) · 7.21 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/bin/bash
################################################################################
# PPJ Compiler Run Script
#
# This script provides a convenient wrapper for executing the PPJ compiler
# from the built JAR file. It handles JAR file detection, Java execution,
# and provides helpful error messages.
#
# Usage:
# ./run.sh <command> [arguments...]
#
# Commands:
# lexer <file> Run lexical analysis only
# syntax <file> Run syntax analysis (includes lexical)
# semantic <file> Run semantic analysis (includes all phases)
# run <file> Execute FRISC assembly code
#
# Options:
# -h, --help Show this help message and exit
# -v, --version Show compiler version information
#
# Examples:
# ./run.sh semantic program.c
# ./run.sh run compiler-bin/a.frisc
# ./run.sh --help
#
# Exit Codes:
# 0 Execution completed successfully
# 1 Execution failed or invalid arguments
# 2 JAR file not found (build required)
# 3 Java execution failed
#
# Author: Karlo Knežević
# Website: https://karloknezevic.github.io/
################################################################################
set -euo pipefail
# Script configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
JAR_FILE="cli/target/ccompiler.jar"
JAVA_CMD="java"
# Colors for output (if terminal supports it)
if [[ -t 1 ]]; then
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
BOLD='\033[1m'
NC='\033[0m' # No Color
else
RED=''
GREEN=''
YELLOW=''
BLUE=''
CYAN=''
NC=''
BOLD=''
fi
################################################################################
# Helper Functions
################################################################################
print_usage() {
cat << EOF
${BOLD}PPJ Compiler Run Script${NC}
${BOLD}Usage:${NC}
./run.sh <command> [arguments...]
./run.sh [OPTIONS]
${BOLD}Commands:${NC}
lexer <file> Run lexical analysis only
syntax <file> Run syntax analysis (includes lexical)
semantic <file> Run semantic analysis (includes all phases)
run <file> Execute FRISC assembly code
${BOLD}Options:${NC}
-h, --help Show this help message and exit
-v, --version Show compiler version information
${BOLD}Examples:${NC}
./run.sh semantic program.c
./run.sh run compiler-bin/a.frisc
./run.sh syntax examples/valid/program1.c
./run.sh --help
${BOLD}Note:${NC}
The compiler JAR file must be built first using:
${CYAN}./build.sh${NC}
EOF
}
print_error() {
echo -e "${RED}${BOLD}Error:${NC} $1" >&2
}
print_success() {
echo -e "${GREEN}${BOLD}✓${NC} $1"
}
print_info() {
echo -e "${BLUE}${BOLD}ℹ${NC} $1"
}
print_warning() {
echo -e "${YELLOW}${BOLD}⚠${NC} $1"
}
check_jar_exists() {
if [[ ! -f "$JAR_FILE" ]]; then
print_error "Compiler JAR file not found: $JAR_FILE"
echo ""
echo "The compiler must be built before it can be executed."
echo ""
echo "To build the compiler, run:"
echo -e " ${CYAN}./build.sh${NC}"
echo ""
echo "Or manually:"
echo -e " ${CYAN}mvn clean package -DskipTests${NC}"
echo ""
exit 2
fi
}
check_java() {
if ! command -v java &> /dev/null; then
print_error "Java runtime not found in PATH"
echo ""
echo "Please install Java 21+ from: https://openjdk.org/"
exit 3
fi
# Check Java version
local java_version
java_version=$(java -version 2>&1 | head -n 1 | cut -d'"' -f2 | sed '/^1\./s///' | cut -d'.' -f1)
if [[ "$java_version" -lt 21 ]]; then
print_warning "Java version $java_version detected. Java 21+ is recommended."
echo ""
fi
}
get_jar_version() {
# Try to extract version from JAR manifest or Maven properties
if command -v unzip &> /dev/null; then
local version
version=$(unzip -p "$JAR_FILE" META-INF/MANIFEST.MF 2>/dev/null | grep -i "Implementation-Version" | cut -d' ' -f2 | tr -d '\r' || echo "unknown")
echo "$version"
else
echo "unknown"
fi
}
print_version() {
check_jar_exists
local version
version=$(get_jar_version)
echo -e "${BOLD}PPJ Compiler${NC}"
echo " Version: $version"
echo " JAR File: $JAR_FILE"
echo ""
if [[ -f "$JAR_FILE" ]]; then
local jar_size
jar_size=$(du -h "$JAR_FILE" | cut -f1)
echo " Size: $jar_size"
fi
echo ""
echo "Java Information:"
java -version 2>&1 | sed 's/^/ /'
}
parse_arguments() {
# Handle help and version flags
if [[ $# -eq 0 ]]; then
print_error "No command specified"
echo ""
print_usage
exit 1
fi
case "$1" in
-h|--help)
print_usage
exit 0
;;
-v|--version)
print_version
exit 0
;;
*)
# Valid command, continue execution
return 0
;;
esac
}
validate_command() {
local command="$1"
local valid_commands=("lexer" "syntax" "semantic" "run")
if [[ ! " ${valid_commands[*]} " =~ " ${command} " ]]; then
print_error "Invalid command: $command"
echo ""
echo "Valid commands are: ${valid_commands[*]}"
echo ""
print_usage
exit 1
fi
}
validate_file() {
local file="$1"
if [[ -z "$file" ]]; then
print_error "No file specified for command"
echo ""
print_usage
exit 1
fi
# For 'run' command, file might be a FRISC assembly file
# For other commands, it should be a C source file
if [[ ! -f "$file" ]] && [[ ! -f "$SCRIPT_DIR/$file" ]]; then
print_warning "File not found: $file"
echo ""
echo "Please ensure the file exists and the path is correct."
exit 1
fi
}
run_compiler() {
local command="$1"
shift
# Validate command
validate_command "$command"
# Validate file if command requires it
if [[ "$command" != "run" ]] || [[ $# -gt 0 ]]; then
if [[ $# -eq 0 ]]; then
print_error "No file specified for command: $command"
echo ""
print_usage
exit 1
fi
validate_file "$1"
fi
# Execute compiler
print_info "Executing compiler command: $command"
echo ""
if "$JAVA_CMD" -jar "$JAR_FILE" "$command" "$@"; then
echo ""
print_success "Command completed successfully"
return 0
else
local exit_code=$?
echo ""
print_error "Command failed with exit code: $exit_code"
return $exit_code
fi
}
################################################################################
# Main Execution
################################################################################
main() {
# Change to script directory
cd "$SCRIPT_DIR" || exit 1
# Parse arguments (help/version handled here)
parse_arguments "$@"
# Check prerequisites
check_java
check_jar_exists
# Run compiler with all arguments
run_compiler "$@"
}
# Run main function
main "$@"