add object field getting syntax obj:field

This commit is contained in:
Mark
2025-02-07 17:50:28 +01:00
parent 3cd8dc02d2
commit e07010dcfc
9 changed files with 146 additions and 5 deletions

View File

@@ -0,0 +1,41 @@
use crate::{
data::object::ObjectFieldsMap,
errors::{CheckError, SourceRange},
info, program,
};
use super::{CompInfo, MersStatement};
/// Extracts a property with the given name from the object
#[derive(Debug)]
pub struct Field {
pub pos_in_src: SourceRange,
pub object: Box<dyn MersStatement>,
pub field: String,
}
impl MersStatement for Field {
fn has_scope(&self) -> bool {
false
}
fn compile_custom(
&self,
info: &mut info::Info<super::Local>,
comp: CompInfo,
) -> Result<Box<dyn program::run::MersStatement>, CheckError> {
Ok(Box::new(program::run::field::Field {
pos_in_src: self.pos_in_src.clone(),
object: self.object.compile(info, comp)?,
field_str: self.field.clone(),
field: info.global.object_fields.get_or_add_field(&self.field),
}))
}
fn source_range(&self) -> SourceRange {
self.pos_in_src.clone()
}
fn inner_statements(&self) -> Vec<&dyn MersStatement> {
vec![&*self.object]
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
}

View File

@@ -20,6 +20,8 @@ pub mod chain;
#[cfg(feature = "parse")]
pub mod custom_type;
#[cfg(feature = "parse")]
pub mod field;
#[cfg(feature = "parse")]
pub mod function;
#[cfg(feature = "parse")]
pub mod r#if;

View File

@@ -0,0 +1,85 @@
use crate::{
data::{self, object::ObjectT, Data, MersDataWInfo, MersTypeWInfo, Type},
errors::{CheckError, EColor, SourceRange},
};
use super::MersStatement;
#[derive(Debug)]
pub struct Field {
pub pos_in_src: SourceRange,
pub object: Box<dyn MersStatement>,
pub field_str: String,
pub field: usize,
}
impl MersStatement for Field {
fn check_custom(
&self,
info: &mut super::CheckInfo,
init_to: Option<&Type>,
) -> Result<data::Type, super::CheckError> {
if init_to.is_some() {
return Err("can't init to statement type Field".to_string().into());
}
let object = self.object.check(info, init_to)?;
let mut o = Type::empty();
for t in object.types.iter() {
if let Some(t) = t.as_any().downcast_ref::<ObjectT>() {
if let Some(t) = t.get(self.field) {
o.add_all(t);
} else {
return Err(CheckError::new().msg(vec![
("can't get field ".to_owned(), None),
(self.field_str.clone(), Some(EColor::ObjectField)),
(" of object ".to_owned(), None),
(t.with_info(info).to_string(), Some(EColor::InitFrom)),
]));
}
} else {
return Err(CheckError::new().msg(vec![
("can't get field ".to_owned(), None),
(self.field_str.clone(), Some(EColor::ObjectField)),
(" of non-object type ".to_owned(), None),
(t.with_info(info).to_string(), Some(EColor::InitFrom)),
]));
}
}
Ok(o)
}
fn run_custom(&self, info: &mut super::Info) -> Result<Data, CheckError> {
let object = self.object.run(info)?;
let object = object.get();
let object = object
.as_any()
.downcast_ref::<data::object::Object>()
.ok_or_else(|| {
format!(
"couldn't extract field {} from non-object value {}",
self.field_str,
object.with_info(info)
)
})?;
Ok(object
.get(self.field)
.ok_or_else(|| {
format!(
"couldn't extract field {} from object {}",
self.field_str,
object.with_info(info)
)
})?
.clone())
}
fn has_scope(&self) -> bool {
false
}
fn source_range(&self) -> SourceRange {
self.pos_in_src.clone()
}
fn inner_statements(&self) -> Vec<&dyn MersStatement> {
vec![&*self.object]
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
}

View File

@@ -23,6 +23,8 @@ pub mod chain;
#[cfg(feature = "run")]
pub mod custom_type;
#[cfg(feature = "run")]
pub mod field;
#[cfg(feature = "run")]
pub mod function;
#[cfg(feature = "run")]
pub mod r#if;