Skip to content

Commit c9a2cf2

Browse files
committed
put pipeline into compiler
1 parent ccebd5c commit c9a2cf2

File tree

7 files changed

+30
-16
lines changed

7 files changed

+30
-16
lines changed

src/compiler.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::errors::SourceError;
2-
use crate::parser::{AstNode, Block, NodeId};
2+
use crate::parser::{AstNode, Block, NodeId, Pipeline};
33
use crate::protocol::Command;
44
use crate::resolver::{DeclId, Frame, NameBindings, ScopeId, VarId, Variable};
55
use crate::typechecker::{TypeId, Types};
@@ -44,7 +44,8 @@ pub struct Compiler {
4444
pub ast_nodes: Vec<AstNode>,
4545
pub node_types: Vec<TypeId>,
4646
// node_lifetimes: Vec<AllocationLifetime>,
47-
pub blocks: Vec<Block>, // Blocks, indexed by BlockId
47+
pub blocks: Vec<Block>, // Blocks, indexed by BlockId
48+
pub pipelines: Vec<Pipeline>, // Pipelines, indexed by PipelineId
4849
pub source: Vec<u8>,
4950
pub file_offsets: Vec<(String, usize, usize)>, // fname, start, end
5051

@@ -87,6 +88,7 @@ impl Compiler {
8788
ast_nodes: vec![],
8889
node_types: vec![],
8990
blocks: vec![],
91+
pipelines: vec![],
9092
source: vec![],
9193
file_offsets: vec![],
9294

src/parser.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ pub struct NodeId(pub usize);
1515
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1616
pub struct BlockId(pub usize);
1717

18+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
19+
pub struct PipelineId(pub usize);
20+
1821
#[derive(Debug, Clone)]
1922
pub struct Block {
2023
pub nodes: Vec<NodeId>,
@@ -234,7 +237,7 @@ pub enum AstNode {
234237
field: NodeId,
235238
},
236239
Block(BlockId),
237-
Pipeline(Pipeline),
240+
Pipeline(PipelineId),
238241
If {
239242
condition: NodeId,
240243
then_block: NodeId,
@@ -315,9 +318,10 @@ impl Parser {
315318
}
316319
expressions.push(self.expression());
317320
}
321+
self.compiler.pipelines.push(Pipeline::new(expressions));
318322
let span_end = self.position();
319323
self.create_node(
320-
AstNode::Pipeline(Pipeline::new(expressions)),
324+
AstNode::Pipeline(PipelineId(self.compiler.pipelines.len() - 1)),
321325
span_start,
322326
span_end,
323327
)

src/resolver.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::protocol::{Command, Declaration};
22
use crate::{
33
compiler::Compiler,
44
errors::{Severity, SourceError},
5-
parser::{AstNode, BlockId, NodeId},
5+
parser::{AstNode, BlockId, NodeId, PipelineId},
66
};
77
use std::collections::HashMap;
88

@@ -338,11 +338,7 @@ impl<'a> Resolver<'a> {
338338
}
339339
}
340340
AstNode::Statement(node) => self.resolve_node(node),
341-
AstNode::Pipeline(ref pipeline) => {
342-
for exp in pipeline.get_expressions() {
343-
self.resolve_node(*exp)
344-
}
345-
}
341+
AstNode::Pipeline(pipeline_id) => self.resolve_pipeline(pipeline_id),
346342
AstNode::Param { .. } => (/* seems unused for now */),
347343
AstNode::Type { .. } => ( /* probably doesn't make sense to resolve? */ ),
348344
AstNode::NamedValue { .. } => (/* seems unused for now */),
@@ -351,6 +347,14 @@ impl<'a> Resolver<'a> {
351347
}
352348
}
353349

350+
pub fn resolve_pipeline(&mut self, pipeline_id: PipelineId) {
351+
let pipeline = &self.compiler.pipelines[pipeline_id.0];
352+
353+
for exp in pipeline.get_expressions() {
354+
self.resolve_node(*exp)
355+
}
356+
}
357+
354358
pub fn resolve_variable(&mut self, unbound_node_id: NodeId) {
355359
let var_name = trim_var_name(self.compiler.get_span_contents(unbound_node_id));
356360

src/snapshots/new_nu_parser__test__node_output@let_.nu.snap

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ snapshot_kind: text
1212
4: Variable (21 to 22) "x"
1313
5: Int (25 to 28) "123"
1414
6: Int (31 to 34) "456"
15-
7: Pipeline(Pipeline { nodes: [NodeId(5), NodeId(6)] }) (25 to 34)
15+
7: Pipeline(PipelineId(0)) (25 to 34)
1616
8: Let { variable_name: NodeId(4), ty: None, initializer: NodeId(7), is_mutable: false } (17 to 34)
1717
9: Variable (36 to 38) "$x"
1818
10: Block(BlockId(0)) (0 to 39)
@@ -36,3 +36,4 @@ register_count: 0
3636
file_count: 0
3737
==== IR ERRORS ====
3838
Error (NodeId 2): node Let { variable_name: NodeId(0), ty: None, initializer: NodeId(1), is_mutable: false } not suported yet
39+

src/snapshots/new_nu_parser__test__node_output@mut_.nu.snap

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ snapshot_kind: text
2222
14: Type { name: NodeId(13), args: None, optional: false } (39 to 42)
2323
15: Int (45 to 48) "123"
2424
16: Int (51 to 54) "344"
25-
17: Pipeline(Pipeline { nodes: [NodeId(15), NodeId(16)] }) (45 to 54)
25+
17: Pipeline(PipelineId(0)) (45 to 54)
2626
18: Let { variable_name: NodeId(12), ty: Some(NodeId(14)), initializer: NodeId(17), is_mutable: true } (32 to 54)
2727
19: Variable (56 to 58) "$y"
2828
20: Assignment (59 to 60)
@@ -68,3 +68,4 @@ register_count: 0
6868
file_count: 0
6969
==== IR ERRORS ====
7070
Error (NodeId 4): node Let { variable_name: NodeId(0), ty: Some(NodeId(2)), initializer: NodeId(3), is_mutable: true } not suported yet
71+

src/snapshots/new_nu_parser__test__node_output@pipeline.nu.snap

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ snapshot_kind: text
88
0: Int (0 to 1) "1"
99
1: Int (4 to 5) "3"
1010
2: Int (8 to 9) "5"
11-
3: Pipeline(Pipeline { nodes: [NodeId(0), NodeId(1), NodeId(2)] }) (0 to 9)
11+
3: Pipeline(PipelineId(0)) (0 to 9)
1212
4: Int (42 to 43) "1"
1313
5: Int (46 to 47) "2"
1414
6: Int (50 to 51) "3"
15-
7: Pipeline(Pipeline { nodes: [NodeId(4), NodeId(5), NodeId(6)] }) (42 to 51)
15+
7: Pipeline(PipelineId(1)) (42 to 51)
1616
8: Block(BlockId(0)) (0 to 52)
1717
==== SCOPE ====
1818
0: Frame Scope, node_id: NodeId(8) (empty)
@@ -30,4 +30,5 @@ snapshot_kind: text
3030
register_count: 0
3131
file_count: 0
3232
==== IR ERRORS ====
33-
Error (NodeId 3): node Pipeline(Pipeline { nodes: [NodeId(0), NodeId(1), NodeId(2)] }) not suported yet
33+
Error (NodeId 3): node Pipeline(PipelineId(0)) not suported yet
34+

src/typechecker.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,8 @@ impl<'a> Typechecker<'a> {
338338

339339
self.set_node_type_id(node_id, block_type);
340340
}
341-
AstNode::Pipeline(ref pipeline) => {
341+
AstNode::Pipeline(pipeline_id) => {
342+
let pipeline = &self.compiler.pipelines[pipeline_id.0];
342343
let expressions = pipeline.get_expressions();
343344
for inner in expressions {
344345
self.typecheck_node(*inner)

0 commit comments

Comments
 (0)