[q-lang-cvs] qcalc calclib.q,1.14,1.15 qcalc.q,1.100,1.101
Brought to you by:
agraef
From: Albert G. <ag...@us...> - 2007-11-07 03:50:49
|
Update of /cvsroot/q-lang/qcalc In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv22911 Modified Files: calclib.q qcalc.q Log Message: add support for symbolic (quoted) cell indices in formulas Index: qcalc.q =================================================================== RCS file: /cvsroot/q-lang/qcalc/qcalc.q,v retrieving revision 1.100 retrieving revision 1.101 diff -C2 -d -r1.100 -r1.101 *** qcalc.q 7 Nov 2007 02:28:45 -0000 1.100 --- qcalc.q 7 Nov 2007 03:50:40 -0000 1.101 *************** *** 2391,2395 **** tokenize S = cat $ regex "gn" ! "\"([^\\\"\\\\]|\\\\.|\\\\\n)*\"|//.*|[$A-Za-z_][$A-Za-z_0-9:]+" S [regskip,reg 0] ++ [[regskip]]; --- 2391,2395 ---- tokenize S = cat $ regex "gn" ! "\"([^\\\"\\\\]|\\\\.|\\\\\n)*\"|//.*|'[(]?[$A-Za-z_][$A-Za-z_0-9:]+[)]?|[$A-Za-z_][$A-Za-z_0-9:]+" S [regskip,reg 0] ++ [[regskip]]; *************** *** 2415,2418 **** --- 2415,2420 ---- special deps X; + deps ''X = []; + deps '(X Y) = deps 'X ++ deps 'Y; deps '(X|Y) = deps 'X ++ deps 'Y; *************** *** 2438,2441 **** --- 2440,2446 ---- special subst X; + // don't perform replacements in quoted parts + subst ''X = ''X; + subst '(X Y) = '(`(subst 'X) `(subst 'Y)); subst '(X|Y) = '(`(subst 'X)|`(subst 'Y)); Index: calclib.q =================================================================== RCS file: /cvsroot/q-lang/qcalc/calclib.q,v retrieving revision 1.14 retrieving revision 1.15 diff -C2 -d -r1.14 -r1.15 *** calclib.q 6 Nov 2007 20:08:50 -0000 1.14 --- calclib.q 7 Nov 2007 03:50:39 -0000 1.15 *************** *** 9,13 **** lives. Otherwise you will have to set your QPATH accordingly. */ ! import hdict; /* The following should be considered private; this data is for use by the --- 9,13 ---- lives. Otherwise you will have to set your QPATH accordingly. */ ! from hdict import emptyhdict; /* The following should be considered private; this data is for use by the *************** *** 26,29 **** --- 26,50 ---- public index, row, column; + /* The following functions can be used to convert between numeric cell indices + like (0,0) and symbolic ones, either in string form ("A1") or as a quoted + symbol ('A1). The cellindex function converts from a string or a quoted + symbol to the numeric index of the form (I,J), whereas cellstr and cellname + convert a numeric index back to a string or a quoted symbol, respectively. + + In the string and quoted symbol form, the '$' symbol is permitted as well. + Note that constructions like '(A$4) aren't really symbols in Q, but + applications of the built-in $ operator, so they have to be surrounded with + parentheses as indicated. Also note that, when converting from a numeric + index back to its symbolic form, the $ indicators are lost since they + aren't included in the numeric representation. + + The most useful routine is the cellindex function which can be used with a + quoted cell symbol to embed symbolic cell names used as parameters to + functions like setval into formulas, where they are adjusted automatically + when copied or filled (actually, this conversion is done by setval et al + automagically). */ + + public cellindex X, cellstr KEY, cellname KEY; + /* Send a status message. The message will be shown in the QCalc status line. This can be used to provide some feedback during longer computations. */ *************** *** 109,122 **** hslider ARGS, vslider ARGS, pushbutton ARGS, togglebutton ARGS; ! /* Set a cell value. The given cell index KEY is in the same format as ! returned by the index function (see above), and X may be any Q expression. ! If the given cell is an ordinary cell (no GUI element), then the current ! cell value is overwritten, so you should make sure that you do not have ! important data there. For GUI elements, the value of the element is changed ! accordingly, instead of overwriting the cell. In both cases, after changing ! the cell value QCalc will update its display and trigger updates in ! dependent cells as usual. Note, however, that the triggered changes will ! *not* take effect immediately, rather they are processed by QCalc when the ! current computation is finished. */ public setval KEY X; --- 130,144 ---- hslider ARGS, vslider ARGS, pushbutton ARGS, togglebutton ARGS; ! /* Set a cell value. The given cell index KEY can either be in the numeric ! format as returned by the index function or in any of the symbolic formats ! supported by the cellindex routine (see above), and X may be any Q ! expression. If the given cell is an ordinary cell (no GUI element), then ! the current cell value is overwritten, so you should make sure that you do ! not have important data there. For GUI elements, the value of the element ! is changed accordingly, instead of overwriting the cell. In both cases, ! after changing the cell value QCalc will update its display and trigger ! updates in dependent cells as usual. Note, however, that the triggered ! changes will *not* take effect immediately, rather they are processed by ! QCalc when the current computation is finished. */ public setval KEY X; *************** *** 127,131 **** convenience functions to create matrices with just one row or column for a given list of values, respectively. In any case the matrix or vector is ! inserted into the table starting at the given index KEY. */ public matrix KEY Xs, rowvect KEY Xs, colvect KEY Xs; --- 149,154 ---- convenience functions to create matrices with just one row or column for a given list of values, respectively. In any case the matrix or vector is ! inserted into the table starting at the given index KEY (given in any of ! the formats supported by setval). */ public matrix KEY Xs, rowvect KEY Xs, colvect KEY Xs; *************** *** 137,143 **** --- 160,202 ---- column = J where (I,J) = get YYKEY; + /* Some helper routines. */ + + from system import regex, reg; + + private colno C, cno C, colstr N, qadic; + + colno C = pred $ foldl ((+).(*26)) 0 $ map cno $ chars C; + cno C = C-"A"+1; + + colstr N = strcat $ reverse ["A"+X|map (("A"+).pred) L] + where [X|L] = qadic 26 N; + = "A" otherwise; + + qadic B N = list $ map (mod B) $ takewhile (>0) $ iterate (div B) N; + + /* Conversions cell indices <-> string or quoted symbol. */ + + cellindex S:String + = (I,J) + where [(C,R)] = + regex "" "^[$]?([A-Z][A-Z]?)[$]?([0-9]+)$" S + (reg 1,reg 2), + I:Int = val R-1, J:Int = colno C; + cellindex 'X = (I,J) + where (I,J) = cellindex $ strcat $ + filter ((<0).flip pos " ()") $ chars $ strq 'X; + + cellstr (I:Int,J:Int) + = sprintf "%s%d" (colstr J,I+1) + if (I>=0) and then (J>=0); + cellname (I,J) = valq S where S:String = cellstr (I,J); + + /* Message display */ + message S:String = printf "\f+++ Message: %s\n" (str S); + /* GUI elements. */ + checkbox S:String = checkbox (S,false); *************** *** 270,287 **** where (I,J) = get YYKEY; ! setval (I:Int,J:Int) X ! = printf "\f+++ Update: %s %s\n" (str (I,J),str X) ! if (I>=0) and then (J>=0); ! matrix (I0:Int,J0:Int) Xs:List = dowith setval Ks (cat Xs) where Ks = [(I0+I,J0+J) : I in [0..N-1], J in [0..M-1]] if all islist Xs and then all ((=M).(#)) Xs where Ys:List = hd Xs, N = #Xs, M = #Ys ! if (I0>=0) and then (J0>=0); ! rowvect (I0:Int,J0:Int) Xs:List ! = matrix (I0,J0) [Xs]; ! colvect (I0:Int,J0:Int) Xs:List ! = matrix (I0,J0) $ map (push []) Xs; --- 329,357 ---- where (I,J) = get YYKEY; ! /* setval and friends. */ ! private indexarg KEY; ! ! indexarg (I:Int,J:Int) ! = (I,J) if (I>=0) and then (J>=0); ! indexarg S:String ! = (I,J) where (I,J) = cellindex S; ! indexarg 'X = (I,J) where (I,J) = cellindex 'X; ! ! setval KEY X = printf "\f+++ Update: %s %s\n" (str (I,J),str X) ! where (I:Int,J:Int) = indexarg KEY; ! ! matrix KEY Xs:List = dowith setval Ks (cat Xs) where Ks = [(I0+I,J0+J) : I in [0..N-1], J in [0..M-1]] if all islist Xs and then all ((=M).(#)) Xs where Ys:List = hd Xs, N = #Xs, M = #Ys ! where (I0:Int,J0:Int) = indexarg KEY; ! rowvect KEY Xs:List ! = matrix (I0,J0) [Xs] ! where (I0:Int,J0:Int) = indexarg KEY; ! colvect KEY Xs:List ! = matrix (I0,J0) $ map (push []) Xs ! where (I0:Int,J0:Int) = indexarg KEY; |