From ecbc58fb917c292f05bed401afe7be0a80971d80 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Wed, 23 Sep 2015 17:53:39 +0200 Subject: different operators can have the same precedence now * SciTECO now has the same operator precedence table as C. * It is numerically important whether different operators have the same precedence. E.g. "5*2/4" used to be evaluated by SciTECO as "5*(2/4)" since division had a higher precedence than multiplication. Within in real (!) numbers this would be the expected evaluation order. Users of other programming languages however would expect the expression to be evaluated as "(5*2)/4" which makes a numerical difference when working with integers. * Operator precedence has been implemented by encoding it into the enumeration values used to represent different operators. Calculating the precedence of a given operator can then be done very efficiently and elegantly (in our case using a plain right shift operation). * documentation updated. We use a precedence table now. --- doc/sciteco.7.template | 62 +++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 28 deletions(-) (limited to 'doc/sciteco.7.template') diff --git a/doc/sciteco.7.template b/doc/sciteco.7.template index 9c6180c..30d6c76 100644 --- a/doc/sciteco.7.template +++ b/doc/sciteco.7.template @@ -726,40 +726,55 @@ command. .SS Operators . The following binary operators are supported with descending +precedence, whereas borders separate operators of the \fIsame\fP precedence: -.TP 2 -.IB base " ^* " power +.TS +expand,box,tab(;); +L LX. +\fIbase\fB ^* \fIpower\fR;T{ Raise \fIbase\fP to \fIpower\fP. -.TP -.IB a " ^/ " b +T} += +\fIa\fB ^/ \fIb\fR;T{ Remainder of \fIa\fP divided by \fIb\fP. It is actually a modulo operation conforming to the host C compiler's modulo semantics. -.TP -.IB a " / " b +T} +\fIa\fB / \fIb\fR;T{ Divide \fIa\fP by \fIb\fP. Naturally it is a division without remainder. -.TP -.IB a " * " b +T} +\fIa\fB * \fIb\fP;T{ Multiply \fIa\fP by \fIb\fP. -.TP -.IB a " - " b +T} += +\fIa\fB - \fIb\fP;T{ Subtract \fIb\fP from \fIa\fP. -.TP -.IB a " + " b +T} +\fIa\fB + \fIb\fP;T{ Add \fIb\fP to \fIa\fP. -.TP -.IB a " & " b +T} += +\fIa\fB & \fIb\fP;T{ Binary AND. -.TP -.IB a " ^# " b +T} += +\fIa\fB ^# \fIb\fP;T{ Binary exclusive OR (XOR). -.TP -.IB a " # " b +T} += +\fIa\fB # \fIb\fP;T{ Binary OR. +T} +.TE .LP +All binary operators are \fIleft-associative\fP, ie. associate +from left to right, in \*(ST. Round brackets may be used for grouping expressions to override -operator precedence. +operator precedence and associativity. +For instance, since \(lq\fB^/\fP\(rq, \(lq\fB/\fP\(rq and \(lq\fB*\fP\(rq +all have the same precedence, the expression \(lq1 ^/ 2 / 3 * 4\(rq is +equivalent to \(lq(((1 ^/ 2) / 3) * 4)\(rq. The opening bracket also represents an argument barrier, ie. the first command after the opening bracket does not see and cannot pop any arguments from the stack. @@ -774,15 +789,6 @@ mandatory in classic TECO, like in \(lq.ZD\(rq. I recommend to use it as much as possible in code where clarity matters. .LP -Note that \*(ST assigns an unique precedence to each operator -instead of having several operators (like addition and subtraction) -share the same precedence and evaluate them according to their -associativity. -Still the precedence rules should ensure that expressions are -evaluated similarily to other programming languages. -When the same operator is used repeatedly, expressions are always -evaluated with left-to-right associativity. -For instance, \(lq2^*3^*4\(rq is evaluated like \(lq(2^*3)^*4\(rq. The arithmetic evaluation order established by the operator precedence or braces must not be confused with the execution order of \*(ST commands which is always strictly from left to right (unless flow control -- cgit v1.2.3