stof 0.3.24

Stof is a unified data interface and interchange format for creating, sharing, and manipulating data. Stof removes the fragile and cumbersome parts of combining and using data in applications.
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
//
// Copyright 2024 Formata, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

use std::{collections::HashSet, sync::Arc, time::SystemTime};
use bytes::Bytes;
use colored::Colorize;
use serde::{Deserialize, Serialize};
use crate::{bytes::BYTES, lang::SError, text::TEXT, SData, SField, SFunc, SVal, BSTOF, STOF};
use super::{runtime::{DocPermissions, Library, Symbol, SymbolTable}, ArrayLibrary, BlobLibrary, BoolLibrary, CustomTypes, DataLibrary, Format, FunctionLibrary, IntoDataRef, IntoNodeRef, MapLibrary, NumberLibrary, ObjectLibrary, SDataRef, SFormats, SGraph, SLibraries, SNodeRef, SProcesses, SetLibrary, StdLibrary, StringLibrary, TupleLibrary};

#[cfg(not(feature = "wasm"))]
use super::FileSystemLibrary;

#[cfg(not(feature = "wasm"))]
use super::TimeLibrary;

#[cfg(feature = "pkg")]
use crate::pkg::PKG;

#[cfg(feature = "json")]
use crate::json::JSON;
#[cfg(feature = "json")]
use crate::json::NDJSON;

#[cfg(feature = "toml")]
use crate::toml::TOML;

#[cfg(feature = "yaml")]
use crate::yaml::YAML;

#[cfg(feature = "xml")]
use crate::xml::XML;

#[cfg(feature = "urlencoded")]
use crate::urlencoded::URLENC;


/// Stof Document.
/// Holds a Graph, containing the data contained within this document as well as
/// any permissions, libraries, etc... needed for this document to manipulate itself.
#[derive(Clone, Serialize, Deserialize)]
pub struct SDoc {
    pub graph: SGraph,
    pub perms: DocPermissions,
    pub types: CustomTypes,

    /// Formats that this Doc supports.
    /// It is up to the local Doc to provide formats.
    #[serde(skip)]
    pub formats: SFormats,

    /// Libraries that this Doc can call when scripting.
    /// It is up to the local Doc to provide libraries.
    #[serde(skip)]
    pub libraries: SLibraries,
    
    #[serde(skip)]
    pub(crate) processes: SProcesses,

    #[serde(skip)]
    pub(crate) env_compiled_paths: HashSet<String>,
}
impl Default for SDoc {
    fn default() -> Self {
        let mut def_graph = SGraph::default();
        def_graph.insert_root("root");
        Self::new(def_graph)
    }
}
impl SDoc {
    /// New document from a graph.
    pub fn new(graph: SGraph) -> Self {
        let mut doc = Self {
            graph,
            types: Default::default(),
            libraries: Default::default(),
            formats: Default::default(),
            perms: Default::default(),
            processes: SProcesses::new(),
            env_compiled_paths: Default::default(),
        };
        doc.load_std_formats();
        doc.load_std_lib();
        doc
    }

    /// New document from a string import format.
    pub fn src(src: &str, format: &str) -> Result<Self, SError> {
        let mut doc = Self::default();
        doc.string_import("main", format, src, "")?;
        Ok(doc)
    }

    /// New document from a file import.
    pub fn file(path: &str, format: &str) -> Result<Self, SError> {
        let mut doc = Self::default();
        doc.file_import("main", format, path, format, "")?;
        Ok(doc)
    }

    /// New document from bytes.
    pub fn bytes(mut bytes: Bytes, format: &str) -> Result<Self, SError> {
        let mut doc = Self::default();
        doc.header_import("main", format, format, &mut bytes, "")?;
        Ok(doc)
    }

    /// Export this document to a text file at "path" using "format".
    pub fn text_file_out(&mut self, path: &str, format: &str) -> Result<(), SError> {
        if let Ok(out) = self.export_min_string("main", format, None) {
            self.fs_write_string("main", path, &out)?;
            return Ok(());
        }
        Err(SError::filesys("main", &self, "text_file_out", "could not export to a text file"))
    }

    /// Export this document to a binary file at "path" using "format".
    pub fn bin_file_out(&mut self, path: &str, format: &str) -> Result<(), SError> {
        if let Ok(out) = self.export_bytes("main", format, None) {
            self.fs_write_blob("main", path, out.to_vec())?;
            return Ok(());
        }
        Err(SError::filesys("main", &self, "bin_file_out", "could not export to a binary file"))
    }


    /*****************************************************************************
     * Formats.
     *****************************************************************************/
    
    /// Load the Stof standard formats.
    fn load_std_formats(&mut self) {
        self.load_format(Arc::new(TEXT{}));
        self.load_format(Arc::new(BYTES{}));

        // STOF format ".stof" text files
        self.load_format(Arc::new(STOF{}));

        // BSTOF format ".bstof" binary files
        self.load_format(Arc::new(BSTOF{}));

        // PKG format
        #[cfg(feature = "pkg")]
        self.load_format(Arc::new(PKG::default()));

        // JSON format ".json" files and NDJSON (newlines between json)
        #[cfg(feature = "json")]
        self.load_format(Arc::new(JSON{}));
        #[cfg(feature = "json")]
        self.load_format(Arc::new(NDJSON{}));

        // TOML format ".toml" files
        #[cfg(feature = "toml")]
        self.load_format(Arc::new(TOML{}));

        // YAML format ".yaml" files
        #[cfg(feature = "yaml")]
        self.load_format(Arc::new(YAML{}));

        // XML format ".xml" files
        #[cfg(feature = "xml")]
        self.load_format(Arc::new(XML{}));

        // URL encoding "urlencoded" format
        #[cfg(feature = "urlencoded")]
        self.load_format(Arc::new(URLENC{}));
    }

    /// Load a format into this document.
    pub fn load_format(&mut self, format: Arc<dyn Format>) {
        self.formats.insert(format);
    }

    /// Available formats
    pub fn available_formats(&self) -> HashSet<String> {
        self.formats.available()
    }

    /// Content type for a format.
    pub fn format_content_type(&self, format: &str) -> Option<String> {
        self.formats.content_type(format)
    }

    /// Header import (content type with bytes).
    pub fn header_import(&mut self, pid: &str, format: &str, content_type: &str, bytes: &mut Bytes, as_name: &str) -> Result<(), SError> {
        self.formats.clone().header_import(format, pid, self, content_type, bytes, as_name)
    }

    /// String import.
    pub fn string_import(&mut self, pid: &str, format: &str, src: &str, as_name: &str) -> Result<(), SError> {
        self.formats.clone().string_import(format, pid, self, src, as_name)
    }

    /// File import.
    /// Stof Syntax: 'import <format> "<path>.<extension>" as <as_name>;'
    /// If <format> isn't supplied, "format" will be "extension".
    /// If <as_name> isn't supplied, the data should be imported into the current doc scope (or main root).
    pub fn file_import(&mut self, pid: &str, format: &str, full_path: &str, extension: &str, as_name: &str) -> Result<(), SError> {
        self.formats.clone().file_import(format, pid, self, full_path, extension, as_name)
    }

    /// Export document string.i
    pub fn export_string(&self, pid: &str, format: &str, node: Option<&SNodeRef>) -> Result<String, SError> {
        self.formats.export_string(format, pid, self, node)
    }

    /// Export document min string.
    pub fn export_min_string(&self, pid: &str, format: &str, node: Option<&SNodeRef>) -> Result<String, SError> {
        self.formats.export_min_string(format, pid, self, node)
    }

    /// Export document bytes.
    pub fn export_bytes(&self, pid: &str, format: &str, node: Option<&SNodeRef>) -> Result<Bytes, SError> {
        self.formats.export_bytes(format, pid, self, node)
    }


    /*****************************************************************************
     * Libraries.
     *****************************************************************************/

    /// Load the Stof standard library.
    fn load_std_lib(&mut self) {
        #[cfg(not(feature = "wasm"))]
        self.load_lib(Arc::new(FileSystemLibrary::default()));

        #[cfg(not(feature = "wasm"))]
        self.load_lib(Arc::new(TimeLibrary::default()));

        self.load_lib(Arc::new(StdLibrary::default()));
        self.load_lib(Arc::new(ObjectLibrary::default()));
        self.load_lib(Arc::new(ArrayLibrary::default()));
        self.load_lib(Arc::new(MapLibrary::default()));
        self.load_lib(Arc::new(SetLibrary::default()));
        self.load_lib(Arc::new(FunctionLibrary::default()));
        self.load_lib(Arc::new(NumberLibrary::default()));
        self.load_lib(Arc::new(StringLibrary::default()));
        self.load_lib(Arc::new(TupleLibrary::default()));
        self.load_lib(Arc::new(BoolLibrary::default()));
        self.load_lib(Arc::new(BlobLibrary::default()));
        self.load_lib(Arc::new(DataLibrary::default()));
    }
    
    /// Load a library into this document.
    pub fn load_lib(&mut self, library: Arc<dyn Library>) {
        self.libraries.insert(library);
    }

    /// Get a library in this doc.
    pub fn library(&self, lib: &str) -> Option<Arc<dyn Library>> {
        if let Some(library) = self.libraries.get(lib) {
            return Some(library.clone());
        }
        None
    }

    /// Available libraries
    pub fn available_libraries(&self) -> HashSet<String> {
        self.libraries.available()
    }

    /// Write a string to a file using the fs library.
    pub fn fs_write_string(&mut self, pid: &str, path: &str, contents: &str) -> Result<(), SError> {
        if let Some(fs) = self.library("fs") {
            fs.call(pid, self, "write", &mut vec![SVal::String(path.to_owned()), SVal::String(contents.to_owned())])?;
            return Ok(());
        }
        Err(SError::filesys("main", &self, "fs_write_string", "no FileSystem 'fs' library loaded into this document"))
    }

    /// Write a blob to a file using the fs library.
    pub fn fs_write_blob(&mut self, pid: &str, path: &str, contents: Vec<u8>) -> Result<(), SError> {
        if let Some(fs) = self.library("fs") {
            fs.call(pid, self, "write_blob", &mut vec![SVal::String(path.to_owned()), SVal::Blob(contents)])?;
            return Ok(());
        }
        Err(SError::filesys("main", &self, "fs_write_blob", "no FileSystem 'fs' library loaded into this document"))
    }

    /// Read a file to a string using the fs library.
    pub fn fs_read_string(&mut self, pid: &str, path: &str) -> Result<String, SError> {
        if let Some(fs) = self.library("fs") {
            let res = fs.call(pid, self, "read", &mut vec![SVal::String(path.to_owned())])?;
            return Ok(res.owned_to_string());
        }
        Err(SError::filesys("main", &self, "fs_read_string", "no FileSystem 'fs' library loaded into this document"))
    }

    /// Read a file to a blob using the fs library.
    pub fn fs_read_blob(&mut self, pid: &str, path: &str) -> Result<Bytes, SError> {
        if let Some(fs) = self.library("fs") {
            let res = fs.call(pid, self, "read_blob", &mut vec![SVal::String(path.to_owned())])?;
            match res {
                SVal::Blob(blob) => {
                    return Ok(Bytes::from(blob));
                },
                _ => {}
            }
        }
        Err(SError::filesys("main", &self, "fs_read_blob", "no FileSystem 'fs' library loaded into this document"))
    }


    /*****************************************************************************
     * General data interface.
     *****************************************************************************/
    
    /// Get a value from this document by path.
    /// If the path points to a field, the value will be retrieved.
    /// If the path points to a function, it will be called.
    pub fn get(&mut self, path: &str, start: Option<&SNodeRef>) -> Option<SVal> {
        if let Some(field) = self.field(path, start) {
            return Some(field.value.clone());
        } else if let Some(func_ref) = self.func(path, start) {
            let mut parameters = Vec::new();
            if let Some(func) = SData::get::<SFunc>(&self.graph, &func_ref) {
                if let Some(params) = func.attributes.get("get") {
                    parameters.push(params.clone());
                }
            }
            if let Ok(res) = SFunc::call(&func_ref, "main", self, parameters, true) {
                self.clean("main");
                return Some(res);
            }
            self.clean("main");
        }
        None
    }


    /*****************************************************************************
     * Field interface.
     *****************************************************************************/
    
    /// Find a field in this document with a path.
    /// Path is dot '.' separated.
    pub fn field(&self, path: &str, start: Option<&SNodeRef>) -> Option<&SField> {
        SField::field(&self.graph, path, '.', start)
    }


    /*****************************************************************************
     * Function interface.
     *****************************************************************************/

    /// Run some Stof in a file.
    /// Will run all #[main] functions.
    /// Set 'throw' to true if you want this function to panic if tests fail.
    pub fn run_file(file: &str, throw: bool) {
        let doc_res = Self::file(file, "stof");
        let mut doc;
        match doc_res {
            Ok(dr) => doc = dr,
            Err(err) => {
                let message = format!("{}: {} - {}", "parse error".red(), err.error_type.to_string().blue(), err.message.dimmed());
                if throw {
                    panic!("{message}");
                } else {
                    println!("{message}");
                    return;
                }
            },
        }

        let res = doc.run(None, None);
        match res {
            Ok(_) => {
                // Don't do anything here
            },
            Err(err) => {
                if throw {
                    panic!("{err}");
                } else {
                    println!("{err}");
                }
            }
        }
    }

    /// Run the main functions on a node or within this document.
    /// Main functions are denoted with an #[main] attribute in the text format. This is the default attribute to run.
    pub fn run(&mut self, context: Option<&SNodeRef>, attribute: Option<String>) -> Result<(), String> {
        let mut search_attr = String::from("main");
        if let Some(att) = attribute {
            search_attr = att;
        }

        let functions;
        if context.is_some() {
            functions = SFunc::recursive_func_refs(&self.graph, context.unwrap());
        } else {
            functions = SFunc::all_funcs(&self.graph);
        }
        let mut errors = Vec::new();
        for func_ref in functions {
            if let Some(func) = SData::get::<SFunc>(&self.graph, &func_ref).cloned() {
                if let Some(attr_val) = func.attributes.get(&search_attr) {
                    let result;
                    if attr_val.is_empty() {
                        result = SFunc::call_internal(&func_ref, "main", self, vec![], true, &func.params, &func.statements, &func.rtype);
                    } else {
                        result = SFunc::call_internal(&func_ref, "main", self, vec![attr_val.clone()], true, &func.params, &func.statements, &func.rtype);
                    }
                    self.clean("main");
                    match result {
                        Ok(_) => {
                            // Nada... keep going!
                        },
                        Err(error) => {
                            let func_nodes = func_ref.nodes(&self.graph);
                            let func_path;
                            if func_nodes.len() > 0 {
                                func_path = func_nodes.first().unwrap().path(&self.graph);
                            } else {
                                func_path = String::from("<unknown>");
                            }

                            errors.push(format!("{} {} ...\n{}", func_path.italic().dimmed(), func.name.blue(), error.to_string(&self.graph)));
                        },
                    }
                }
            }
        }
        if errors.len() > 0 {
            let mut error = String::default();
            for err in errors {
                error.push_str(&format!("\n{} @ {}\n", "error".bold().red(), err));
            }
            Err(error)
        } else {
            Ok(())
        }
    }
    
    /// Find a function in this document with a path.
    /// Path is dot '.' separated.
    pub fn func(&self, path: &str, start: Option<&SNodeRef>) -> Option<SDataRef> {
        SFunc::func_ref(&self.graph, path, '.', start)
    }

    /// Call a function in this document with a path.
    pub fn call_func(&mut self, path: &str, start: Option<&SNodeRef>, params: Vec<SVal>) -> Result<SVal, SError> {
        if let Some(func_ref) = SFunc::func_ref(&self.graph, path, '.', start) {
            let res = SFunc::call(&func_ref, "main", self, params, true);
            self.clean("main");
            return res;
        }
        Err(SError::call("main", &self, &format!("did not find a function at the path '{}' to call", path)))
    }

    /// Test some Stof in a file.
    /// Will run all #[test] functions.
    /// Set 'throw' to true if you want this function to panic if tests fail.
    pub fn test_file(file: &str, throw: bool) {
        let compile_start = SystemTime::now();
        let doc_res = Self::file(file, "stof");
        let dur = (compile_start.elapsed().unwrap().as_secs_f32() * 100.0).round() / 100.0;
        println!("{} {}s", "stof compiled in".dimmed(), dur);
        let mut doc;
        match doc_res {
            Ok(dr) => doc = dr,
            Err(err) => {
                let message = format!("{}: {} - {}", "parse error".red(), err.error_type.to_string().blue(), err.message.dimmed());
                if throw {
                    panic!("{message}");
                } else {
                    println!("{message}");
                    return;
                }
            },
        }

        let res = doc.run_tests(throw, None);
        match res {
            Ok(res) => {
                println!("{res}");
            },
            Err(err) => {
                panic!("{err}");
            }
        }
    }

    /// Test some Stof.
    /// Will run all #[test] functions in 'stof'.
    /// Set 'throw' to true if you want this function to panic if tests fail.
    pub fn test(stof: &str, throw: bool, context: Option<&SNodeRef>) {
        let doc_res = Self::src(stof, "stof");
        let mut doc;
        match doc_res {
            Ok(dr) => doc = dr,
            Err(err) => {
                let message = format!("{}: {} - {}", "parse error".red(), err.error_type.to_string().blue(), err.message.dimmed());
                if throw {
                    panic!("{message}");
                } else {
                    println!("{message}");
                    return;
                }
            },
        }

        let res = doc.run_tests(throw, context);
        match res {
            Ok(res) => {
                println!("{res}");
            },
            Err(err) => {
                panic!("{err}");
            }
        }
    }

    /// Run the test functions on a node or within this document, throwing an error if any fail.
    /// Test functions are denoted with a #[test(<result_expr_eq>)] attribute.
    pub fn run_tests(&mut self, throw: bool, context: Option<&SNodeRef>) -> Result<String, String> {
        let mut functions;
        if context.is_some() {
            functions = SFunc::recursive_func_refs(&self.graph, context.unwrap());
        } else {
            functions = SFunc::all_funcs(&self.graph);
        }
        functions.retain(|f| {
            if let Some(func) = SData::get::<SFunc>(&self.graph, f) {
                func.attributes.contains_key("test")
            } else {
                false
            }
        });
        let mut functions: Vec<SDataRef> = functions.into_iter().collect();
        functions.sort_by(|a, b| {
            a.first_path(&self.graph).cmp(&b.first_path(&self.graph))
        });

        let total = functions.len();
        println!("{} {} {}", "running".bold(), total, "Stof tests".bold());
        let mut failures = Vec::new();
        let mut profiles = Vec::new();
        let start = SystemTime::now();
        for func_ref in functions {
            if let Some(func) = SData::get::<SFunc>(&self.graph, &func_ref).cloned() {
                if let Some(res_test_val) = func.attributes.get("test") {
                    let silent = func.attributes.contains_key("silent");
                    let mut result = SFunc::call_internal(&func_ref, "main", self, vec![], true, &func.params, &func.statements, &func.rtype);
                    self.clean("main");

                    let func_nodes = func_ref.nodes(&self.graph);
                    let func_path;
                    if func_nodes.len() > 0 {
                        func_path = func_nodes.first().unwrap().path(&self.graph);
                    } else {
                        func_path = String::from("<unknown>");
                    }

                    if let Some(error_val) = func.attributes.get("errors") {
                        if result.is_err() {
                            result = Ok(error_val.clone());
                        } else {
                            result = Err(SError::custom("main", &self, "TestError", "expected function to throw an error"));
                        }
                    }

                    match result {
                        Ok(res_val) => {
                            let name = func.name.clone();
                            if !res_test_val.is_empty() && &res_val != res_test_val {
                                if !silent {
                                    println!("{} {} {} ... {}", "test".purple(), func_path.italic().dimmed(), name.blue(), "failed".bold().red());
                                }

                                let err_str = format!("{:?} does not equal {:?}", res_val, res_test_val);
                                failures.push((func, format!("\t{}: {} at {}: {}", "failed".bold().red(), name.blue(), func_path.italic().dimmed(), err_str.bold())));
                            } else {
                                if !silent {
                                    println!("{} {} {} ... {}", "test".purple(), func_path.italic().dimmed(), name.blue(), "ok".bold().green());
                                }
                                
                                // This is a successful running of the test! Now check if we should profile the function
                                if let Some(profile_val) = func.attributes.get("profile") {
                                    if profile_val.is_empty() || profile_val.truthy() {
                                        let mut iterations = 100;
                                        match profile_val {
                                            SVal::Number(num) => {
                                                iterations = num.int() as u128;
                                            },
                                            _ => {}
                                        }

                                        let profile_start = SystemTime::now();
                                        for _ in 0..iterations {
                                            let _ = SFunc::call_internal(&func_ref, "main", self, vec![], true, &func.params, &func.statements, &func.rtype);
                                            self.clean("main");
                                        }
                                        let total_duration = profile_start.elapsed().unwrap();
                                        let total_ns = total_duration.as_nanos();
                                        let each_ns = total_ns / iterations;
                                        
                                        let dur = (total_duration.as_secs_f32() * 100.0).round() / 100.0;
                                        profiles.push(format!("\t{} {} ... {} iterations; {}s ({}ms); {}ns per call", func_path.italic().dimmed(), name.blue(), iterations, dur, total_duration.as_millis(), each_ns));
                                    }
                                }
                            }
                        },
                        Err(err) => {
                            let name = func.name.clone();
                            if !silent {
                                println!("{} {} {} ... {}", "test".purple(), func_path.italic().dimmed(), name.blue(), "failed".bold().red());
                            }

                            let err_str = err.to_string(&self.graph);
                            failures.push((func, format!("{}: {} at {} ...\n{}", "failed".bold().red(), name.blue(), func_path.italic().dimmed(), err_str.bold())));
                        }
                    }
                }
            }
        }

        let duration = start.elapsed().unwrap();
        let mut output = "\n".to_string();
        let mut result = "ok".bold().green();
        if failures.len() > 0 {
            result = "failed".bold().red();
            output.push_str(&format!("{} failures:\n", failures.len()));
            for failure in &failures {
                output.push_str(&format!("{}\n\n", failure.1));
            }
            output.push('\n');
        }
        if profiles.len() > 0 {
            output.push_str(&format!("{} profiles:\n", profiles.len()));
            for profile in &profiles {
                output.push_str(&format!("{}\n", profile));
            }
            output.push('\n');
        }
        let passed = total - failures.len();
        let dur = (duration.as_secs_f32() * 100.0).round() / 100.0;
        output.push_str(&format!("test result: {}. {} passed; {} failed; finished in {}s", result, passed, failures.len(), dur));

        if throw && failures.len() > 0 {
            return Err(output);
        }
        return Ok(output);
    }


    /*****************************************************************************
     * Stof Parser/Runtime Interface.
     *****************************************************************************/

    /// Self pointer.
    pub(crate) fn self_ptr(&self, pid: &str) -> Option<SNodeRef> {
        if let Some(process) = self.processes.get(pid) {
            return process.self_ptr();
        }
        None
    }

    /// Push a node ref to the self stack of a process.
    pub(crate) fn push_self(&mut self, pid: &str, node: impl IntoNodeRef) {
        if let Some(process) = self.processes.get_mut(pid) {
            process.self_stack.push(node.node_ref());
        }
    }

    /// Pop a node ref from the self stack of a process.
    pub(crate) fn pop_self(&mut self, pid: &str) -> Option<SNodeRef> {
        if let Some(process) = self.processes.get_mut(pid) {
            process.self_stack.pop()
        } else {
            None
        }
    }

    /// New table.
    /// Returns the current table, replacing it with a new one.
    /// This happens for function calls.
    pub(crate) fn new_table(&mut self, pid: &str) -> SymbolTable {
        if let Some(processes) = self.processes.get_mut(pid) {
            return processes.new_table();
        }
        SymbolTable::default()
    }

    /// Push a new scope to the symbol table.
    pub(crate) fn new_scope(&mut self, pid: &str) {
        if let Some(process) = self.processes.get_mut(pid) {
            process.table.new_scope();
        }
    }

    /// Has a variable with this name in the current scope of the symbol table?
    pub(crate) fn has_var_with_name_in_current(&mut self, pid: &str, name: &str) -> bool {
        if let Some(process) = self.processes.get_mut(pid) {
            process.table.has_in_current(name)
        } else {
            false
        }
    }

    /// End a scope in the table.
    pub(crate) fn end_scope(&mut self, pid: &str) {
        if let Some(process) = self.processes.get_mut(pid) {
            process.table.end_scope();
        }
    }

    /// Set table.
    pub(crate) fn set_table(&mut self, pid: &str, table: SymbolTable) {
        if let Some(process) = self.processes.get_mut(pid) {
            process.set_table(table);
        }
    }

    /// Push a function reference to the call stack.
    pub(crate) fn push_call_stack(&mut self, pid: &str, dref: impl IntoDataRef) {
        if let Some(process) = self.processes.get_mut(pid) {
            process.push_call_stack(dref);
        }
    }

    /// Pop a function reference from the call stack.
    pub(crate) fn pop_call_stack(&mut self, pid: &str) {
        if let Some(process) = self.processes.get_mut(pid) {
            process.pop_call_stack();
        }
    }

    /// Add a variable to the current scope.
    pub(crate) fn add_variable<T>(&mut self, pid: &str, name: &str, value: T) where T: Into<SVal> {
        if let Some(process) = self.processes.get_mut(pid) {
            process.add_variable(name, value);
        }
    }

    /// Set a variable.
    /// Will not add the variable if not already present.
    /// Sets current scope or above variables!
    pub(crate) fn set_variable<T>(&mut self, pid: &str, name: &str, value: T) -> bool where T: Into<SVal> {
        if let Some(process) = self.processes.get_mut(pid) {
            process.set_variable(name, value)
        } else {
            false
        }
    }

    /// Drop a symbol from the current scope.
    pub(crate) fn drop(&mut self, pid: &str, name: &str) -> Option<Symbol> {
        if let Some(processes) = self.processes.get_mut(pid) {
            processes.drop(name)
        } else {
            None
        }
    }

    /// Get a symbol from the current scope or above.
    pub(crate) fn get_symbol(&mut self, pid: &str, name: &str) -> Option<&Symbol> {
        if let Some(process) = self.processes.get_mut(pid) {
            process.get_symbol(name)
        } else {
            None
        }
    }

    /// Push a value onto the stack.
    pub(crate) fn push<T>(&mut self, pid: &str, value: T) where T: Into<SVal> {
        if let Some(process) = self.processes.get_mut(pid) {
            process.push(value);
        }
    }

    /// Pop a value from the stack.
    pub(crate) fn pop(&mut self, pid: &str) -> Option<SVal> {
        if let Some(process) = self.processes.get_mut(pid) {
            process.pop()
        } else {
            None
        }
    }

    /// Clean for scripting.
    pub(crate) fn clean(&mut self, pid: &str) {
        if let Some(processes) = self.processes.get_mut(pid) {
            processes.clean();
        }
    }

    /// Bubble control flow?
    pub(crate) fn bubble_control_flow(&self, pid: &str) -> bool {
        if let Some(process) = self.processes.get(pid) {
            process.bubble_control_flow > 0
        } else {
            false
        }
    }

    /// Increment bubble control flow.
    pub(crate) fn inc_bubble_control(&mut self, pid: &str) {
        if let Some(process) = self.processes.get_mut(pid) {
            process.bubble_control_flow += 1;
        }
    }

    /// Deincrement bubble control flow.
    pub(crate) fn dinc_bubble_control(&mut self, pid: &str) {
        if let Some(process) = self.processes.get_mut(pid) {
            process.bubble_control_flow -= 1;
        }
    }
}