Rename environment -> memory
This commit is contained in:
parent
d913443e97
commit
5bfd79669e
@ -38,7 +38,7 @@ impl<'a, 'b> Evaluator<'a, 'b> {
|
|||||||
let mut acc = vec![];
|
let mut acc = vec![];
|
||||||
for (def_id, function) in reduced.functions.into_iter() {
|
for (def_id, function) in reduced.functions.into_iter() {
|
||||||
let mem = (&def_id).into();
|
let mem = (&def_id).into();
|
||||||
self.state.environments.insert(mem, MemoryValue::Function(function));
|
self.state.memory.insert(mem, MemoryValue::Function(function));
|
||||||
}
|
}
|
||||||
|
|
||||||
for statement in reduced.entrypoint.into_iter() {
|
for statement in reduced.entrypoint.into_iter() {
|
||||||
@ -78,7 +78,7 @@ impl<'a, 'b> Evaluator<'a, 'b> {
|
|||||||
match stmt {
|
match stmt {
|
||||||
Statement::Binding { ref id, expr, constant: _ } => {
|
Statement::Binding { ref id, expr, constant: _ } => {
|
||||||
let evaluated = self.expression(expr)?;
|
let evaluated = self.expression(expr)?;
|
||||||
self.state.environments.insert(id.into(), evaluated.into());
|
self.state.memory.insert(id.into(), evaluated.into());
|
||||||
Ok(StatementOutput::Nothing)
|
Ok(StatementOutput::Nothing)
|
||||||
}
|
}
|
||||||
Statement::Expression(expr) => {
|
Statement::Expression(expr) => {
|
||||||
@ -119,7 +119,7 @@ impl<'a, 'b> Evaluator<'a, 'b> {
|
|||||||
Expression::Lookup(kind) => match kind {
|
Expression::Lookup(kind) => match kind {
|
||||||
Lookup::Function(ref id) => {
|
Lookup::Function(ref id) => {
|
||||||
let mem = id.into();
|
let mem = id.into();
|
||||||
match self.state.environments.lookup(&mem) {
|
match self.state.memory.lookup(&mem) {
|
||||||
// This just checks that the function exists in "memory" by ID, we don't
|
// This just checks that the function exists in "memory" by ID, we don't
|
||||||
// actually retrieve it until `apply_function()`
|
// actually retrieve it until `apply_function()`
|
||||||
Some(MemoryValue::Function(_)) => Primitive::Callable(Callable::UserDefined(*id)),
|
Some(MemoryValue::Function(_)) => Primitive::Callable(Callable::UserDefined(*id)),
|
||||||
@ -128,21 +128,21 @@ impl<'a, 'b> Evaluator<'a, 'b> {
|
|||||||
}
|
}
|
||||||
Lookup::Param(n) => {
|
Lookup::Param(n) => {
|
||||||
let mem = n.into();
|
let mem = n.into();
|
||||||
match self.state.environments.lookup(&mem) {
|
match self.state.memory.lookup(&mem) {
|
||||||
Some(MemoryValue::Primitive(prim)) => prim.clone(),
|
Some(MemoryValue::Primitive(prim)) => prim.clone(),
|
||||||
e => return Err(format!("Param lookup error, got {:?}", e).into()),
|
e => return Err(format!("Param lookup error, got {:?}", e).into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Lookup::SelfParam => {
|
Lookup::SelfParam => {
|
||||||
let mem = Memory::self_param();
|
let mem = Memory::self_param();
|
||||||
match self.state.environments.lookup(&mem) {
|
match self.state.memory.lookup(&mem) {
|
||||||
Some(MemoryValue::Primitive(prim)) => prim.clone(),
|
Some(MemoryValue::Primitive(prim)) => prim.clone(),
|
||||||
e => return Err(format!("SelfParam lookup error, got {:?}", e).into()),
|
e => return Err(format!("SelfParam lookup error, got {:?}", e).into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Lookup::LocalVar(ref id) | Lookup::GlobalVar(ref id) => {
|
Lookup::LocalVar(ref id) | Lookup::GlobalVar(ref id) => {
|
||||||
let mem = id.into();
|
let mem = id.into();
|
||||||
match self.state.environments.lookup(&mem) {
|
match self.state.memory.lookup(&mem) {
|
||||||
Some(MemoryValue::Primitive(expr)) => expr.clone(),
|
Some(MemoryValue::Primitive(expr)) => expr.clone(),
|
||||||
_ =>
|
_ =>
|
||||||
return Err(
|
return Err(
|
||||||
@ -155,7 +155,7 @@ impl<'a, 'b> Evaluator<'a, 'b> {
|
|||||||
let mem = lval.into();
|
let mem = lval.into();
|
||||||
let evaluated = self.expression(rval)?;
|
let evaluated = self.expression(rval)?;
|
||||||
println!("Inserting {:?} into {:?}", evaluated, mem);
|
println!("Inserting {:?} into {:?}", evaluated, mem);
|
||||||
self.state.environments.insert(mem, MemoryValue::Primitive(evaluated));
|
self.state.memory.insert(mem, MemoryValue::Primitive(evaluated));
|
||||||
Primitive::unit()
|
Primitive::unit()
|
||||||
}
|
}
|
||||||
Expression::Call { box f, args } => self.call_expression(f, args, None)?,
|
Expression::Call { box f, args } => self.call_expression(f, args, None)?,
|
||||||
@ -311,9 +311,9 @@ impl<'a, 'b> Evaluator<'a, 'b> {
|
|||||||
let cond = self.expression(cond)?;
|
let cond = self.expression(cond)?;
|
||||||
|
|
||||||
for alt in alternatives.into_iter() {
|
for alt in alternatives.into_iter() {
|
||||||
let mut new_scope = self.state.environments.new_scope(None);
|
let mut new_scope = self.state.memory.new_scope(None);
|
||||||
if matches(&cond, &alt.pattern, &mut new_scope) {
|
if matches(&cond, &alt.pattern, &mut new_scope) {
|
||||||
let mut new_state = State { environments: new_scope };
|
let mut new_state = State { memory: new_scope };
|
||||||
let mut evaluator = Evaluator::new(&mut new_state, self.type_context);
|
let mut evaluator = Evaluator::new(&mut new_state, self.type_context);
|
||||||
let output = evaluator.block(alt.item);
|
let output = evaluator.block(alt.item);
|
||||||
self.early_returning = evaluator.early_returning;
|
self.early_returning = evaluator.early_returning;
|
||||||
@ -338,7 +338,7 @@ impl<'a, 'b> Evaluator<'a, 'b> {
|
|||||||
Callable::Builtin(builtin) => self.apply_builtin(builtin, args),
|
Callable::Builtin(builtin) => self.apply_builtin(builtin, args),
|
||||||
Callable::UserDefined(def_id) => {
|
Callable::UserDefined(def_id) => {
|
||||||
let mem = (&def_id).into();
|
let mem = (&def_id).into();
|
||||||
match self.state.environments.lookup(&mem) {
|
match self.state.memory.lookup(&mem) {
|
||||||
Some(MemoryValue::Function(FunctionDefinition { body })) => {
|
Some(MemoryValue::Function(FunctionDefinition { body })) => {
|
||||||
let body = body.clone(); //TODO ideally this clone would not happen
|
let body = body.clone(); //TODO ideally this clone would not happen
|
||||||
self.apply_function(body, args, self_expr)
|
self.apply_function(body, args, self_expr)
|
||||||
@ -496,17 +496,17 @@ impl<'a, 'b> Evaluator<'a, 'b> {
|
|||||||
evaluated_args.push(self.expression(arg)?);
|
evaluated_args.push(self.expression(arg)?);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut frame_state = State { environments: self.state.environments.new_scope(None) };
|
let mut frame_state = State { memory: self.state.memory.new_scope(None) };
|
||||||
let mut evaluator = Evaluator::new(&mut frame_state, self.type_context);
|
let mut evaluator = Evaluator::new(&mut frame_state, self.type_context);
|
||||||
|
|
||||||
if let Some(evaled) = self_expr {
|
if let Some(evaled) = self_expr {
|
||||||
let mem = Memory::self_param();
|
let mem = Memory::self_param();
|
||||||
evaluator.state.environments.insert(mem, MemoryValue::Primitive(evaled));
|
evaluator.state.memory.insert(mem, MemoryValue::Primitive(evaled));
|
||||||
}
|
}
|
||||||
for (n, evaled) in evaluated_args.into_iter().enumerate() {
|
for (n, evaled) in evaluated_args.into_iter().enumerate() {
|
||||||
let n = n as u8;
|
let n = n as u8;
|
||||||
let mem = n.into();
|
let mem = n.into();
|
||||||
evaluator.state.environments.insert(mem, MemoryValue::Primitive(evaled));
|
evaluator.state.memory.insert(mem, MemoryValue::Primitive(evaled));
|
||||||
}
|
}
|
||||||
evaluator.block(body)
|
evaluator.block(body)
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,7 @@ type EvalResult<T> = Result<T, RuntimeError>;
|
|||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct State<'a> {
|
pub struct State<'a> {
|
||||||
environments: ScopeStack<'a, Memory, MemoryValue>,
|
memory: ScopeStack<'a, Memory, MemoryValue>,
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO - eh, I dunno, maybe it doesn't matter exactly how memory works in the tree-walking
|
//TODO - eh, I dunno, maybe it doesn't matter exactly how memory works in the tree-walking
|
||||||
@ -158,7 +158,7 @@ impl From<Literal> for Primitive {
|
|||||||
|
|
||||||
impl<'a> State<'a> {
|
impl<'a> State<'a> {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self { environments: ScopeStack::new(Some("global".to_string())) }
|
Self { memory: ScopeStack::new(Some("global".to_string())) }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn evaluate(
|
pub fn evaluate(
|
||||||
|
Loading…
Reference in New Issue
Block a user