zenocore 0.1.0

Zero-dependency core engine for ZenoLang — lexer, parser, executor, and scope for the zeno-rs ecosystem
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
use crate::diagnostic::Diagnostic;
use crate::executor::{Engine, InputMeta, SlotMeta};
use crate::scope::Value;
use std::collections::HashMap;
use std::sync::Arc;
use super::resolve_node_value;

pub fn register(engine: &mut Engine) {
    // ==========================================
    // ARRAY.PUSH
    // ==========================================
    engine.register(
        "array.push",
        Arc::new(|engine, _ctx, node, scope| {
            let mut target_name = String::new();
            let mut items = Vec::new();

            if let Some(ref val) = node.value {
                target_name = val.trim_start_matches('$').to_string();
            }

            for c in &node.children {
                if c.name == "in" || c.name == "list" {
                    if let Some(ref cv) = c.value {
                        target_name = cv.trim_start_matches('$').to_string();
                    }
                }
                if c.name == "val" || c.name == "value" || c.name == "item" {
                    items.push(engine.resolve_shorthand_value(c, scope));
                }
            }

            if target_name.is_empty() {
                return Err(Diagnostic {
                    r#type: "error".to_string(),
                    message: "array.push: target list not specified".to_string(),
                    filename: node.filename.clone(),
                    line: node.line,
                    col: node.col,
                    slot: Some("array.push".to_string()),
                });
            }

            let mut list = match scope.get(&target_name) {
                Some(Value::List(l)) => l.clone(),
                Some(Value::Nil) | None => Vec::new(),
                Some(other) => vec![other],
            };

            list.extend(items);
            scope.set(&target_name, Value::List(list));
            Ok(())
        }),
        SlotMeta {
            description: "Add one or more items to the end of an array.".to_string(),
            example: "array.push: $my_list\n  val: 'New Item'".to_string(),
            inputs: {
                let mut m = HashMap::new();
                m.insert("in".to_string(), InputMeta { description: "Target list".to_string(), required: false, r#type: "string".to_string() });
                m.insert("list".to_string(), InputMeta { description: "Target list".to_string(), required: false, r#type: "string".to_string() });
                m.insert("val".to_string(), InputMeta { description: "Value to push".to_string(), required: false, r#type: "any".to_string() });
                m.insert("value".to_string(), InputMeta { description: "Value to push".to_string(), required: false, r#type: "any".to_string() });
                m.insert("item".to_string(), InputMeta { description: "Value to push".to_string(), required: false, r#type: "any".to_string() });
                m
            },
            required_blocks: Vec::new(),
            value_type: String::new(),
        },
    );

    // ==========================================
    // COLLECTIONS.GET
    // ==========================================
    engine.register(
        "collections.get",
        Arc::new(|engine, _ctx, node, scope| {
            let mut list = Vec::new();
            let mut index = 0;
            let mut target = "item".to_string();

            if node.value.is_some() {
                list = resolve_node_value(engine, node, scope).to_list();
            }

            for c in &node.children {
                if c.name == "in" || c.name == "list" {
                    list = engine.resolve_shorthand_value(c, scope).to_list();
                }
                if c.name == "index" || c.name == "i" {
                    index = engine.resolve_shorthand_value(c, scope).to_int() as usize;
                }
                if c.name == "as" {
                    if let Some(ref cv) = c.value {
                        target = cv.trim_start_matches('$').to_string();
                    }
                }
            }

            if list.is_empty() {
                scope.set(&target, Value::Nil);
                return Ok(());
            }

            if index >= list.len() {
                return Err(Diagnostic {
                    r#type: "error".to_string(),
                    message: "collections.get: index out of bounds".to_string(),
                    filename: node.filename.clone(),
                    line: node.line,
                    col: node.col,
                    slot: Some("collections.get".to_string()),
                });
            }

            scope.set(&target, list[index].clone());
            Ok(())
        }),
        SlotMeta {
            description: "Get item from array at index.".to_string(),
            example: "collections.get: $list\n  index: 0\n  as: $item".to_string(),
            inputs: {
                let mut m = HashMap::new();
                m.insert("in".to_string(), InputMeta { description: "Source list".to_string(), required: false, r#type: "list".to_string() });
                m.insert("list".to_string(), InputMeta { description: "Source list".to_string(), required: false, r#type: "list".to_string() });
                m.insert("index".to_string(), InputMeta { description: "Index".to_string(), required: true, r#type: "int".to_string() });
                m.insert("i".to_string(), InputMeta { description: "Index".to_string(), required: false, r#type: "int".to_string() });
                m.insert("as".to_string(), InputMeta { description: "Target variable".to_string(), required: false, r#type: "any".to_string() });
                m
            },
            required_blocks: Vec::new(),
            value_type: String::new(),
        },
    );

    // ==========================================
    // ARRAY.POP
    // ==========================================
    engine.register(
        "array.pop",
        Arc::new(|_engine, _ctx, node, scope| {
            let mut target_name = String::new();
            let mut dst_name = "popped_item".to_string();

            if let Some(ref val) = node.value {
                target_name = val.trim_start_matches('$').to_string();
            }

            for c in &node.children {
                if c.name == "in" || c.name == "list" {
                    if let Some(ref cv) = c.value {
                        target_name = cv.trim_start_matches('$').to_string();
                    }
                }
                if c.name == "as" {
                    if let Some(ref cv) = c.value {
                        dst_name = cv.trim_start_matches('$').to_string();
                    }
                }
            }

            if target_name.is_empty() {
                return Err(Diagnostic {
                    r#type: "error".to_string(),
                    message: "array.pop: target list not specified".to_string(),
                    filename: node.filename.clone(),
                    line: node.line,
                    col: node.col,
                    slot: Some("array.pop".to_string()),
                });
            }

            let current_val = scope.get(&target_name).unwrap_or(Value::Nil);
            let mut list = current_val.to_list();

            if list.is_empty() {
                scope.set(&dst_name, Value::Nil);
                return Ok(());
            }

            let popped = list.pop().unwrap();
            scope.set(&target_name, Value::List(list));
            scope.set(&dst_name, popped);
            Ok(())
        }),
        SlotMeta {
            description: "Remove and return the last item of an array.".to_string(),
            example: "array.pop: $stack\n  as: $item".to_string(),
            inputs: {
                let mut m = HashMap::new();
                m.insert("in".to_string(), InputMeta { description: "Source list".to_string(), required: false, r#type: "string".to_string() });
                m.insert("list".to_string(), InputMeta { description: "Source list".to_string(), required: false, r#type: "string".to_string() });
                m.insert("as".to_string(), InputMeta { description: "Target variable".to_string(), required: false, r#type: "any".to_string() });
                m
            },
            required_blocks: Vec::new(),
            value_type: String::new(),
        },
    );

    // ==========================================
    // ARRAY.JOIN
    // ==========================================
    engine.register(
        "array.join",
        Arc::new(|engine, _ctx, node, scope| {
            let mut list = Vec::new();
            let mut separator = ",".to_string();
            let mut target = "joined_string".to_string();

            if node.value.is_some() {
                list = resolve_node_value(engine, node, scope).to_list();
            }

            for c in &node.children {
                if c.name == "list" {
                    list = engine.resolve_shorthand_value(c, scope).to_list();
                }
                if c.name == "sep" || c.name == "separator" {
                    separator = engine.resolve_shorthand_value(c, scope).to_string_coerce();
                }
                if c.name == "as" {
                    if let Some(ref cv) = c.value {
                        target = cv.trim_start_matches('$').to_string();
                    }
                }
            }

            let str_list: Vec<String> = list.iter().map(|item| item.to_string_coerce()).collect();
            scope.set(&target, Value::String(str_list.join(&separator)));
            Ok(())
        }),
        SlotMeta {
            description: "Join array elements into a string with a separator.".to_string(),
            example: "array.join: $tags\n  sep: ', '\n  as: $tag_str".to_string(),
            inputs: {
                let mut m = HashMap::new();
                m.insert("list".to_string(), InputMeta { description: "Source list".to_string(), required: false, r#type: "list".to_string() });
                m.insert("sep".to_string(), InputMeta { description: "Separator".to_string(), required: false, r#type: "string".to_string() });
                m.insert("separator".to_string(), InputMeta { description: "Separator".to_string(), required: false, r#type: "string".to_string() });
                m.insert("as".to_string(), InputMeta { description: "Target variable".to_string(), required: false, r#type: "any".to_string() });
                m
            },
            required_blocks: Vec::new(),
            value_type: String::new(),
        },
    );

    // ==========================================
    // MAP.SET
    // ==========================================
    engine.register(
        "map.set",
        Arc::new(|engine, _ctx, node, scope| {
            let mut target_name = String::new();

            if let Some(ref val) = node.value {
                target_name = val.trim_start_matches('$').to_string();
            }

            for c in &node.children {
                if c.name == "map" {
                    if let Some(ref cv) = c.value {
                        target_name = cv.trim_start_matches('$').to_string();
                    }
                }
            }

            if target_name.is_empty() {
                return Err(Diagnostic {
                    r#type: "error".to_string(),
                    message: "map.set: target map not specified".to_string(),
                    filename: node.filename.clone(),
                    line: node.line,
                    col: node.col,
                    slot: Some("map.set".to_string()),
                });
            }

            let mut map_val = match scope.get(&target_name) {
                Some(Value::Map(m)) => m.clone(),
                _ => HashMap::new(),
            };

            let mut explicit_key = String::new();
            let mut explicit_val = Value::Nil;
            let mut has_explicit = false;

            for c in &node.children {
                if c.name == "key" {
                    explicit_key = engine.resolve_shorthand_value(c, scope).to_string_coerce();
                    has_explicit = true;
                } else if c.name == "val" || c.name == "value" {
                    explicit_val = engine.resolve_shorthand_value(c, scope);
                } else if c.name != "map" {
                    map_val.insert(c.name.clone(), engine.resolve_shorthand_value(c, scope));
                }
            }

            if has_explicit && !explicit_key.is_empty() {
                map_val.insert(explicit_key, explicit_val);
            }

            scope.set(&target_name, Value::Map(map_val));
            Ok(())
        }),
        SlotMeta {
            description: "Set values in a map/object.".to_string(),
            example: "map.set: $user\n  age: 30".to_string(),
            inputs: {
                let mut m = HashMap::new();
                m.insert("map".to_string(), InputMeta { description: "Target map".to_string(), required: false, r#type: "string".to_string() });
                m.insert("key".to_string(), InputMeta { description: "Key".to_string(), required: false, r#type: "string".to_string() });
                m.insert("val".to_string(), InputMeta { description: "Value".to_string(), required: false, r#type: "any".to_string() });
                m.insert("value".to_string(), InputMeta { description: "Value to push".to_string(), required: false, r#type: "any".to_string() });
                m.insert("*".to_string(), InputMeta { description: "Dynamic properties".to_string(), required: false, r#type: "any".to_string() });
                m
            },
            required_blocks: Vec::new(),
            value_type: String::new(),
        },
    );

    // ==========================================
    // MAP.KEYS
    // ==========================================
    engine.register(
        "map.keys",
        Arc::new(|engine, _ctx, node, scope| {
            let mut val = Value::Nil;
            let mut target = "keys".to_string();

            if node.value.is_some() {
                val = resolve_node_value(engine, node, scope);
            }

            for c in &node.children {
                if c.name == "map" {
                    val = engine.resolve_shorthand_value(c, scope);
                }
                if c.name == "as" {
                    if let Some(ref cv) = c.value {
                        target = cv.trim_start_matches('$').to_string();
                    }
                }
            }

            let keys_list = match val {
                Value::Map(m) => m.keys().map(|k| Value::String(k.clone())).collect(),
                _ => Vec::new(),
            };

            scope.set(&target, Value::List(keys_list));
            Ok(())
        }),
        SlotMeta {
            description: "Get all keys of a map.".to_string(),
            example: "map.keys: $user\n  as: $fields".to_string(),
            inputs: {
                let mut m = HashMap::new();
                m.insert("map".to_string(), InputMeta { description: "Source map".to_string(), required: false, r#type: "map".to_string() });
                m.insert("as".to_string(), InputMeta { description: "Target variable".to_string(), required: false, r#type: "any".to_string() });
                m
            },
            required_blocks: Vec::new(),
            value_type: String::new(),
        },
    );

    // ==========================================
    // LEN
    // ==========================================
    engine.register(
        "len",
        Arc::new(|engine, _ctx, node, scope| {
            let mut val = Value::Nil;
            let mut target = "len".to_string();

            if node.value.is_some() {
                val = resolve_node_value(engine, node, scope);
            }

            for c in &node.children {
                if c.name == "as" {
                    if let Some(ref cv) = c.value {
                        target = cv.trim_start_matches('$').to_string();
                    }
                }
                if c.name == "in" || c.name == "list" || c.name == "val" || c.name == "value" {
                    val = engine.resolve_shorthand_value(c, scope);
                }
            }

            let length = match val {
                Value::Nil => 0,
                Value::String(s) => s.len() as i64,
                Value::List(l) => l.len() as i64,
                Value::Map(m) => m.len() as i64,
                _ => 0,
            };

            scope.set(&target, Value::Int(length));
            Ok(())
        }),
        SlotMeta {
            description: "Get the length of a string, array, or map.".to_string(),
            example: "len: $my_list\n  as: $count".to_string(),
            inputs: {
                let mut m = HashMap::new();
                m.insert("in".to_string(), InputMeta { description: "Collection".to_string(), required: false, r#type: "any".to_string() });
                m.insert("list".to_string(), InputMeta { description: "Collection".to_string(), required: false, r#type: "any".to_string() });
                m.insert("val".to_string(), InputMeta { description: "Collection".to_string(), required: false, r#type: "any".to_string() });
                m.insert("value".to_string(), InputMeta { description: "Collection".to_string(), required: false, r#type: "any".to_string() });
                m.insert("as".to_string(), InputMeta { description: "Target variable".to_string(), required: false, r#type: "any".to_string() });
                m
            },
            required_blocks: Vec::new(),
            value_type: String::new(),
        },
    );
}