Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_austenite/src/eval/lib/dict.rs

3.2 KiB, 1 run

created by r1870400018:63232, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1// U3 owns this file. Methods on `dictionary`; `insert` and `remove` change a place, so `methods.rs`
2// carries them out. `dictionary(module)` turns a module's bindings into a dictionary.
3
4use crate::diag::DiagnosticKind;
5use crate::eval::args::Args;
6use crate::eval::lib::array;
7use crate::eval::lib::foundations::{
8 finish,
9 mismatch,
10 need,
11 receiver,
12 repr_str,
13 str_of,
14};
15use crate::eval::scope::Scope;
16use crate::eval::value::{
17 Dict,
18 Value,
19};
20use crate::eval::Engine;
21
22use oxedyne_fe2o3_core::prelude::*;
23
24
25native_fns! {
26 pub enum DictFn {
27 Construct => "dictionary",
28 Len => "len",
29 At => "at",
30 Keys => "keys",
31 Values => "values",
32 Pairs => "pairs",
33 Filter => "filter",
34 Map => "map",
35 }
36}
37
38pub fn define(_scope: &mut Scope) {}
39
40pub fn method(name: &str) -> Option<DictFn> {
41 DictFn::ALL.iter().copied().find(|f| f.name() == name && *f != DictFn::Construct)
42}
43
44pub fn call(f: DictFn, engine: &mut Engine, mut args: Args) -> Outcome<Value> {
45 let span = args.span;
46 if f == DictFn::Construct {
47 let v = res!(need(engine, &mut args, "value"));
48 res!(finish(engine, args));
49 return match v {
50 Value::Module(m) => {
51 let mut names: Vec<(&String, &Value, crate::syntax::Span)> =
52 m.scope.map.iter().map(|(k, b)| (k, &b.value, b.span)).collect();
53 // A module's bindings come out in definition order where spans say it, else by name.
54 names.sort_by(|a, b| (a.2.file.0, a.2.start, a.0).cmp(&(b.2.file.0, b.2.start, b.0)));
55 let mut d = Dict::new();
56 for (k, v, _) in names {
57 d.insert(k, v.clone());
58 }
59 Ok(Value::dict(d))
60 }
61 other => Err(mismatch(engine, span, "module", &other)),
62 };
63 }
64 let d = match res!(receiver(&mut args)) {
65 Value::Dict(d) => d,
66 other => return Err(mismatch(engine, span, "dictionary", &other)),
67 };
68 let out = match f {
69 DictFn::Len => Value::Int(d.len() as i64),
70 DictFn::At => {
71 let k = res!(need(engine, &mut args, "key"));
72 let k = res!(str_of(engine, span, k));
73 let default = res!(args.named::<Value>("default"));
74 match (d.get(&k), default) {
75 (Some(v), _) => v.clone(),
76 (None, Some(x)) => x,
77 (None, None) => return Err(engine.error(DiagnosticKind::Type, span, fmt!(
78 "dictionary does not contain key {} and no default value was specified", repr_str(&k)))),
79 }
80 }
81 DictFn::Keys => Value::array(d.keys().map(Value::str).collect()),
82 DictFn::Values => Value::array(d.iter().map(|(_, v)| v.clone()).collect()),
83 DictFn::Pairs => Value::array(d.iter().map(|(k, v)| Value::array(vec![Value::str(k), v.clone()])).collect()),
84 // Both see the value only; the keys stay as they are.
85 DictFn::Filter | DictFn::Map => {
86 let g = res!(need(engine, &mut args, if f == DictFn::Filter { "test" } else { "mapper" }));
87 let g = res!(array::func_of(engine, span, g));
88 let mut out = Dict::new();
89 for (k, v) in d.iter() {
90 let r = res!(array::apply(engine, span, &g, vec![v.clone()]));
91 if f == DictFn::Map {
92 out.insert(k, r);
93 continue;
94 }
95 match r {
96 Value::Bool(true) => out.insert(k, v.clone()),
97 Value::Bool(false) => (),
98 other => return Err(mismatch(engine, span, "boolean", &other)),
99 }
100 }
101 Value::dict(out)
102 }
103 DictFn::Construct => Value::None,
104 };
105 res!(finish(engine, args));
106 Ok(out)
107}