@@ -25,7 +25,7 @@ use serde_json::{from_str, to_string, Value};
2525use serde_norway:: { from_reader, from_str as from_yaml_str} ;
2626
2727use crate :: pdl:: ast:: {
28- ArrayBlock , CallBlock , Closure , FunctionBlock , IfBlock , IncludeBlock , ListOrString ,
28+ ArrayBlock , CallBlock , Closure , DataBlock , FunctionBlock , IfBlock , IncludeBlock , ListOrString ,
2929 MessageBlock , ModelBlock , ObjectBlock , PdlBlock , PdlParser , PdlResult , PdlUsage ,
3030 PythonCodeBlock , ReadBlock , RepeatBlock , Role , Scope , SequencingBlock , StringOrBoolean ,
3131} ;
@@ -102,6 +102,7 @@ impl<'a> Interpreter<'a> {
102102 PdlBlock :: If ( block) => self . run_if ( block, context) . await ,
103103 PdlBlock :: Include ( block) => self . run_include ( block, context) . await ,
104104 PdlBlock :: Model ( block) => self . run_model ( block, context) . await ,
105+ PdlBlock :: Data ( block) => self . run_data ( block, context) . await ,
105106 PdlBlock :: Object ( block) => self . run_object ( block, context) . await ,
106107 PdlBlock :: PythonCode ( block) => self . run_python_code ( block, context) . await ,
107108 PdlBlock :: Read ( block) => self . run_read ( block, context) . await ,
@@ -183,7 +184,6 @@ impl<'a> Interpreter<'a> {
183184 } )
184185 . collect :: < Result < _ , _ > > ( ) ?,
185186 ) ) ,
186- // v => Ok(v.clone()),
187187 }
188188 }
189189
@@ -252,9 +252,6 @@ impl<'a> Interpreter<'a> {
252252 } ?;
253253
254254 if let Some ( def) = & variable {
255- if self . debug {
256- eprintln ! ( "def {def} -> {:?}" , value) ;
257- }
258255 if let Some ( scope) = self . scope . last_mut ( ) {
259256 if self . debug {
260257 eprintln ! ( "Def {} -> {}" , def, result) ;
@@ -575,6 +572,39 @@ impl<'a> Interpreter<'a> {
575572 }
576573 }
577574
575+ fn resultify ( & self , value : & Value ) -> PdlResult {
576+ match value {
577+ Value :: Null => "" . into ( ) ,
578+ Value :: Bool ( b) => b. into ( ) ,
579+ Value :: Number ( n) => n. clone ( ) . into ( ) ,
580+ Value :: String ( s) => s. clone ( ) . into ( ) ,
581+ Value :: Array ( a) => {
582+ PdlResult :: List ( a. iter ( ) . map ( |v| self . resultify ( v) ) . collect :: < Vec < _ > > ( ) )
583+ }
584+ Value :: Object ( m) => PdlResult :: Dict (
585+ m. iter ( )
586+ . map ( |( k, v) | ( k. clone ( ) , self . resultify ( v) ) )
587+ . collect :: < HashMap < _ , _ > > ( ) ,
588+ ) ,
589+ }
590+ }
591+
592+ async fn run_data ( & mut self , block : & DataBlock , _context : Context ) -> Interpretation {
593+ if self . debug {
594+ eprintln ! ( "Data raw={:?} {:?}" , block. raw, block. data) ;
595+ }
596+
597+ let mut trace = block. clone ( ) ;
598+ if let Some ( true ) = block. raw {
599+ let result = self . def ( & block. def , & self . resultify ( & block. data ) , & block. parser ) ?;
600+ Ok ( ( result, vec ! [ ] , PdlBlock :: Data ( trace) ) )
601+ } else {
602+ let result = self . def ( & block. def , & self . eval_complex ( & block. data ) ?, & block. parser ) ?;
603+ trace. data = from_str ( to_string ( & result) ?. as_str ( ) ) ?;
604+ Ok ( ( result, vec ! [ ] , PdlBlock :: Data ( trace) ) )
605+ }
606+ }
607+
578608 async fn run_object ( & mut self , block : & ObjectBlock , context : Context ) -> Interpretation {
579609 if self . debug {
580610 eprintln ! ( "Object {:?}" , block. object) ;
@@ -583,6 +613,7 @@ impl<'a> Interpreter<'a> {
583613 let mut messages = vec ! [ ] ;
584614 let mut result_map = HashMap :: new ( ) ;
585615 let mut trace_map = HashMap :: new ( ) ;
616+
586617 let mut iter = block. object . iter ( ) ;
587618 while let Some ( ( k, v) ) = iter. next ( ) {
588619 let ( this_result, this_messages, this_trace) =
@@ -660,8 +691,9 @@ impl<'a> Interpreter<'a> {
660691 }
661692
662693 fn push_and_extend_scope ( & mut self , scope : HashMap < String , PdlResult > ) {
663- let cur_scope = self . scope . last ( ) . unwrap_or ( & HashMap :: new ( ) ) . clone ( ) ;
664- self . scope . push ( scope) ;
694+ let mut new_scope = self . scope . last ( ) . unwrap_or ( & HashMap :: new ( ) ) . clone ( ) ;
695+ new_scope. extend ( scope) ;
696+ self . scope . push ( new_scope) ;
665697 }
666698
667699 fn push_and_extend_scope_with (
@@ -679,28 +711,22 @@ impl<'a> Interpreter<'a> {
679711
680712 async fn process_defs (
681713 & mut self ,
682- map : & Option < HashMap < String , PdlBlock > > ,
714+ defs : & Option < HashMap < String , PdlBlock > > ,
683715 ) -> Result < ( ) , PdlError > {
684- let cur_scope = self . scope . last ( ) . unwrap_or ( & HashMap :: new ( ) ) . clone ( ) ;
685- let new_scope = match map {
686- Some ( defs) => {
687- // this is all non-optimal
688- let mut scope: Scope = HashMap :: from ( cur_scope) ;
689- let mut iter = defs. iter ( ) ;
690- while let Some ( ( var, def) ) = iter. next ( ) {
691- let ( result, _, _) = self . run_quiet ( def, vec ! [ ] ) . await ?;
692- scope. insert (
693- var. clone ( ) ,
694- result,
695- //from_str(to_string(&block).unwrap().as_str()).unwrap(),
696- ) ;
697- }
698- scope
716+ let mut new_scope: Scope = HashMap :: new ( ) ;
717+ if let Some ( cur_scope) = self . scope . last ( ) {
718+ new_scope. extend ( cur_scope. clone ( ) ) ;
719+ }
720+ self . scope . push ( new_scope) ;
721+
722+ if let Some ( defs) = defs {
723+ let mut iter = defs. iter ( ) ;
724+ while let Some ( ( var, def) ) = iter. next ( ) {
725+ let ( result, _, _) = self . run_quiet ( def, vec ! [ ] ) . await ?;
726+ let _ = self . def ( & Some ( var. clone ( ) ) , & result, & None ) ;
699727 }
700- None => cur_scope,
701- } ;
728+ }
702729
703- self . scope . push ( new_scope) ;
704730 Ok ( ( ) )
705731 }
706732
@@ -737,6 +763,7 @@ impl<'a> Interpreter<'a> {
737763 output_messages. extend ( this_messages) ;
738764 output_blocks. push ( trace) ;
739765 }
766+
740767 self . scope . pop ( ) ;
741768
742769 let trace = block. with_items ( output_blocks) ;
0 commit comments