-
Notifications
You must be signed in to change notification settings - Fork 3
/
smlrun
executable file
·107 lines (89 loc) · 2.26 KB
/
smlrun
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
#!/bin/bash
#
# smlrun - run a SML program defined in a SML or MLB file using SML/NJ
#
# Chris Cannam, 2015-2022. MIT licence
set -e
usage() {
me=$(basename "$0")
echo 1>&2
echo "$me: Run a SML program defined in a MLB file using SML/NJ" 1>&2
echo 1>&2
echo "Usage: $me [-v] file.sml" 1>&2
echo " $me [-u] [-v] file.mlb" 1>&2
echo "where" 1>&2
echo " -u: Unique: \"use\" each file only once, regardless of how many times it" 1>&2
echo " appears in the MLB file. See \"polybuild\" for discussion." 1>&2
echo " -v: Verbose: Do not suppress output from interactive environment." 1>&2
echo 1>&2
exit 2
}
if echo | sml | grep -q Jersey ; then :
else
echo "*** Error: SML/NJ binary 'sml' not in path" 1>&2
exit 1
fi
unique_arg=""
if [ "$1" = "-u" ]; then
unique_arg="-u"
shift
fi
verbose=no
if [ "$1" = "-v" ]; then
verbose=yes
shift
fi
arg="$1"
if [ -z "$arg" ]; then
usage
fi
shift
set -u
mydir=$(dirname "$0")
. "$mydir/smlbuild-include.sh"
out=$(get_outfile "$arg")
tmpout=$(get_tmpsmlfile "$arg")
trap "rm -f ${tmpout}" 0
case "$verbose" in
yes)
expand_arg -u "$arg" | \
sed 's/^\(.*\)$/use "\1";/' | # wrap filenames in REPL use calls
(
cat -
cat <<EOF
val _ = main ();
val _ = OS.Process.exit (OS.Process.success);
EOF
) > "$tmpout"
sml "$tmpout" "$@"
;;
no)
expand_arg -u "$arg" | \
sed 's/^\(.*\)$/use "\1";/' | # wrap filenames in REPL use calls
(
cat <<EOF
val smlrun__cp =
let val x = !Control.Print.out in
Control.Print.out := { say = fn _ => (), flush = fn () => () };
x
end;
val smlrun__prev = ref "";
Control.Print.out := {
say = fn s =>
(if String.isSubstring " Error" s orelse String.isSubstring "failed: " s
then (Control.Print.out := smlrun__cp;
(#say smlrun__cp) (!smlrun__prev);
(#say smlrun__cp) s)
else (smlrun__prev := s; ())),
flush = fn s => ()
};
EOF
cat -
cat <<EOF
val _ = main ();
val _ = OS.Process.exit (OS.Process.success);
EOF
) > "$tmpout"
CM_VERBOSE=false sml "$tmpout" "$@" | tail -n +3
;;
esac