Skip to content

Commit 66a6d14

Browse files
committed
Updated spec
1 parent 07c9d76 commit 66a6d14

File tree

1 file changed

+86
-64
lines changed

1 file changed

+86
-64
lines changed

content/docs/spec/language.md

Lines changed: 86 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
+++
2-
date = '2025-02-08T18:47:22+05:00'
3-
draft = false
4-
title = 'Language Specification'
5-
toc = true
6-
+++
71
## Comments
82

93
Comments can be added using `//`:
@@ -180,22 +174,15 @@ will be used to check if the test failed or not.
180174

181175
### Variable Declaration
182176

183-
Global Variables can be declared like:
177+
Variables can be declared like:
184178

185179
```
186-
[attributes] var TYPE var0, var1, var2;
180+
var TYPE var0, var1, var2;
187181
```
188182

189-
- `attributes` (optional). See Attributes section.
190183
- `TYPE` is the data type of the variables
191184
- `var0`, `var1`, `var2` are the names of the variables. Comma separated.
192185

193-
Local Variables declared as:
194-
195-
```
196-
var [attributes] [static] TYPE var0, var1, var2;
197-
```
198-
199186
Value assignment can also be done in the variable declaration statement like:
200187

201188
```
@@ -237,7 +224,7 @@ var const int FIFTEEN = 15;
237224
Or use the short syntax:
238225

239226
```
240-
const TYPE NAME = VALUE;
227+
const int FIFTEEN = 15;
241228
```
242229

243230
---
@@ -391,12 +378,12 @@ Where it is not possible to detect whether `auto` should resolve to `T` or
391378

392379
An array type provides the following operations:
393380

394-
- Resize (change length): `$arrayLen(A, l)`. This intrinsic results in a
381+
- Resize (change length): `$arrLen(A, l)`. This intrinsic results in a
395382
runtime function call, that resizes array `A` to length `l`
396383
- 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,
398385
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
400387
slice.
401388

402389
### `$slice(X)`
@@ -510,7 +497,7 @@ Anonymous structs have all members as public, and visibility cannot be changed.
510497

511498
### Equivalence
512499

513-
Structs defined through the `struct NAME{...}` syntax are always different:
500+
Structs defined through the `struct NAME{...}` syntax are always unique:
514501

515502
```
516503
struct Foo{ string a; int i; }
@@ -522,8 +509,8 @@ bar = foo; // error, incompatible types
522509
```
523510

524511
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.
527514

528515
```
529516
struct Foo{ int i; }
@@ -538,11 +525,11 @@ bar = baz; // valid
538525
bar = foo; // invalid
539526
```
540527

541-
### `this` member in struct
528+
### `this` alias in struct
542529

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
544531
operations on rather than the struct itself. `this` can only be an alias, not
545-
directly a member name:
532+
directly a member:
546533

547534
```
548535
struct Length{
@@ -556,6 +543,9 @@ writeln(len + 5); // Length + int is not implemented, evaluates to len.len + 5
556543
writeln(len.unit); // prints "cm"
557544
```
558545

546+
This works by allowing implicit conversions of the struct type into the aliased
547+
member's type.
548+
559549
### Constructing Structs
560550

561551
Structs can be constructed as:
@@ -886,8 +876,8 @@ var auto err = ErrorType.FileNotFound;
886876
An enum can exist by itself, with or without a value:
887877

888878
```
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
891881
```
892882

893883
### Constants
@@ -1037,21 +1027,9 @@ fn main() -> void{
10371027

10381028
## Attributes
10391029

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.
10531031

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):
10551033

10561034
```
10571035
#Tag fn foo(#Xyz int i) -> void;
@@ -1061,15 +1039,15 @@ pub #Bar struct Baz{}
10611039
Any value or data type is a valid tag:
10621040

10631041
```
1064-
enum Xyz;
1042+
enum auto Xyz;
10651043
10661044
pub #Xyz fn foo() -> void;
10671045
#"hello" struct Bar{}
10681046
```
10691047

10701048
### Intrinsics
10711049

1072-
The compiler provides following intrinsics for working with Attributes:
1050+
The compiler provides following intrinsics for working with attributes:
10731051

10741052
- `$attrsOf(X)` - Gets sequence of all attributes of all types for symbol `X`
10751053
- `$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;
11021080
To expose public members of an imported module:
11031081

11041082
```
1105-
pub import(MODULE_NAME);
1083+
pub import MODULE_NAME;
11061084
```
11071085

11081086
### Visibility
@@ -1197,7 +1175,7 @@ for (auto val; data)
11971175
11981176
// is equivalent to:
11991177
auto range = data.opRange;
1200-
while !range.empty {
1178+
while !range.isEmpty {
12011179
var auto val = range.front;
12021180
writeln(val);
12031181
range.popFront;
@@ -1206,13 +1184,13 @@ while !range.empty {
12061184

12071185
### Ranges
12081186

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:
12111189

12121190
```
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
12161194
```
12171195

12181196
To make a data type `X` iterable, `X.opRange` should return a range, for
@@ -1227,14 +1205,14 @@ fn opRange(X) -> RangeObjectOfX{
12271205
### Ranges Example
12281206

12291207
```
1230-
struct ArrayRange $($type T){
1231-
@const T[] arr; // ref to array
1208+
struct ArrayRange $($type T) {
1209+
$slice(T) arr; // ref to array
12321210
uint index;
12331211
}
12341212
fn opRange $($type T) (@const T[] arr) -> ArrayRange(T){
1235-
return {arr = arr, index = 0};
1213+
return {arr = arr, index = 0}.(ArrayRange(T));
12361214
}
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);
12381216
alias front $(alias R : ArrayRange(T), $type T) = R.arr[R.index];
12391217
alias popFront $(alias R : ArrayRange(T), $type T) = void{ R.index ++; };
12401218
```
@@ -1299,6 +1277,7 @@ case int { ... }
12991277

13001278
Operators are read in this order (higher precedence to lower), comma separated:
13011279

1280+
- `- A`
13021281
- `A . B`, `A -> B`
13031282
- `A [ B`
13041283
- `@ A`
@@ -1437,6 +1416,7 @@ All variations of the assignment operator that combine an arithmetic operator
14371416

14381417
Following operators can be overloaded:
14391418

1419+
- `- A`
14401420
- `A [ B`
14411421
- `A ++`
14421422
- `A --`
@@ -2011,7 +1991,7 @@ This also means that Sequences cannot be multi-dimensional.
20111991

20121992
---
20131993

2014-
## Calling through dot operator
1994+
## Calling Through Dot Operator
20151995

20161996
You can write `a.b` instead of `b(a)`. For example, if you have a function
20171997
`foo`:
@@ -2067,7 +2047,7 @@ $name(args...)
20672047
These will be added as the compiler and runtime is developed, since they are
20682048
directly dependent on the underlying data structures in the compiler/runtime.
20692049

2070-
An initial list of intrinsics is provided:
2050+
### Types
20712051

20722052
- `type` - template paramater type, can accept data type
20732053
- `noinit` - a data type, of zero size, and no default value
@@ -2078,24 +2058,66 @@ An initial list of intrinsics is provided:
20782058
- `char(X)` - data type, an X bits character
20792059
- `slice(X)` - data type, fixed size contiguous block, elements of type `X`
20802060
- `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+
20842075
- `unionIs(T)` - whether a union's tag indicates `this` member being stored
20852076
- `unionIs(T.M)` - whether a union's tag indicates `M` member being stored, or
20862077
member of type `M`.
2087-
- `vt` - a special intrinsic data type for Virtual Table.
2078+
2079+
### Attributes
2080+
20882081
- `attrsOf(X)` - gets sequence of all attributes of `X`.
20892082
- `byAttrs(X, A)` - gets sequence of all symbols, that are members of `X`,
20902083
which are of type `A`. `X` can be a module, struct, enum, or function
20912084
(parameters are considered members). `A` can be a type: attributes of type
20922085
`A` will be matched. `A` can be a value: attributes of type and value of `A`
20932086
will be matched.
2087+
2088+
## Misc.
2089+
20942090
- `debug` - whether building in debug mode or not.
20952091
- `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.
20992092
- `err(str)` - Emits error as a compiler error
2100-
- `typeOf(symbol)` - data type of a symbol
21012093

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

Comments
 (0)