Skip to content

Commit

Permalink
Impelement simplified variable under initialize.
Browse files Browse the repository at this point in the history
  • Loading branch information
YSawc committed Oct 10, 2020
1 parent 6ccacc6 commit 3c4b0c6
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/node_arr/node_arr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::super::node::node::*;
use super::super::parser::error::*;
use super::super::token::token::*;
use super::super::var::var::*;
use super::super::simplified::*;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct NodeArr {
Expand Down Expand Up @@ -51,7 +52,8 @@ impl NodeArr {
NodeKind::Ident(s) => s,
_ => unreachable!(),
};
let n = n.rhs.as_ref().unwrap().as_ref().to_owned();
let mut n = vex(&mut n.to_owned().rhs.unwrap().to_owned(), l.to_owned());
n = simplified::exec(n);
let v = Var::new(s, n);
l.push(v);
continue;
Expand Down
2 changes: 1 addition & 1 deletion src/simplified/simplified.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn prepare(ns: NodeSt) -> NodeSt {
exec(ns)
}

fn exec(ns: NodeSt) -> NodeSt {
pub fn exec(ns: NodeSt) -> NodeSt {
match ns.c.value {
NodeKind::Add
| NodeKind::Sub
Expand Down
3 changes: 2 additions & 1 deletion src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod parser;
pub mod token;
pub mod simplified;
pub mod token;
pub mod var;
1 change: 1 addition & 0 deletions src/tests/var/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod tests;
44 changes: 44 additions & 0 deletions src/tests/var/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#[cfg(test)]
use super::super::super::location::location::*;
#[cfg(test)]
use super::super::super::node::node::*;
#[cfg(test)]
use super::super::super::node_arr::node_arr::*;
// #[cfg(test)]
// use super::super::super::parser::error::*;
#[cfg(test)]
use super::super::super::token::token::*;
#[cfg(test)]
use super::super::super::var::var::*;

#[test]
fn simplified_variable_under_initialize_test() {
let t = Token::tokenize("int a = 2; int b = 8*a; int c = 2*b+a; 0;").unwrap();
let l = NodeArr::w_parser(t).unwrap().l;
let mut e: Vec<Var> = Vec::new();
e.push(Var {
s: "a".to_string(),
n: NodeSt {
c: Node::number(2, Loc::new(10, 11)),
lhs: None,
rhs: None,
},
});
e.push(Var {
s: "b".to_string(),
n: NodeSt {
c: Node::number(16, Loc::new(21, 23)),
lhs: None,
rhs: None,
},
});
e.push(Var {
s: "c".to_string(),
n: NodeSt {
c: Node::number(34, Loc::new(34, 38)),
lhs: None,
rhs: None,
},
});
assert_eq!(e, l);
}
3 changes: 3 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ assert 4 'return 4;'
assert 1 'int a = 3; 1;'
assert 4 'int a = 3; int b = 4; b;'
assert 36 'int a = 3; int b = 4; b*a*3;'
assert 60 'int a = 3; int b = 4; b*a*5;' simplified
assert 54 'int a = 3; int b = a*2; b*a*3;'
assert 54 'int a = 3; int b = a*2; b*a*3;' simplified

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

0 comments on commit 3c4b0c6

Please sign in to comment.