aboutsummaryrefslogtreecommitdiffstats
path: root/tests/variables.bad
blob: ce765bc9ab1da2cf7d8a3af56d42c613066d4ef3 (plain)
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
; Basic variable declaration with and without default values. The type could be
; inferred, but can also be manually specified.
let a: str
let b: f64
let c:int = 20

; No infix '=', instead we use `set` to bind values.
set a = "hello"
set b = 1.2
set c = (1 + 2 - 3)

; Struct definitions.
struct vec {
    x: f64
    y: f64
    z: f64
}
let v = vec : { x = 2.0 y = 3.0 }

; Default values are allowed, including const expressions.
struct person {
    name: str = "joe"
    age: int = 18 * 2
}

; We can use the dot operator to access fields.
let player_a: person
set player_a.name = "alex"
set player_a.age = 32
let player_b = player_a

; We can have anonymous struct fields.
struct entity {
    pos: vec
    vel: vec
    attr: {
        id: int
        name: str
    }
}

; Symbols followed by curly braces output struct literals.
let particle = entity : {
    ; Two ways of initializing inner fields.
    pos = vec : { x = 1.0 y = 2.0 }
    attr = {
        id = 1
        name = "particle"
    }

    ; Missing initialization fields default to zero.
    vel = vec : { y = -3.0 }
}
set particle = entity : {}

; ; We can have static arrays and have indexed access.
; let numbers: u32[0xff]
; set numbers[0] = 32
; set numbers[1] = 42

; ; Arrays are syntactic sugar for pointers (@).
; let ptr:@u32 = numbers
; set ptr[10] = 33

; ; Strings hold a .mem and .size fields with the number of bytes it holds.
; let hello: str = "hello world"
; set c[1] = 'a' ; "hallo world"