You need to implement a compiler program that performs syntax checking on a given input file and provides appropriate output based on its validity.
Functionality:
The compiler should accept an input file and check its contents for syntax errors.
If the input file contains syntax errors:
The compiler should return a non-zero exit code.
The output must include the word "error" somewhere in the message.
If the input file is valid:
The compiler should return a zero exit code.
Inputs:
The program will take one command-line argument: the path to the input file which contains the code to be analyzed.
The input file will be provided at runtime when the program is executed.
Outputs:
The compiler should print messages to standard output that indicate whether the input file is valid or contains errors, ensuring that if there are errors, the output contains the word "error".
File Locations and Naming:
Implement the compiler program in a file named compiler located in the /app/ directory.
Ensure that this file is executable.
Notes:
Do not hard-code the expected messages; they should be informative based on the syntax checks performed.
The solution must adhere to conventions of Unix-like environments for execution.
Tests
The following test script will be used to verify your solution:
bash
#!/bin/bash
compiler=${1}
in_file=${2}
compiler_out=$("${compiler}" --parsetest "${in_file}" 2>&1)
compiler_retval=$?
# if the return code not 0, "error" must be in the output
if [[ ${compiler_retval} -ne 0 && ${compiler_out} != *"error"* ]]; then
echo "ERROR: Compiler return value is != 0 but output doesn't contain 'error'"
echo "Output:"
echo "${compiler_out}"
exit 1
fi
if [[ "${compiler_retval}" -eq 0 ]]; then
echo "ERROR: Compiler returned ${compiler_retval} but ${in_file} is supposed to be invalid"
exit 1
fi
exit 0