diff --git a/src/builtin_parser/runner.rs b/src/builtin_parser/runner.rs index 20ad11f..3121365 100644 --- a/src/builtin_parser/runner.rs +++ b/src/builtin_parser/runner.rs @@ -14,7 +14,7 @@ use self::{ use super::{ parser::{Ast, Expression, Operator}, - Number, Spanned, SpanExtension, + Number, SpanExtension, Spanned, }; use bevy::{ prelude::*, @@ -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, @@ -442,8 +451,11 @@ enum Path { fn eval_path( expr: Spanned, - environment: &Environment, - registrations: &[&TypeRegistration], + EvalParams { + world, + environment, + registrations, + }: EvalParams, ) -> Result, RunError> { match expr.value { Expression::Variable(variable) => { @@ -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) => { @@ -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:#?}"), } } diff --git a/src/builtin_parser/runner/environment.rs b/src/builtin_parser/runner/environment.rs index d3b1d7e..1275e46 100644 --- a/src/builtin_parser/runner/environment.rs +++ b/src/builtin_parser/runner/environment.rs @@ -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; @@ -91,6 +91,13 @@ pub struct Function { pub argument_count: usize, pub body: Box, } +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 { @@ -184,6 +191,7 @@ 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), Moved, @@ -191,6 +199,7 @@ pub enum Variable { } /// The environment stores all variables and functions. +#[derive(Debug)] pub struct Environment { parent: Option>, variables: HashMap, diff --git a/src/builtin_parser/runner/stdlib.rs b/src/builtin_parser/runner/stdlib.rs index c5b4666..1e5418e 100644 --- a/src/builtin_parser/runner/stdlib.rs +++ b/src/builtin_parser/runner/stdlib.rs @@ -53,6 +53,10 @@ fn ref_depth(Spanned { span, value }: Spanned) -> Result { }) } +fn print_env(env: &mut Environment) { + info!("{env:?}"); +} + /// Disposes of a [`Value`]. fn drop(_: Value) {} @@ -62,5 +66,6 @@ pub fn register(environment: &mut Environment) { fn dbg; fn ref_depth; fn drop; + fn print_env; }); } diff --git a/src/builtin_parser/runner/unique_rc.rs b/src/builtin_parser/runner/unique_rc.rs index f0cc020..8c6e679 100644 --- a/src/builtin_parser/runner/unique_rc.rs +++ b/src/builtin_parser/runner/unique_rc.rs @@ -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(Rc>); impl UniqueRc { /// Get a reference to the inner [`Rc`] of [`UniqueRc`].