#[repr(u8)]
pub enum VVal {
Show 23 variants None, Err(Rc<RefCell<(VVal, SynPos)>>), Bol(bool), Sym(Symbol), Chr(VValChr), Str(Rc<String>), Byt(Rc<Vec<u8>>), Int(i64), Flt(f64), Syn(SynPos), Pair(Rc<(VVal, VVal)>), Opt(Option<Rc<VVal>>), Iter(Rc<RefCell<VValIter>>), Lst(Rc<RefCell<Vec<VVal>>>), Map(Rc<RefCell<FnvHashMap<Symbol, VVal>>>), Fun(Rc<VValFun>), DropFun(Rc<DropFun>), FVec(Box<NVec<f64>>), IVec(Box<NVec<i64>>), Ref(Rc<RefCell<VVal>>), HRef(Rc<RefCell<VVal>>), WWRef(Weak<RefCell<VVal>>), Usr(Box<dyn VValUserData>),
}
Expand description

VVal aka. VariantValue is a data structure to represent all kinds of WLambda data structures.

It’s used for the AST, for internal data and for runtime data structures.

Variants

None

The none value, the default value of all non initialized data.

Err(Rc<RefCell<(VVal, SynPos)>>)

The err value is a special sentinel value for representing any kind of application error condition. It’s created using the special $e or $error syntax.

Bol(bool)

Representation of a boolean value.

Sym(Symbol)

Representation of an interned string aka symbol or key.

Chr(VValChr)

Representation of a single character (or byte)

Str(Rc<String>)

Representation of a unicode/text string.

Byt(Rc<Vec<u8>>)

Representation of a byte buffer.

Int(i64)

Integer value

Flt(f64)

Float value

Syn(SynPos)

A syntax node in the AST, records the position too.

Pair(Rc<(VVal, VVal)>)

A pair

Opt(Option<Rc<VVal>>)

An optional value. While VVal::None basically has the same meaning as “no value”, an optional value provides functions a way to say that they don’t even return none value but nothing. Yes, it sounds weird, and it is weird. But in case of iterator functions that iterate over an array you can find out whether the iterator is at the end or will provide more values.

Iter(Rc<RefCell<VValIter>>)

A special internal iterator type for built in VVal data structures.

Lst(Rc<RefCell<Vec<VVal>>>)

A list (or vector) of VVals.

Map(Rc<RefCell<FnvHashMap<Symbol, VVal>>>)

A mapping of strings to VVals.

Fun(Rc<VValFun>)

A function, see also VValFun

DropFun(Rc<DropFun>)

A guarded VVal, that executes a given function when it is no longer referenced.

FVec(Box<NVec<f64>>)

A numerical (mathematical) vector storing integers. See NVec for more information.

IVec(Box<NVec<i64>>)

A numerical (mathematical) vector storing floats. See NVec for more information.

Ref(Rc<RefCell<VVal>>)

A (strong) reference to a VVal.

HRef(Rc<RefCell<VVal>>)

A hidden strong reference to a VVal. Generated either explicitly by $& or if a variable is captured by a closure.

WWRef(Weak<RefCell<VVal>>)

A (weak) reference to a VVal. Might turn VVal::None anytime.

Usr(Box<dyn VValUserData>)

A vval that can box some user data which can later be accessed from inside user supplied Rust functions via std::any::Any.

Implementations

Creates a pair from two VVals.

Returns an iterator of the current value.

Creates a new WLambda function that wraps the given Rust closure. See also VValFun::new_fun. For a more detailed discussion of the parameters.

 use wlambda::compiler::EvalContext;
 use wlambda::vval::{VVal, VValFun, Env};

 let mut ctx = wlambda::compiler::EvalContext::new_empty_global_env();

 ctx.set_global_var("xyz",
     &VVal::new_fun(
         move |env: &mut Env, argc: usize| {
             Ok(VVal::new_str("xyz"))
         }, None, None, false));

 assert_eq!(ctx.eval("xyz[]").unwrap().s_raw(), "xyz")

Returns true if this vval is containing (multiple) other VVals. Usually true for: Maps, Vectors, Pairs and Opt.

Helpful if you want to iterate through a data structure by recursively iterating over a vval. Without getting iterations over individual string characters.

This function returns you an iterator over the VVal. It will iterate over data such as VVal::Str, VVal::Sym, VVal::Lst, VVal::Map and VVal::Byt.

See also: VVal::with_iter which is the better option for iterating over a VVal, because it catches the special case when the VVal itself is an iterator already. This function will not return the same iterator again when called on an VVal iterator value!

This functionality provides the for keyword/function in WLambda.

use wlambda::*;

let some_vec = VVal::vec();
some_vec.push(VVal::Int(10));
some_vec.push(VVal::Int(22));
some_vec.push(VVal::Int(36));

let mut sum = 0;
for (v, _) in some_vec.iter() {
    sum += v.i();
}

assert_eq!(sum, 68);

Calls the given callback with an iterator through the vval. It catches the special case when the VVal itself is an iterator.

use wlambda::VVal;
use std::rc::Rc;
use std::cell::RefCell;

let x = VVal::vec();
x.push(VVal::Int(10));
x.push(VVal::Int(20));

let it = x.as_iter();

let mut sum = 0;
it.with_iter(|iter| {
    for (v, _) in iter {
        sum += v.i();
    }
});

assert_eq!(sum, 30);

This method will disable all arity checks of the function in the VVal. Does nothing if the VVal is not a function.

It is used for disabling checks of drop functions, as they are evaluated in their own environment and there is no proper way to report errors upwards.

This function is an internal function that clones a function reference and assigns new upvalues to it for making a new closure instance.

Returns the string data or converts the value to a string for displaying. The conversion format is not for reading the value in again via the WLambda parser, it’s for accessing the data as pure as possible.

Use this if you need the raw unescaped contents of VVal::Str, VVal::Sym, VVal::Byt and other VVals.

As this is used usually for generating output a VVal::None is turned into an empty string

There is also with_s_ref() which allows you to work with a reference to the string, in case you don’t need to own the copy!

use wlambda::VVal;

assert_eq!(VVal::None.s_raw(), "");
assert_eq!(VVal::new_str("Test").s_raw(), "Test");

Like s_raw() but returns a reference to the string.

Use this if you need the raw unescaped contents of VVal::Str, VVal::Sym, VVal::Byt and other VVals.

As this is used usually for generating output a VVal::None is turned into an empty string

use wlambda::VVal;

VVal::None.with_s_ref(
    |s: &str| assert_eq!(s, ""));

VVal::new_str("Test").with_s_ref(
    |s: &str| assert_eq!(s, "Test"));

Accesses the user data of type T directly. If the user data is not of that type it returns None.

Quick access method for retrieving the VVal at index idx. Returns VVal::None if the VVal is not a VVal::Lst or no such index exists. See also the shorthands v_i, v_f, v_s and v_s_raw.

 use wlambda::VVal;
 let v = VVal::vec();
 v.push(VVal::Int(10));
 v.push(VVal::Int(11));

 assert_eq!(v.v_(1).i(), 11);
 assert_eq!(v.v_i(1),    11);

Quick access method for retrieving the VVal at key idx. Returns VVal::None if the VVal is not a VVal::Map or no such index exists. See also the shorthands v_ik, v_fk, v_sk, v_bk and v_s_rawk.

 use wlambda::VVal;
 let v = VVal::map();
 v.set_key_str("aaa", VVal::Int(12));
 v.set_key_str("abc", VVal::Int(13));
 v.set_key_str("zyy", VVal::Int(14));

 assert_eq!(v.v_k("abc").i(), 13);
 assert_eq!(v.v_ik("aaa"),    12);
 assert_eq!(v.v_ik("zyy"),    14);

Quick access of an integer at the given idx. See also v_.

 let v = wlambda::VVal::vec();
 v.push(wlambda::VVal::Int(11));
 assert_eq!(v.v_i(0),    11);

Quick access of the integer at the given key. See also v_k.

 let v = wlambda::VVal::map();
 v.set_key_str("aaa", wlambda::VVal::new_str("10"));
 assert_eq!(v.v_ik("aaa"), 10);

Quick access of an bool at the given idx. See also v_.

 let v = wlambda::VVal::vec();
 v.push(wlambda::VVal::Bol(true));
 assert_eq!(v.v_b(0),    true);

Quick access of the integer at the given key. See also v_k.

 let v = wlambda::VVal::map();
 v.set_key_str("aaa", wlambda::VVal::Bol(true));
 assert_eq!(v.v_bk("aaa"), true);

Quick access of a character at the given idx. See also v_.

 let v = wlambda::VVal::vec();
 v.push(wlambda::VVal::Int(11));
 assert_eq!(v.v_c(0),    '\u{0b}');

Quick access of the character at the given key. See also v_k.

 let v = wlambda::VVal::map();
 v.set_key_str("aaa", wlambda::VVal::new_char('@'));
 assert_eq!(v.v_ck("aaa"), '@');

Quick access of a byte at the given idx. See also v_.

 let v = wlambda::VVal::vec();
 v.push(wlambda::VVal::Int(11));
 assert_eq!(v.v_byte(0), b'\x0b');

Quick access of the byte at the given key. See also v_k.

 let v = wlambda::VVal::map();
 v.set_key_str("aaa", wlambda::VVal::new_byte(b'@'));
 assert_eq!(v.v_bytek("aaa"), b'@');

Quick access of a raw string at the given idx. See also v_.

 let v = wlambda::VVal::vec();
 v.push(wlambda::VVal::Int(12));
 assert_eq!(v.v_s_raw(0), "12");

Quick access of a raw string as reference at the given idx. See also v_.

 let v = wlambda::VVal::vec();
 v.push(wlambda::VVal::new_str("12"));
 assert_eq!(v.v_with_s_ref(0, |s: &str| s.chars().nth(0).unwrap()), '1');

Quick access of the string at the given key. See also v_k.

 let v = wlambda::VVal::map();
 v.set_key_str("aaa", wlambda::VVal::new_str("XYX"));
 assert_eq!(v.v_s_rawk("aaa"), "XYX");

Quick access of the string as reference at the given key. See also v_k.

 let v = wlambda::VVal::map();
 v.set_key_str("aaa", wlambda::VVal::new_str("XYX"));
 assert!(v.v_with_s_refk("aaa", |s: &str| s == "XYX"));

Quick access of the string representation at the given idx. See also v_.

 let v = wlambda::VVal::vec();
 v.push(wlambda::VVal::Int(13));
 assert_eq!(v.v_s(0), "13");

Quick access of the string represenation at the given key. See also v_k.

 let v = wlambda::VVal::map();
 v.set_key_str("aaa", wlambda::VVal::Flt(12.2));
 assert_eq!(v.v_sk("aaa"), "12.2");

Quick access of the float at the given idx. See also v_.

 let v = wlambda::VVal::vec();
 v.push(wlambda::VVal::Flt(13.2));
 assert_eq!(v.v_f(0), 13.2);

Quick access of the float at the given key. See also v_k.

 let v = wlambda::VVal::map();
 v.set_key_str("aaa", wlambda::VVal::Flt(12.2));
 assert_eq!(v.v_fk("aaa"), 12.2);

Returns a string representation of the VVal data structure. It handles cyclic data structures fine. The purpose is to return an unambigous represenation of the data structure. That means strings are quoted and VVal::None becomes $n for instance.

If you need strings in pure form, use the s_raw() method.

use wlambda::VVal;

let v = VVal::None;
assert_eq!(v.s(), "$n");

assert_eq!(VVal::new_str("Foo").s(), "\"Foo\"");

Serializes the VVal (non cyclic) structure to a msgpack byte vector.

Creates a VVal structure from a msgpack byte vector.

Serializes the VVal (non cyclic) structure to a JSON string.

Creates a VVal structure from a JSON string.

Trait Implementations

Performs the conversion.

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Deserialize this value from the given Serde deserializer. Read more

Performs the conversion.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.