Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
55 changes: 49 additions & 6 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ pub enum AstNode {
// Empty values
Null,

// Unit type
Unit,

// Operators
Equal,
NotEqual,
Expand Down Expand Up @@ -102,8 +105,9 @@ pub enum AstNode {
is_mutable: bool,
},
While {
condition: NodeId,
block: NodeId,
cond_block: Option<(NodeId, NodeId)>,
short_flag: Option<NodeId>,
long_flag: Option<NodeId>,
},
For {
variable: NodeId,
Expand Down Expand Up @@ -133,6 +137,10 @@ pub enum AstNode {
params: Option<NodeId>,
block: NodeId,
},
Alias {
new_name: NodeId,
old_name: NodeId,
},

/// Long flag ('--' + one or more letters)
FlagLong,
Expand Down Expand Up @@ -376,9 +384,14 @@ impl Parser {
self.record_or_closure()
} else if self.is_lparen() {
self.lparen();
let output = self.expression();
self.rparen();
output
if self.is_rparen() {
self.rparen();
self.create_node(AstNode::Unit, span_start, span_start + 2)
} else {
let output = self.expression();
self.rparen();
output
}
} else if self.is_lsquare() {
self.list_or_table()
} else if self.is_keyword(b"true") || self.is_keyword(b"false") {
Expand Down Expand Up @@ -917,6 +930,10 @@ impl Parser {

let pattern_result = self.simple_expression(NAME_STRICT);

if self.is_comma() {
self.next();
}

match_arms.push((pattern, pattern_result));
} else if self.is_newline() {
self.next();
Expand Down Expand Up @@ -1280,6 +1297,8 @@ impl Parser {
code_body.push(self.continue_statement());
} else if self.is_keyword(b"break") {
code_body.push(self.break_statement());
} else if self.is_keyword(b"alias") {
code_body.push(self.alias_statement());
} else {
let exp_span_start = self.position();
let expression = self.expression_or_assignment();
Expand Down Expand Up @@ -1314,11 +1333,24 @@ impl Parser {
let span_start = self.position();
self.keyword(b"while");

if self.is_operator() {
// TODO: flag parsing
self.next();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe throw an error here with a WIP note instead of silently ignoring flags.

}

let condition = self.expression();
let block = self.block(BlockContext::Curlies);
let span_end = self.get_span_end(block);

self.create_node(AstNode::While { condition, block }, span_start, span_end)
self.create_node(
AstNode::While {
cond_block: Some((condition, block)),
short_flag: None,
long_flag: None,
},
span_start,
span_end,
)
}

pub fn for_statement(&mut self) -> NodeId {
Expand Down Expand Up @@ -1391,6 +1423,17 @@ impl Parser {
self.create_node(AstNode::Break, span_start, span_end)
}

pub fn alias_statement(&mut self) -> NodeId {
let _span = span!();
let span_start = self.position();
self.keyword(b"alias");
let new_name = self.name();
self.equals();
let old_name = self.name();
let span_end = self.get_span_end(old_name);
self.create_node(AstNode::Alias { new_name, old_name }, span_start, span_end)
}

pub fn is_operator(&mut self) -> bool {
match self.peek() {
Some(Token {
Expand Down
11 changes: 10 additions & 1 deletion src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,12 @@ impl<'a> Resolver<'a> {

self.resolve_block(block, block_id, Some(def_scope));
}
AstNode::Alias {
new_name,
old_name: _,
} => {
self.define_decl(new_name);
}
AstNode::Params(ref params) => {
for param in params {
if let AstNode::Param { name, .. } = self.compiler.ast_nodes[param.0] {
Expand All @@ -256,7 +262,10 @@ impl<'a> Resolver<'a> {
self.resolve_node(initializer);
self.define_variable(variable_name, is_mutable)
}
AstNode::While { condition, block } => {
AstNode::While {
cond_block: Some((condition, block)),
..
} => {
self.resolve_node(condition);
self.resolve_node(block);
}
Expand Down
41 changes: 41 additions & 0 deletions src/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
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 (26 to 29) "bar"
8: Name (32 to 35) "foo"
9: Alias { new_name: NodeId(7), old_name: NodeId(8) } (20 to 35)
10: Name (37 to 40) "bar"
11: Int (41 to 42) "1"
12: Call { parts: [NodeId(10), NodeId(11)] } (41 to 42)
13: Block(BlockId(1)) (0 to 43)
==== SCOPE ====
0: Frame Scope, node_id: NodeId(13)
decls: [ bar: NodeId(7), foo: NodeId(0) ]
1: Frame Scope, node_id: NodeId(5)
variables: [ a: NodeId(1) ]
==== TYPES ====
0: unknown
1: unknown
2: any
3: none
4: unknown
5: unknown
6: none
7: unknown
8: unknown
9: none
10: unknown
11: int
12: any
13: any
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>>
23 changes: 12 additions & 11 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,22 +60,22 @@ 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: ()
27: ()
20: int
21: forbidden
22: string
23: string
24: int
25: list<any>
26: list<any>
27: none
28: unknown
29: string
30: 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: none
20: unknown
21: closure
22: stream<binary>
23: stream<binary>
23 changes: 12 additions & 11 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: ()
23: ()
24: ()
19: int
20: list<list<int>>
21: list<any>
22: list<any>
23: none
24: none
Loading