unlab-gpu 0.1.0

Micro scripting language for neural networks that uses unmtx-gpu.
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
# Unlab reference

## Copyright and license

Copyright (c) 2026 Ɓukasz Szpakowski

This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at https://mozilla.org/MPL/2.0/.

## Introduction

This document is the reference for the Unlab scripting language. The Unlab scripting language is micro
neural scriting language for GPU. This reference describes the syntax of this scripting language and
semantics of this scriping language.

## Notation

This document uses [EBNF](https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_form) to
describes the lexical definitions of this scriping language and syntax of this scripting language.

## Lexical conventions

### Whitespace

The lexical definition of whitespace is:

    whitespace = whitespace char, {whitespace char};
    whitespace char = ?UTF-8 character with White_Space property?

The whitespace can be separators between tokens.

### Newline

The lexical definition of newline token is:

    newline = (cr, lf) | lf | ";";
    cr = ?CR character?;
    lf = ?LF character?;

### Comments

The lexical definition of comment is:

    comment = ("#" | "%"), {?any character except LF character?};

### Punctuation

The lexical definition of punctuation tokens is:

    punctuation = "("
                | ")"
                | "["
                | "]"
                | "{"
                | "}"
                | ".["
                | ".]"
                | "?"
                | "*"
                | "/"
                | "+"
                | "-"
                | ".*"
                | "./"
                | ".+"
                | ".-"
                | "<"
                | ">="
                | ">"
                | "<="
                | "="
                | "=="
                | "!="
                | "'"
                | "."
                | ":"
                | "::"
                | ",";

A punctuation token is operator or separator.

### Keywords

The lexical definition of keywords is:

    keyword = "and"
            | "break"
            | "by"
            | "continue"
            | "else"
            | "end"
            | "false"
            | "fill"
            | "for"
            | "function"
            | "if"
            | "in"
            | "inf"
            | "module"
            | "nan"
            | "none"
            | "not"
            | "or"
            | "quit"
            | "return"
            | "root"
            | "to"
            | "true"
            | "while";

Keywords are reserved words which aren't identifiers.

### Number literals

The lexical definition of number literal is:

    number = integer | float;
    integer = "0", ("X" | "x"), hex digit, {hex digit}
            | digit, {digit};
    float = digit, {digit}, ".", digit, {digit}, [exponent]
          | digit, {digit}, exponent;
    exponent = ("E" | "e"), [("+" | "-")], digit, {digit};
    digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";
    hex digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
              | "A" | "B" | "C" | "D" | "E" | "F"
              | "a" | "b" | "c" | "d" | "e" | "f";

Number literals are used to directly represent number values which can be integer numbers or
floating-point numbers. The integer numbers can be decimal system or hexadecimal system.

### String literals

The lexical definition of string literal is:

    string = '"', {string char | escape}, '"';
    string char = ?any character except '"' character, '\' character, and LF character?;
    escape = ascii escape | unicode escape;
    ascii escape = "\", oct digit, oct digit, oct digit
                 | "\", oct digit, oct digit
                 | "\", oct digit
                 | "\a" | "\b" | "\t" | "\n" | "\v" | "\f" | "\r"
                 | "\", escape char;
    escape char = ?any character except LF character?;
    unicode escape = "\U", hex digit, hex digit, hex digit, hex digit, hex digit, hex digit
                   | "\u", hex digit, hex digit, hex digit, hex digit;
    oct digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7";
    hex digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
              | "A" | "B" | "C" | "D" | "E" | "F"
              | "a" | "b" | "c" | "d" | "e" | "f";

A string literal is a text that is closed by the `"` characters. Ascii escapes can be used in the
string literals as the C programming language. The string literal also can have unicode escapes which
begin the `U` character for 24-bit code or the `u` character for 16-bit code.

### Idendifiers

The lexical definition of identifiers literals is:

    identifier = first identifier char, {identifier char};
    first identifier char = "_" | alphabetic char;
    identifier char = "_" | alphabetic char | numeric char;
    alphabetic char = ?UTF-8 character with Alphabetic property?;
    numeric char = ?UTF-8 character with Nd category, Nl category, or No category?;

## Values

Value types are:

- none
- boolean
- integer number
- floating-point number
- reference to immutable object
- strong reference to mutable object
- weak reference to mutable object

The integer numbers are 64-bit and floating-point numbers are 32-bit.

### Immutable objects

Immutable object types are:

- string
- integer range
- floating-point range
- matrix
- function
- matrix array
- matrix row slice
- error
- window identifier

### Mutable objects

Mutable object types are:

- array
- structure

### Value properties

Value types and object types with properties are:

| Value type or object type | Properties          | Index type | Element type          | Boolean    |
| ------------------------- | ------------------- | ---------- | --------------------- | ---------- |
| none                      |                     |            |                       | `false`    |
| boolean                   |                     |            |                       |            |
| integer number            |                     |            |                       | `a != 0`   |
| floating-point number     |                     |            |                       | `a != 0.0` |
| string                    | iterable, indexable | number     | string                | `true`     |
| integer range             | iterable            |            | integer number        | `true`     |
| floating-point range      | iterable            |            | floating-point number | `true`     |
| function                  | applicable          |            |                       | `true`     |
| matrix                    |                     |            |                       | `true`     |
| matrix array              | iterable, indexable | number     | matrix row slice      | `true`     |
| matrix row slice          | iterable, indexable | number     | floating-point number | `true`     |
| error                     |                     |            |                       | `false`    |
| window identifier         |                     |            |                       | `true`     |
| array                     | iterable, indexable | number     | any value             | `true`     |
| structure                 | indexable           | string     | any value             | `true`     |

Strong references only have the properties for the mutable objects.

A string element is a character that is represented by other string.

An element of matrix array is a row of matrix array that is referred by a matrix row slice.

A structure element is a structure field and a structure index is a field identifier that refers to
the structure field.

## Nodes

The syntax of nodes is:

    nodes = {neline}, [node, {newline, {newline}, node}, [newline, {newline}]]
    node = definition | statement;

A syntax tree contains the nodes which can be definitions and/or statements.

## Definitions

The syntax of definition is:

    definition = module definition
               | function definition;

The definition is a module definition or a function definition.

### Module definitions

The syntax of module definition is:

    module definition = "module", identifier, newline,
                        nodes,
                        "end";

The module with same identifier can only be defined once in other module. The modules uses own
namespace that is separate from the variable namespace.

### Function definitions

The syntax of function definition is:

    function definition = "function", identifier, "(", arguments, ")", newline,
                          statements,
                          "end";
    arguments = [argument, {",", argument}, [","]];
    argument = identifier

The function is a variable that can be applied to arguments. If some variable with identifier is 
defined in a module, the function with same identifier can't be defined in the module. The function
arguments are checked whether they are repeated.

## Statements

The syntax of statements is:

    statements = {newline}, [statement, {newline, {newline}, statement}, [newline, {newline}]]
    statement = if statement
              | for statement
              | while statement
              | break statement
              | continue statement
              | return statement
              | quit statement
              | assign statement
              | expression;

### Assignment statements

The syntax of assignment statement is:

    assign statement = expression, "=", expression;

The assignment stament is a statement that assigns the second expression value to a variable, an
element, or a field. An error occurs if the first expression isn't assignable. The variable or the
structure field is created by this statement if the variable or the structure field doesn't exist.

### If statements

The syntax of if statement is:

    if statement = "if", expression, newline, statements,
                   {
                       "else", "if" expression, newline,
                       statements
                   },
                   [
                       "else", newline,
                       statements
                   ],
                   "end";

The if statement is a statement that executes the condition statements for the first fulfilled
condition and then skips other statements with conditions. If any condition isn't fulfilled, the
statements after the `else` keyword is executed. The condition is fulfilled if the condition
expression can be converted to `true`.

### For statements

The syntax of for statement is:

    for statement = "for", identifier, "in", expression, newline,
                    statements,
                    "end";

The for stamement is a loop that iterates over an iterable value and executes the statements for
elements of iterable value. The element of iterable value is stored a variable with identifier.

### While statements

The syntax of while statement is:

    while statement = "while", expression, newline
                      statements,
                      "end";

The while statement is a loop that executes the statements for iterations until expression can be
converted to `true` if the break statements doesn't occur.

### Break statements

The syntax of break statement is:

    break statement = "break";

The break statement stops a loop. If the break statement is used outside the loop, an error occurs.

### Continue statements

The syntax of continue statement is:

    continue statement = "continue";

The continue statement skips the rest statements of loop to the next iteration. If the continue
statement also is used outside a loop, an error occurs.

### Return statements

The syntax of return statement is:

    return statement = "return", [expression];

The return statement leaves from a function and then the function returns a value of expression or the
`None` value if the expression isn't passed. If the return statement is used outside a function, the
error occurs. If the return statement is the last interpreted stetement in the function, the return
statement can be replaced by the expression. If the last interpreted statement in the function is the
assignment statement, the return statement without the expression can be omitted.

### Quit statements

The syntax of quit statement is:

    quit statement = "quit";

The quit statement leaves from a script or an interpreter.

## Expressions

The syntax of expression is:

    expression = "(", expression, ")"
               | literal
               | variable
               | function application
               | unary op expression
               | binary op expression
               | logical expression
               | field access expression
               | range expression
               | error propagation expression;

### Variables

The syntax of variable is:

    variable = name;

### Function application

The syntax of function application is:

    function application = expression, "(", expressions, ")";
    expressions = [expression, {",", expression}, [","]];

The function application applies the function to the arguments. If the first operand isn't a function,
an error occurs.

### Expressions of unary operators

The syntax of expression of unary operator is:

    unary op expression = "-", expression
                        | ".-", expression
                        | "not", expression
                        | expression, "'";

The `-` operator negates the number or the matrix.

The `.-` operator recursively negates the floating-point numbers and/or the matrices. One element or
one field is ignored if it isn't a floating-point number, a matrix, an array, or a structure and
one operand is an array or a structure. The operand can be a number, a matrix, an array, or a
structure. If the operand is an integer number, the operand is converted to a floating-point number
and then is negated.

The `not` operator converts the operand to a boolean value and then negates the boolean
value.

The `'` operator transposes matrix.

### Expressions of binary operators

The syntax of expression of binary operator is:

    binary op expression = expression, "[", expression, "]"
                         | expression, "*", expression
                         | expression, ".*", expression
                         | expression, "/", expression
                         | expression, "./", expression
                         | expression, "+", expression
                         | expression, ".+", expression
                         | expression, "-", expression
                         | expression, ".-", expression
                         | expression, "<", expression
                         | expression, ">=", expression
                         | expression, ">", expression
                         | expression, "<=", expression
                         | expression, "==", expression
                         | expression, "!=", expression;

The `[]` operator is an index operator that allows to access to elements or fields. An indexing
for numbers begins from one. An expression created by this operator is assignable if the first
operand is an array or a structure.

The `*` operator multiplies the number or matrix by the number or the matrix.

The `.*` operator multiplies the number or the elements of matrix by the number or the elements of
matrix.

The `/` operator divides the number or the matrix by the number.

The `.*` operator divides the number or the elements of matrix by the number or the elements of
matrix.

The `+` operator adds the number or matrix to the number or the matrix. Also, two strings, two arrays,
two structures can be added by this operator. If two fields in two structures have same field
idendifier, the field from the first operand assigns to a field of result structure.

The `.+` operator adds the number or the elements of matrix to the number or the elements of matrix.

The `-` operator subtracts the number or matrix from the number or the matrix.

The `.-` operator subtracts the number or the elements of matrix from the number or the elements of
matrix.

The arithmetic binary operator without dot converts one value to a floating-point number and then
performs operation if one value is an integer number.

The arithmetic binary operator with dot recursively performs an operation on the floating-point
numbers and/or the matrices. Two elements or two fields are compares with types if they aren't
floating-point numbers, matrices, arrays, or a structures and two operands are arrays or structures.
If two elements or two fields aren't equal, an error occurs. One element or one field is ignored if it
isn't a floating-point number, a matrix, an array, or a structure; one operand is an array or a
structure and other operand is a number. The operand can be a number, a matrix, an array or a
structure. Two operands can't be a matrix and an array or a structure. If the operand is an integer
number, the operand is converted to a floating-point number and then there performs the
operation.

The comparison operator except the `==` operator and the `!=` operator comperes the boolean value to
the boolean value, the number to the number, the string to the string.

The `==` operator comperes two values. The result of this operator is `true` if two values are equal,
otherwise `false`.

The `!=` operator comperes two values. The result of this operator is `false` if two values are equal,
otherwise `true`.

The `==` operator and the `!=` operator don't compare two matrices. The result of these operators is
`false` for the `==` operator or `true` for the `!=` operator if two values are matrices. These
operators don't compare value types for integer numbers and floating-point numbers.

### Expressions of logical operators

The syntax of expression of logical operator is:

    logical op expression = expression, "and", expression
                          | expression, "or", expression;

The `and` operator performs the logical-AND operation. The result of this operator is the second
operand if the first operand is `true` after conversion to the boolean value,
otherwise the first operand.

The `or` operator performs the logical-OR operation. The result of this operator is the first
operand if the first operand is `true` after conversion to the boolean value,
otherwise the second operand.

These operators evaluate the second oerand if it is necessary.

### Expressions of field access

The syntax of expression of field access is:

    field access expression = expression, ".", identifier;

The expression of field access allows to access to the structure fields by the identifier. The
expression of field access is assignable.

### Range expressions

The syntax of range expression is:

    range expression = expression, "to", expression, ["by", expression];

The range expression creates a range. The operands in the range expression must be numbers. The range
is a floating-point range if at least one operand is floating-point number, otherwise an integer
range. If the third operand isn't passed, the step of range expression is one.

### Expressions of propagation error

The syntax of expression of propagation error is:

    propagation error expression = expression, "?";

The expression of error propagation allows to propagate error. If the operand is the `none` value or
an error value, there leaves from a function with the result that is this value or prints the error
outside the function.

### Expression precedence

Expressions and operators with arities and priorities are:

| Expression or operator | Arity          | Priority |
| ---------------------- | -------------- | -------- |
| parenthesis            |                | 12       |
| literal                |                | 12       |
| name                   |                | 12       |
| function application   |                | 11       |
| `[]`                   | binary         | 11       |
| `.`                    | binary         | 11       |
| `?`                    | unary          | 10       |
| `'`                    | unary          | 9        |
| `-`                    | unary          | 8        |
| `.-`                   | unary          | 8        |
| `not`                  | unary          | 8        |
| `*`                    | binary         | 7        |
| `.*`                   | binary         | 7        |
| `/`                    | binary         | 7        |
| `./`                   | binary         | 7        |
| `+`                    | binary         | 6        |
| `.+`                   | binary         | 6        |
| `-`                    | binary         | 6        |
| `.-`                   | binary         | 6        |
| `to` `by`              | binary/ternary | 5        |
| `<`                    | binary         | 4        |
| `>=`                   | binary         | 4        |
| `>`                    | binary         | 4        |
| `<=`                   | binary         | 4        |
| `==`                   | binary         | 3        |
| `!=`                   | binary         | 3        |
| `and`                  | binary         | 2        |
| `or`                   | binary         | 1        |

Associative of all expression and all operators is left to right.

## Literals

The syntax of literal is:

    literal = none literal
            | boolean literal
            | integer number literal
            | float number literal
            | string literal
            | matrix literal
            | array literal
            | structure literal;

### None literals

The syntax of none literal is:

    none literal = "none";

### Boolean literals

The syntax of none literal is:

    none literal = "false"
                 | "true";

### Integer literals

The syntax of integer literal is:

    integer number literal = integer;

### Floating-point literals

The syntax of float-point literal is:

    float number literal = float
                         | "inf"
                         | "nan";

The floating-point literal can also be an infinity or a NaN.

### String literals

The syntax of string literal is:

    string literal = string;

### Matrix literals

The syntax of matrix literal is:

    matrix literal = "[", {newline}, fillable matrix row, "]";
    fillable matrix rows = matrix row, newline, {newline}, "fill", expression, {newline}
                         | matrix rows;
    matrix rows = {newline}, [matrix row, {newline, {newline}, matrix row}, [newline, {newline}]];
    matrix row = fillable expressions;
    fillable expressions = expression, "fill", expression
                         | expressions;
    expressions = [expression, {",", expression}, [","]];

The matrix or the matrix row can be filled with the filling matrix or the filling expression by using
the `fill` keyword. The filling matrix row or the filling expression is separately evaluated for each
matrix row or each element. The expression after the `fill` keyword specifies the number of rows for
the filled matrix or the number of columns for the filled matrix row. Each element of matrix literal
must be a number that is converted to floating-point number. If this element isn't a number, an error
occurs.

### Array literal

The syntax of array literal is:

    array literal = ".[", {newline}, fillable expressions, {newline}, ".]";
    fillable expressions = expression, "fill", expression
                         | expressions;
    expressions = [expression, {",", expression}, [","]];

The array can also be filled with the filling expression by the `fill` keyword. The filling expresson
is separately evaluated for each element. The expression after the `fill` keyword specifies the number
of elements.

### Structure literal

The syntax of structure literal is:

    structure literal = "{", field pairs, "}";
    field pairs = {newline}, [field pair, {newline, {newline}, field pair}, [newline, {newline}]];
    field pair = identifier, ":", expression;

The field with same identifier can only be defined once in same structure.

## Name

The syntax of name is:

    name = absolute name
         | relative name
         | variable name;

### Root module and current module

The root module is the hihgest module in the module tree and is only one.

The current module can be a module that is currently defined or a module of the current function. The
current function is a function that is currently executed by an interpreter.

### Absolute names

The syntax of absolute name is:

    absolute name = ["::"], "root", "::", identifier, {"::", identifier};

The identifiers of absolute name except the last identifier refer to the descendant modules from the
root module. The last identifier refers to the variable that is in the last referred module or the
root module. The first identifier can't refer to an imported module in the root module.

### Relative names

The syntax of relative name is:

    relative name = "::", identifier, {"::", identifier}
                  | identfier, "::", identifier, {"::", identifier};

The identifiers of relative name except the last identifier refer to the descendant modules from the
current module if all these modules from the current module exist, otherwise the descendant modules
from the root module. The last identifier refers to the variable that is in the last referred module
from the current module or the root module. The first identifier for the current module can refer to
an imported module or an imported variable in the current module if the variable or the module with
the first identifier isn't defined.

### Variable names

The syntax of variable name is:

    variable name = identifier;

The identifier can refer to:

- the local variable if the local variable exists for reading or an interpreter is inside a function
  for writing
- the variable that is defined in the current module if the variable exists in the current module
- the variable that is imported in the current module if the variable is imported in the current
  module
- the variable that is defined in the root module if the variable exists in the root module