Skip to content

Commit

Permalink
Implement variable expansion.
Browse files Browse the repository at this point in the history
  • Loading branch information
YSawc committed Oct 10, 2020
1 parent 3df3c89 commit 6ccacc6
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/node_arr/node_arr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl NodeArr {
NodeKind::Ident(s) => s,
_ => unreachable!(),
};
let n = n.lhs.as_ref().unwrap().as_ref().to_owned();
let n = n.rhs.as_ref().unwrap().as_ref().to_owned();
let v = Var::new(s, n);
l.push(v);
continue;
Expand All @@ -64,7 +64,11 @@ impl NodeArr {
))
}
},
_ => n,
_ => {
let n = vex(&mut n.to_owned(), l.to_owned());
// println!("_n : {:?}", _n);
n
}
},
Err(e) => return Err(e),
});
Expand Down
11 changes: 10 additions & 1 deletion src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,15 @@ impl NodeSt {
return Err(ParseError::Eof);
}

Ok(Self::new_num(it.next().unwrap().to_owned())?)
match it.peek().unwrap() {
Token {
value: TokenKind::Ident(s),
..
} => Ok(Self::new_ident(
s.to_string(),
it.next().unwrap().to_owned(),
)),
_ => Ok(Self::new_num(it.next().unwrap().to_owned())?),
}
}
}
21 changes: 21 additions & 0 deletions src/tests/parser/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,27 @@ fn parser_assign_test() {
assert_eq!(e, l)
}

#[test]
fn variable_expansion_test() {
let t = Token::tokenize("int a = 3; int b = 5; b*a;").unwrap();
let l = NodeArr::w_parser(t).unwrap().ret_node_st;
let e = NodeSt {
c: Node::mul(Loc::new(23, 24)),
lhs: Some(Box::new(NodeSt {
c: Node::number(5, Loc::new(21, 22)),
lhs: None,
rhs: None,
})),
rhs: Some(Box::new(NodeSt {
c: Node::number(3, Loc::new(10, 11)),
lhs: None,
rhs: None,
})),
};

assert_eq!(e, l)
}

#[test]
fn return_with_unclosed_test() {
let t = Token::tokenize("return 3").unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/token/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl Token {
s.push(input.chars().nth(i).unwrap());
i += 1;
}
println!("s: {}", s);
// println!("s: {}", s);
p_data.push(Self::ident(s, Loc::new(t as u8 + b, (i as u8 + 1) + b)));
continue;
}
Expand Down
46 changes: 46 additions & 0 deletions src/var/var.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::super::node::node::*;
use super::super::node_arr::node_arr::*;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Var {
Expand All @@ -18,3 +19,48 @@ impl Var {
}
}
}

pub fn vex(ns: &mut NodeSt, vv: Vec<Var>) -> NodeSt {
if ns.lhs != None {
let l = Some(Box::new(vex(
&mut ns.lhs.as_ref().unwrap().to_owned().as_ref().to_owned(),
vv.to_owned(),
)));
if ns.lhs != l {
ns.lhs = l;
};
}

if ns.rhs != None {
let r = Some(Box::new(vex(
&mut ns.rhs.as_ref().unwrap().to_owned().as_ref().to_owned(),
vv.to_owned(),
)));
if ns.rhs != r {
ns.rhs = r;
};
}

match ns.c.value.to_owned() {
NodeKind::Ident(s) => {
let n = NodeArr::find_l(s, vv.to_owned()).unwrap();
// println!("n.n.c : {:?}", n.n.c);
ns.c = n.n.c;
// println!("ns.c: {:?}", ns.c);
if n.n.lhs != None {
ns.lhs = Some(Box::new(vex(
&mut n.n.lhs.as_ref().unwrap().to_owned().as_ref().to_owned(),
vv.to_owned(),
)));
}
if n.n.rhs != None {
ns.rhs = Some(Box::new(vex(
&mut n.n.rhs.as_ref().unwrap().to_owned().as_ref().to_owned(),
vv.to_owned(),
)));
}
return ns.to_owned();
}
_ => return ns.to_owned(),
};
}
3 changes: 2 additions & 1 deletion test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ assert 4 '4'
assert 3 '2; 3'
assert 4 'return 4;'
assert 1 'int a = 3; 1;'
assert 2 'int a = 3; return 2;'
assert 4 'int a = 3; int b = 4; b;'
assert 36 'int a = 3; int b = 4; b*a*3;'

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

0 comments on commit 6ccacc6

Please sign in to comment.