DEV: simple expressions don't leave their values on the stack.

This commit is contained in:
Jeremy Wall 2019-08-20 12:41:37 -05:00
parent f1180dbc5f
commit 45819f5406
3 changed files with 11 additions and 4 deletions

View File

@ -584,7 +584,7 @@ macro_rules! assert_parse_cases {
let env = Rc::new(RefCell::new(Environment::new(Vec::new(), Vec::new()))); let env = Rc::new(RefCell::new(Environment::new(Vec::new(), Vec::new())));
let mut vm = VM::new(ops.clone(), env); let mut vm = VM::new(ops.clone(), env);
vm.run().unwrap(); vm.run().unwrap();
assert_eq!(vm.pop().unwrap().0, Rc::new(case.1)); assert_eq!(vm.last.unwrap().0, Rc::new(case.1));
} }
}; };
@ -769,7 +769,7 @@ fn simple_trace() {
let env = Rc::new(RefCell::new(Environment::new(Vec::new(), Vec::new()))); let env = Rc::new(RefCell::new(Environment::new(Vec::new(), Vec::new())));
let mut vm = VM::new(ops.clone(), env); let mut vm = VM::new(ops.clone(), env);
vm.run().unwrap(); vm.run().unwrap();
assert_eq!(vm.pop().unwrap().0, Rc::new(P(Int(2)))); assert_eq!(vm.last.unwrap().0, Rc::new(P(Int(2))));
let err_out = &vm.env.borrow().stderr; let err_out = &vm.env.borrow().stderr;
assert_eq!( assert_eq!(
String::from_utf8_lossy(err_out).to_owned(), String::from_utf8_lossy(err_out).to_owned(),

View File

@ -55,7 +55,11 @@ impl AST {
fn translate_stmts(stmts: Vec<Statement>, mut ops: &mut PositionMap, root: &Path) { fn translate_stmts(stmts: Vec<Statement>, mut ops: &mut PositionMap, root: &Path) {
for stmt in stmts { for stmt in stmts {
match stmt { match stmt {
Statement::Expression(expr) => Self::translate_expr(expr, &mut ops, root), Statement::Expression(expr) => {
let expr_pos = expr.pos().clone();
Self::translate_expr(expr, &mut ops, root);
ops.push(Op::Pop, expr_pos);
}
Statement::Assert(_, _) => { Statement::Assert(_, _) => {
unimplemented!("Assert statements are not implmented yet") unimplemented!("Assert statements are not implmented yet")
} }

View File

@ -38,6 +38,7 @@ where
runtime: runtime::Builtins, runtime: runtime::Builtins,
ops: OpPointer, ops: OpPointer,
pub env: Rc<RefCell<Environment<O, E>>>, pub env: Rc<RefCell<Environment<O, E>>>,
pub last: Option<(Rc<Value>, Position)>,
} }
impl<'a, O, E> VM<O, E> impl<'a, O, E> VM<O, E>
@ -56,6 +57,7 @@ where
runtime: runtime::Builtins::new(), runtime: runtime::Builtins::new(),
ops: ops, ops: ops,
env: env, env: env,
last: None,
} }
} }
@ -66,6 +68,7 @@ where
runtime: self.runtime.clone(), runtime: self.runtime.clone(),
ops: self.ops.clone(), ops: self.ops.clone(),
env: self.env.clone(), env: self.env.clone(),
last: self.last,
} }
} }
@ -135,7 +138,7 @@ where
return Ok(()); return Ok(());
} }
Op::Pop => { Op::Pop => {
self.pop()?; self.last = Some(self.pop()?);
} }
Op::Typ => self.op_typ()?, Op::Typ => self.op_typ()?,
Op::Runtime(h) => self.op_runtime(h, pos)?, Op::Runtime(h) => self.op_runtime(h, pos)?,