Skip to content

Commit

Permalink
Implement simplified mixed in variable.
Browse files Browse the repository at this point in the history
  • Loading branch information
YSawc committed Oct 27, 2020
1 parent 516ef95 commit f843da7
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ sudo apt install llvm-runtime
- [x] [not checkes for under score variable](https://github.com/YSawc/lio/commit/0c95ef3d9c57e8578d584aaef5dc42fca986a3c9)
- [x] [checker for return type](https://github.com/YSawc/lio/commit/cb7864e64982aeb98adda36f606e96cb451b0784)
- [ ] multiple error notification
- [ ] tokenize
- [x] [tokenize](https://github.com/YSawc/lio/commit/d09ae5afe26fd3daf1dcddd3dd333224cffe247c)
- [ ] parser
- [ ] whole of program
- [ ] command selector in several mathine architecture
Expand Down
47 changes: 34 additions & 13 deletions src/simplified/simplified.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,46 @@ impl NodeSt {
| NodeKind::LE
| NodeKind::G
| NodeKind::GE => {
let mut lnn = true;
let mut rnn = true;
let ln = self.lhs.as_ref().unwrap().as_ref().to_owned().simplified();
let llf = ln.c.loc.f;
let l = match ln.c.value {
NodeKind::Num(n) => n,
_ => unreachable!(),
_ => {
lnn = false;
0
}
};
let r = match self
.rhs
.as_ref()
.unwrap()
.as_ref()
.to_owned()
.simplified()
.c
.value
{

let rn = self.rhs.as_ref().unwrap().as_ref().to_owned().simplified();
let r = match rn.c.value {
NodeKind::Num(n) => n,
_ => unreachable!(),
_ => {
rnn = false;
0
}
};

if !lnn || !rnn {
self.lhs = match lnn {
true => Some(Box::new(NodeSt::num(
l,
Loc::new(llf, (llf as i8 + (l / 10) + 1) as u8),
))),
false => Some(Box::new(ln)),
};

self.rhs = match rnn {
true => Some(Box::new(NodeSt::num(
r,
Loc::new(llf, (llf as i8 + (r / 10) + 1) as u8),
))),
false => Some(Box::new(rn)),
};
return self;
}

self = match self.c.value {
NodeKind::Add => {
NodeSt::num(l + r, Loc::new(llf, (llf as i8 + ((l + r) / 10) + 1) as u8))
Expand Down Expand Up @@ -91,7 +112,7 @@ impl NodeSt {
_ => unreachable!(),
}
}
NodeKind::Num(_) | NodeKind::UnderScore | NodeKind::NewVar(_) => (),
NodeKind::Num(_) | NodeKind::UnderScore | NodeKind::NewVar(_) | NodeKind::Var(_) => (),
_ => unreachable!(),
};
self
Expand Down
30 changes: 30 additions & 0 deletions src/tests/simplified/simplified/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,33 @@ fn simplified_with_minus_test() {
let l = NodeSt::num(1, Loc::new(12, 13));
assert_eq!(l, n);
}

#[test]
fn simplified_with_variable_test() {
let t = Token::tokenize("fn int { int a = 1; 2*3-a;}").unwrap();
let mut t = t.iter().peekable();
let n = NodeArr::w_parser(&mut t, vec![]).unwrap().0;
let n = n.simplified().ret_node_st;
let l = NodeSt {
c: Annot {
value: NodeKind::Sub,
loc: Loc { f: 26, e: 27 },
},
lhs: Some(Box::new(NodeSt {
c: Annot {
value: NodeKind::Num(6),
loc: Loc { f: 23, e: 24 },
},
..Default::default()
})),
rhs: Some(Box::new(NodeSt {
c: Annot {
value: NodeKind::Var(0),
loc: Loc { f: 24, e: 26 },
},
..Default::default()
})),
..Default::default()
};
assert_eq!(l, n);
}

0 comments on commit f843da7

Please sign in to comment.