Skip to content

Commit af83a36

Browse files
committed
fix(warn): 修复循环错误的警告触发条件问题.
1 parent 1ac311b commit af83a36

6 files changed

Lines changed: 14 additions & 8 deletions

File tree

lib/math.exf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ function sqrt(n) {
2424
while (diff > precision) {
2525
last = x;
2626
x = (x + n / x) / 2;
27-
27+
2828
diff = x * x - n;
2929
if (diff < 0) { diff = 0 - diff; }
30-
30+
3131
if (x == last) {
32-
diff = 0;
32+
diff = 0;
3333
}
3434
}
3535

src/compiler/ast/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ pub enum ASTStmtTree {
8484
token: Token,
8585
cond: ASTExprTree,
8686
body: Vec<ASTStmtTree>,
87+
is_easy: bool,
8788
},
8889
Function {
8990
// function identifier() {}

src/compiler/parser/while.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ pub fn while_eval(parser: &mut Parser) -> Result<ASTStmtTree, ParserError> {
1010
let mut token = parser.next_parser_token()?;
1111
let head = token.clone();
1212
let cond: ASTExprTree;
13-
13+
let is_easy;
1414
match token.t_type {
1515
LP => {
1616
if token.text() == "{" {
1717
let tk_b = token.clone();
1818
parser.cache = Some(token);
19+
is_easy = true;
1920
cond = Literal(Token::new(
2021
SmolStr::new("true"),
2122
tk_b.line,
@@ -24,6 +25,7 @@ pub fn while_eval(parser: &mut Parser) -> Result<ASTStmtTree, ParserError> {
2425
TokenType::True,
2526
));
2627
} else {
28+
is_easy = false;
2729
parser.cache = Some(token);
2830
cond = parser.parser_cond()?;
2931
}
@@ -40,5 +42,5 @@ pub fn while_eval(parser: &mut Parser) -> Result<ASTStmtTree, ParserError> {
4042

4143
let body = blk_eval(parser)?;
4244

43-
Ok(ASTStmtTree::Loop { token:head,cond, body })
45+
Ok(ASTStmtTree::Loop { token:head,cond, body, is_easy })
4446
}

src/compiler/semantic/block.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ pub fn block_semantic(
5151
token: _token,
5252
cond,
5353
body,
54+
is_easy,
5455
} => {
55-
let ret_m = while_semantic(semantic, &cond, body, code, locals)?;
56+
let ret_m = while_semantic(semantic, &cond, body, code, locals, is_easy)?;
5657
opcodes.append_code(&ret_m);
5758
}
5859
ASTStmtTree::Break(token) =>{

src/compiler/semantic/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ impl<'a> Semantic<'a> {
7474
token: _token,
7575
cond,
7676
body,
77+
is_easy,
7778
} => {
78-
let ret_m = while_semantic(self, &cond, body, code, &mut global)?;
79+
let ret_m = while_semantic(self, &cond, body, code, &mut global, is_easy)?;
7980
code.get_code_table().append_code(&ret_m);
8081
}
8182
ASTStmtTree::Function { name, args, body } => {

src/compiler/semantic/while.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub fn while_semantic(
1414
body: Vec<ASTStmtTree>,
1515
code: &mut Code,
1616
locals: &mut LocalMap,
17+
is_easy: bool,
1718
) -> Result<OpCodeTable, ParserError> {
1819
semantic
1920
.compiler_data()
@@ -29,7 +30,7 @@ pub fn while_semantic(
2930
));
3031
}
3132

32-
if matches!(exp.0, Operand::ImmBool(_)) && !semantic.file.has_warnings(LoopNoExpr) {
33+
if matches!(exp.0, Operand::ImmBool(_)) && !semantic.file.has_warnings(LoopNoExpr) && !is_easy {
3334
Compiler::warning_info_expr(
3435
semantic.file,
3536
"'while(true)' can be written as 'while'.",

0 commit comments

Comments
 (0)