-
Notifications
You must be signed in to change notification settings - Fork 2
/
ttvars
executable file
·96 lines (71 loc) · 1.91 KB
/
ttvars
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
#!/bin/zsh
# See: https://github.com/hkoba/mytools/wiki/ttvars
emulate -L zsh
setopt extended_glob
scriptfn=$0
bindir=$(cd $scriptfn:h; print $PWD)
function die { echo 1>&2 $*; exit 1 }
function usage {
if ((ARGC)); then print -- $* 1>&2; fi
cat 1>&2 <<EOF; exit 1
Usage: ${scriptfn:t} Vars Vars...
Example:
${scriptfn:t} y,n yes,no
${scriptfn:t} 'x <= 3, x > 3' 'y <= 5, y > 5' 'z <= 0, z > 0'
Or even simply:
${scriptfn:t} x y z
EOF
}
if [[ -n $PROLOG_CMD && -x $PROLOG_CMD ]]; then
prolog=$PROLOG_CMD
elif (($+commands[gprolog])); then
prolog=gprolog
elif (($+commands[swipl])); then
prolog=swipl
else
die "Can't find prolog interpreter!"
fi
function list {
print -- "[${(j/,/)argv}]"
}
function literal2boolpairs {
local exp
for exp in ${argv//<\=/\=<}; do
if [[ $exp == [_[:alpha:]][_[:alnum:]]# ]]; then
print "[$exp=1, $exp=0]"
else
print -R "[$exp]"
fi
done
}
function cmd_gprolog {
# To suppress compilation messages from consult, I use redirection here.
# To use this, /dev/fd/3 should be available
[[ -w /dev/fd/1 ]] || die /dev/fd/N is not available on your system.
local script input
input=$(list $argv)
script=(
"consult('$solver')"
"open('/dev/fd/3',write,Out)"
"set_output(Out)"
"tt($input, write_tsv)"
)
[[ -n $o_verbose ]] || exec 2>/dev/null
gprolog --init-goal "(${(j/,/)script};halt(0))" 3>&1 1>&2
}
function cmd_swipl {
local script input
input=$(list $argv)
script=(
"tt($input, write_tsv)"
)
[[ -n $o_verbose ]] || exec 2>/dev/null
swipl -f $solver -g "(${(j/,/)script};halt(0))"
}
solver=$scriptfn.prolog
[[ -r $solver ]] || die "Can't find solver program $solver"
zparseopts -D -K h=o_help v=o_verbose x=o_xtrace || usage
[[ -n $o_help ]] && usage
((ARGC)) || usage
[[ -n $o_xtrace ]] && set $o_xtrace
cmd_$prolog ${(f)"$(literal2boolpairs "$@")"}