Debian Codegraph @

help/expressions

raw
1 ! $Id: expressions,v 1.3 2002-02-19 19:43:28 f Exp $
2 !
3 ! Copyright (c) 1990-2001 Michael Sandrof, Troy Rollo, Matthew Green,
4 ! and other ircII contributors.
5 !
6 ! All rights reserved. See the HELP SCROLLZ COPYRIGHT file for more
7 ! information.
8 !
9 This help file describes expresssions available in ${}, and the
10 @, IF, WHILE, and FOREACH commands.
11
12 Variable expressions are used to perform arithmetic, string and
13 boolean operations. Their syntax is similar to the syntax of most
14 computer languages, and is modelled on C syntax. A simple example
15 of this, is that when ${A + 2} is used, and the ASSIGNed variable
16 A has the value '3', the result is 5.
17
18 Brackets can be used in these expressions, so (A+2)*3 would give a
19 value of (3+2)*3 == 15, whereas A+2*3 would give a value of 9, because
20 multiplication is performed before addition.
21
22 The common arithmetic operations (+, -, *, /) are all available.
23 Additionally, there is a string concatenation operator (##).
24 This treats both sides as strings and pastes them together. Thus
25 if A is 3 and B is 7, "A##B" becomes "37", but "A+B" becomes 10.
26
27 You can also assign values to a variable in these expressions
28 using the '=' operator. Thus if you have the expression "C = A+B",
29 it will assign the value 10 to C. This has a number of affects.
30 The most common case is the @ command, where you can enter this expression
31 literally:
32
33 @ C = A + B
34
35 The '=' operator returns the value assigned. Thus you can extend
36 this to:
37
38 @ D = C = A + B
39
40 Which will assign the value 10 to both C and D. In a $ expression,
41 you might want to assign a value to a variable, and display it at the
42 same time. Thus you might do the following:
43
44 echo The value of C is now ${C = A+B}
45
46 which would set C to 10 and display "The value of C is now 10".
47
48 Comparison operations also exist. These return 1 if the comparison is
49 valid (true) and 0 if it is invalid (false). The comparison operations
50 available are:
51
52 == (equal to)
53 > (greater than)
54 >= (greater than or equal to)
55 < (less than)
56 <= (less than or equal to)
57 != (not equal to)
58
59 Thus, the following expressions would result in the following results:
60 ( recall A = 3 and B = 7 )
61
62 A == B FALSE 0
63 A == 3 TRUE 1
64 A > 3 FALSE 0
65 A >= 3 TRUE 1
66 A != 3 TRUE 1
67
68 These expressions can be combined with || (OR), && (AND) and ^^ (XOR).
69 So (A == 3) || (B==3) would be TRUE, and (A == 2) && (B == 7) would be
70 FALSE. You can also negate expressions with ! (NOT), so !(A == 3) would
71 be FALSE. These boolean expressions are used primarily in IF and WHILE.
72
73 A string expression is considered to be true if it is non empty. So
74 if E has the value "word", then E || ( A > 3) is true, because E has
75 a non empty value, and !E is false.
76
77 Functions can also appear in expressions. The arguments to functions
78 are evaluated as normal $ type expressions. Thus the following alias:
79
80 alias test echo ${ MID(3 2 $0) + 5) }
81
82 is executed with /test 123456, would give 34 (Two digits from position 3
83 in $0) + 5, giving 39. If the function returns another function name, that
84 function name can be called with another set of brackets. Thus if you have
85 the following set of aliases:
86
87 alias fptr
88 {
89 if ( [$0] )
90 { @ function_return = func1 }
91 { @ function_return = func2 }
92 }
93 alias func1 @ function_return = this is $0
94 alias func2 @ function_return = that is $0
95 alias check echo ${ fptr($0)($1) }
96
97 then /check 0 1 would print "that is 1", and /check 1 0 would print
98 "this is 0".
99
100 Array expressions can also be used in this way. For example:
101
102 assign A.1.1 One One was a racehorse
103 assign A.1.2 Two Two was one too
104 assign A.2.1 One One won one race
105 assign A.2.2 Two Two won one too
106 alias rhyme echo ${ A[$0][$1] }
107
108 would cause /rhyme 2 1 to print "One One won one race".
109
110 Function and array expressions can be combined to give a form
111 of pointer arithmetic like that seen in C. If a function
112 RhymeNum exists as follows:
113
114 alias RhymeNum @ function_return = [A]
115 and rhyme is changed to:
116 alias rhyme echo ${ RhymeNum()[$0][$1] }
117
118 /rhyme 2 1 still prints "One One won one race". This is because
119 RhymeNum returns A, and the expression then becomes A[$0][$1].
120 The $0 is expanded to 2, so it becomes A.2[$1], and the $1 is
121 expanded to 1, so it becomes A.2.1, which is then substituted as
122 a variable for "One One won one race". The reverse is also possible.
123 For example, a robot might have the following:
124
125 alias thing.0 @ function_return = laughs his silly head off
126 alias thing.1 @ function_return = growls menacingly
127 alias thing.2 @ function_return = smiles like a crocodile
128 alias thing.3 @ function_return = wails uncontrollably
129 alias something SAY WereBot ${ thing[$RANDOM(4)]() }
130
131 Will cause WereBot to say that it's laughing itself silly, growling
132 menacingly, smiling like a crocodile or wailing uncontrollably.
133 Assuming RANDOM(4) results in a value of 2, This expands to
134 thing.2(), which is then considered to be a function and substituted
135 to "smiles like a crocodile", the end result being that WereBot will
136 say "WereBot smiles like a crocodile".
137
138 Finally, there are occasions when you need to get back to the $
139 substitution level. This can be done by enclosing text to be used at
140 this level in [..]. For example, [A] substitutes to a literal 'A',
141 whereas A on its own substitutes to 3, and [$0] is needed to get
142 argument 0, because 0 on its own is taken to be the number 0.
143 Example:
144
145 alias something SAY WereBot ${ [$0][$RANDOM(4)]() }
146
147 will case /something thing to first expand [$0] to thing, giving
148 thing[$RANDOM(4)](), and if $RANDOM(4) returns 1, this becomes
149 thing.1(), which expands to "growls menacingly", and causes
150 WereBot to say "WereBot growls menacingly".
151
152
153 The following is the parse tree for expressions such as those
154
155 NU_EXPR = NU_CONJ
156 NU_CONJ = NU_CONJ && NU_CONJ |
157 NU_CONJ || NU_CONJ |
158 NU_CONJ ^^ NU_CONJ |
159 NU_ASSN
160 NU_ASSN = varexp = NU_ASSN |
161 NU_COMP
162 NU_COMP = NU_COMP == NU_COMP |
163 NU_COMP != NU_COMP |
164 NU_COMP > NU_COMP |
165 NU_COMP >= NU_COMP |
166 NU_COMP < NU_COMP |
167 NU_COMP <= NU_COMP |
168 NU_ADD
169 NU_ADD = NU_ADD + NU_ADD |
170 NU_ADD - NU_ADD |
171 NU_ADD ## NU_ADD |
172 NU_MULT
173 NU_MULT = NU_MULT * NU_MULT |
174 NU_MULT / NU_MULT |
175 NU_UNIT
176 NU_UNIT = token NUX_MODIF |
177 unaryop token |
178 ( NU_EXPR ) |
179 [ expression ] NUX_MODIF
180
181 NUX_MODIF = ( expression ) NUX_MODIF |
182 [ expression ] NUX_MODIF
183
184 Special cases:
185 If a (...) or {...} construct is quoted with \(...\) or \{...\}
186 then variable expansion will take place on the first available
187 parse run. However, after parsed, the \'s are eaten. e.g.
188 alias bonk echo ($0) will return "($*)" regardless of
189 the argument to the alias. No expansion takes place. However, in
190 alias bonk echo \($0\)
191 expansion takes place as the special meaning of the () is taken
192 away by \. This will return "(arguments to bonk)".