aboutsummaryrefslogtreecommitdiffstats
path: root/tests/variables.bad
blob: 34293dc90c953fd49376b1eaf001ae21d14023fc (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
68
69
; 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
    ; TODO: ...
    ; attr: {
    ;     id: u64
    ;     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 }
    ; TODO: Get rid of this, unnecessary complexity on the implementation, let's
    ; just do the top option.
    ; attr.id = 1
    ; attr.name = "particle"

    ; Missing initialization fields default to zero.
    vel = vec : { y = -3.0 }
}
; let particle = entity : {}
; TODO: Now we can get rid of parenthesis on if/while statements

; ; 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"