Skip to content

Commit

Permalink
Implement underscore variable unchecked when unused.
Browse files Browse the repository at this point in the history
  • Loading branch information
YSawc committed Oct 17, 2020
1 parent b5980b4 commit 0c95ef3
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/node_arr/node_arr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,13 @@ impl NodeArr {
&mut uv,
);
n = simplified::exec(n);
let v = Var::new(_s, n);
let v = match _s.as_bytes()[0] {
b'_' => Var::mnew(_s, n),
_ => Var::new(_s, n),
};
if v.m {
uv.push(v.to_owned().s);
}
l.push(v);
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/program/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,13 @@ impl Program {
let mut n =
beta(&mut n.to_owned().rhs.unwrap().to_owned(), ev, &mut uv);
n = simplified::exec(n);
let v = Var::new(_s, n);
let v = match _s.as_bytes()[0] {
b'_' => Var::mnew(_s, n),
_ => Var::new(_s, n),
};
if v.m {
uv.push(v.to_owned().s);
}
g.push(v);
}
}
Expand Down
22 changes: 22 additions & 0 deletions src/tests/parser/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use super::super::super::node_arr::node_arr::*;
#[cfg(test)]
use super::super::super::parser::error::*;
#[cfg(test)]
use super::super::super::program::program::*;
#[cfg(test)]
use super::super::super::token::token::*;

#[test]
Expand Down Expand Up @@ -268,3 +270,23 @@ fn eof_around_closed_else_stmt_test() {
};
assert!(n)
}

// #[test]
// fn unused_variable_check_for_under_score_grobal_variable() {
// let t = Token::tokenize("int _g = 10; fn { _ }").unwrap();
// let n = match Program::w_parser(t) {
// Ok(_) => true,
// Err(_) => false,
// };
// assert!(n)
// }

#[test]
fn unused_variable_check_for_under_score_local_variable() {
let t = Token::tokenize("fn { int _g = 10; _ }").unwrap();
let n = match Program::w_parser(t) {
Ok(_) => true,
Err(_) => false,
};
assert!(n)
}
3 changes: 3 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ assert 0 'int g = 2; fn { int v = 4; 1+v*g/4-1; _ }' simplified
assert 10 'int g = 2; int l = 3; int o = 4; fn int { int v = 4; l+v*o/g-1; }' simplified
assert 0 'int g = 2; int l = 3; int o = 4; fn { int v = 4; l+v*o/g-1; _ }'
assert 3 'int g = 10; fn int { g; int g = 3; g }'
assert 0 'int _g = 10; fn { _ }'
assert 0 'fn { int _u = 8; _ }'
assert 16 'fn int { int _u = 8; int a = 2; a*_u }'

echo "------------------------------"
echo "All test passed!"

0 comments on commit 0c95ef3

Please sign in to comment.