Skip to content

Commit

Permalink
feat(service): type checking on if condition
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Jan 2, 2025
1 parent 6cea895 commit 477509c
Show file tree
Hide file tree
Showing 12 changed files with 302 additions and 222 deletions.
41 changes: 31 additions & 10 deletions crates/service/src/checker/typeck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
data_set,
files::FilesCtx,
helpers,
types_analyzer::{OperandType, TypesAnalyzerCtx},
types_analyzer::{OperandType, TypesAnalyzerCtx, ValType},
InternUri, LanguageService,
};
use itertools::{EitherOrBoth, Itertools};
Expand Down Expand Up @@ -79,15 +79,23 @@ pub fn check_global(
}

pub fn unfold(node: SyntaxNode, sequence: &mut Vec<Instr>) {
node.children()
.filter_map(|child| {
if child.kind() == SyntaxKind::OPERAND {
child.first_child().and_then(Instr::cast)
} else {
None
}
})
.for_each(|child| unfold(child.syntax().clone(), sequence));
match node.kind() {
SyntaxKind::PLAIN_INSTR => node
.children()
.filter_map(|child| {
if child.kind() == SyntaxKind::OPERAND {
child.first_child().and_then(Instr::cast)
} else {
None
}
})
.for_each(|child| unfold(child.syntax().clone(), sequence)),
SyntaxKind::BLOCK_IF => node
.children()
.filter_map(Instr::cast)
.for_each(|child| unfold(child.syntax().clone(), sequence)),
_ => {}
}
if let Some(node) = Instr::cast(node) {
sequence.push(node);
}
Expand Down Expand Up @@ -187,6 +195,19 @@ fn check_block_like(
check_block_like(diags, shared, node, init_stack, &results);
}
BlockInstr::If(block_if) => {
if let Some(mut diag) =
type_stack.check(&[(OperandType::Val(ValType::I32), None)], &instr)
{
diag.range = helpers::rowan_range_to_lsp_range(
shared.line_index,
block_if
.keyword()
.map(|token| token.text_range())
.unwrap_or_else(|| node.text_range()),
);
diag.message.push_str(" for the condition of `if` block");
diags.push(diag);
}
if let Some(then_block) = block_if.then_block() {
check_block_like(
diags,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ expression: response
{
"range": {
"start": {
"line": 7,
"line": 8,
"character": 8
},
"end": {
"line": 9,
"line": 10,
"character": 24
}
},
Expand All @@ -26,11 +26,11 @@ expression: response
"uri": "untitled:test",
"range": {
"start": {
"line": 9,
"line": 10,
"character": 10
},
"end": {
"line": 9,
"line": 10,
"character": 23
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,6 @@ expression: response
"source": "wat",
"message": "expected types [i32, i32], found [i32] at the end"
},
{
"range": {
"start": {
"line": 9,
"character": 7
},
"end": {
"line": 9,
"character": 8
}
},
"severity": 1,
"code": "type-check",
"source": "wat",
"message": "expected types [i32, i32], found [i32, i32, i32] at the end"
},
{
"range": {
"start": {
Expand Down Expand Up @@ -71,22 +55,6 @@ expression: response
}
]
},
{
"range": {
"start": {
"line": 18,
"character": 7
},
"end": {
"line": 18,
"character": 8
}
},
"severity": 1,
"code": "type-check",
"source": "wat",
"message": "expected types [i32, i32], found [i32, i32, i32] at the end"
},
{
"range": {
"start": {
Expand All @@ -102,22 +70,6 @@ expression: response
"code": "type-check",
"source": "wat",
"message": "expected types [i32, i32], found [i32, i32, i32] at the end"
},
{
"range": {
"start": {
"line": 28,
"character": 7
},
"end": {
"line": 28,
"character": 8
}
},
"severity": 1,
"code": "type-check",
"source": "wat",
"message": "expected types [i32, i32], found [i32, i32, i32] at the end"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ expression: response
{
"range": {
"start": {
"line": 8,
"line": 9,
"character": 6
},
"end": {
"line": 8,
"line": 9,
"character": 13
}
},
Expand All @@ -26,11 +26,11 @@ expression: response
"uri": "untitled:test",
"range": {
"start": {
"line": 7,
"line": 8,
"character": 6
},
"end": {
"line": 7,
"line": 8,
"character": 17
}
}
Expand Down
Loading

0 comments on commit 477509c

Please sign in to comment.