diff options
author | Bad Diode <bd@badd10de.dev> | 2024-06-25 10:36:02 +0200 |
---|---|---|
committer | Bad Diode <bd@badd10de.dev> | 2024-06-25 10:36:02 +0200 |
commit | bcc40682a0d699e8c36df62975c12cdea491d167 (patch) | |
tree | 6511cdef1730d39bb81fceaf9839658654280f32 /tests | |
parent | 9eb5c9bfa3fcf8d7b03efd7d4def71e39ae3f799 (diff) | |
download | bdl-bcc40682a0d699e8c36df62975c12cdea491d167.tar.gz bdl-bcc40682a0d699e8c36df62975c12cdea491d167.zip |
Add struct literals typechecking
Diffstat (limited to 'tests')
-rw-r--r-- | tests/semantics.bad | 52 | ||||
-rw-r--r-- | tests/variables.bad | 53 |
2 files changed, 56 insertions, 49 deletions
diff --git a/tests/semantics.bad b/tests/semantics.bad index 95e827b..60e47d1 100644 --- a/tests/semantics.bad +++ b/tests/semantics.bad | |||
@@ -1,27 +1,33 @@ | |||
1 | enum weekdays { | 1 | struct vec { |
2 | mon = 1 | 2 | x: f64 |
3 | tue | 3 | y: f64 |
4 | wed | 4 | z: f64 |
5 | thu | ||
6 | fri | ||
7 | sat | ||
8 | sun | ||
9 | } | 5 | } |
10 | let a = weekdays.tue | 6 | let v = vec : { x = 1.0 } |
11 | ; let b = a | 7 | ; enum weekdays { |
12 | 8 | ; mon = 1 | |
13 | struct item { | 9 | ; tue |
14 | id: int | 10 | ; wed |
15 | name: str | 11 | ; thu |
16 | } | 12 | ; fri |
17 | 13 | ; sat | |
18 | ; let a: item = item { | 14 | ; sun |
19 | ; id = 1 | 15 | ; } |
20 | ; } | 16 | ; let a = weekdays.tue |
21 | ; let a: item = item ; this shouldn't work, or should it return the default val? | 17 | ; ; let b = a |
22 | let c: item | 18 | |
23 | set c.id = 1 | 19 | ; struct item { |
24 | let d = c.id | 20 | ; id: int |
21 | ; name: str | ||
22 | ; } | ||
23 | |||
24 | ; ; let a: item = item { | ||
25 | ; ; id = 1 | ||
26 | ; ; } | ||
27 | ; ; let a: item = item ; this shouldn't work, or should it return the default val? | ||
28 | ; let c: item | ||
29 | ; set c.id = 1 | ||
30 | ; let d = c.id | ||
25 | ; set a.name = "hello" | 31 | ; set a.name = "hello" |
26 | ; let b = a.id | 32 | ; let b = a.id |
27 | ; let c = a.name | 33 | ; let c = a.name |
diff --git a/tests/variables.bad b/tests/variables.bad index 88e04d1..34293dc 100644 --- a/tests/variables.bad +++ b/tests/variables.bad | |||
@@ -11,10 +11,11 @@ set c = (1 + 2 - 3) | |||
11 | 11 | ||
12 | ; Struct definitions. | 12 | ; Struct definitions. |
13 | struct vec { | 13 | struct vec { |
14 | x: f32 | 14 | x: f64 |
15 | y: f32 | 15 | y: f64 |
16 | z: f32 | 16 | z: f64 |
17 | } | 17 | } |
18 | let v = vec : { x = 2.0 y = 3.0 } | ||
18 | 19 | ||
19 | ; Default values are allowed, including const expressions. | 20 | ; Default values are allowed, including const expressions. |
20 | struct person { | 21 | struct person { |
@@ -28,41 +29,41 @@ set player_a.name = "alex" | |||
28 | set player_a.age = 32 | 29 | set player_a.age = 32 |
29 | let player_b = player_a | 30 | let player_b = player_a |
30 | 31 | ||
31 | ; Anonymous structs can also be declared inline. | ||
32 | let user: { id: u64 name: str } | ||
33 | set user.id = 10 | ||
34 | set user.name = "haxor" | ||
35 | |||
36 | ; We can have anonymous struct fields. | 32 | ; We can have anonymous struct fields. |
37 | struct entity { | 33 | struct entity { |
38 | pos: vec | 34 | pos: vec |
39 | vel: vec | 35 | vel: vec |
40 | attr: { | 36 | ; TODO: ... |
41 | id: u64 | 37 | ; attr: { |
42 | name: str | 38 | ; id: u64 |
43 | } | 39 | ; name: str |
40 | ; } | ||
44 | } | 41 | } |
45 | 42 | ||
46 | ; Symbols followed by curly braces output struct literals. | 43 | ; Symbols followed by curly braces output struct literals. |
47 | let particle = entity : { | 44 | let particle = entity : { |
48 | ; Two ways of initializing inner fields. | 45 | ; Two ways of initializing inner fields. |
49 | pos = vec : { x = 1 y = 2 } | 46 | pos = vec : { x = 1.0 y = 2.0 } |
50 | attr.id = 1 | 47 | ; TODO: Get rid of this, unnecessary complexity on the implementation, let's |
51 | attr.name = "particle" | 48 | ; just do the top option. |
49 | ; attr.id = 1 | ||
50 | ; attr.name = "particle" | ||
52 | 51 | ||
53 | ; Missing initialization fields default to zero. | 52 | ; Missing initialization fields default to zero. |
54 | vel = vec : { y = -3 } | 53 | vel = vec : { y = -3.0 } |
55 | } | 54 | } |
55 | ; let particle = entity : {} | ||
56 | ; TODO: Now we can get rid of parenthesis on if/while statements | ||
56 | 57 | ||
57 | ; We can have static arrays and have indexed access. | 58 | ; ; We can have static arrays and have indexed access. |
58 | let numbers: u32[0xff] | 59 | ; let numbers: u32[0xff] |
59 | set numbers[0] = 32 | 60 | ; set numbers[0] = 32 |
60 | set numbers[1] = 42 | 61 | ; set numbers[1] = 42 |
61 | 62 | ||
62 | ; Arrays are syntactic sugar for pointers (@). | 63 | ; ; Arrays are syntactic sugar for pointers (@). |
63 | let ptr:@u32 = numbers | 64 | ; let ptr:@u32 = numbers |
64 | set ptr[10] = 33 | 65 | ; set ptr[10] = 33 |
65 | 66 | ||
66 | ; Strings hold a .mem and .size fields with the number of bytes it holds. | 67 | ; ; Strings hold a .mem and .size fields with the number of bytes it holds. |
67 | let hello: str = "hello world" | 68 | ; let hello: str = "hello world" |
68 | set c[1] = 'a' ; "hallo world" | 69 | ; set c[1] = 'a' ; "hallo world" |