add scope ops
This commit is contained in:
parent
4ead8e702e
commit
382cfdd903
18
src/run.rs
18
src/run.rs
|
|
@ -1,4 +1,4 @@
|
|||
use std::{collections::HashMap, fmt::Debug, rc::Rc};
|
||||
use std::{borrow::Cow, collections::HashMap, fmt::Debug, rc::Rc};
|
||||
|
||||
use crate::{
|
||||
ast::{Constant, Expression, Location},
|
||||
|
|
@ -36,6 +36,20 @@ pub struct Scope {
|
|||
pub functions: HashMap<String, Function>,
|
||||
}
|
||||
|
||||
impl Scope {
|
||||
pub fn get(&self, name: impl AsRef<str>) -> Value {
|
||||
self.variables.get(name.as_ref()).cloned()
|
||||
}
|
||||
|
||||
pub fn set(&mut self, name: Cow<str>, value: Value) {
|
||||
if let Some(value) = value {
|
||||
self.variables.insert(name.into(), value);
|
||||
} else {
|
||||
self.variables.remove(name.as_ref());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Eval {
|
||||
fn eval(&self, scope: &mut Scope) -> Value;
|
||||
}
|
||||
|
|
@ -54,7 +68,7 @@ impl Eval for Constant {
|
|||
impl Eval for Location {
|
||||
fn eval(&self, scope: &mut Scope) -> Value {
|
||||
match self {
|
||||
Location::Variable { name } => scope.variables.get(name).cloned(),
|
||||
Location::Variable { name } => scope.get(name),
|
||||
Location::Field { table, index } => {
|
||||
let table = table.eval(scope);
|
||||
let Some(ValueInner::Table(table)) = table else {
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user