comparison vext @ 303:523f8f1789b4

Switch to Vext
author Chris Cannam
date Mon, 10 Jul 2017 18:57:08 +0100
parents
children d741e2c90eab
comparison
equal deleted inserted replaced
302:1e6cee321cc0 303:523f8f1789b4
1 #!/bin/bash
2
3 # Disable shellcheck warnings for useless-use-of-cat. UUOC is good
4 # practice, not bad: clearer, safer, less error-prone.
5 # shellcheck disable=SC2002
6
7 sml="$VEXT_SML"
8
9 set -eu
10
11 mydir=$(dirname "$0")
12 program="$mydir/vext.sml"
13
14 # We need one of Poly/ML, SML/NJ, or MLton. Since we're running a
15 # single-file SML program as if it were a script, our order of
16 # preference is based on startup speed.
17
18 if [ -z "$sml" ]; then
19 if sml -h 2>&1 | grep -q 'Standard ML of New Jersey'; then
20 sml="smlnj"
21 # We would prefer Poly/ML to SML/NJ, except that Poly v5.7 has a
22 # nasty bug that occasionally causes it to deadlock on startup.
23 # That appears to be fixed in their repo, so we could promote it
24 # up the order again at some point in future
25 elif echo | poly -v 2>/dev/null | grep -q 'Poly/ML'; then
26 sml="poly"
27 elif mlton 2>&1 | grep -q 'MLton'; then
28 sml="mlton"
29 else cat 1>&2 <<EOF
30
31 ERROR: No supported SML compiler or interpreter found
32 EOF
33 cat <<EOF
34
35 The Vext external source code manager needs a Standard ML (SML)
36 compiler or interpreter to run.
37
38 Please ensure you have one of the following SML implementations
39 installed and present in your PATH, and try again.
40
41 1. Standard ML of New Jersey
42 - often found in a distribution package called: smlnj
43 - executable name: sml
44
45 2. Poly/ML
46 - often found in a distribution package called: polyml
47 - executable name: poly
48
49 3. MLton
50 - often found in a distribution package called: mlton
51 - executable name: mlton
52
53 EOF
54 exit 2
55 fi
56 fi
57
58 tmp_sml=$(mktemp /tmp/vext-XXXXXXXX.sml)
59 tmp_out=$(mktemp /tmp/vext-XXXXXXXX.bin)
60
61 trap 'rm -f $tmp_sml $tmp_out' 0
62
63 arglist=""
64 for arg in "$@"; do
65 if [ -n "$arglist" ]; then arglist="$arglist,"; fi
66 if echo "$arg" | grep -q '[^a-z]' ; then
67 arglist="$arglist\"usage\""
68 else
69 arglist="$arglist\"$arg\""
70 fi
71 done
72
73 case "$sml" in
74 poly) echo 'use "'"$program"'"; vext ['"$arglist"'];' |
75 poly -q --error-exit ;;
76 mlton)
77 cat "$program" > "$tmp_sml"
78 echo 'val _ = main ()' >> "$tmp_sml"
79 mlton -output "$tmp_out" "$tmp_sml"
80 "$tmp_out" "$@" ;;
81 smlnj)
82 cat "$program" | (
83 cat <<EOF
84 val smlrun__cp =
85 let val x = !Control.Print.out in
86 Control.Print.out := { say = fn _ => (), flush = fn () => () };
87 x
88 end;
89 val smlrun__prev = ref "";
90 Control.Print.out := {
91 say = fn s =>
92 (if String.isSubstring " Error" s
93 then (Control.Print.out := smlrun__cp;
94 (#say smlrun__cp) (!smlrun__prev);
95 (#say smlrun__cp) s)
96 else (smlrun__prev := s; ())),
97 flush = fn s => ()
98 };
99 EOF
100 cat -
101 cat <<EOF
102 val _ = vext [$arglist];
103 val _ = OS.Process.exit (OS.Process.success);
104 EOF
105 ) > "$tmp_sml"
106 CM_VERBOSE=false sml "$tmp_sml" ;;
107 *)
108 echo "Unknown SML implementation name: $sml";
109 exit 2 ;;
110 esac
111