Skip to content

Commit 9396340

Browse files
committed
no more duplicate allocs: instantiate and replace root of the redex direcctly
1 parent b7481e7 commit 9396340

3 files changed

Lines changed: 36 additions & 7 deletions

File tree

src/compiler/mod.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,10 @@ fn sc_step(state: &mut TiState, sc_addr: Addr, arg_names: Vec<lang::Name>, body:
115115
globals.push_front(arg.clone());
116116
}
117117
let is_let = body.is_let();
118-
let result_addr = heap.instantiate(body, globals);
119118
for _ in 0..arg_names_len {
120119
stack.pop();
121120
}
122-
heap.update(
123-
stack.pop().unwrap_or(sc_addr),
124-
Node::Ind(result_addr),
125-
);
126-
stack.push(result_addr);
121+
heap.instantiate_and_update(body, *stack.last().unwrap_or(&sc_addr), globals);
127122
if !is_let {
128123
for (name, _) in arg_bindings {
129124
for i in 0..globals.len() {

src/core/mod.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,38 @@ impl Heap<Node> {
134134
CoreExpr::Lam(_, _) => panic!("unable to instantiate lam"),
135135
}
136136
}
137+
138+
pub fn instantiate_and_update(
139+
&mut self,
140+
body: CoreExpr,
141+
root_addr: Addr,
142+
env: &mut ASSOC<Name, Addr>,
143+
) {
144+
match body {
145+
CoreExpr::Var(a) => {
146+
let a_addr = env
147+
.iter()
148+
.find(|(n, _)| *n == a)
149+
.map(|(_, addr)| *addr)
150+
.expect(&format!("undefined name {}", a));
151+
self.update(root_addr, Node::Ind(a_addr));
152+
}
153+
CoreExpr::Num(n) => self.update(root_addr, Node::Num(n)),
154+
CoreExpr::Constr { .. } => panic!("unable to instantiate constr yet"),
155+
CoreExpr::Ap(e1, e2) => {
156+
let a1 = self.instantiate(*e1, env);
157+
let a2 = self.instantiate(*e2, env);
158+
self.update(root_addr, Node::Ap(a1, a2));
159+
}
160+
CoreExpr::Let { defs, body } => {
161+
for (name, expr) in defs {
162+
let addr = self.alloc(Node::SuperComb(name.clone(), vec![], expr));
163+
env.push_front((name, addr));
164+
}
165+
self.instantiate_and_update(*body, root_addr, env);
166+
}
167+
CoreExpr::Case(_, _) => panic!("unable to instantiate case"),
168+
CoreExpr::Lam(_, _) => panic!("unable to instantiate lam"),
169+
}
170+
}
137171
}

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ fn run(s: String) -> String {
1515
}
1616

1717
fn main() {
18-
let program = String::from("id x = x; main = twice twice id 3");
18+
let program = String::from("oct g x = let h = twice g in let k = twice h in k (k x); main = oct I 4");
1919
println!("{}", run(program));
2020
}

0 commit comments

Comments
 (0)