Skip to content

Commit

Permalink
bugfix: if_stmt orelse postion (#612)
Browse files Browse the repository at this point in the history
  • Loading branch information
He1pa authored Jul 20, 2023
1 parent 40f2c2c commit b20aa9f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
16 changes: 14 additions & 2 deletions kclvm/parser/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,8 +563,20 @@ impl<'a> Parser<'a> {

// fix elif-list and else
while let Some(mut x) = elif_list.pop() {
x.node.orelse = if_stmt.orelse;
let pos = x.clone().pos();
x.node.orelse = if_stmt.orelse.clone();
let pos = if if_stmt.orelse.is_empty() {
x.clone().pos()
} else {
let start_pos = x.clone().pos();
let end_pos = if_stmt.orelse.last().unwrap().pos();
(
start_pos.0.clone(),
start_pos.1,
start_pos.2,
end_pos.3,
end_pos.4,
)
};
let t = Box::new(Node::node_with_pos(Stmt::If(x.node), pos));
if_stmt.orelse = vec![t];
}
Expand Down
1 change: 1 addition & 0 deletions kclvm/parser/testdata/if-02.k
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ elif a + 100:
ddd = 4
else:
eee = 5
fff = 6
2 changes: 1 addition & 1 deletion kclvm/parser/testdata/if-02.k.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"filename":"if-02.k","pkg":"__main__","doc":"","name":"__main__","body":[{"node":{"Assign":{"targets":[{"node":{"names":[{"node":"a","filename":"if-02.k","line":1,"column":0,"end_line":1,"end_column":1}],"pkgpath":"","ctx":"Store"},"filename":"if-02.k","line":1,"column":0,"end_line":1,"end_column":1}],"value":{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":1}}},"filename":"if-02.k","line":1,"column":4,"end_line":1,"end_column":5},"type_annotation":null,"ty":null}},"filename":"if-02.k","line":1,"column":0,"end_line":1,"end_column":5},{"node":{"If":{"body":[{"node":{"Assign":{"targets":[{"node":{"names":[{"node":"bbb","filename":"if-02.k","line":4,"column":4,"end_line":4,"end_column":7}],"pkgpath":"","ctx":"Store"},"filename":"if-02.k","line":4,"column":4,"end_line":4,"end_column":7}],"value":{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":2}}},"filename":"if-02.k","line":4,"column":10,"end_line":4,"end_column":11},"type_annotation":null,"ty":null}},"filename":"if-02.k","line":4,"column":4,"end_line":4,"end_column":11}],"cond":{"node":{"Identifier":{"names":[{"node":"a","filename":"if-02.k","line":3,"column":3,"end_line":3,"end_column":4}],"pkgpath":"","ctx":"Load"}},"filename":"if-02.k","line":3,"column":3,"end_line":3,"end_column":4},"orelse":[{"node":{"If":{"body":[{"node":{"Assign":{"targets":[{"node":{"names":[{"node":"ccc","filename":"if-02.k","line":6,"column":4,"end_line":6,"end_column":7}],"pkgpath":"","ctx":"Store"},"filename":"if-02.k","line":6,"column":4,"end_line":6,"end_column":7}],"value":{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":3}}},"filename":"if-02.k","line":6,"column":10,"end_line":6,"end_column":11},"type_annotation":null,"ty":null}},"filename":"if-02.k","line":6,"column":4,"end_line":6,"end_column":11}],"cond":{"node":{"Binary":{"left":{"node":{"Identifier":{"names":[{"node":"a","filename":"if-02.k","line":5,"column":5,"end_line":5,"end_column":6}],"pkgpath":"","ctx":"Load"}},"filename":"if-02.k","line":5,"column":5,"end_line":5,"end_column":6},"op":{"Bin":"Add"},"right":{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":10}}},"filename":"if-02.k","line":5,"column":9,"end_line":5,"end_column":11}}},"filename":"if-02.k","line":5,"column":5,"end_line":5,"end_column":11},"orelse":[{"node":{"If":{"body":[{"node":{"Assign":{"targets":[{"node":{"names":[{"node":"ddd","filename":"if-02.k","line":8,"column":4,"end_line":8,"end_column":7}],"pkgpath":"","ctx":"Store"},"filename":"if-02.k","line":8,"column":4,"end_line":8,"end_column":7}],"value":{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":4}}},"filename":"if-02.k","line":8,"column":10,"end_line":8,"end_column":11},"type_annotation":null,"ty":null}},"filename":"if-02.k","line":8,"column":4,"end_line":8,"end_column":11}],"cond":{"node":{"Binary":{"left":{"node":{"Identifier":{"names":[{"node":"a","filename":"if-02.k","line":7,"column":5,"end_line":7,"end_column":6}],"pkgpath":"","ctx":"Load"}},"filename":"if-02.k","line":7,"column":5,"end_line":7,"end_column":6},"op":{"Bin":"Add"},"right":{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":100}}},"filename":"if-02.k","line":7,"column":9,"end_line":7,"end_column":12}}},"filename":"if-02.k","line":7,"column":5,"end_line":7,"end_column":12},"orelse":[{"node":{"Assign":{"targets":[{"node":{"names":[{"node":"eee","filename":"if-02.k","line":10,"column":4,"end_line":10,"end_column":7}],"pkgpath":"","ctx":"Store"},"filename":"if-02.k","line":10,"column":4,"end_line":10,"end_column":7}],"value":{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":5}}},"filename":"if-02.k","line":10,"column":10,"end_line":10,"end_column":11},"type_annotation":null,"ty":null}},"filename":"if-02.k","line":10,"column":4,"end_line":10,"end_column":11}]}},"filename":"if-02.k","line":7,"column":0,"end_line":9,"end_column":0}]}},"filename":"if-02.k","line":5,"column":0,"end_line":7,"end_column":0}]}},"filename":"if-02.k","line":3,"column":0,"end_line":10,"end_column":12}],"comments":[]}
{"filename":"if-02.k","pkg":"__main__","doc":"","name":"__main__","body":[{"node":{"Assign":{"targets":[{"node":{"names":[{"node":"a","filename":"if-02.k","line":1,"column":0,"end_line":1,"end_column":1}],"pkgpath":"","ctx":"Store"},"filename":"if-02.k","line":1,"column":0,"end_line":1,"end_column":1}],"value":{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":1}}},"filename":"if-02.k","line":1,"column":4,"end_line":1,"end_column":5},"type_annotation":null,"ty":null}},"filename":"if-02.k","line":1,"column":0,"end_line":1,"end_column":5},{"node":{"If":{"body":[{"node":{"Assign":{"targets":[{"node":{"names":[{"node":"bbb","filename":"if-02.k","line":4,"column":4,"end_line":4,"end_column":7}],"pkgpath":"","ctx":"Store"},"filename":"if-02.k","line":4,"column":4,"end_line":4,"end_column":7}],"value":{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":2}}},"filename":"if-02.k","line":4,"column":10,"end_line":4,"end_column":11},"type_annotation":null,"ty":null}},"filename":"if-02.k","line":4,"column":4,"end_line":4,"end_column":11}],"cond":{"node":{"Identifier":{"names":[{"node":"a","filename":"if-02.k","line":3,"column":3,"end_line":3,"end_column":4}],"pkgpath":"","ctx":"Load"}},"filename":"if-02.k","line":3,"column":3,"end_line":3,"end_column":4},"orelse":[{"node":{"If":{"body":[{"node":{"Assign":{"targets":[{"node":{"names":[{"node":"ccc","filename":"if-02.k","line":6,"column":4,"end_line":6,"end_column":7}],"pkgpath":"","ctx":"Store"},"filename":"if-02.k","line":6,"column":4,"end_line":6,"end_column":7}],"value":{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":3}}},"filename":"if-02.k","line":6,"column":10,"end_line":6,"end_column":11},"type_annotation":null,"ty":null}},"filename":"if-02.k","line":6,"column":4,"end_line":6,"end_column":11}],"cond":{"node":{"Binary":{"left":{"node":{"Identifier":{"names":[{"node":"a","filename":"if-02.k","line":5,"column":5,"end_line":5,"end_column":6}],"pkgpath":"","ctx":"Load"}},"filename":"if-02.k","line":5,"column":5,"end_line":5,"end_column":6},"op":{"Bin":"Add"},"right":{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":10}}},"filename":"if-02.k","line":5,"column":9,"end_line":5,"end_column":11}}},"filename":"if-02.k","line":5,"column":5,"end_line":5,"end_column":11},"orelse":[{"node":{"If":{"body":[{"node":{"Assign":{"targets":[{"node":{"names":[{"node":"ddd","filename":"if-02.k","line":8,"column":4,"end_line":8,"end_column":7}],"pkgpath":"","ctx":"Store"},"filename":"if-02.k","line":8,"column":4,"end_line":8,"end_column":7}],"value":{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":4}}},"filename":"if-02.k","line":8,"column":10,"end_line":8,"end_column":11},"type_annotation":null,"ty":null}},"filename":"if-02.k","line":8,"column":4,"end_line":8,"end_column":11}],"cond":{"node":{"Binary":{"left":{"node":{"Identifier":{"names":[{"node":"a","filename":"if-02.k","line":7,"column":5,"end_line":7,"end_column":6}],"pkgpath":"","ctx":"Load"}},"filename":"if-02.k","line":7,"column":5,"end_line":7,"end_column":6},"op":{"Bin":"Add"},"right":{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":100}}},"filename":"if-02.k","line":7,"column":9,"end_line":7,"end_column":12}}},"filename":"if-02.k","line":7,"column":5,"end_line":7,"end_column":12},"orelse":[{"node":{"Assign":{"targets":[{"node":{"names":[{"node":"eee","filename":"if-02.k","line":10,"column":4,"end_line":10,"end_column":7}],"pkgpath":"","ctx":"Store"},"filename":"if-02.k","line":10,"column":4,"end_line":10,"end_column":7}],"value":{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":5}}},"filename":"if-02.k","line":10,"column":10,"end_line":10,"end_column":11},"type_annotation":null,"ty":null}},"filename":"if-02.k","line":10,"column":4,"end_line":10,"end_column":11},{"node":{"Assign":{"targets":[{"node":{"names":[{"node":"fff","filename":"if-02.k","line":11,"column":4,"end_line":11,"end_column":7}],"pkgpath":"","ctx":"Store"},"filename":"if-02.k","line":11,"column":4,"end_line":11,"end_column":7}],"value":{"node":{"NumberLit":{"binary_suffix":null,"value":{"Int":6}}},"filename":"if-02.k","line":11,"column":10,"end_line":11,"end_column":11},"type_annotation":null,"ty":null}},"filename":"if-02.k","line":11,"column":4,"end_line":11,"end_column":11}]}},"filename":"if-02.k","line":7,"column":0,"end_line":11,"end_column":11}]}},"filename":"if-02.k","line":5,"column":0,"end_line":11,"end_column":11}]}},"filename":"if-02.k","line":3,"column":0,"end_line":11,"end_column":12}],"comments":[]}
17 changes: 8 additions & 9 deletions kclvm/tools/src/LSP/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,15 +511,14 @@ fn goto_local_var_def_test() {
let res = goto_definition(&program, &pos, &prog_scope);
compare_goto_res(res, (&file, 43, 4, 43, 9));

// todo: fix if stmt position error
// let pos = KCLPos {
// filename: file.clone(),
// line: 51,
// column: Some(11),
// };

// let res = goto_definition(&program, &pos, &prog_scope);
// compare_goto_res(res, (&file, 43, 4, 43, 9));
let pos = KCLPos {
filename: file.clone(),
line: 51,
column: Some(11),
};

let res = goto_definition(&program, &pos, &prog_scope);
compare_goto_res(res, (&file, 43, 4, 43, 9));
}

#[test]
Expand Down

0 comments on commit b20aa9f

Please sign in to comment.