Skip to content

Commit 4543afb

Browse files
authored
Use term type *args* rather than *params* (#52)
This PR distinguishes between the terms "parameter" and "argument." Right now, the code sort of treats the two terms interchangeably, which is fine, but the pedant in me could not tolerate it :) This will also make it less confusing when we add type parameters to the language in the future, because currently, type *arguments* are referred to as parameters.
1 parent 0838103 commit 4543afb

9 files changed

+75
-77
lines changed

src/parser.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ pub enum AstNode {
6161
Name,
6262
Type {
6363
name: NodeId,
64-
params: Option<NodeId>,
64+
args: Option<NodeId>,
6565
optional: bool,
6666
},
67+
TypeArgs(Vec<NodeId>),
6768
Variable,
6869

6970
// Booleans
@@ -611,19 +612,12 @@ impl Parser {
611612

612613
// Explicit closure case
613614
if self.is_pipe() {
614-
let args = Some(self.signature_params(ParamsContext::Pipes));
615+
let params = Some(self.signature_params(ParamsContext::Pipes));
615616
let block = self.block(BlockContext::Closure);
616617
self.rcurly();
617618
span_end = self.position();
618619

619-
return self.create_node(
620-
AstNode::Closure {
621-
params: args,
622-
block,
623-
},
624-
span_start,
625-
span_end,
626-
);
620+
return self.create_node(AstNode::Closure { params, block }, span_start, span_end);
627621
}
628622

629623
let rollback_point = self.get_rollback_point();
@@ -927,11 +921,11 @@ impl Parser {
927921
self.create_node(AstNode::Params(param_list), span_start, span_end)
928922
}
929923

930-
pub fn type_params(&mut self) -> NodeId {
924+
pub fn type_args(&mut self) -> NodeId {
931925
let _span = span!();
932926
let span_start = self.position();
933927
let span_end;
934-
let param_list = {
928+
let arg_list = {
935929
self.less_than();
936930

937931
let mut output = vec![];
@@ -955,17 +949,17 @@ impl Parser {
955949
output
956950
};
957951

958-
self.create_node(AstNode::Params(param_list), span_start, span_end)
952+
self.create_node(AstNode::TypeArgs(arg_list), span_start, span_end)
959953
}
960954

961955
pub fn typename(&mut self) -> NodeId {
962956
let _span = span!();
963957
if let (Token::Bareword, span) = self.tokens.peek() {
964958
let name = self.name();
965-
let mut params = None;
959+
let mut args = None;
966960
if self.is_less_than() {
967961
// We have generics
968-
params = Some(self.type_params());
962+
args = Some(self.type_args());
969963
}
970964

971965
let optional = if self.is_question_mark() {
@@ -979,7 +973,7 @@ impl Parser {
979973
self.create_node(
980974
AstNode::Type {
981975
name,
982-
params,
976+
args,
983977
optional,
984978
},
985979
span.start,

src/snapshots/[email protected]

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ input_file: tests/calls.nu
1515
8: Name (28 to 36) "existing"
1616
9: Name (38 to 39) "a"
1717
10: Name (41 to 47) "string"
18-
11: Type { name: NodeId(10), params: None, optional: false } (41 to 47)
18+
11: Type { name: NodeId(10), args: None, optional: false } (41 to 47)
1919
12: Param { name: NodeId(9), ty: Some(NodeId(11)) } (38 to 47)
2020
13: Name (49 to 50) "b"
2121
14: Name (52 to 58) "string"
22-
15: Type { name: NodeId(14), params: None, optional: false } (52 to 58)
22+
15: Type { name: NodeId(14), args: None, optional: false } (52 to 58)
2323
16: Param { name: NodeId(13), ty: Some(NodeId(15)) } (49 to 58)
2424
17: Name (60 to 61) "c"
2525
18: Name (63 to 66) "int"
26-
19: Type { name: NodeId(18), params: None, optional: false } (63 to 66)
26+
19: Type { name: NodeId(18), args: None, optional: false } (63 to 66)
2727
20: Param { name: NodeId(17), ty: Some(NodeId(19)) } (60 to 66)
2828
21: Params([NodeId(12), NodeId(16), NodeId(20)]) (37 to 67)
2929
22: Variable (72 to 74) "$a"

src/snapshots/[email protected]

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22
source: src/test.rs
33
expression: evaluate_example(path)
44
input_file: tests/closure3.nu
5-
snapshot_kind: text
65
---
76
==== COMPILER ====
87
0: Variable (4 to 11) "closure"
98
1: Name (16 to 17) "a"
109
2: Name (19 to 22) "int"
11-
3: Type { name: NodeId(2), params: None, optional: false } (19 to 22)
10+
3: Type { name: NodeId(2), args: None, optional: false } (19 to 22)
1211
4: Param { name: NodeId(1), ty: Some(NodeId(3)) } (16 to 22)
1312
5: Name (24 to 25) "b"
1413
6: Name (27 to 30) "int"
15-
7: Type { name: NodeId(6), params: None, optional: false } (27 to 30)
14+
7: Type { name: NodeId(6), args: None, optional: false } (27 to 30)
1615
8: Param { name: NodeId(5), ty: Some(NodeId(7)) } (24 to 30)
1716
9: Params([NodeId(4), NodeId(8)]) (15 to 31)
1817
10: Variable (32 to 34) "$a"

src/snapshots/[email protected]

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ input_file: tests/def.nu
99
2: Param { name: NodeId(1), ty: None } (9 to 10)
1010
3: Name (11 to 12) "y"
1111
4: Name (14 to 17) "int"
12-
5: Type { name: NodeId(4), params: None, optional: false } (14 to 17)
12+
5: Type { name: NodeId(4), args: None, optional: false } (14 to 17)
1313
6: Param { name: NodeId(3), ty: Some(NodeId(5)) } (11 to 17)
1414
7: Name (19 to 20) "z"
1515
8: Name (22 to 26) "list"
1616
9: Name (27 to 31) "list"
1717
10: Name (32 to 35) "int"
18-
11: Type { name: NodeId(10), params: None, optional: false } (32 to 35)
19-
12: Params([NodeId(11)]) (31 to 36)
20-
13: Type { name: NodeId(9), params: Some(NodeId(12)), optional: false } (27 to 31)
21-
14: Params([NodeId(13)]) (26 to 37)
22-
15: Type { name: NodeId(8), params: Some(NodeId(14)), optional: false } (22 to 26)
18+
11: Type { name: NodeId(10), args: None, optional: false } (32 to 35)
19+
12: TypeArgs([NodeId(11)]) (31 to 36)
20+
13: Type { name: NodeId(9), args: Some(NodeId(12)), optional: false } (27 to 31)
21+
14: TypeArgs([NodeId(13)]) (26 to 37)
22+
15: Type { name: NodeId(8), args: Some(NodeId(14)), optional: false } (22 to 26)
2323
16: Param { name: NodeId(7), ty: Some(NodeId(15)) } (19 to 26)
2424
17: Params([NodeId(2), NodeId(6), NodeId(16)]) (8 to 39)
2525
18: Variable (44 to 46) "$x"

src/snapshots/new_nu_parser__test__node_output@def_return_type.nu.snap

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ input_file: tests/def_return_type.nu
77
0: Name (4 to 7) "foo"
88
1: Params([]) (8 to 11)
99
2: Name (14 to 21) "nothing"
10-
3: Type { name: NodeId(2), params: None, optional: false } (14 to 21)
10+
3: Type { name: NodeId(2), args: None, optional: false } (14 to 21)
1111
4: Name (25 to 29) "list"
1212
5: Name (30 to 33) "any"
13-
6: Type { name: NodeId(5), params: None, optional: false } (30 to 33)
14-
7: Params([NodeId(6)]) (29 to 34)
15-
8: Type { name: NodeId(4), params: Some(NodeId(7)), optional: false } (25 to 29)
13+
6: Type { name: NodeId(5), args: None, optional: false } (30 to 33)
14+
7: TypeArgs([NodeId(6)]) (29 to 34)
15+
8: Type { name: NodeId(4), args: Some(NodeId(7)), optional: false } (25 to 29)
1616
9: InOutType(NodeId(3), NodeId(8)) (14 to 35)
1717
10: InOutTypes([NodeId(9)]) (14 to 35)
1818
11: List([]) (37 to 38)
@@ -21,20 +21,20 @@ input_file: tests/def_return_type.nu
2121
14: Name (46 to 49) "bar"
2222
15: Params([]) (50 to 53)
2323
16: Name (58 to 64) "string"
24-
17: Type { name: NodeId(16), params: None, optional: false } (58 to 64)
24+
17: Type { name: NodeId(16), args: None, optional: false } (58 to 64)
2525
18: Name (68 to 72) "list"
2626
19: Name (73 to 79) "string"
27-
20: Type { name: NodeId(19), params: None, optional: false } (73 to 79)
28-
21: Params([NodeId(20)]) (72 to 80)
29-
22: Type { name: NodeId(18), params: Some(NodeId(21)), optional: false } (68 to 72)
27+
20: Type { name: NodeId(19), args: None, optional: false } (73 to 79)
28+
21: TypeArgs([NodeId(20)]) (72 to 80)
29+
22: Type { name: NodeId(18), args: Some(NodeId(21)), optional: false } (68 to 72)
3030
23: InOutType(NodeId(17), NodeId(22)) (58 to 80)
3131
24: Name (82 to 85) "int"
32-
25: Type { name: NodeId(24), params: None, optional: false } (82 to 85)
32+
25: Type { name: NodeId(24), args: None, optional: false } (82 to 85)
3333
26: Name (89 to 93) "list"
3434
27: Name (94 to 97) "int"
35-
28: Type { name: NodeId(27), params: None, optional: false } (94 to 97)
36-
29: Params([NodeId(28)]) (93 to 98)
37-
30: Type { name: NodeId(26), params: Some(NodeId(29)), optional: false } (89 to 93)
35+
28: Type { name: NodeId(27), args: None, optional: false } (94 to 97)
36+
29: TypeArgs([NodeId(28)]) (93 to 98)
37+
30: Type { name: NodeId(26), args: Some(NodeId(29)), optional: false } (89 to 93)
3838
31: InOutType(NodeId(25), NodeId(30)) (82 to 99)
3939
32: InOutTypes([NodeId(23), NodeId(31)]) (56 to 101)
4040
33: List([]) (103 to 104)

src/snapshots/new_nu_parser__test__node_output@invalid_types.nu.snap

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ input_file: tests/invalid_types.nu
88
1: Name (9 to 10) "x"
99
2: Name (12 to 16) "list"
1010
3: Name (17 to 20) "int"
11-
4: Type { name: NodeId(3), params: None, optional: false } (17 to 20)
11+
4: Type { name: NodeId(3), args: None, optional: false } (17 to 20)
1212
5: Name (22 to 28) "string"
13-
6: Type { name: NodeId(5), params: None, optional: false } (22 to 28)
14-
7: Params([NodeId(4), NodeId(6)]) (16 to 29)
15-
8: Type { name: NodeId(2), params: Some(NodeId(7)), optional: false } (12 to 16)
13+
6: Type { name: NodeId(5), args: None, optional: false } (22 to 28)
14+
7: TypeArgs([NodeId(4), NodeId(6)]) (16 to 29)
15+
8: Type { name: NodeId(2), args: Some(NodeId(7)), optional: false } (12 to 16)
1616
9: Param { name: NodeId(1), ty: Some(NodeId(8)) } (9 to 16)
1717
10: Params([NodeId(9)]) (8 to 30)
1818
11: Variable (33 to 35) "$x"
@@ -21,8 +21,8 @@ input_file: tests/invalid_types.nu
2121
14: Name (42 to 45) "bar"
2222
15: Name (47 to 48) "y"
2323
16: Name (50 to 54) "list"
24-
17: Params([]) (54 to 56)
25-
18: Type { name: NodeId(16), params: Some(NodeId(17)), optional: false } (50 to 54)
24+
17: TypeArgs([]) (54 to 56)
25+
18: Type { name: NodeId(16), args: Some(NodeId(17)), optional: false } (50 to 54)
2626
19: Param { name: NodeId(15), ty: Some(NodeId(18)) } (47 to 54)
2727
20: Params([NodeId(19)]) (46 to 57)
2828
21: Variable (60 to 62) "$y"
@@ -63,8 +63,8 @@ input_file: tests/invalid_types.nu
6363
23: ()
6464
24: ()
6565
==== TYPE ERRORS ====
66-
Error (NodeId 7): list must have only one type parameter (to allow selection of types, use oneof<int, string> -- WIP)
67-
Error (NodeId 17): list must have one type parameter
66+
Error (NodeId 7): list must have only one type argument (to allow selection of types, use oneof<int, string> -- WIP)
67+
Error (NodeId 17): list must have one type argument
6868
==== IR ====
6969
register_count: 0
7070
file_count: 0

src/snapshots/new_nu_parser__test__node_output@let_mismatch.nu.snap

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,32 @@
22
source: src/test.rs
33
expression: evaluate_example(path)
44
input_file: tests/let_mismatch.nu
5-
snapshot_kind: text
65
---
76
==== COMPILER ====
87
0: Variable (4 to 5) "x"
98
1: Name (7 to 13) "number"
10-
2: Type { name: NodeId(1), params: None, optional: false } (7 to 13)
9+
2: Type { name: NodeId(1), args: None, optional: false } (7 to 13)
1110
3: Int (16 to 18) "10"
1211
4: Let { variable_name: NodeId(0), ty: Some(NodeId(2)), initializer: NodeId(3), is_mutable: false } (0 to 18)
1312
5: Variable (32 to 33) "y"
1413
6: Name (35 to 38) "any"
15-
7: Type { name: NodeId(6), params: None, optional: false } (35 to 38)
14+
7: Type { name: NodeId(6), args: None, optional: false } (35 to 38)
1615
8: String (41 to 47) ""spam""
1716
9: Let { variable_name: NodeId(5), ty: Some(NodeId(7)), initializer: NodeId(8), is_mutable: false } (28 to 47)
1817
10: Variable (60 to 61) "z"
1918
11: Name (63 to 69) "string"
20-
12: Type { name: NodeId(11), params: None, optional: false } (63 to 69)
19+
12: Type { name: NodeId(11), args: None, optional: false } (63 to 69)
2120
13: Int (72 to 75) "123"
2221
14: Let { variable_name: NodeId(10), ty: Some(NodeId(12)), initializer: NodeId(13), is_mutable: false } (56 to 75)
2322
15: Variable (91 to 92) "w"
2423
16: Name (94 to 98) "list"
2524
17: Name (99 to 103) "list"
2625
18: Name (104 to 107) "int"
27-
19: Type { name: NodeId(18), params: None, optional: false } (104 to 107)
28-
20: Params([NodeId(19)]) (103 to 108)
29-
21: Type { name: NodeId(17), params: Some(NodeId(20)), optional: false } (99 to 103)
30-
22: Params([NodeId(21)]) (98 to 109)
31-
23: Type { name: NodeId(16), params: Some(NodeId(22)), optional: false } (94 to 98)
26+
19: Type { name: NodeId(18), args: None, optional: false } (104 to 107)
27+
20: TypeArgs([NodeId(19)]) (103 to 108)
28+
21: Type { name: NodeId(17), args: Some(NodeId(20)), optional: false } (99 to 103)
29+
22: TypeArgs([NodeId(21)]) (98 to 109)
30+
23: Type { name: NodeId(16), args: Some(NodeId(22)), optional: false } (94 to 98)
3231
24: String (116 to 119) "'a'"
3332
25: List([NodeId(24)]) (114 to 120)
3433
26: List([NodeId(25)]) (112 to 122)

src/snapshots/new_nu_parser__test__node_output@mut_.nu.snap

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
source: src/test.rs
33
expression: evaluate_example(path)
44
input_file: tests/mut_.nu
5-
snapshot_kind: text
65
---
76
==== COMPILER ====
87
0: Variable (4 to 5) "x"
98
1: Name (7 to 10) "int"
10-
2: Type { name: NodeId(1), params: None, optional: false } (7 to 10)
9+
2: Type { name: NodeId(1), args: None, optional: false } (7 to 10)
1110
3: Int (13 to 16) "123"
1211
4: Let { variable_name: NodeId(0), ty: Some(NodeId(2)), initializer: NodeId(3), is_mutable: true } (0 to 16)
1312
5: Variable (18 to 20) "$x"

src/typechecker.rs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,19 @@ impl<'a> Typechecker<'a> {
225225
}
226226
AstNode::Type {
227227
name,
228-
params,
228+
args,
229229
optional,
230230
} => {
231-
let ty_id = self.typecheck_type(name, params, optional);
231+
let ty_id = self.typecheck_type(name, args, optional);
232232
self.set_node_type_id(node_id, ty_id);
233233
}
234+
AstNode::TypeArgs(ref args) => {
235+
for arg in args {
236+
self.typecheck_node(*arg);
237+
}
238+
// Type argument lists are not supposed to be evaluated
239+
self.set_node_type_id(node_id, FORBIDDEN_TYPE);
240+
}
234241
AstNode::List(ref items) => {
235242
if let Some(first_id) = items.first() {
236243
self.typecheck_node(*first_id);
@@ -643,23 +650,23 @@ impl<'a> Typechecker<'a> {
643650
};
644651
let AstNode::Type {
645652
name: in_name,
646-
params: in_params,
653+
args: in_args,
647654
optional: in_optional,
648655
} = *self.compiler.get_node(*in_ty)
649656
else {
650657
panic!("internal error: type is not a type");
651658
};
652659
let AstNode::Type {
653660
name: out_name,
654-
params: out_params,
661+
args: out_args,
655662
optional: out_optional,
656663
} = *self.compiler.get_node(*out_ty)
657664
else {
658665
panic!("internal error: type is not a type");
659666
};
660667
InOutType {
661-
in_type: self.typecheck_type(in_name, in_params, in_optional),
662-
out_type: self.typecheck_type(out_name, out_params, out_optional),
668+
in_type: self.typecheck_type(in_name, in_args, in_optional),
669+
out_type: self.typecheck_type(out_name, out_args, out_optional),
663670
}
664671
})
665672
.collect::<Vec<_>>()
@@ -767,7 +774,7 @@ impl<'a> Typechecker<'a> {
767774
fn typecheck_type(
768775
&mut self,
769776
name_id: NodeId,
770-
params_id: Option<NodeId>,
777+
args_id: Option<NodeId>,
771778
_optional: bool,
772779
) -> TypeId {
773780
let name = self.compiler.get_span_contents(name_id);
@@ -778,24 +785,24 @@ impl<'a> Typechecker<'a> {
778785
// b"binary" => SyntaxShape::Binary,
779786
// b"block" => // not possible to pass blocks
780787
b"list" => {
781-
if let Some(params_id) = params_id {
782-
self.typecheck_node(params_id);
788+
if let Some(args_id) = args_id {
789+
self.typecheck_node(args_id);
783790

784-
if let AstNode::Params(params) = self.compiler.get_node(params_id) {
785-
if params.len() > 1 {
791+
if let AstNode::TypeArgs(args) = self.compiler.get_node(args_id) {
792+
if args.len() > 1 {
786793
let types =
787-
String::from_utf8_lossy(self.compiler.get_span_contents(params_id));
788-
self.error(format!("list must have only one type parameter (to allow selection of types, use oneof{} -- WIP)", types), params_id);
794+
String::from_utf8_lossy(self.compiler.get_span_contents(args_id));
795+
self.error(format!("list must have only one type argument (to allow selection of types, use oneof{} -- WIP)", types), args_id);
789796
self.push_type(Type::List(UNKNOWN_TYPE))
790-
} else if params.is_empty() {
791-
self.error("list must have one type parameter", params_id);
797+
} else if args.is_empty() {
798+
self.error("list must have one type argument", args_id);
792799
self.push_type(Type::List(UNKNOWN_TYPE))
793800
} else {
794-
let params_ty_id = self.type_id_of(params[0]);
795-
self.push_type(Type::List(params_ty_id))
801+
let args_ty_id = self.type_id_of(args[0]);
802+
self.push_type(Type::List(args_ty_id))
796803
}
797804
} else {
798-
panic!("params are not params");
805+
panic!("args are not args");
799806
}
800807
} else {
801808
LIST_ANY_TYPE

0 commit comments

Comments
 (0)