Skip to content

Instantly share code, notes, and snippets.

@eliasdorneles
Last active February 28, 2018 00:53
Show Gist options
  • Save eliasdorneles/59f441bb80618886f00b7c4e336814e6 to your computer and use it in GitHub Desktop.
Save eliasdorneles/59f441bb80618886f00b7c4e336814e6 to your computer and use it in GitHub Desktop.
A hack to simulate a REPL for VOC
#!/bin/bash
# For arrow-keys history and other cmdline goodies, install rlwrap and replace
# the first line by: #!/usr/bin/rlwrap bash
# This is a hack to simulate a REPL for VOC (http://pybee.org/voc)
#
# Behind the scenes, every time you try a new expression it appends it to a temporary
# file, recompiles it and re-runs it, omitting the previous output.
#
# This has the caveat that if some expression you enter raises an exception,
# it will be always raised no matter you give in the next input.
#
# This is an experiment I did because I wanted a quick way of trying things without
# having to create a bunch of files everytime.
VOC_DIR=../voc
abort() {
echo "$*"; exit 1;
}
usage() {
abort "Usage: $(basename $0) [-h|--help]"
}
require() {
type $1 >/dev/null 2>/dev/null
}
classpath=.
while [ "${1#-}" != "$1" ]; do
case "$1" in
-h|--help) usage;;
-cp|--classpath) classpath="$2:."; shift;;
*) usage;;
esac
shift
done
pyfile="_tempfile.py"
basefile=$(basename $pyfile .py)
workdir="$(dirname $0)"
(cd $VOC_DIR && ant java)
cd "$workdir" || abort "Could not enter directory $workdir"
> $pyfile
lines_previous_output=0
while read -p '>>> ' line
do
if echo "$line" | grep -vq -E '[^=]=[^=]|^from|^import|^print|^$|^raise |^del |^try:' && echo "$line" | grep -vq '^print'; then
line="_ = ${line}; print(repr(_))"
fi
echo "$line" >> $pyfile
voc $pyfile
output=$(java -cp $VOC_DIR/dist/python-java-support.jar:$classpath python.${basefile})
java_status=$?
if [ "$output" != "" ]; then
if [ "$lines_previous_output" != "0" ]; then
echo "$output" | sed "1,$lines_previous_output d"
else
echo "$output"
fi
lines_previous_output=$(echo "$output" | wc -l)
fi
if [ $java_status != 0 ]; then
sed -i '$d' $pyfile
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment