Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 75 additions & 4 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,82 @@ impl Compiler {
// TODO: This should say PARSER, not COMPILER
let mut result = "==== COMPILER ====\n".to_string();

fn display_items<T>(items: &[T], display_item: impl Fn(&T) -> String) -> String {
if items.len() <= 7 {
items
.iter()
.map(display_item)
.collect::<Vec<String>>()
.join(",")
} else {
let mut visible_items = Vec::with_capacity(5);
visible_items.extend(items.iter().take(2).map(&display_item));
visible_items.push("...".to_string());
visible_items.extend(items[items.len() - 2..].iter().map(display_item));
visible_items.join(",")
}
}

fn display_nodes(nodes: &[NodeId]) -> String {
display_items(nodes, |node_id| node_id.to_string())
}

fn display_node_pairs(pairs: &[(NodeId, NodeId)]) -> String {
display_items(pairs, |(lhs, rhs)| format!("{lhs}: {rhs}"))
}

for (idx, ast_node) in self.ast_nodes.iter().enumerate() {
result.push_str(&format!(
"{}: {:?} ({} to {})",
idx, ast_node, self.spans[idx].start, self.spans[idx].end
));
let sub_nodes_str = match ast_node {
AstNode::Block(block_id) => Some(display_nodes(&self.blocks[block_id.0].nodes)),
AstNode::TypeArgs(type_args_id) => {
Some(display_nodes(&self.type_args[type_args_id.0].args))
}
AstNode::Params(params_id) => Some(display_nodes(&self.params[params_id.0].nodes)),
AstNode::InOutTypes(in_out_types_id) => {
Some(display_nodes(&self.in_out_types[in_out_types_id.0].nodes))
}
AstNode::Call(call_id) => Some(display_nodes(&self.calls[call_id.0].parts)),
AstNode::List(list_id) => Some(display_nodes(&self.lists[list_id.0].items)),
AstNode::Table(table_id) => {
let table = &self.tables[table_id.0];
Some(format!(
"header: {}, rows: {}",
table.header,
display_nodes(&table.rows)
))
}
AstNode::Record(record_id) => {
let record = &self.records[record_id.0];
Some(format!("pairs: {}", display_node_pairs(&record.pairs)))
}
AstNode::Match(match_id) => {
let match_node = &self.matches[match_id.0];
Some(format!(
"target: {}, arms: {}",
match_node.target,
display_node_pairs(&match_node.match_arms)
))
}
AstNode::Pipeline(pipeline_id) => {
Some(display_nodes(&self.pipelines[pipeline_id.0].nodes))
}
_ => None,
};

match sub_nodes_str {
Some(sub_nodes) if sub_nodes.is_empty() => result.push_str(&format!(
"{}: {:?} ({} to {}) - sub_nodes is empty",
idx, ast_node, self.spans[idx].start, self.spans[idx].end
)),
Some(sub_nodes) => result.push_str(&format!(
"{}: {:?} ({} to {}) - sub_nodes: {}",
idx, ast_node, self.spans[idx].start, self.spans[idx].end, sub_nodes
)),
None => result.push_str(&format!(
"{}: {:?} ({} to {})",
idx, ast_node, self.spans[idx].start, self.spans[idx].end
)),
}

if matches!(
ast_node,
Expand Down
10 changes: 10 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Display;

use crate::compiler::{Compiler, RollbackPoint, Span};
use crate::errors::{Severity, SourceError};
use crate::lexer::{Token, Tokens};
Expand All @@ -9,9 +11,17 @@ pub struct Parser {
tokens: Tokens,
}

/// This is bottom-level ast node ids, every AstNode will be created
/// and save into compilder.ast_nodes
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct NodeId(pub usize);

impl Display for NodeId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct BlockId(pub usize);

Expand Down
5 changes: 2 additions & 3 deletions src/snapshots/new_nu_parser__test__node_output@alias.nu.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: src/test.rs
assertion_line: 77
expression: evaluate_example(path)
input_file: tests/alias.nu
---
Expand All @@ -10,8 +9,8 @@ input_file: tests/alias.nu
2: Alias { new_name: NodeId(0), old_name: NodeId(1) } (0 to 25)
3: Name (27 to 32) "fancy"
4: Name (33 to 38) "alias"
5: Call(CallId(0)) (33 to 38)
6: Block(BlockId(0)) (0 to 39)
5: Call(CallId(0)) (33 to 38) - sub_nodes: 3,4
6: Block(BlockId(0)) (0 to 39) - sub_nodes: 2,5
==== SCOPE ====
0: Frame Scope, node_id: NodeId(6)
decls: [ fancy alias: NodeId(0) ]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: src/test.rs
assertion_line: 77
expression: evaluate_example(path)
input_file: tests/binary_ops_exact.nu
---
Expand All @@ -10,10 +9,10 @@ input_file: tests/binary_ops_exact.nu
2: Int (5 to 6) "1"
3: BinaryOp { lhs: NodeId(0), op: NodeId(1), rhs: NodeId(2) } (0 to 6)
4: True (8 to 12)
5: List(ListId(0)) (7 to 12)
5: List(ListId(0)) (7 to 12) - sub_nodes: 4
6: Append (14 to 16)
7: False (18 to 23)
8: List(ListId(1)) (17 to 23)
8: List(ListId(1)) (17 to 23) - sub_nodes: 7
9: BinaryOp { lhs: NodeId(5), op: NodeId(6), rhs: NodeId(8) } (7 to 23)
10: Int (25 to 26) "1"
11: Plus (27 to 28)
Expand All @@ -35,9 +34,9 @@ input_file: tests/binary_ops_exact.nu
27: In (73 to 75)
28: Int (77 to 78) "1"
29: Int (80 to 81) "2"
30: List(ListId(2)) (76 to 81)
30: List(ListId(2)) (76 to 81) - sub_nodes: 28,29
31: BinaryOp { lhs: NodeId(26), op: NodeId(27), rhs: NodeId(30) } (71 to 81)
32: Block(BlockId(0)) (0 to 83)
32: Block(BlockId(0)) (0 to 83) - sub_nodes: 3,9,13,17,21,25,31
==== SCOPE ====
0: Frame Scope, node_id: NodeId(32) (empty)
==== TYPES ====
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ input_file: tests/binary_ops_mismatch.nu
13: NotRegexMatch (39 to 41)
14: String (42 to 48) ""true""
15: BinaryOp { lhs: NodeId(12), op: NodeId(13), rhs: NodeId(14) } (34 to 48)
16: Block(BlockId(0)) (0 to 49)
16: Block(BlockId(0)) (0 to 49) - sub_nodes: 3,7,11,15
==== SCOPE ====
0: Frame Scope, node_id: NodeId(16) (empty)
==== TYPES ====
Expand Down Expand Up @@ -53,3 +53,4 @@ register_count: 0
file_count: 0
==== IR ERRORS ====
Error (NodeId 0): node String not suported yet

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
source: src/test.rs
expression: evaluate_example(path)
input_file: tests/binary_ops_spaces.nu
snapshot_kind: text
---
==== COMPILER ====
0: Int (0 to 1) "1"
Expand All @@ -21,9 +20,10 @@ snapshot_kind: text
13: Plus (17 to 18)
14: Int (18 to 19) "2"
15: BinaryOp { lhs: NodeId(12), op: NodeId(13), rhs: NodeId(14) } (16 to 19)
16: Block(BlockId(0)) (0 to 20)
16: Block(BlockId(0)) (0 to 20) - sub_nodes: 3,7,11,15
==== COMPILER ERRORS ====
Error (NodeId 5): missing space before operator
Error (NodeId 9): missing space after operator
Error (NodeId 13): missing space before operator
Error (NodeId 13): missing space after operator

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: src/test.rs
assertion_line: 77
expression: evaluate_example(path)
input_file: tests/binary_ops_subtypes.nu
---
Expand All @@ -18,43 +17,43 @@ input_file: tests/binary_ops_subtypes.nu
10: Float (24 to 27) "1.0"
11: BinaryOp { lhs: NodeId(8), op: NodeId(9), rhs: NodeId(10) } (20 to 27)
12: Int (29 to 30) "1"
13: List(ListId(0)) (28 to 30)
13: List(ListId(0)) (28 to 30) - sub_nodes: 12
14: Append (32 to 34)
15: Float (36 to 39) "1.0"
16: List(ListId(1)) (35 to 39)
16: List(ListId(1)) (35 to 39) - sub_nodes: 15
17: BinaryOp { lhs: NodeId(13), op: NodeId(14), rhs: NodeId(16) } (28 to 39)
18: Float (42 to 45) "1.0"
19: Int (46 to 47) "1"
20: List(ListId(2)) (41 to 47)
20: List(ListId(2)) (41 to 47) - sub_nodes: 18,19
21: Append (49 to 51)
22: String (53 to 56) ""a""
23: List(ListId(3)) (52 to 56)
23: List(ListId(3)) (52 to 56) - sub_nodes: 22
24: BinaryOp { lhs: NodeId(20), op: NodeId(21), rhs: NodeId(23) } (41 to 56)
25: Int (60 to 61) "1"
26: List(ListId(4)) (59 to 61)
26: List(ListId(4)) (59 to 61) - sub_nodes: 25
27: Int (64 to 65) "2"
28: List(ListId(5)) (63 to 65)
29: List(ListId(6)) (58 to 66)
28: List(ListId(5)) (63 to 65) - sub_nodes: 27
29: List(ListId(6)) (58 to 66) - sub_nodes: 26,28
30: Append (68 to 70)
31: Int (73 to 74) "3"
32: List(ListId(7)) (72 to 74)
33: List(ListId(8)) (71 to 75)
32: List(ListId(7)) (72 to 74) - sub_nodes: 31
33: List(ListId(8)) (71 to 75) - sub_nodes: 32
34: BinaryOp { lhs: NodeId(29), op: NodeId(30), rhs: NodeId(33) } (58 to 75)
35: Int (79 to 80) "1"
36: List(ListId(9)) (78 to 80)
36: List(ListId(9)) (78 to 80) - sub_nodes: 35
37: Int (83 to 84) "2"
38: List(ListId(10)) (82 to 84)
39: List(ListId(11)) (77 to 85)
38: List(ListId(10)) (82 to 84) - sub_nodes: 37
39: List(ListId(11)) (77 to 85) - sub_nodes: 36,38
40: Append (87 to 89)
41: Float (92 to 95) "3.0"
42: List(ListId(12)) (91 to 95)
43: List(ListId(13)) (90 to 96)
42: List(ListId(12)) (91 to 95) - sub_nodes: 41
43: List(ListId(13)) (90 to 96) - sub_nodes: 42
44: BinaryOp { lhs: NodeId(39), op: NodeId(40), rhs: NodeId(43) } (77 to 96)
45: Int (98 to 99) "1"
46: In (100 to 102)
47: Float (104 to 107) "1.0"
48: Int (109 to 110) "1"
49: List(ListId(14)) (103 to 110)
49: List(ListId(14)) (103 to 110) - sub_nodes: 47,48
50: BinaryOp { lhs: NodeId(45), op: NodeId(46), rhs: NodeId(49) } (98 to 110)
51: Float (112 to 115) "2.3"
52: Modulo (116 to 119)
Expand All @@ -64,19 +63,19 @@ input_file: tests/binary_ops_subtypes.nu
56: Int (127 to 128) "2"
57: String (130 to 131) "c"
58: Int (133 to 134) "3"
59: Record(RecordId(0)) (123 to 135)
60: List(ListId(15)) (122 to 135)
59: Record(RecordId(0)) (123 to 135) - sub_nodes: pairs: 55: 56,57: 58
60: List(ListId(15)) (122 to 135) - sub_nodes: 59
61: Append (137 to 139)
62: String (142 to 143) "a"
63: Int (145 to 146) "3"
64: String (148 to 149) "b"
65: Float (151 to 154) "1.5"
66: String (156 to 157) "c"
67: String (159 to 164) ""foo""
68: Record(RecordId(1)) (141 to 165)
69: List(ListId(16)) (140 to 165)
68: Record(RecordId(1)) (141 to 165) - sub_nodes: pairs: 62: 63,64: 65,66: 67
69: List(ListId(16)) (140 to 165) - sub_nodes: 68
70: BinaryOp { lhs: NodeId(60), op: NodeId(61), rhs: NodeId(69) } (122 to 165)
71: Block(BlockId(0)) (0 to 167)
71: Block(BlockId(0)) (0 to 167) - sub_nodes: 3,7,...,54,70
==== SCOPE ====
0: Frame Scope, node_id: NodeId(71) (empty)
==== TYPES ====
Expand Down
15 changes: 7 additions & 8 deletions src/snapshots/new_nu_parser__test__node_output@calls.nu.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: src/test.rs
assertion_line: 77
expression: evaluate_example(path)
input_file: tests/calls.nu
---
Expand All @@ -12,7 +11,7 @@ input_file: tests/calls.nu
4: Plus (18 to 19)
5: Int (20 to 21) "2"
6: BinaryOp { lhs: NodeId(3), op: NodeId(4), rhs: NodeId(5) } (16 to 21)
7: Call(CallId(0)) (5 to 22)
7: Call(CallId(0)) (5 to 22) - sub_nodes: 0,1,2,6
8: Name (28 to 36) "existing"
9: Name (38 to 39) "a"
10: Name (41 to 47) "string"
Expand All @@ -26,12 +25,12 @@ input_file: tests/calls.nu
18: Name (63 to 66) "int"
19: Type { name: NodeId(18), args: None, optional: false } (63 to 66)
20: Param { name: NodeId(17), ty: Some(NodeId(19)) } (60 to 66)
21: Params(ParamsId(0)) (37 to 67)
21: Params(ParamsId(0)) (37 to 67) - sub_nodes: 12,16,20
22: Variable (72 to 74) "$a"
23: Variable (76 to 78) "$b"
24: Variable (80 to 82) "$c"
25: List(ListId(0)) (70 to 82)
26: Block(BlockId(0)) (68 to 85)
25: List(ListId(0)) (70 to 82) - sub_nodes: 22,23,24
26: Block(BlockId(0)) (68 to 85) - sub_nodes: 25
27: Def { name: NodeId(8), type_params: None, params: NodeId(21), in_out_types: None, block: NodeId(26), env: false, wrapped: false } (24 to 85)
28: Name (86 to 94) "existing"
29: Name (95 to 98) "foo"
Expand All @@ -40,11 +39,11 @@ input_file: tests/calls.nu
32: String (107 to 110) ""r""
33: BinaryOp { lhs: NodeId(30), op: NodeId(31), rhs: NodeId(32) } (100 to 110)
34: Int (112 to 113) "3"
35: Call(CallId(1)) (95 to 113)
35: Call(CallId(1)) (95 to 113) - sub_nodes: 28,29,33,34
36: Name (115 to 128) "foo/bar/spam
"
37: Call(CallId(2)) (127 to 127)
38: Block(BlockId(1)) (0 to 128)
37: Call(CallId(2)) (127 to 127) - sub_nodes: 36
38: Block(BlockId(1)) (0 to 128) - sub_nodes: 7,27,35,37
==== SCOPE ====
0: Frame Scope, node_id: NodeId(38)
decls: [ existing: NodeId(8) ]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: src/test.rs
assertion_line: 77
expression: evaluate_example(path)
input_file: tests/calls_invalid.nu
---
Expand All @@ -10,17 +9,17 @@ input_file: tests/calls_invalid.nu
2: Name (13 to 16) "int"
3: Type { name: NodeId(2), args: None, optional: false } (13 to 16)
4: Param { name: NodeId(1), ty: Some(NodeId(3)) } (10 to 16)
5: Params(ParamsId(0)) (8 to 18)
6: Block(BlockId(0)) (19 to 21)
5: Params(ParamsId(0)) (8 to 18) - sub_nodes: 4
6: Block(BlockId(0)) (19 to 21) - sub_nodes is empty
7: Def { name: NodeId(0), type_params: None, params: NodeId(5), in_out_types: None, block: NodeId(6), env: false, wrapped: false } (0 to 21)
8: Name (22 to 25) "foo"
9: Int (26 to 27) "1"
10: Int (28 to 29) "2"
11: Call(CallId(0)) (26 to 29)
11: Call(CallId(0)) (26 to 29) - sub_nodes: 8,9,10
12: Name (30 to 33) "foo"
13: String (34 to 42) ""string""
14: Call(CallId(1)) (34 to 42)
15: Block(BlockId(1)) (0 to 43)
14: Call(CallId(1)) (34 to 42) - sub_nodes: 12,13
15: Block(BlockId(1)) (0 to 43) - sub_nodes: 7,11,14
==== SCOPE ====
0: Frame Scope, node_id: NodeId(15)
decls: [ foo: NodeId(0) ]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: src/test.rs
assertion_line: 77
expression: evaluate_example(path)
input_file: tests/closure.nu
---
Expand All @@ -9,15 +8,15 @@ input_file: tests/closure.nu
1: Param { name: NodeId(0), ty: None } (3 to 4)
2: Name (6 to 7) "b"
3: Param { name: NodeId(2), ty: None } (6 to 7)
4: Params(ParamsId(0)) (2 to 8)
4: Params(ParamsId(0)) (2 to 8) - sub_nodes: 1,3
5: Variable (9 to 11) "$a"
6: Plus (12 to 13)
7: Variable (14 to 16) "$b"
8: BinaryOp { lhs: NodeId(5), op: NodeId(6), rhs: NodeId(7) } (9 to 16)
9: Block(BlockId(0)) (9 to 16)
9: Block(BlockId(0)) (9 to 16) - sub_nodes: 8
10: Closure { params: Some(NodeId(4)), block: NodeId(9) } (0 to 17)
11: Variable (18 to 20) "$a"
12: Block(BlockId(1)) (0 to 42)
12: Block(BlockId(1)) (0 to 42) - sub_nodes: 10,11
==== SCOPE ====
0: Frame Scope, node_id: NodeId(12) (empty)
1: Frame Scope, node_id: NodeId(9)
Expand Down
Loading
Loading