zuzu-rust 0.2.0

Rust implementation of ZuzuScript
Documentation
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
=encoding utf8

=head1 NAME

std/path/z/operators - Operator definitions for ZPath.

=head1 IMPLEMENTATION SUPPORT

This module is supported by all implementations of ZuzuScript.

=head1 DESCRIPTION

This module defines the base ZPath operator model and the standard
operator table used by C<std/path/z>.

=head1 EXPORTS

=head2 Traits

=over

=item C<EvalHelpers>

Shared helpers for operator and function definitions.

=over

=item C<< helper.wrap(value) >>

Parameters: C<value> is any value. Returns: C<Array>. Wraps C<value> as
a one-item ZPath node array.

=item C<< helper.wrap_for_array(value) >>

Parameters: C<value> is any value. Returns: C<Node>. Wraps C<value> as a
ZPath node for array results.

=back

=back

=head2 Classes

=over

=item C<< Operator({ spelling: String, kind: String, precedence: Number, ... }) >>

Constructs an operator definition. Returns: C<Operator>.

=over

=item C<< operator.is_unary() >>, C<< operator.is_binary() >>

Parameters: none. Returns: C<Boolean>. Reports whether the operator is
unary or binary.

=item C<< operator.requires_whitespace() >>

Parameters: none. Returns: C<Boolean>. Reports whether the lexer
requires whitespace around the operator.

=item C<< operator.lexer_should_ignore() >>

Parameters: none. Returns: C<Boolean>. Reports whether the lexer should
ignore this operator definition.

=item C<< operator.is_right_associative() >>

Parameters: none. Returns: C<Boolean>. Reports whether the operator is
right associative.

=item C<< operator.char_length() >>

Parameters: none. Returns: C<Number>. Returns the operator spelling
length.

=item C<< operator.precedence_is(lvl) >>

Parameters: C<lvl> is a precedence level. Returns: C<Boolean>. Returns
true when the operator has that precedence.

=back

=back

=head2 Constants

=over

=item C<STANDARD_OPERATORS>

Type: C<Array>. Standard ZPath operator definitions.

=back

=head1 COPYRIGHT AND LICENCE

B<< std/path/z/operators >> is copyright Toby Inkster.

It is free software; you may redistribute it and/or modify it under
the terms of either the Artistic License 1.0 or the GNU General Public
License version 2.

=cut

from std/path/z/node import Node;

function _floaty_modulus ( ln, rn ) {
	let count := floor( ln / rn ); //
	return ln - ( count * rn );
}

trait EvalHelpers {
	method _handle_numeric_operand ( ev, ctx, expr ) {
		const result := ev.eval_expr( expr, ev.nested_ctx( ctx ) );
		return 0 unless result.length;
		return ev.to_number( result[0] );
	}
	
	method _handle_stringy_operand ( ev, ctx, expr ) {
		const result := ev.eval_expr( expr, ev.nested_ctx( ctx ) );
		return 0 unless result.length;
		return ev.to_number( result[0] );
	}
	
	method wrap ( value ) {
		return [ Node.wrap( value ) ];
	}
	
	method wrap_for_array ( value ) {
		return Node.wrap( value );
	}
}

class Operator with EvalHelpers {
	let String spelling with get;
	let String alias with get, has;
	let String kind with get;
	let Number precedence with get;
	let Boolean unary      := false;
	let Boolean require_ws := false;
	let Boolean lex_ignore := false;
	let Boolean right_assoc := false;
	let Function f;
	
	method is_unary () {
		return unary;
	}
	
	method is_binary () {
		return not unary;
	}
	
	method requires_whitespace () {
		return require_ws;
	}
	
	method lexer_should_ignore () {
		return lex_ignore;
	}

	method is_right_associative () {
		return right_assoc;
	}
	
	method char_length () {
		return length spelling;
	}
	
	method precedence_is ( lvl ) {
		return precedence = lvl;
	}
}

const STANDARD_OPERATORS := [
	new Operator(
		spelling: "||",
		kind: "OROR",
		precedence: 2,
		f: function ( op, ev, ast, ctx, left, right ) {
			const left_val := ev.eval_expr( left, ev.nested_ctx( ctx ) );
			if ( left_val.length and ev.truthy( left_val[0] ) ) {
				return op.wrap( true );
			}
			const right_val := ev.eval_expr( right, ev.nested_ctx( ctx ) );
			if ( right_val.length and ev.truthy( right_val[0] ) ) {
				return op.wrap( true );
			}
			return op.wrap( false );
		},
	),
	
	new Operator(
		spelling: "&&",
		kind: "ANDAND",
		precedence: 4,
		f: function ( op, ev, ast, ctx, left, right ) {
			const left_val := ev.eval_expr( left, ev.nested_ctx( ctx ) );
			if ( left_val.length and ev.truthy( left_val[0] ) ) {
				const right_val := ev.eval_expr( right, ev.nested_ctx( ctx ) );
				if ( right_val.length and ev.truthy( right_val[0] ) ) {
					return op.wrap( true );
				}
			}
			return op.wrap( false );
		},
	),
	
	new Operator(
		spelling: "==",
		kind: "EQEQ",
		precedence: 12,
		f: function ( op, ev, ast, ctx, left, right ) {
			const left_vals  := ev.eval_expr( left, ev.nested_ctx( ctx ) );
			const right_vals := ev.eval_expr( right, ev.nested_ctx( ctx ) );
			let is_eq := false;
			
			if ( left_vals and right_vals ) {
				for ( let ln in left_vals ) {
					last if is_eq;
					for ( let rn in right_vals ) {
						last if is_eq;
						if ( ev.equals( ln, rn ) ) {
							is_eq := true;
						}
					}
				}
			}
			
			return op.wrap( is_eq );
		},
	),
	
	new Operator(
		spelling: "!=",
		kind: "NEQ",
		precedence: 12,
		f: function ( op, ev, ast, ctx, left, right ) {
			const left_vals  := ev.eval_expr( left, ev.nested_ctx( ctx ) );
			const right_vals := ev.eval_expr( right, ev.nested_ctx( ctx ) );
			let is_eq := false;
			
			if ( left_vals and right_vals ) {
				for ( let ln in left_vals ) {
					last if is_eq;
					for ( let rn in right_vals ) {
						last if is_eq;
						if ( ev.equals( ln, rn ) ) {
							is_eq := true;
						}
					}
				}
			}
			
			return op.wrap( not is_eq );
		},
	),
	
	new Operator(
		spelling: ">=",
		kind: "GE",
		precedence: 14,
		f: function ( op, ev, ast, ctx, left, right ) {
			let left_val  := op._handle_numeric_operand( ev, ctx, left );
			let right_val := op._handle_numeric_operand( ev, ctx, right );
			if ( ( left_val ≡ null ) or ( right_val ≡ null ) ) {
				left_val  := op._handle_stringy_operand( ev, ctx, left );
				right_val := op._handle_stringy_operand( ev, ctx, right );
				return op.wrap( left_val ge right_val );
			}
			return op.wrap( left_val ≥ right_val );
		},
	),
	
	new Operator(
		spelling: "<=",
		kind: "LE",
		precedence: 14,
		f: function ( op, ev, ast, ctx, left, right ) {
			let left_val  := op._handle_numeric_operand( ev, ctx, left );
			let right_val := op._handle_numeric_operand( ev, ctx, right );
			if ( ( left_val ≡ null ) or ( right_val ≡ null ) ) {
				left_val  := op._handle_stringy_operand( ev, ctx, left );
				right_val := op._handle_stringy_operand( ev, ctx, right );
				return op.wrap( left_val le right_val );
			}
			return op.wrap( left_val ≤ right_val );
		},
	),
	
	new Operator(
		spelling: "+", 
		kind: "PLUS",
		require_ws: true,
		precedence: 16,
		f: function ( op, ev, ast, ctx, left, right ) {
			const left_val  := op._handle_numeric_operand( ev, ctx, left );
			const right_val := op._handle_numeric_operand( ev, ctx, right );
			return op.wrap( left_val + right_val );
		},
	),
	
	new Operator(
		spelling: "-", 
		kind: "MINUS",
		require_ws: true,
		precedence: 16,
		f: function ( op, ev, ast, ctx, left, right ) {
			const left_val  := op._handle_numeric_operand( ev, ctx, left );
			const right_val := op._handle_numeric_operand( ev, ctx, right );
			return op.wrap( left_val - right_val );
		},
	),
	
	new Operator(
		spelling: "%", 
		kind: "PCT",
		require_ws: true,
		precedence: 18,
		f: function ( op, ev, ast, ctx, left, right ) {
			const left_val  := op._handle_numeric_operand( ev, ctx, left );
			const right_val := op._handle_numeric_operand( ev, ctx, right );
			if ( ( left_val ~ /\./ ) or ( right_val ~ /\./ ) ) {
				return op.wrap( _floaty_modulus( left_val, right_val ) );
			}
			return op.wrap( left_val mod right_val );
		},
	),
	
	new Operator(
		spelling: "*", 
		kind: "TIMES",
		require_ws: true,
		precedence: 18,
		lex_ignore: true,
		alias: "STAR",
		f: function ( op, ev, ast, ctx, left, right ) {
			const left_val  := op._handle_numeric_operand( ev, ctx, left );
			const right_val := op._handle_numeric_operand( ev, ctx, right );
			return op.wrap( left_val × right_val );
		},
	),
	
	new Operator(
		spelling: "/", 
		kind: "DIVIDE",
		require_ws: true,
		precedence: 18,
		lex_ignore: true,
		alias: "SLASH",
		f: function ( op, ev, ast, ctx, left, right ) {
			const left_val  := op._handle_numeric_operand( ev, ctx, left );
			const right_val := op._handle_numeric_operand( ev, ctx, right );
			return op.wrap( left_val ÷ right_val );
		},
	),
	
	new Operator(
		spelling: "^", 
		kind: "BXOR",
		precedence: 8,
		f: function ( op, ev, ast, ctx, left, right ) {
			const left_val  := op._handle_numeric_operand( ev, ctx, left );
			const right_val := op._handle_numeric_operand( ev, ctx, right );
			return op.wrap( left_val ^ right_val );
		},
	),
	
	new Operator(
		spelling: "&", 
		kind: "BAND",
		precedence: 10,
		f: function ( op, ev, ast, ctx, left, right ) {
			const left_val  := op._handle_numeric_operand( ev, ctx, left );
			const right_val := op._handle_numeric_operand( ev, ctx, right );
			return op.wrap( left_val & right_val );
		},
	),
	
	new Operator(
		spelling: "|", 
		kind: "BOR",
		precedence: 6,
		f: function ( op, ev, ast, ctx, left, right ) {
			const left_val  := op._handle_numeric_operand( ev, ctx, left );
			const right_val := op._handle_numeric_operand( ev, ctx, right );
			return op.wrap( left_val | right_val );
		},
	),
	
	new Operator(
		spelling: ">", 
		kind: "GT",
		precedence: 14,
		f: function ( op, ev, ast, ctx, left, right ) {
			let left_val  := op._handle_numeric_operand( ev, ctx, left );
			let right_val := op._handle_numeric_operand( ev, ctx, right );
			if ( ( left_val ≡ null ) or ( right_val ≡ null ) ) {
				left_val  := op._handle_stringy_operand( ev, ctx, left );
				right_val := op._handle_stringy_operand( ev, ctx, right );
				return op.wrap( left_val gt right_val );
			}
			return op.wrap( left_val > right_val );
		},
	),
	
	new Operator(
		spelling: "<", 
		kind: "LT",
		precedence: 14,
		f: function ( op, ev, ast, ctx, left, right ) {
			let left_val  := op._handle_numeric_operand( ev, ctx, left );
			let right_val := op._handle_numeric_operand( ev, ctx, right );
			if ( ( left_val ≡ null ) or ( right_val ≡ null ) ) {
				left_val  := op._handle_stringy_operand( ev, ctx, left );
				right_val := op._handle_stringy_operand( ev, ctx, right );
				return op.wrap( left_val le right_val );
			}
			return op.wrap( left_val < right_val );
		},
	),
	
	new Operator(
		spelling: "!", 
		kind: "NOT",
		unary: true,
		precedence: 20,
		f: function ( op, ev, ast, ctx, expr ) {
			const got := ev.eval_expr( expr, ctx );
			const value := got.length() > 0 ? got[0] : null;
			return op.wrap( not ev.truthy(value) );
		},
	),
	
	new Operator(
		spelling: "~", 
		kind: "BNOT",
		unary: true,
		precedence: 20,
		f: function ( op, ev, ast, ctx, expr ) {
			const value := op._handle_numeric_operand( ev, ctx, expr );
			return op.wrap( ~value );
		},
	),
];