batsh-dev-team / Batsh

A language that compiles to Bash and Windows Batch
http://batsh.org
4.32k stars 169 forks source link

There should be macro-style support for determining whether we're compiling to batch or bash scripts #58

Open coriolinus opened 7 years ago

coriolinus commented 7 years ago

Right now, there's no good way to execute built-in commands which vary between systems. You're stuck with stuff like this:

//! Not platform independent!
//! If on Windows, change this to "move"
call("mv", source, dest);

It would be much more powerful if we could use extremely simple macro-style syntax to implement platform-dependent behavior. The above would, under this proposal, look something like this:

#IF_BASH {
   move = "mv";
}
#IF_BATCH {
   move = "move";
}
call(move, source, dest);
operatorequals commented 5 years ago

I know it is a bit late but: gcc is your friend. You can very well use the gcc preprocessor to achieve not only #defines but #includes too. I made this script for the exact purpose:

#!/bin/sh
FILE="$1"
if [ ! -f "$FILE" ]; then
        echo "File '$FILE' not found"
        exit 1;
fi

quiet=""
#quiet="2>/dev/null"

tmpfile=$(mktemp '/tmp/XXXX-btsh.c')
cp "$FILE" "$tmpfile"
gcc  -E "$tmpfile" $quiet | egrep -v '^#'

rm "$tmpfile"