Amber Compiles to Bash

Amber Compiles to Bash

It certainly isn’t a new idea to compile a language into an intermediate language. The original C++ compiler outputs C code, for example. Enhanced versions of Fortran were often just conversions of new syntax to old syntax. Of course, it makes sense to output to some language that can run on lots of different platforms. So, using that logic, Amber makes perfect sense. It targets — no kidding — bash. You write with nice modern syntax and compile-time checks. The output is a bash script. Admittedly, sometimes a hard-to-read bash script, but still.


If you want to see the source code, it is available on GitHub. Since Windows doesn’t really support bash — if you don’t count things like Cygwin and WSL — Amber only officially supports Linux and MacOS. In addition to compiling files, Amber can also execute scripts directly which can be useful for a quick one-liner. If you use Visual Studio Code, you can find a syntax highlighter extension for Amber.



To get a flavor of how it works, here’s a simple Amber file to rename some files:



let files = ["config.json", "file.txt", "audio.mp3"]

loop index, file in files {
$mv {file} {index}{file}$ failed {
echo "Failed to rename {file}"
}
}

Here’s the output shell script:



__AMBER_ARRAY_0=("config.json" "file.txt" "audio.mp3");
__0_files=("${__AMBER_ARRAY_0[@]}");
index=0;
for file in "${__0_files[@]}"
do
mv ${file} ${index}${file}
__AMBER_STATUS=$?;
if [ $__AMBER_STATUS != 0 ]; then
echo "Failed to rename ${file}"
fi
let index=${index}+1
done

Looks much easier. Still, bash isn’t that hard. If you must have bash scripts for some reason, this might be worth a look. As a general-p ..

Support the originator by clicking the read the rest link below.