netidx_bscript/node/
genn.rs

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