From a99794b57566615eff8ec48ad5121653f4fc7ae0 Mon Sep 17 00:00:00 2001 From: Bill Burdick Date: Fri, 22 Oct 2010 13:30:30 +0200 Subject: [PATCH] initial commit --- calc/README | 23 +++++++++++++++++++++++ calc/calc | 27 +++++++++++++++++++++++++++ calc/calcgo | 46 ++++++++++++++++++++++++++++++++++++++++++++++ calc/calcrc | 10 ++++++++++ 4 files changed, 106 insertions(+) create mode 100644 calc/README create mode 100755 calc/calc create mode 100755 calc/calcgo create mode 100755 calc/calcrc diff --git a/calc/README b/calc/README new file mode 100644 index 0000000..0c472f7 --- /dev/null +++ b/calc/README @@ -0,0 +1,23 @@ +Calc parses the text in a window and finds variables of the form /[a-zA-Z0-9_]+ *= *[0-9e.+\-]*/ It sends the values to standard input of the script at the bottom of the window (which starts with /^#!/), one variable per line, = (variables without values are given as 0). The script is expected to output changed variables, one per line, in the same format. Calc parses those values and replaces them in the Acme window. + +Installation: put calcrc, calcgo and calc in your path. + +To use calcgo, you need to have Go installed. You can use calcrc with normal Plan9 tools. + + view this file in Acme and middle-click 'calc' a few times to watch y and z change. + +x = 3 , y= 25 +z=27 + +To use calcrc, delete from here up to /* END +#!/usr/bin/env calcgo +z=x + y +y++ + +/* END + +#!/usr/bin/env calcrc +z=`{expr $x + $y} +y=`{expr $y + 1} + +#*/ diff --git a/calc/calc b/calc/calc new file mode 100755 index 0000000..a112ba7 --- /dev/null +++ b/calc/calc @@ -0,0 +1,27 @@ +#!/usr/bin/env rc + +# assign var value -- find var in window and replace its old value with the new one +# variables are words made of letters, numbers, and/or _, followed by '=' and, optionally, a number +fn assign { + if (echo -n '/(^|[^a-zA-Z0-9_])'$1' *= */+#0;/[0-9.e+\-]*/'|9p write acme/7/addr >[2] /dev/null) { + echo -n 'dot=addr'|9p write acme/7/ctl + echo -n $2 | 9p write acme/7/data + } + if not echo 'Could not find variable:' $1 +} + +# copy calc commands from window into /tmp/cmd +9p read acme/$winid/body|sed -e '/^#!/,$p;d'>/tmp/cmd +chmod a+x /tmp/cmd +rm -f /tmp/cmd.errs +9p read acme/7/body | + # extract variables and values from window + sed -n -e '/^#!/q;s/([a-zA-Z0-9_]+) *= *([0-9.e+\-]*)/\n\1=\2\n/g;h;:v;s/\n(.|\n)*//;s/^[a-zA-Z0-9_]+=$/&0/;/^[a-zA-Z0-9_]+=[0-9.e+\-]*/p;g;/\n/!d;s/[^\n]*\n//;h;bv' | + # pipe vars (VAR=VALUE) to command + /tmp/cmd | + # parse vars from commmand and convert to "assign" calls (above function) + awk '{loc=index($0, "=");print "assign (" substr($0, 1, loc - 1) ") (" substr($0, loc + 1) ")"}' | + # pipe assign commands to rc to plug the values back into the window + rc +if (test -s /tmp/cmd.errs) 9p write acme/$winid/errors < /tmp/cmd.errs +exit 0 diff --git a/calc/calcgo b/calc/calcgo new file mode 100755 index 0000000..ec0938c --- /dev/null +++ b/calc/calcgo @@ -0,0 +1,46 @@ +#!/usr/bin/env rc +compile=8g +link=8l +base=/tmp/calcscript +errs=$1^.errs +script=$base^.go +oldVals='' +newVals='' +file=$1 +*=() +while (line=`{read}) { + var=`{echo $line|sed -e 's/^([^=]*)=.*$/\1/'} + value=`{echo $line|sed -e 's/^[^=]*=(.*)$/\1/'} + *=($* $var $value) + if (! ~ $oldVals '') { + oldVals=$"oldVals', ' + newVals=$"newVals', ' + } + oldVals=$"oldVals'"'$var'" : '$"value + newVals=$"newVals'"'$var'" : '$var +} +cat < $script +package main +import . "fmt" +func main() { +here +while (! ~ $#* 0) { + echo (' '$1^' := float64('$2')') >> $script + shift 2 +} +<$file{ +read>/dev/null +cat >> $script +} +cat<> $script + func() { + oldVars := map[string]float64{$oldVals} + for k, v := range map[string]float64{$newVals} { + if v != oldVars[k] { + Println(k, "=", v) + } + } + }() +} +here +$compile -o $base^.8 $script > $errs && $link -o $base $base^.8 >> $errs && $base diff --git a/calc/calcrc b/calc/calcrc new file mode 100755 index 0000000..a9241d8 --- /dev/null +++ b/calc/calcrc @@ -0,0 +1,10 @@ +#!/usr/bin/env rc +while (line=`{read}) { + CALC_VARS=($CALC_VARS `{echo $line|sed -e 's/^([^=]*)=.*/\1/'}) + eval $line + eval 'CALC_OLD_'$line +} +. $1 +for (v in $CALC_VARS) { + if (test $$v '!=' $(CALC_OLD_$v)) echo $v '=' $$v +} -- 2.11.4.GIT