Skip to content

Commit

Permalink
Improve type-checking (#19)
Browse files Browse the repository at this point in the history
Adds type-checking to the following cases:

- block outputs (reflect the type of the last statement, none otherwise)
- parameters in def (any if not specified, else the specified type)
- if else (reflect the type of the blocks if the blocks are equal, if
they are unequal then forbidden, and the if branch type otherwise)

---------

Signed-off-by: innocentzero <[email protected]>
  • Loading branch information
InnocentZero authored Dec 16, 2024
1 parent 44fcadf commit eba4f27
Show file tree
Hide file tree
Showing 35 changed files with 592 additions and 134 deletions.
25 changes: 25 additions & 0 deletions src/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
source: src/test.rs
expression: evaluate_example(path)
input_file: tests/alias.nu
snapshot_kind: text
---
==== COMPILER ====
0: Name (4 to 7) "foo"
1: Name (9 to 10) "a"
2: Param { name: NodeId(1), ty: None } (9 to 10)
3: Params([NodeId(2)]) (8 to 11)
4: Variable (14 to 16) "$a"
5: Block(BlockId(0)) (12 to 18)
6: Def { name: NodeId(0), params: NodeId(3), return_ty: None, block: NodeId(5) } (0 to 18)
7: Name (20 to 25) "alias"
8: Name (26 to 29) "bar"
9: Garbage (30 to 31)
10: String (32 to 35) "foo"
11: Call { parts: [NodeId(7), NodeId(8), NodeId(9), NodeId(10)] } (26 to 35)
12: Name (37 to 40) "bar"
13: Int (41 to 42) "1"
14: Call { parts: [NodeId(12), NodeId(13)] } (41 to 42)
15: Block(BlockId(1)) (0 to 43)
==== COMPILER ERRORS ====
Error (NodeId 9): incomplete expression
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
source: src/test.rs
expression: evaluate_example(path)
input_file: tests/binary_ops_exact.nu
snapshot_kind: text
---
==== COMPILER ====
0: Int (0 to 1) "1"
Expand Down Expand Up @@ -50,4 +51,4 @@ input_file: tests/binary_ops_exact.nu
18: forbidden
19: bool
20: bool
21: ()
21: bool
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
source: src/test.rs
expression: evaluate_example(path)
input_file: tests/binary_ops_mismatch.nu
snapshot_kind: text
---
==== COMPILER ====
0: String (0 to 3) ""a""
Expand All @@ -21,18 +22,18 @@ input_file: tests/binary_ops_mismatch.nu
0: Frame Scope, node_id: NodeId(12) (empty)
==== TYPES ====
0: string
1: forbidden
1: error
2: float
3: unknown
3: error
4: string
5: forbidden
5: error
6: float
7: unknown
7: error
8: bool
9: forbidden
9: error
10: string
11: unknown
12: ()
11: error
12: error
==== TYPE ERRORS ====
Error (NodeId 1): type mismatch: unsupported addition between string and float
Error (NodeId 5): type mismatch: unsupported append between string and float
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
source: src/test.rs
expression: evaluate_example(path)
input_file: tests/binary_ops_subtypes.nu
snapshot_kind: text
---
==== COMPILER ====
0: Int (0 to 1) "1"
Expand Down Expand Up @@ -94,4 +95,4 @@ input_file: tests/binary_ops_subtypes.nu
40: list<float>
41: list<list<float>>
42: list<list<number>>
43: ()
43: list<list<number>>
21 changes: 11 additions & 10 deletions src/snapshots/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
source: src/test.rs
expression: evaluate_example(path)
input_file: tests/calls.nu
snapshot_kind: text
---
==== COMPILER ====
0: Name (0 to 4) "spam"
Expand Down Expand Up @@ -59,21 +60,21 @@ input_file: tests/calls.nu
9: unknown
10: unknown
11: string
12: unknown
12: string
13: unknown
14: unknown
15: string
16: unknown
16: string
17: unknown
18: unknown
19: int
20: unknown
21: unknown
22: unknown
23: unknown
24: unknown
25: list<unknown>
26: ()
20: int
21: forbidden
22: string
23: string
24: int
25: list<any>
26: list<any>
27: ()
28: unknown
29: string
Expand All @@ -83,4 +84,4 @@ input_file: tests/calls.nu
33: string
34: int
35: any
36: ()
36: any
61 changes: 61 additions & 0 deletions src/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
source: src/test.rs
expression: evaluate_example(path)
input_file: tests/closure3.nu
snapshot_kind: text
---
==== COMPILER ====
0: Variable (4 to 11) "closure"
1: Name (16 to 17) "a"
2: Name (19 to 22) "int"
3: Type { name: NodeId(2), params: None, optional: false } (19 to 22)
4: Param { name: NodeId(1), ty: Some(NodeId(3)) } (16 to 22)
5: Name (24 to 25) "b"
6: Name (27 to 30) "int"
7: Type { name: NodeId(6), params: None, optional: false } (27 to 30)
8: Param { name: NodeId(5), ty: Some(NodeId(7)) } (24 to 30)
9: Params([NodeId(4), NodeId(8)]) (15 to 31)
10: Variable (32 to 34) "$a"
11: Plus (35 to 36)
12: Variable (37 to 39) "$b"
13: LessThan (40 to 41)
14: Int (42 to 43) "5"
15: BinaryOp { lhs: NodeId(10), op: NodeId(11), rhs: NodeId(12) } (32 to 39)
16: BinaryOp { lhs: NodeId(15), op: NodeId(13), rhs: NodeId(14) } (32 to 43)
17: Block(BlockId(0)) (32 to 43)
18: Closure { params: Some(NodeId(9)), block: NodeId(17) } (14 to 44)
19: Let { variable_name: NodeId(0), ty: None, initializer: NodeId(18), is_mutable: false } (0 to 44)
20: Name (46 to 52) "filter"
21: Variable (53 to 61) "$closure"
22: Call { parts: [NodeId(20), NodeId(21)] } (53 to 61)
23: Block(BlockId(1)) (0 to 62)
==== SCOPE ====
0: Frame Scope, node_id: NodeId(23)
variables: [ closure: NodeId(0) ]
1: Frame Scope, node_id: NodeId(17)
variables: [ a: NodeId(1), b: NodeId(5) ]
==== TYPES ====
0: closure
1: unknown
2: unknown
3: int
4: int
5: unknown
6: unknown
7: int
8: int
9: forbidden
10: int
11: forbidden
12: int
13: forbidden
14: int
15: int
16: bool
17: bool
18: closure
19: ()
20: unknown
21: closure
22: stream<binary>
23: stream<binary>
19 changes: 10 additions & 9 deletions src/snapshots/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
source: src/test.rs
expression: evaluate_example(path)
input_file: tests/def.nu
snapshot_kind: text
---
==== COMPILER ====
0: Name (4 to 7) "foo"
Expand Down Expand Up @@ -41,22 +42,22 @@ input_file: tests/def.nu
3: unknown
4: unknown
5: int
6: unknown
6: int
7: unknown
8: unknown
9: unknown
10: unknown
11: int
12: unknown
12: forbidden
13: list<int>
14: unknown
14: forbidden
15: list<list<int>>
16: unknown
17: unknown
16: list<list<int>>
17: forbidden
18: unknown
19: unknown
20: unknown
21: list<unknown>
22: ()
19: int
20: list<list<int>>
21: list<any>
22: list<any>
23: ()
24: ()
62 changes: 30 additions & 32 deletions src/snapshots/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
source: src/test.rs
expression: evaluate_example(path)
input_file: tests/for.nu
snapshot_kind: text
---
==== COMPILER ====
0: Variable (4 to 5) "x"
Expand Down Expand Up @@ -35,7 +36,7 @@ input_file: tests/for.nu
28: BinaryOp { lhs: NodeId(22), op: NodeId(23), rhs: NodeId(27) } (73 to 85)
29: Block(BlockId(1)) (67 to 87)
30: For { variable: NodeId(17), range: NodeId(21), block: NodeId(29) } (49 to 87)
31: Block(BlockId(2)) (0 to 87)
31: Block(BlockId(2)) (0 to 88)
==== SCOPE ====
0: Frame Scope, node_id: NodeId(31)
variables: [ x: NodeId(0) ]
Expand All @@ -47,35 +48,32 @@ input_file: tests/for.nu
0: int
1: int
2: ()
3: unknown
4: unknown
5: unknown
6: unknown
7: unknown
8: unknown
9: unknown
10: unknown
11: unknown
12: unknown
13: unknown
14: unknown
15: unknown
16: unknown
17: unknown
18: unknown
19: unknown
20: unknown
21: unknown
22: unknown
23: unknown
24: unknown
25: unknown
26: unknown
27: unknown
28: unknown
29: unknown
30: unknown
3: int
4: int
5: int
6: int
7: list<int>
8: int
9: forbidden
10: int
11: forbidden
12: int
13: int
14: ()
15: ()
16: ()
17: int
18: int
19: int
20: int
21: list<int>
22: int
23: forbidden
24: int
25: forbidden
26: int
27: int
28: ()
29: ()
30: ()
31: ()
==== TYPE ERRORS ====
Error (NodeId 16): unsupported ast node 'For { variable: NodeId(3), range: NodeId(7), block: NodeId(15) }' in typechecker
Error (NodeId 30): unsupported ast node 'For { variable: NodeId(17), range: NodeId(21), block: NodeId(29) }' in typechecker
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
source: src/test.rs
expression: evaluate_example(path)
input_file: tests/for_break_continue.nu
snapshot_kind: text
---
==== COMPILER ====
0: Variable (4 to 5) "x"
Expand Down Expand Up @@ -47,34 +48,35 @@ input_file: tests/for_break_continue.nu
0: int
1: int
2: ()
3: unknown
4: unknown
5: unknown
6: unknown
7: unknown
8: unknown
9: unknown
10: unknown
11: unknown
3: int
4: int
5: int
6: int
7: list<int>
8: int
9: forbidden
10: int
11: bool
12: unknown
13: unknown
14: unknown
15: unknown
16: unknown
17: unknown
18: unknown
14: oneof<(), unknown>
15: int
16: forbidden
17: int
18: bool
19: unknown
20: unknown
21: unknown
22: unknown
23: unknown
24: unknown
25: unknown
26: unknown
27: unknown
28: unknown
29: unknown
30: unknown
21: oneof<(), unknown>
22: int
23: forbidden
24: int
25: forbidden
26: int
27: int
28: ()
29: ()
30: ()
31: ()
==== TYPE ERRORS ====
Error (NodeId 30): unsupported ast node 'For { variable: NodeId(3), range: NodeId(7), block: NodeId(29) }' in typechecker
Error (NodeId 12): unsupported ast node 'Break' in typechecker
Error (NodeId 19): unsupported ast node 'Continue' in typechecker
Loading

0 comments on commit eba4f27

Please sign in to comment.