graphix_compiler/node/
genn.rs

1use super::{bind::Ref, callsite::CallSite, Constant, Nop, NOP};
2use crate::{
3    expr::{ExprId, ModPath},
4    typ::{FnType, Type},
5    BindId, ExecCtx, Node, Rt, Scope, UserEvent,
6};
7use enumflags2::BitFlags;
8use netidx::publisher::{Typ, Value};
9use poolshark::local::LPooled;
10use std::collections::HashMap;
11
12/// generate a no op with the specific type
13pub fn nop<R: Rt, E: UserEvent>(typ: Type) -> Node<R, E> {
14    Nop::new(typ)
15}
16
17/// bind a variable and return a node referencing it
18pub fn bind<R: Rt, E: UserEvent>(
19    ctx: &mut ExecCtx<R, E>,
20    scope: &ModPath,
21    name: &str,
22    typ: Type,
23    top_id: ExprId,
24) -> (BindId, Node<R, E>) {
25    let id = ctx.env.bind_variable(scope, name, typ.clone()).id;
26    ctx.rt.ref_var(id, top_id);
27    (id, Box::new(Ref { spec: NOP.clone(), typ, id, top_id }))
28}
29
30/// generate a reference to a bind id
31pub fn reference<R: Rt, E: UserEvent>(
32    ctx: &mut ExecCtx<R, E>,
33    id: BindId,
34    typ: Type,
35    top_id: ExprId,
36) -> Node<R, E> {
37    ctx.rt.ref_var(id, top_id);
38    Box::new(Ref { spec: NOP.clone(), typ, id, top_id })
39}
40
41pub fn constant<R: Rt, E: UserEvent>(v: Value) -> Node<R, E> {
42    Box::new(Constant {
43        spec: NOP.clone(),
44        typ: Type::Primitive(Typ::get(&v).into()),
45        value: v,
46    })
47}
48
49/// generate and return an apply node for the given lambda
50pub fn apply<R: Rt, E: UserEvent>(
51    fnode: Node<R, E>,
52    scope: Scope,
53    args: Vec<Node<R, E>>,
54    typ: &FnType,
55    top_id: ExprId,
56) -> Node<R, E> {
57    let ftype = typ.reset_tvars();
58    ftype.alias_tvars(&mut LPooled::take());
59    Box::new(CallSite {
60        spec: NOP.clone(),
61        rtype: ftype.rtype.clone(),
62        ftype: Some(ftype),
63        named_args: HashMap::default(),
64        args,
65        scope,
66        flags: BitFlags::empty(),
67        fnode,
68        function: None,
69        top_id,
70    })
71}