Skip to content

Commit

Permalink
Add derefencing for variable modification
Browse files Browse the repository at this point in the history
  • Loading branch information
doonv committed Jan 4, 2024
1 parent 2f27011 commit d2b33ff
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 11 deletions.
47 changes: 37 additions & 10 deletions src/builtin_parser/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use self::{

use super::{
parser::{Ast, Expression, Operator},
Number, Spanned, SpanExtension,
Number, SpanExtension, Spanned,
};
use bevy::{
prelude::*,
Expand Down Expand Up @@ -122,7 +122,16 @@ fn eval_expression(
Expression::VarAssign {
name,
value: value_expr,
} => match eval_path(*name, environment, registrations)?.value {
} => match eval_path(
*name,
EvalParams {
world,
environment,
registrations,
},
)?
.value
{
Path::Variable(variable) => {
let value = eval_expression(
*value_expr,
Expand Down Expand Up @@ -442,8 +451,11 @@ enum Path {

fn eval_path(
expr: Spanned<Expression>,
environment: &Environment,
registrations: &[&TypeRegistration],
EvalParams {
world,
environment,
registrations,
}: EvalParams,
) -> Result<Spanned<Path>, RunError> {
match expr.value {
Expression::Variable(variable) => {
Expand All @@ -468,7 +480,14 @@ fn eval_path(
}
}
Expression::Member { left, right } => {
let left = eval_path(*left, environment, registrations)?;
let left = eval_path(
*left,
EvalParams {
world,
environment,
registrations,
},
)?;

match left.value {
Path::Variable(variable) => {
Expand All @@ -477,15 +496,23 @@ fn eval_path(
Path::Resource(mut resource) => {
resource.path.push('.');
resource.path += &right;
Ok(Spanned {
span: left.span,
value: Path::Resource(resource),
})

Ok(left.span.wrap(Path::Resource(resource)))
}
Path::NewVariable(name) => Err(RunError::VariableNotFound(left.span.wrap(name))),
}
}
_ => todo!(),
Expression::Dereference(inner) => {
if let Expression::Variable(variable) = inner.value {
let rc = environment.get(&variable, inner.span)?;
let weak = rc.borrow();

Ok(expr.span.wrap(Path::Variable(weak)))
} else {
Err(RunError::CannotBorrowValue(expr.span))
}
}
expr => todo!("{expr:#?}"),
}
}

Expand Down
11 changes: 10 additions & 1 deletion src/builtin_parser/runner/environment.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Environment and function registeration
use std::collections::HashMap;
use std::{collections::HashMap, fmt::Debug};

use bevy::{ecs::world::World, log::warn, reflect::TypeRegistration};
use logos::Span;
Expand Down Expand Up @@ -91,6 +91,13 @@ pub struct Function {
pub argument_count: usize,
pub body: Box<FunctionType>,
}
impl Debug for Function {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Function")
.field("argument_count", &self.argument_count)
.finish_non_exhaustive()
}
}

/// Trait that represents a [`Fn`] that can be turned into a [`Function`].
pub trait IntoFunction<T> {
Expand Down Expand Up @@ -184,13 +191,15 @@ impl_into_function!(T1, T2, T3, T4, T5, T6, T7);
impl_into_function!(T1, T2, T3, T4, T5, T6, T7, T8);

/// A variable inside the [`Environment`].
#[derive(Debug)]
pub enum Variable {
Unmoved(UniqueRc<Value>),
Moved,
Function(Function),
}

/// The environment stores all variables and functions.
#[derive(Debug)]
pub struct Environment {
parent: Option<Box<Environment>>,
variables: HashMap<String, Variable>,
Expand Down
5 changes: 5 additions & 0 deletions src/builtin_parser/runner/stdlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ fn ref_depth(Spanned { span, value }: Spanned<Value>) -> Result<f64, RunError> {
})
}

fn print_env(env: &mut Environment) {
info!("{env:?}");
}

/// Disposes of a [`Value`].
fn drop(_: Value) {}

Expand All @@ -62,5 +66,6 @@ pub fn register(environment: &mut Environment) {
fn dbg;
fn ref_depth;
fn drop;
fn print_env;
});
}
1 change: 1 addition & 0 deletions src/builtin_parser/runner/unique_rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::{
/// reference.
///
/// **TODO:** This is actually going to be a standard library feature. Use [`alloc::rc::UniqueRc`] when it is stabilized.
#[derive(Debug)]
pub struct UniqueRc<T: ?Sized>(Rc<RefCell<T>>);
impl<T: ?Sized> UniqueRc<T> {
/// Get a reference to the inner [`Rc`] of [`UniqueRc`].
Expand Down

0 comments on commit d2b33ff

Please sign in to comment.