Skip to content

Instantly share code, notes, and snippets.

@sebgod
Last active August 29, 2015 14:01
Show Gist options
  • Save sebgod/165a91ab199df2d42691 to your computer and use it in GitHub Desktop.
Save sebgod/165a91ab199df2d42691 to your computer and use it in GitHub Desktop.
Mercury shell script
#!/bin/sh
if [ $# -eq 0 ]; then
exit 1 # the first argument *MUST* be the executed script
fi
if [ -x $1 ]; then
SHELL_SCRIPT=$(basename $0)
SCRIPT=$1
mv -f $SCRIPT ${SCRIPT}.tmp
shift
MODULE=$(basename $SCRIPT .m)
TARGET_FILE=${MODULE}.m
DIR=$(dirname $SCRIPT)
echo "% automatically generated from: $SCRIPT">$DIR/$TARGET_FILE
echo ":- module ${MODULE}.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is cc_multi.
:- implementation.
'\$0' = \"$SCRIPT\".
'\$dp0' = \"$DIR\".
'\$#' = $#.
'\$pwd' = \"$PWD\".
:- import_module list,string, enum, float, math, int, dir, maybe.
:- pragma source_file(\"${MODULE}.m\").
#2">>$DIR/$TARGET_FILE
tail -n +2 ${SCRIPT}.tmp | \
sed 's/^main\((!IO)\)\?\s*:-/main(!IO) :- command_line_arguments(Args, !IO),/'>>$DIR/$TARGET_FILE
cd $DIR
MCFLAGS="--infer-all --no-inform-inferred --make --use-grade-subdirs"
( mmc $MCFLAGS -- $MODULE 2>/dev/null && \
mv -f ${SCRIPT}.tmp ${SCRIPT} && \
exec ./${MODULE} $*
) || ( ([ -r ${SCRIPT}.tmp ] && mv -f ${SCRIPT}.tmp ${SCRIPT} || true ) && \
cat ${MODULE}.err )
rm ${MODULE}.err
else
exit 2
fi
#!/usr/bin/env mmi
square(X) = Square :- int.pow(X, 2, Square).
main(!IO) :-
Number = (if Args = [A0 | _] then det_to_int(A0) else 2),
print("args = ", !IO), write_list(Args, ",", write, !IO), nl(!IO),
format("square(%d) = %d\n", [i(Number), i(square(Number))], !IO),
format("sloc = \"%s:%d\"\n", [s($file), i($line)], !IO),
format("$module = \"%s\"\n", [s($module)], !IO),
format("$file = \"%s\"\n", [s($file)], !IO),
format("$grade = \"%s\"\n", [s($grade)], !IO),
format("$0 = \"%s\"\n", [s('$0')], !IO),
format("$pwd = \"%s\"\n", [s('$pwd')], !IO),
format("$dp0 = \"%s\"\n", [s('$dp0')], !IO),
format("$# = %d\n", [i('$#')], !IO).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment