Skip to content

Commit

Permalink
Implement beta simplified.
Browse files Browse the repository at this point in the history
  • Loading branch information
YSawc committed Oct 17, 2020
1 parent d53edee commit dd6dd3d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 10 deletions.
13 changes: 7 additions & 6 deletions src/node_arr/node_arr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use super::super::node::node::*;
use super::super::parser::error::*;
use super::super::program::program::*;
use super::super::simplified::*;
use super::super::simplified::beta::*;
use super::super::token::token::*;
use super::super::var::var::*;

Expand Down Expand Up @@ -119,7 +120,7 @@ impl NodeArr {
let mut ev = ev.to_owned();
ev.push(l.to_owned());

r = vex(&mut n.to_owned().lhs.unwrap().to_owned(), ev, &mut uv);
r = beta(&mut n.to_owned().lhs.unwrap().to_owned(), ev, &mut uv);

r.to_owned()
}
Expand All @@ -143,7 +144,7 @@ impl NodeArr {
false => return Err(ParseError::UnusedVariable(f.n.c.loc)),
}
uv.retain(|s| s != &f.to_owned().s.to_owned());
let mut n = vex(
let mut n = beta(
&mut n.to_owned().rhs.unwrap().to_owned(),
ev.to_owned(),
&mut uv,
Expand All @@ -155,7 +156,7 @@ impl NodeArr {
l.push(ff);
}
_ => {
let mut n = vex(
let mut n = beta(
&mut n.to_owned().rhs.unwrap().to_owned(),
ev.to_owned(),
&mut uv,
Expand Down Expand Up @@ -188,7 +189,7 @@ impl NodeArr {
true => (),
false => return Err(ParseError::UnusedVariable(f.n.c.loc)),
}
let mut n = vex(
let mut n = beta(
&mut n.to_owned().rhs.unwrap().to_owned(),
ev.to_owned(),
&mut uv,
Expand Down Expand Up @@ -252,7 +253,7 @@ impl NodeArr {
if it.peek().unwrap().to_owned().to_owned().value == TokenKind::RBrace {
b = true;
}
let mut c = vex(
let mut c = beta(
&mut n.to_owned().cond.unwrap().to_owned(),
ev.to_owned(),
&mut uv,
Expand Down Expand Up @@ -286,7 +287,7 @@ impl NodeArr {

let mut ev = ev.to_owned();
ev.push(l.to_owned());
let n = vex(&mut n.to_owned(), ev, &mut uv);
let n = beta(&mut n.to_owned(), ev, &mut uv);
if b {
r = n.to_owned();
}
Expand Down
5 changes: 3 additions & 2 deletions src/program/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use super::super::node::node::*;
use super::super::node_arr::node_arr::*;
use super::super::parser::error::*;
// use super::super::parser::parser::*;
use super::super::simplified::beta::*;
use super::super::simplified::*;
use super::super::token::token::*;
use super::super::var::var::*;
Expand Down Expand Up @@ -78,7 +79,7 @@ impl Program {
}
uv.retain(|s| s != &f.to_owned().s.to_owned());
let mut n =
vex(&mut n.to_owned().rhs.unwrap().to_owned(), ev, &mut uv);
beta(&mut n.to_owned().rhs.unwrap().to_owned(), ev, &mut uv);
n = simplified::exec(n);
f.n = n;
let ff = f.to_owned();
Expand All @@ -87,7 +88,7 @@ impl Program {
}
_ => {
let mut n =
vex(&mut n.to_owned().rhs.unwrap().to_owned(), ev, &mut uv);
beta(&mut n.to_owned().rhs.unwrap().to_owned(), ev, &mut uv);
n = simplified::exec(n);
let v = Var::new(_s, n);
g.push(v);
Expand Down
15 changes: 15 additions & 0 deletions src/simplified/beta.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use super::super::node::node::*;
use super::super::var::var::*;
use rustc_hash::FxHashMap;

pub fn beta(ns: &mut NodeSt, ev: Vec<Vec<Var>>, uv: &mut Vec<String>) -> NodeSt {
let mut map: FxHashMap<String, Var> = FxHashMap::default();
vex(ns, ev, uv, &mut map)
}

pub fn find_map(s: String, m: &mut FxHashMap<String, Var>) -> Option<Var> {
match m.get(&s.to_string()) {
Some(v) => Some(v.to_owned()),
None => None,
}
}
1 change: 1 addition & 0 deletions src/simplified/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod beta;
pub mod simplified;
22 changes: 20 additions & 2 deletions src/var/var.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use super::super::node::node::*;
// use super::super::node_arr::node_arr::*;
use super::super::program::program::*;
use super::super::simplified::beta::*;
use rustc_hash::FxHashMap;

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

pub fn vex(ns: &mut NodeSt, ev: Vec<Vec<Var>>, uv: &mut Vec<String>) -> NodeSt {
pub fn vex(
ns: &mut NodeSt,
ev: Vec<Vec<Var>>,
uv: &mut Vec<String>,
map: &mut FxHashMap<String, Var>,
) -> NodeSt {
if ns.lhs != None {
let l = Some(Box::new(vex(
&mut ns.lhs.as_ref().unwrap().to_owned().as_ref().to_owned(),
ev.to_owned(),
uv,
map,
)));
if ns.lhs != l {
ns.lhs = l;
Expand All @@ -38,6 +46,7 @@ pub fn vex(ns: &mut NodeSt, ev: Vec<Vec<Var>>, uv: &mut Vec<String>) -> NodeSt {
&mut ns.rhs.as_ref().unwrap().to_owned().as_ref().to_owned(),
ev.to_owned(),
uv,
map,
)));
if ns.rhs != r {
ns.rhs = r;
Expand All @@ -46,7 +55,14 @@ pub fn vex(ns: &mut NodeSt, ev: Vec<Vec<Var>>, uv: &mut Vec<String>) -> NodeSt {

match ns.c.value.to_owned() {
NodeKind::Ident(s) => {
let n = Program::find_v(s, ev.to_owned()).unwrap();
let n = match find_map(s.to_owned(), map) {
Some(v) => v.to_owned(),
None => {
let v = Program::find_v(s.to_owned(), ev.to_owned()).unwrap();
map.insert(s, v.to_owned());
v
}
};
ns.c = n.to_owned().n.c;
if uv.contains(&n.to_owned().s.to_owned()) {
} else {
Expand All @@ -57,13 +73,15 @@ pub fn vex(ns: &mut NodeSt, ev: Vec<Vec<Var>>, uv: &mut Vec<String>) -> NodeSt {
&mut n.n.lhs.as_ref().unwrap().to_owned().as_ref().to_owned(),
ev.to_owned(),
uv,
map,
)));
}
if n.n.rhs != None {
ns.rhs = Some(Box::new(vex(
&mut n.n.rhs.as_ref().unwrap().to_owned().as_ref().to_owned(),
ev.to_owned(),
uv,
map,
)));
}
return ns.to_owned();
Expand Down

0 comments on commit dd6dd3d

Please sign in to comment.