mirror of
https://github.com/Dummi26/mers.git
synced 2025-12-14 11:16:17 +01:00
make objects work better, especially destructuring
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
use std::{fmt::Debug, sync::Arc};
|
||||
|
||||
use mers_lib::data::MersDataWInfo;
|
||||
use mers_lib::prelude_compile::*;
|
||||
|
||||
use mers_lib::{
|
||||
@@ -15,7 +16,8 @@ fn variable() -> Res {
|
||||
run_code(Config::new(), format!("x := {n}, x"))?,
|
||||
TypedData(
|
||||
Type::new(data::int::IntT),
|
||||
Data::new(data::int::Int(n as _))
|
||||
Data::new(data::int::Int(n as _)),
|
||||
mers_lib::info::Info::neverused(),
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -26,7 +28,11 @@ fn variable() -> Res {
|
||||
fn mutating_a_variable() -> Res {
|
||||
assert_eq!(
|
||||
run_code(Config::new(), "x := 5, &x = 2, x")?,
|
||||
TypedData(Type::new(data::int::IntT), Data::new(data::int::Int(2)))
|
||||
TypedData(
|
||||
Type::new(data::int::IntT),
|
||||
Data::new(data::int::Int(2)),
|
||||
mers_lib::info::Info::neverused()
|
||||
),
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
@@ -35,7 +41,11 @@ fn mutating_a_variable() -> Res {
|
||||
fn variable_shadowing() -> Res {
|
||||
assert_eq!(
|
||||
run_code(Config::new(), "x := 5, { x := 2, &x = 3 }, x")?,
|
||||
TypedData(Type::new(data::int::IntT), Data::new(data::int::Int(5)))
|
||||
TypedData(
|
||||
Type::new(data::int::IntT),
|
||||
Data::new(data::int::Int(5)),
|
||||
mers_lib::info::Info::neverused()
|
||||
)
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
@@ -44,7 +54,11 @@ fn variable_shadowing() -> Res {
|
||||
fn identity_function() -> Res {
|
||||
assert_eq!(
|
||||
run_code(Config::new(), "id := x -> x, 4.id")?,
|
||||
TypedData(Type::new(data::int::IntT), Data::new(data::int::Int(4)))
|
||||
TypedData(
|
||||
Type::new(data::int::IntT),
|
||||
Data::new(data::int::Int(4)),
|
||||
mers_lib::info::Info::neverused()
|
||||
)
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
@@ -59,13 +73,18 @@ fn run_code(cfg: Config, code: impl Into<String>) -> Result<TypedData, CheckErro
|
||||
let compiled = parsed.compile(&mut i1, Default::default())?;
|
||||
let output_type = compiled.check(&mut i3, Default::default())?;
|
||||
let output_data = compiled.run(&mut i2)?;
|
||||
Ok(TypedData(output_type, output_data))
|
||||
Ok(TypedData(output_type, output_data, i2))
|
||||
}
|
||||
|
||||
struct TypedData(Type, Data);
|
||||
struct TypedData(Type, Data, mers_lib::program::run::Info);
|
||||
impl Debug for TypedData {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "Type: {}, Data: {}", self.0, self.1.get())
|
||||
write!(
|
||||
f,
|
||||
"Type: {}, Data: {}",
|
||||
self.0.with_info(&self.2),
|
||||
self.1.get().with_info(&self.2)
|
||||
)
|
||||
}
|
||||
}
|
||||
impl PartialEq for TypedData {
|
||||
@@ -75,16 +94,16 @@ impl PartialEq for TypedData {
|
||||
let d1 = self.1 == other.1;
|
||||
let d2 = other.1 == self.1;
|
||||
if t1 && !t2 {
|
||||
panic!("self is same type as other, but other is not same type as self (non-symmetrical eq)! self={}, other={}", self.0, other.0);
|
||||
panic!("self is same type as other, but other is not same type as self (non-symmetrical eq)! self={}, other={}", self.0.with_info(&self.2), other.0.with_info(&self.2));
|
||||
}
|
||||
if t2 && !t1 {
|
||||
panic!("other is same type as self, but self is not same type as other (non-symmetrical eq)! other={}, self={}", other.0, self.0);
|
||||
panic!("other is same type as self, but self is not same type as other (non-symmetrical eq)! other={}, self={}", other.0.with_info(&self.2), self.0.with_info(&self.2));
|
||||
}
|
||||
if d1 && !d2 {
|
||||
panic!("self is same data as other, but other is not same data as self (non-symmetrical eq)! self={}, other={}", self.1.get(), other.1.get());
|
||||
panic!("self is same data as other, but other is not same data as self (non-symmetrical eq)! self={}, other={}", self.1.get().with_info(&self.2), other.1.get().with_info(&self.2));
|
||||
}
|
||||
if d2 && !d1 {
|
||||
panic!("other is same data as self, but self is not same data as other (non-symmetrical eq)! other={}, self={}", other.1.get(), self.1.get());
|
||||
panic!("other is same data as self, but self is not same data as other (non-symmetrical eq)! other={}, self={}", other.1.get().with_info(&self.2), self.1.get().with_info(&self.2));
|
||||
}
|
||||
t1 && d1
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user