File: expressions.adoc

package info (click to toggle)
nickle 2.107
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 3,756 kB
  • sloc: ansic: 27,954; yacc: 1,874; lex: 954; sh: 204; makefile: 13; lisp: 1
file content (241 lines) | stat: -rw-r--r-- 4,256 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
= Nickle Expressions

Expression types are listed in order of increasing precedence. 

== Variable declarations

Variable declarations are expressions in Nickle; the value of a declaration is the value of the last variable with an initialization expression.
For example, 

----

> int val;
> val = (int i=2, j=3);
> print val
global int val = 3;
----

If no initialization expressions are present, it is an error to use the value of the expression. 

----

> val = (int i,j);
Unhandled exception "uninitialized_value" at <stdin>:73
        "Uninitialized value"
----

Because they are expressions, declarations can be used in contructs such as: 

----

for (int i = 0; i  10; i++)
{
        
}
----

== Anonymous function declarations

Functions may be declared anonymously and used immediately, e.g. 

----

> (int func ( int a, int b ) { return a + b; })(2,3)
5
----

Any context available to the function at definition time will be available whenever it is executed.
See Storage classes in the section on Variables. 

== Binary operators

Addition::
`a + b`

Subtraction::
`a - b`

Multiplication::
`a * b`

Division, Integer division::
`a / b` +
`a // b`

Remainder::
`a % b`

Exponentiation::
`a ** b`

Left shift, Right shift::
`a << b` +
`a >> b`

Binary xor, Binary and, Binary or::
`a ^ b` +
`a & b` +
`a | b`

Boolean and, Boolean or::
`a && b` +
`a || b`

Equal, Not equal::
`a == b` +
`a != b`

Less than, Less than or equal to::
`a < b` +
`a <= b`

Greater than, Greater than or equal to::
`a > b` +
`a >= b`

Assignment::
`a = b`

Assignment with operator::
`a += b` +
`a -= b` +
`a *= b` +
`a /= b` +
`a //= b` +
`a %= b` +
`a **= b` +
`a >>= b` +
`a <<= b` +
`a ^= b` +
`a &= b` +
`a |= b`

== Unary operators

Dereference:::
`* a`

Reference:::
`& a`

Negation:::
`- a`

Bitwise inverse:::
`~ a`

Logical not:::
`! a`

Factorial:::
`a !`

Increment:::
`+++++a+++` +
`+++a+++++`

Decrement:::
`--a` +
`a--`

== Constants

=== Integer constants

Integer constants with a leading zero are interpreted in octal, with a leading 0x in hex-decimal and with a leading 0b in binary.
Here are some examples: 

----

12      /* 12, decimal */
014     /* 12, octal */
0xc     /* 12, hex */
0b1100  /* 12, binary */
----

=== Rational constants

Rational constants are the combination of an integer part, a mantissa with an initial and repeating part, and an exponent.
All of these pieces are optional, but at least one of the parts (other than the exponent) must be present.
If no fractional part is given, the resulting type of the value is int rather than rational.
Some examples: 

----

> 12
12
> 12.5
12.5
> .34
0.34
> .{56}
0.{56}
> .34e3
340
> .{56}e12
565656565656.{56}
----

=== String constants

String constants are surrounded in double quotes, e.g.  `"hello,
world"`. Characters preceded by a backslash stand for themselves,
including double-quote (`"`) and blackslash (`\`). The following
backslashed characters are special:

* `\n` is newline
* `\r` is carriage return
* `\b` is backspace
* `\t` is tab
* `\f` is formfeed


== Variables

The name of a variable is replaced by the value of that variable in
expressions.  If the name is of a pointer, preceding it with the `*`
dereference operator yields the value of its target.

== Struct and union references

The construct 

_str_ `.` _name_

yields the element _name_ of the struct or union _str_
To retrieve elements from a struct or union pointer, as in C, use 

_str_ `+->+` _name_

== Array references

_array_ `[` _expr_ `]` +
_array_ `[` expr `,` _expr_ `,` ... `,` _expr_ `]`

Elements of an array are indexed with square braces.  Elements of
multidimensional arrays have indices in each dimension as in the
second form.  Pointers to arrays can be indexed by placing the `*`
dereference operator between the name of the array and the first
square brace:

_array_ `*[` _expr_ `]`

This is, however, deprecated in favor of using references to arrays, which have no such problems. 

== The fork operator

The fork operator evaluates its argument in a child thread.
See the section on Concurrency. 

== Comma operator

_expr_ `,` _expr_

Evaluates each expression in turn, the resulting value is that of the right hand expression.
For example, 

----

> 1+2, 5!, 3**4, 27/3
9
----