Skip to content

Commit f64c79f

Browse files
authored
feat(tolk): support arrays from Tolk 1.3 (#263)
Fixes #262
1 parent 45cdab4 commit f64c79f

File tree

8 files changed

+36541
-30893
lines changed

8 files changed

+36541
-30893
lines changed
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
========================================================================
2+
Array type — Basic
3+
========================================================================
4+
struct array<T> {}
5+
6+
fun main() {
7+
val a: array<int> = [1, 2, 3];
8+
//! ^ array<int>
9+
}
10+
------------------------------------------------------------------------
11+
ok
12+
13+
========================================================================
14+
Array type — Inference from elements
15+
========================================================================
16+
struct array<T> {}
17+
18+
fun main() {
19+
val a = [1, 2, 3];
20+
//! ^ array<int>
21+
}
22+
------------------------------------------------------------------------
23+
ok
24+
25+
========================================================================
26+
Array type — Explicit literal
27+
========================================================================
28+
struct array<T> {}
29+
30+
fun main() {
31+
val a = array<int>[1, 2];
32+
//! ^ array<int>
33+
}
34+
------------------------------------------------------------------------
35+
ok
36+
37+
========================================================================
38+
Array type — Mixed elements
39+
========================================================================
40+
struct array<T> {}
41+
42+
fun main() {
43+
val a = [1, null];
44+
//! ^ array<int?>
45+
}
46+
------------------------------------------------------------------------
47+
ok
48+
49+
========================================================================
50+
Array type — Empty array
51+
========================================================================
52+
struct array<T> {}
53+
54+
fun main() {
55+
val a = [];
56+
//! ^ array<unknown>
57+
}
58+
------------------------------------------------------------------------
59+
ok
60+
61+
========================================================================
62+
Tuple type — Tuple hint
63+
========================================================================
64+
fun main() {
65+
val a: [int, bool] = [1, true];
66+
//! ^ [int, bool]
67+
}
68+
------------------------------------------------------------------------
69+
ok
70+
71+
========================================================================
72+
Array type — Numeric index access
73+
========================================================================
74+
struct array<T> {}
75+
76+
fun main() {
77+
val a: array<int> = [1, 2, 3];
78+
val x = a.0;
79+
//! ^ int
80+
val y = a.100;
81+
//! ^ int
82+
}
83+
------------------------------------------------------------------------
84+
ok
85+
86+
========================================================================
87+
Array type — Generic deduction
88+
========================================================================
89+
struct array<T> {}
90+
91+
fun first<T>(arr: array<T>): T {
92+
return arr.0;
93+
}
94+
95+
fun main() {
96+
val a = [1, 2, 3];
97+
val x = first(a);
98+
//! ^ int
99+
}
100+
------------------------------------------------------------------------
101+
ok
102+
103+
========================================================================
104+
Array type — Struct hint in Tuple
105+
========================================================================
106+
struct Foo {
107+
foo: int
108+
}
109+
110+
fun main() {
111+
val a: [Foo] = [{foo: 10}];
112+
//! ^ int
113+
val item = a.0;
114+
val field = item.foo;
115+
//! ^ int
116+
}
117+
------------------------------------------------------------------------
118+
ok
119+
120+
========================================================================
121+
Array type — Struct hint in Array
122+
========================================================================
123+
struct array<T> {}
124+
125+
struct Foo {
126+
foo: int
127+
}
128+
129+
fun main() {
130+
val a: array<Foo> = [{foo: 10}];
131+
//! ^ int
132+
val field = a.0.foo;
133+
//! ^ int
134+
}
135+
------------------------------------------------------------------------
136+
ok
137+
138+
========================================================================
139+
Array type — Struct hint in Explicit Array
140+
========================================================================
141+
struct array<T> {}
142+
143+
struct Foo {
144+
foo: int
145+
}
146+
147+
fun main() {
148+
val a = array<Foo>[{foo: 10}];
149+
//! ^ int
150+
val field = a.0.foo;
151+
//! ^ int
152+
}
153+
------------------------------------------------------------------------
154+
ok
155+
156+
========================================================================
157+
Array type — lisp_list hint
158+
========================================================================
159+
struct Foo { foo: int }
160+
161+
struct lisp_list<T> {}
162+
163+
fun main() {
164+
val a: lisp_list<Foo> = [{foo: 10}];
165+
//! ^ int
166+
}
167+
------------------------------------------------------------------------
168+
ok

server/src/languages/tolk/psi/Reference.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
UnionTy,
3333
NullTy,
3434
EnumTy,
35+
ArrayTy,
3536
} from "@server/languages/tolk/types/ty"
3637
import {parentOfType} from "@server/psi/utils"
3738
import {inferenceOf, typeOf} from "@server/languages/tolk/type-inference"
@@ -445,6 +446,9 @@ export class Reference {
445446
) {
446447
return receiverType.innerTy.name() === expected.innerTy.name()
447448
}
449+
if (receiverType instanceof ArrayTy && expected instanceof ArrayTy) {
450+
return true
451+
}
448452
}
449453

450454
if (receiver?.type === "nullable_type") {

server/src/languages/tolk/tree-sitter-tolk/grammar.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,14 @@ const TOLK_GRAMMAR = {
603603
parenthesized_expression: $ => seq("(", field("inner", $._expression), optional(","), ")"),
604604
tensor_expression: $ =>
605605
choice(seq("(", ")"), seq("(", commaSep2($._expression), optional(","), ")")),
606-
typed_tuple: $ => seq("[", commaSep($._expression), optional(","), "]"),
606+
typed_tuple: $ =>
607+
seq(
608+
optional(field("type", $._type_hint)),
609+
"[",
610+
field("elements", commaSep($._expression)),
611+
optional(","),
612+
"]",
613+
),
607614

608615
lambda_expression: $ =>
609616
prec.right(

server/src/languages/tolk/tree-sitter-tolk/src/grammar.json

Lines changed: 87 additions & 49 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)