Skip to content

Commit

Permalink
Add flag framework for while
Browse files Browse the repository at this point in the history
Signed-off-by: innocentzero <[email protected]>
  • Loading branch information
InnocentZero committed Dec 8, 2024
1 parent 7c3ed88 commit 3130dd6
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 36 deletions.
20 changes: 17 additions & 3 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,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 @@ -1332,11 +1333,24 @@ impl Parser {
let span_start = self.position();
self.keyword(b"while");

if self.is_operator() {
// TODO: flag parsing
self.next();
}

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
5 changes: 4 additions & 1 deletion src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,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
30 changes: 9 additions & 21 deletions src/snapshots/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,12 @@ snapshot_kind: text
9: Int (32 to 33) "1"
10: BinaryOp { lhs: NodeId(7), op: NodeId(8), rhs: NodeId(9) } (26 to 33)
11: Block(BlockId(0)) (22 to 35)
12: While { condition: NodeId(6), block: NodeId(11) } (10 to 35)
13: Block(BlockId(1)) (0 to 36)
==== SCOPE ====
0: Frame Scope, node_id: NodeId(13)
variables: [ x: NodeId(0) ]
1: Frame Scope, node_id: NodeId(11) (empty)
==== TYPES ====
0: int
1: int
2: none
3: int
4: forbidden
5: int
6: bool
7: int
8: forbidden
9: int
10: none
11: none
12: none
13: none
12: While { cond_block: Some((NodeId(6), NodeId(11))), short_flag: None, long_flag: None } (10 to 35)
13: Name (44 to 45) "h"
14: Call { parts: [NodeId(13)] } (45 to 45)
15: Garbage (45 to 46)
16: Block(BlockId(1)) (45 to 46)
17: While { cond_block: Some((NodeId(14), NodeId(16))), short_flag: None, long_flag: None } (37 to 46)
18: Block(BlockId(2)) (0 to 46)
==== COMPILER ERRORS ====
Error (NodeId 15): expected: left bracket '{'
26 changes: 15 additions & 11 deletions src/typechecker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,20 +383,24 @@ impl<'a> Typechecker<'a> {

self.set_node_type_id(node_id, UNIT_TYPE);
}
AstNode::While { condition, block } => {
self.typecheck_node(condition);
AstNode::While { cond_block, .. } => {
if let Some((condition, block)) = cond_block {
self.typecheck_node(condition);

// the condition should always evaluate to a boolean
if self.type_of(condition) != Type::Bool {
self.error("The condition for while loop is not a boolean", condition);
}
// the condition should always evaluate to a boolean
if self.type_of(condition) != Type::Bool {
self.error("The condition for while loop is not a boolean", condition);
}

self.typecheck_node(block);
if self.type_id_of(block) != NONE_TYPE {
self.error("Blocks in looping constructs cannot return values", block);
}
self.typecheck_node(block);
if self.type_id_of(block) != NONE_TYPE {
self.error("Blocks in looping constructs cannot return values", block);
}

self.set_node_type_id(node_id, self.type_id_of(block));
self.set_node_type_id(node_id, self.type_id_of(block));
} else {
self.set_node_type_id(node_id, NONE_TYPE);
}
}
AstNode::Match {
ref target,
Expand Down
2 changes: 2 additions & 0 deletions tests/while.nu
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ mut x = 0
while 1 < 2 {
$x += 1
}

while -h

0 comments on commit 3130dd6

Please sign in to comment.