1
- +++
2
- date = ' 2025-02-08T18:47:22+05:00'
3
- draft = false
4
- title = ' Language Specification'
5
- toc = true
6
- +++
7
1
## Comments
8
2
9
3
Comments can be added using ` // ` :
@@ -180,22 +174,15 @@ will be used to check if the test failed or not.
180
174
181
175
### Variable Declaration
182
176
183
- Global Variables can be declared like:
177
+ Variables can be declared like:
184
178
185
179
```
186
- [attributes] var TYPE var0, var1, var2;
180
+ var TYPE var0, var1, var2;
187
181
```
188
182
189
- - ` attributes ` (optional). See Attributes section.
190
183
- ` TYPE ` is the data type of the variables
191
184
- ` var0 ` , ` var1 ` , ` var2 ` are the names of the variables. Comma separated.
192
185
193
- Local Variables declared as:
194
-
195
- ```
196
- var [attributes] [static] TYPE var0, var1, var2;
197
- ```
198
-
199
186
Value assignment can also be done in the variable declaration statement like:
200
187
201
188
```
@@ -237,7 +224,7 @@ var const int FIFTEEN = 15;
237
224
Or use the short syntax:
238
225
239
226
```
240
- const TYPE NAME = VALUE ;
227
+ const int FIFTEEN = 15 ;
241
228
```
242
229
243
230
---
@@ -391,12 +378,12 @@ Where it is not possible to detect whether `auto` should resolve to `T` or
391
378
392
379
An array type provides the following operations:
393
380
394
- - Resize (change length): ` $arrayLen (A, l) ` . This intrinsic results in a
381
+ - Resize (change length): ` $arrLen (A, l) ` . This intrinsic results in a
395
382
runtime function call, that resizes array `A` to length `l`
396
383
- implicit cast to ` $slice(X) `
397
- - Get Length: ` $arrayLen (A) ` . This intrinsics tells length of array or slice,
384
+ - Get Length: ` $arrLen (A) ` . This intrinsics tells length of array or slice,
398
385
in number of elements it can hold.
399
- - Get Element: ` $arrayInd (A, i) ` . Gets element at ` i ` th index in ` A ` array or
386
+ - Get Element: ` $arrInd (A, i) ` . Gets element at ` i ` th index in ` A ` array or
400
387
slice.
401
388
402
389
### ` $slice(X) `
@@ -510,7 +497,7 @@ Anonymous structs have all members as public, and visibility cannot be changed.
510
497
511
498
### Equivalence
512
499
513
- Structs defined through the ` struct NAME{...} ` syntax are always different :
500
+ Structs defined through the ` struct NAME{...} ` syntax are always unique :
514
501
515
502
```
516
503
struct Foo{ string a; int i; }
@@ -522,8 +509,8 @@ bar = foo; // error, incompatible types
522
509
```
523
510
524
511
Anonymous structs, can be implicitly casted to any other struct type, as long
525
- as the ` To ` type is a superset of the ` From ` type. If the ` To ` type has any
526
- additional members, those must be auto-initializable.
512
+ as the destination type is a superset of the source type. If the destination
513
+ type has any additional members, those must be auto-initializable.
527
514
528
515
```
529
516
struct Foo{ int i; }
@@ -538,11 +525,11 @@ bar = baz; // valid
538
525
bar = foo; // invalid
539
526
```
540
527
541
- ### ` this ` member in struct
528
+ ### ` this ` alias in struct
542
529
543
- Having ` X.this ` defined , where X is a struct, will add a fallback member to do
530
+ Having ` X.this ` alias , where X is a struct, will add a fallback member to do
544
531
operations on rather than the struct itself. ` this ` can only be an alias, not
545
- directly a member name :
532
+ directly a member:
546
533
547
534
```
548
535
struct Length{
@@ -556,6 +543,9 @@ writeln(len + 5); // Length + int is not implemented, evaluates to len.len + 5
556
543
writeln(len.unit); // prints "cm"
557
544
```
558
545
546
+ This works by allowing implicit conversions of the struct type into the aliased
547
+ member's type.
548
+
559
549
### Constructing Structs
560
550
561
551
Structs can be constructed as:
@@ -886,8 +876,8 @@ var auto err = ErrorType.FileNotFound;
886
876
An enum can exist by itself, with or without a value:
887
877
888
878
```
889
- enum Foo = 5;
890
- enum Bar;
879
+ enum auto Foo = 5;
880
+ enum auto Bar; // Bar can never be instantiated, but is a valid type
891
881
```
892
882
893
883
### Constants
@@ -1037,21 +1027,9 @@ fn main() -> void{
1037
1027
1038
1028
## Attributes
1039
1029
1040
- The ` # ` prefix operator can be used to tag declarations. Following declarations
1041
- can be tagged:
1042
-
1043
- - ` fn `
1044
- - ` struct `
1045
- - ` union `
1046
- - ` enum `
1047
- - ` alias `
1048
- - ` var `
1049
- - function parameters
1050
- - struct members
1051
- - union members
1052
- - enum members
1030
+ The ` # ` prefix operator can be used to tag definitions.
1053
1031
1054
- To tag any of these, prefix the definition (after optional ` pub ` or ` ipub ` ):
1032
+ Prefix the definition (after the ` pub ` or ` ipub ` , if in global scope ):
1055
1033
1056
1034
```
1057
1035
#Tag fn foo(#Xyz int i) -> void;
@@ -1061,15 +1039,15 @@ pub #Bar struct Baz{}
1061
1039
Any value or data type is a valid tag:
1062
1040
1063
1041
```
1064
- enum Xyz;
1042
+ enum auto Xyz;
1065
1043
1066
1044
pub #Xyz fn foo() -> void;
1067
1045
#"hello" struct Bar{}
1068
1046
```
1069
1047
1070
1048
### Intrinsics
1071
1049
1072
- The compiler provides following intrinsics for working with Attributes :
1050
+ The compiler provides following intrinsics for working with attributes :
1073
1051
1074
1052
- ` $attrsOf(X) ` - Gets sequence of all attributes of all types for symbol ` X `
1075
1053
- ` $byAttrs(X, A) ` - Gets all symbols that are children of ` X ` , which can be
@@ -1102,7 +1080,7 @@ import MODULE_NAME as y, MODULE_NAME as z;
1102
1080
To expose public members of an imported module:
1103
1081
1104
1082
```
1105
- pub import( MODULE_NAME) ;
1083
+ pub import MODULE_NAME;
1106
1084
```
1107
1085
1108
1086
### Visibility
@@ -1197,7 +1175,7 @@ for (auto val; data)
1197
1175
1198
1176
// is equivalent to:
1199
1177
auto range = data.opRange;
1200
- while !range.empty {
1178
+ while !range.isEmpty {
1201
1179
var auto val = range.front;
1202
1180
writeln(val);
1203
1181
range.popFront;
@@ -1206,13 +1184,13 @@ while !range.empty {
1206
1184
1207
1185
### Ranges
1208
1186
1209
- A data type ` T ` can qualify as a range if the following functions can be called
1210
- on it:
1187
+ A data type ` T ` can qualify as a range if the following operations can be
1188
+ evaluated on it:
1211
1189
1212
1190
```
1213
- T.empty() ; // should return true if there are no more values left to pop
1214
- T.front() ; // current value
1215
- T.popFront() ; // pop current element
1191
+ T.isEmpty ; // should return true if there are no more values left to pop
1192
+ T.front; // current value
1193
+ T.popFront; // pop current element
1216
1194
```
1217
1195
1218
1196
To make a data type ` X ` iterable, ` X.opRange ` should return a range, for
@@ -1227,14 +1205,14 @@ fn opRange(X) -> RangeObjectOfX{
1227
1205
### Ranges Example
1228
1206
1229
1207
```
1230
- struct ArrayRange $($type T){
1231
- @const T[] arr; // ref to array
1208
+ struct ArrayRange $($type T) {
1209
+ $slice(T) arr; // ref to array
1232
1210
uint index;
1233
1211
}
1234
1212
fn opRange $($type T) (@const T[] arr) -> ArrayRange(T){
1235
- return {arr = arr, index = 0};
1213
+ return {arr = arr, index = 0}.(ArrayRange(T)) ;
1236
1214
}
1237
- alias empty $(alias R : ArrayRange(T), $type T) = (R.index >= R.arr.length);
1215
+ alias isEmpty $(alias R : ArrayRange(T), $type T) = (R.index >= R.arr.length);
1238
1216
alias front $(alias R : ArrayRange(T), $type T) = R.arr[R.index];
1239
1217
alias popFront $(alias R : ArrayRange(T), $type T) = void{ R.index ++; };
1240
1218
```
@@ -1299,6 +1277,7 @@ case int { ... }
1299
1277
1300
1278
Operators are read in this order (higher precedence to lower), comma separated:
1301
1279
1280
+ - ` - A `
1302
1281
- ` A . B ` , ` A -> B `
1303
1282
- ` A [ B `
1304
1283
- ` @ A `
@@ -1437,6 +1416,7 @@ All variations of the assignment operator that combine an arithmetic operator
1437
1416
1438
1417
Following operators can be overloaded:
1439
1418
1419
+ - ` - A `
1440
1420
- ` A [ B `
1441
1421
- ` A ++ `
1442
1422
- ` A -- `
@@ -2011,7 +1991,7 @@ This also means that Sequences cannot be multi-dimensional.
2011
1991
2012
1992
---
2013
1993
2014
- ## Calling through dot operator
1994
+ ## Calling Through Dot Operator
2015
1995
2016
1996
You can write ` a.b ` instead of ` b(a) ` . For example, if you have a function
2017
1997
` foo ` :
@@ -2067,7 +2047,7 @@ $name(args...)
2067
2047
These will be added as the compiler and runtime is developed, since they are
2068
2048
directly dependent on the underlying data structures in the compiler/runtime.
2069
2049
2070
- An initial list of intrinsics is provided:
2050
+ ### Types
2071
2051
2072
2052
- ` type ` - template paramater type, can accept data type
2073
2053
- ` noinit ` - a data type, of zero size, and no default value
@@ -2078,24 +2058,66 @@ An initial list of intrinsics is provided:
2078
2058
- ` char(X) ` - data type, an X bits character
2079
2059
- ` slice(X) ` - data type, fixed size contiguous block, elements of type ` X `
2080
2060
- ` array(X) ` - data type, contiguous block, elements of type ` X `
2081
- - ` arrayLen(A) ` - array length get, for array ` A `
2082
- - ` arrayLen(A, l) ` - array length set, to ` l ` , for array ` A `
2083
- - ` arrayInd(A, i) ` - Gets element from array, at index ` i ` , for array ` A `
2061
+ - ` vt ` - an intrinsic data type for Virtual Table.
2062
+ - ` isType(T) ` - whether ` T ` resolves to a type.
2063
+ - ` typeOf(symbol) ` - data type of a symbol
2064
+
2065
+ ### Arrays & Sequences
2066
+
2067
+ - ` arrLen(A) ` - array length get, for array ` A `
2068
+ - ` arrLen(A, l) ` - array length set, to ` l ` , for array ` A `
2069
+ - ` arrInd(A, i) ` - Gets element from array, at index ` i ` , for array ` A `
2070
+ - ` seqLen(T...) ` - gets length of the ` T... ` sequence, in ` uint ` .
2071
+ - ` seqInd(T..., uint I) ` - gets ` I ` th element in the ` T... ` sequence.
2072
+
2073
+ ### Unions & Aggregates
2074
+
2084
2075
- ` unionIs(T) ` - whether a union's tag indicates ` this ` member being stored
2085
2076
- ` unionIs(T.M) ` - whether a union's tag indicates ` M ` member being stored, or
2086
2077
member of type `M`.
2087
- - ` vt ` - a special intrinsic data type for Virtual Table.
2078
+
2079
+ ### Attributes
2080
+
2088
2081
- ` attrsOf(X) ` - gets sequence of all attributes of ` X ` .
2089
2082
- ` byAttrs(X, A) ` - gets sequence of all symbols, that are members of ` X ` ,
2090
2083
which are of type `A`. `X` can be a module, struct, enum, or function
2091
2084
(parameters are considered members). `A` can be a type: attributes of type
2092
2085
`A` will be matched. `A` can be a value: attributes of type and value of `A`
2093
2086
will be matched.
2087
+
2088
+ ## Misc.
2089
+
2094
2090
- ` debug ` - whether building in debug mode or not.
2095
2091
- ` stackTrace ` - only available in ` $debug ` . Gives a stack trace.
2096
- - ` isType(T) ` - whether ` T ` resolves to a type.
2097
- - ` seqLen(T...) ` - gets length of the ` T... ` sequence, in ` uint ` .
2098
- - ` seqInd(T..., uint I) ` - gets ` I ` th element in the ` T... ` sequence.
2099
2092
- ` err(str) ` - Emits error as a compiler error
2100
- - ` typeOf(symbol) ` - data type of a symbol
2101
2093
2094
+ ### Printing
2095
+
2096
+ These should only be used for debugging.
2097
+
2098
+ - ` rtWrite(T...) ` - prints parameters, at runtime. ** Only for debugging Alis
2099
+ itself. May not be available in final version**
2100
+ - ` rtWriteln(T...) ` - prints parameters, at runtime. ** Only for debugging Alis
2101
+ itself. May not be available in final version**
2102
+ - ` ctWriteln(T...) ` - prints parameters, at compile time.
2103
+
2104
+ ### Arithmetic Operations
2105
+
2106
+ Any intrinsic here that accepts 2 parameters, both should be of the same type.
2107
+
2108
+ - ` arithNeg(X) ` - returns ` X ` with negated sign
2109
+ - ` arithBinNot(X) ` - returns bitwise not of ` X `
2110
+ - ` arithBinOr(X, Y) ` - returns bitwise or of ` X ` and ` Y `
2111
+ - ` arithBinAnd(X, Y) ` - returns bitwise and of ` X ` and ` Y ` .
2112
+ - ` arithBinXor(X, Y) ` - returns bitwise xor of ` X ` and ` Y `
2113
+ - ` arithAdd(X, Y) ` - returns ` X+Y `
2114
+ - ` arithSub(X, Y) ` - returns ` X-Y `
2115
+ - ` arithMul(X, Y) ` - returns ` X*Y `
2116
+ - ` arithDiv(X, Y) ` - returns ` X/Y `
2117
+ - ` arithMod(X, Y) ` - returns ` X%Y ` . Only for integers, not floats.
2118
+
2119
+ For the left/right shift, ` X ` and ` Y ` must be integers. Same type is not
2120
+ required:
2121
+
2122
+ - ` arithLShift(X, Y) ` - returns ` X << Y `
2123
+ - ` arithRShift(X, Y) ` - returns ` X >> Y `
0 commit comments