graphix-compiler 0.3.1

A dataflow language for UIs and network programming, compiler
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
use super::{compiler::compile, error::ECHAIN, Nop};
use crate::{
    deref_typ,
    expr::{Expr, ExprId},
    node::lambda::LambdaDef,
    typ::{FnType, Type},
    wrap, Apply, BindId, CFlag, Event, ExecCtx, Node, PrintFlag, Refs, Rt, Scope, Update,
    UserEvent,
};
use anyhow::{bail, Result};
use arcstr::ArcStr;
use enumflags2::BitFlags;
use fxhash::{FxHashMap, FxHashSet};
use netidx::subscriber::Value;
use netidx_value::Typ;
use poolshark::local::LPooled;
use std::{collections::hash_map::Entry, mem};
use triomphe::Arc as TArc;

fn compile_apply_args<R: Rt, E: UserEvent>(
    ctx: &mut ExecCtx<R, E>,
    flags: BitFlags<CFlag>,
    scope: &Scope,
    top_id: ExprId,
    args: &TArc<[(Option<ArcStr>, Expr)]>,
) -> Result<(Vec<Node<R, E>>, FxHashMap<ArcStr, (Option<Node<R, E>>, bool)>)> {
    let mut named: FxHashMap<ArcStr, (Option<Node<R, E>>, bool)> = FxHashMap::default();
    let mut nodes: Vec<Node<R, E>> = vec![];
    for (name, expr) in args.iter() {
        let n = compile(ctx, flags, expr.clone(), scope, top_id)?;
        match name {
            None => nodes.push(n),
            Some(k) => match named.entry(k.clone()) {
                Entry::Occupied(_) => bail!("duplicate named argument {k}"),
                Entry::Vacant(e) => {
                    e.insert((Some(n), false));
                }
            },
        }
    }
    Ok((nodes, named))
}

fn is_arith_error(t: &Type) -> bool {
    t.with_deref(|t| {
        t.map(|t| match t {
            Type::Variant(name, param) => {
                &**name == "ArithError"
                    && param.len() == 1
                    && param[0] == Type::Primitive(Typ::String.into())
            }
            Type::Error(e) => match &**e {
                t @ Type::Variant(_, _) => is_arith_error(t),
                Type::Ref { scope: _, name, params } => {
                    *name == *ECHAIN && params.len() == 1 && is_arith_error(&params[0])
                }
                _ => false,
            },
            _ => false,
        })
        .unwrap_or(false)
    })
}

#[derive(Debug)]
pub(crate) struct CallSite<R: Rt, E: UserEvent> {
    pub(super) spec: TArc<Expr>,
    pub(super) ftype: Option<FnType>,
    pub(super) rtype: Type,
    pub(super) fnode: Node<R, E>,
    pub(super) named_args: FxHashMap<ArcStr, (Option<Node<R, E>>, bool)>,
    pub(super) args: Vec<Node<R, E>>,
    pub(super) function: Option<(Value, Box<dyn Apply<R, E>>)>,
    pub(super) flags: BitFlags<CFlag>,
    pub(super) scope: Scope,
    pub(super) top_id: ExprId,
}

impl<R: Rt, E: UserEvent> CallSite<R, E> {
    pub(crate) fn compile(
        ctx: &mut ExecCtx<R, E>,
        flags: BitFlags<CFlag>,
        spec: Expr,
        scope: &Scope,
        top_id: ExprId,
        args: &TArc<[(Option<ArcStr>, Expr)]>,
        f: &TArc<Expr>,
    ) -> Result<Node<R, E>> {
        let fnode = compile(ctx, flags, (**f).clone(), scope, top_id)?;
        let spec = TArc::new(spec);
        let (args, named_args) = compile_apply_args(ctx, flags, scope, top_id, args)?;
        let site = Self {
            spec,
            ftype: None,
            rtype: Type::empty_tvar(),
            named_args,
            args,
            fnode,
            function: None,
            flags,
            top_id,
            scope: scope.clone(),
        };
        Ok(Box::new(site))
    }

    fn bind(
        &mut self,
        ctx: &mut ExecCtx<R, E>,
        scope: Scope,
        flags: BitFlags<CFlag>,
        fv: Value,
        f: &LambdaDef<R, E>,
        event: &mut Event<E>,
        set: &mut Vec<BindId>,
    ) -> Result<()> {
        let mut flags = flags;
        // we already warned about this
        flags.remove(CFlag::WarnUnhandled);
        macro_rules! compile_default {
            ($i:expr, $f:expr) => {{
                match &$f.argspec[$i].labeled {
                    None | Some(None) => bail!("expected default value"),
                    Some(Some(expr)) => ctx.with_restored($f.env.clone(), |ctx| {
                        let scope = Scope {
                            dynamic: scope.dynamic.clone(),
                            lexical: $f.scope.lexical.clone(),
                        };
                        let n = compile(ctx, flags, expr.clone(), &scope, self.top_id)?;
                        let mut refs = Refs::default();
                        n.refs(&mut refs);
                        refs.with_external_refs(|id| {
                            if let Some(v) = ctx.cached.get(&id) {
                                if let Entry::Vacant(e) = event.variables.entry(id) {
                                    e.insert(v.clone());
                                    set.push(id);
                                }
                            }
                        });
                        Ok::<_, anyhow::Error>(n)
                    })?,
                }
            }};
        }
        let ftype = match &self.function {
            Some((_, f)) => &f.typ(),
            None => match self.ftype.as_ref() {
                Some(ftype) => ftype,
                None => {
                    let ftype = &*f.typ;
                    self.ftype = Some(ftype.clone());
                    for (i, arg) in ftype.args.iter().enumerate() {
                        if let Some((name, default)) = &arg.label {
                            match self.named_args.get_mut(name) {
                                None if !*default => {
                                    bail!("BUG: in bind missing required argument {name}")
                                }
                                None => {
                                    self.args.insert(i, Nop::new(arg.typ.clone()));
                                    self.named_args.insert(name.clone(), (None, true));
                                }
                                Some((n, _)) => {
                                    if let Some(n) = n.take() {
                                        self.args.insert(i, n)
                                    }
                                }
                            }
                        }
                    }
                    ftype
                }
            },
        };
        for arg in ftype.args.iter() {
            if let Some((name, _)) = &arg.label {
                let (n, is_default) = self.named_args.get_mut(name).unwrap();
                if *is_default {
                    let mut n = self.args.remove(0);
                    n.delete(ctx);
                } else {
                    *n = Some(self.args.remove(0));
                }
            }
        }
        let mut labeled: LPooled<FxHashSet<ArcStr>> = LPooled::take();
        for (i, arg) in f.typ.args.iter().enumerate() {
            if let Some((name, _)) = &arg.label {
                labeled.insert(name.clone());
                match self.named_args.entry(name.clone()) {
                    Entry::Occupied(mut e) => match e.get_mut().0.take() {
                        Some(n) => self.args.insert(i, n),
                        None => self.args.insert(i, compile_default!(i, f)),
                    },
                    Entry::Vacant(e) => {
                        e.insert((None, true));
                        self.args.insert(i, compile_default!(i, f))
                    }
                }
            }
        }
        self.named_args.retain(|name, (n, _)| {
            let keep = labeled.contains(name);
            if !keep && let Some(n) = n {
                n.delete(ctx)
            }
            keep
        });
        let rf = (f.init)(&scope, ctx, &mut self.args, self.top_id, false)?;
        self.function = Some((fv, rf));
        Ok(())
    }
}

impl<R: Rt, E: UserEvent> Update<R, E> for CallSite<R, E> {
    fn update(&mut self, ctx: &mut ExecCtx<R, E>, event: &mut Event<E>) -> Option<Value> {
        let mut set: LPooled<Vec<BindId>> = LPooled::take();
        let bound = match (&self.function, self.fnode.update(ctx, event)) {
            (_, None) => false,
            (Some((fv, _)), Some(v)) if fv == &v => false,
            (_, Some(v)) => match v.downcast_ref::<LambdaDef<R, E>>() {
                None => panic!("value {v:?} is not a function"),
                Some(lb) => {
                    let scope = self.scope.clone();
                    self.bind(ctx, scope, self.flags, v.clone(), lb, event, &mut set)
                        .expect("failed to bind to lambda");
                    true
                }
            },
        };
        match &mut self.function {
            None => None,
            Some((_, f)) if !bound => f.update(ctx, &mut self.args, event),
            Some((_, f)) => {
                let init = mem::replace(&mut event.init, true);
                let mut refs = Refs::default();
                f.refs(&mut refs);
                refs.with_external_refs(|id| {
                    if let Entry::Vacant(e) = event.variables.entry(id) {
                        if let Some(v) = ctx.cached.get(&id) {
                            e.insert(v.clone());
                            set.push(id);
                        }
                    }
                });
                let res = f.update(ctx, &mut self.args, event);
                event.init = init;
                for id in set.drain(..) {
                    event.variables.remove(&id);
                }
                res
            }
        }
    }

    fn delete(&mut self, ctx: &mut ExecCtx<R, E>) {
        let Self {
            spec: _,
            rtype: _,
            ftype: _,
            fnode,
            named_args: _,
            args,
            function,
            flags: _,
            top_id: _,
            scope: _,
        } = self;
        if let Some((_, f)) = function {
            f.delete(ctx)
        }
        fnode.delete(ctx);
        for n in args {
            n.delete(ctx)
        }
    }

    fn sleep(&mut self, ctx: &mut ExecCtx<R, E>) {
        let Self {
            spec: _,
            rtype: _,
            ftype: _,
            fnode,
            named_args: _,
            args,
            function,
            flags: _,
            top_id: _,
            scope: _,
        } = self;
        if let Some((_, f)) = function {
            f.sleep(ctx)
        }
        fnode.sleep(ctx);
        for n in args {
            n.sleep(ctx)
        }
    }

    fn typ(&self) -> &Type {
        &self.rtype
    }

    fn spec(&self) -> &Expr {
        &self.spec
    }

    fn typecheck(&mut self, ctx: &mut ExecCtx<R, E>) -> Result<()> {
        wrap!(self.fnode, self.fnode.typecheck(ctx))?;
        let ftype = match self.ftype.as_ref() {
            Some(ftype) => ftype, // already initialized
            None => {
                let ftype = deref_typ!("fn", ctx, self.fnode.typ(),
                    Some(Type::Fn(ftype)) => Ok(ftype.clone())
                )?;
                let ftype = ftype.reset_tvars();
                ftype.alias_tvars(&mut LPooled::take());
                self.ftype = Some(ftype.clone());
                let ftype = self.ftype.as_ref().unwrap();
                let args_len = self.args.len() + self.named_args.len();
                if ftype.args.len() < args_len && ftype.vargs.is_none() {
                    bail!(
                        "too many arguments, expected {}, received {}",
                        ftype.args.len(),
                        args_len
                    )
                }
                let mut labeled: LPooled<FxHashSet<ArcStr>> = LPooled::take();
                for (i, arg) in ftype.args.iter().enumerate() {
                    if let Some((name, default)) = &arg.label {
                        labeled.insert(name.clone());
                        match self.named_args.get_mut(name) {
                            None if !*default => {
                                bail!("missing required argument {name}")
                            }
                            None => {
                                self.args.insert(i, Nop::new(arg.typ.clone()));
                                self.named_args.insert(name.clone(), (None, true));
                            }
                            Some((n, _)) => {
                                if let Some(n) = n.take() {
                                    self.args.insert(i, n)
                                }
                            }
                        }
                    }
                    if i >= self.args.len() {
                        bail!("missing required argument")
                    }
                }
                for name in self.named_args.keys() {
                    if !labeled.contains(name) {
                        bail!("unknown labeled argument {name}")
                    }
                }
                ftype
            }
        };
        for (n, arg) in self.args.iter_mut().zip(ftype.args.iter()) {
            // associate the fntype arg with the arg before typechecking the arg
            arg.typ.contains(&ctx.env, n.typ())?;
            wrap!(n, n.typecheck(ctx))?;
            wrap!(n, arg.typ.check_contains(&ctx.env, n.typ()))?;
        }
        if self.args.len() > ftype.args.len()
            && let Some(typ) = &ftype.vargs
        {
            for n in &mut self.args[ftype.args.len()..] {
                // associate the fntype arg with the arg before typechecking the arg
                typ.contains(&ctx.env, n.typ())?;
                wrap!(n, n.typecheck(ctx))?;
                wrap!(n, typ.check_contains(&ctx.env, n.typ()))?
            }
        }
        for (tv, tc) in ftype.constraints.read().iter() {
            wrap!(self, tc.check_contains(&ctx.env, &Type::TVar(tv.clone())))?;
        }
        if let Some(t) = ftype.throws.with_deref(|t| t.cloned()) {
            match ctx.env.lookup_catch(&self.scope.dynamic) {
                Ok(id) => {
                    if let Some(bind) = ctx.env.by_id.get(&id)
                        && let Type::TVar(tv) = &bind.typ
                    {
                        let tv = tv.read();
                        let mut ty = tv.typ.write();
                        *ty = match &*ty {
                            None => Some(t),
                            Some(inner) => Some(inner.union(&ctx.env, &t)?),
                        };
                    }
                }
                Err(_) if t == Type::Bottom => (), // it doesn't throw any errors
                Err(_) if is_arith_error(&t) => {
                    if self
                        .flags
                        .contains(CFlag::WarnUnhandledArith | CFlag::WarningsAreErrors)
                    {
                        bail!(
                            "ERROR: {} at {} error {} raised from function call {} will not be caught",
                            self.spec.ori, self.spec.pos, t, self.fnode.spec()
                        )
                    }
                    if self.flags.contains(CFlag::WarnUnhandledArith) {
                        eprintln!(
                            "WARNING: {} at {} error {} raised from function call {} will not be caught",
                            self.spec.ori, self.spec.pos, t, self.fnode.spec()
                        )
                    }
                }
                Err(_) => {
                    if self
                        .flags
                        .contains(CFlag::WarnUnhandled | CFlag::WarningsAreErrors)
                    {
                        bail!(
                            "ERROR: {} at {} error {} raised from function call {} will not be caught",
                            self.spec.ori, self.spec.pos, t, self.fnode.spec()
                        )
                    }
                    if self.flags.contains(CFlag::WarnUnhandled) {
                        eprintln!(
                            "WARNING: {} at {} error {} raised from function call {} will not be caught",
                            self.spec.ori, self.spec.pos, t, self.fnode.spec()
                        )
                    }
                }
            }
        }
        wrap!(self.fnode, self.rtype.check_contains(&ctx.env, &ftype.rtype))?;
        Ok(())
    }

    fn refs(&self, refs: &mut Refs) {
        let Self {
            spec: _,
            rtype: _,
            ftype: _,
            fnode,
            named_args: _,
            args,
            function,
            flags: _,
            top_id: _,
            scope: _,
        } = self;
        if let Some((_, fun)) = function {
            fun.refs(refs)
        }
        fnode.refs(refs);
        for n in args {
            n.refs(refs)
        }
    }
}