Skip to content

Commit fc61944

Browse files
committed
comments and eval_json
Signed-off-by: Nick Mitchell <[email protected]>
1 parent c3abd8d commit fc61944

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

pdl-live-react/src-tauri/src/pdl/interpreter.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,20 +165,21 @@ impl<'a> Interpreter<'a> {
165165
}
166166
}
167167

168-
fn eval_complex(&self, expr: &Value) -> Result<PdlResult, PdlError> {
168+
/// Traverse the given JSON Value, applying `self.eval()` to the value elements within.
169+
fn eval_json(&self, expr: &Value) -> Result<PdlResult, PdlError> {
169170
match expr {
170171
Value::Null => Ok("".into()),
171172
Value::Bool(b) => Ok(PdlResult::Bool(*b)),
172173
Value::Number(n) => Ok(PdlResult::Number(n.clone())),
173174
Value::String(s) => self.eval(s),
174175
Value::Array(a) => Ok(PdlResult::List(
175176
a.iter()
176-
.map(|v| self.eval_complex(v))
177+
.map(|v| self.eval_json(v))
177178
.collect::<Result<_, _>>()?,
178179
)),
179180
Value::Object(o) => Ok(PdlResult::Dict(
180181
o.iter()
181-
.map(|(k, v)| match self.eval_complex(v) {
182+
.map(|(k, v)| match self.eval_json(v) {
182183
Ok(v) => Ok((k.clone(), v)),
183184
Err(e) => Err(e),
184185
})
@@ -197,7 +198,7 @@ impl<'a> Interpreter<'a> {
197198
s, x
198199
))),
199200
},
200-
ListOrString::List(l) => l.iter().map(|v| self.eval_complex(v)).collect(),
201+
ListOrString::List(l) => l.iter().map(|v| self.eval_json(v)).collect(),
201202
}
202203
}
203204

@@ -225,6 +226,7 @@ impl<'a> Interpreter<'a> {
225226
Ok((trace, messages, PdlBlock::String(msg.clone())))
226227
}
227228

229+
/// If `file_path` is not absolute, join it with self.cwd
228230
fn path_to(&self, file_path: &String) -> PathBuf {
229231
let mut path = self.cwd.clone();
230232
path.push(file_path);
@@ -247,7 +249,7 @@ impl<'a> Interpreter<'a> {
247249
)))
248250
}
249251
} else {
250-
//self.eval_complex(value)
252+
//self.eval_json(value)
251253
Ok(value.clone())
252254
}?;
253255

@@ -302,7 +304,7 @@ impl<'a> Interpreter<'a> {
302304
let res = match self.eval(&block.call)? {
303305
PdlResult::Closure(c) => {
304306
if let Some(args) = &block.args {
305-
match self.eval_complex(args)? {
307+
match self.eval_json(args)? {
306308
PdlResult::Dict(m) => {
307309
self.push_and_extend_scope_with(m, c.scope);
308310
Ok(())
@@ -572,6 +574,7 @@ impl<'a> Interpreter<'a> {
572574
}
573575
}
574576

577+
/// Transform a JSON Value into a PdlResult object
575578
fn resultify(&self, value: &Value) -> PdlResult {
576579
match value {
577580
Value::Null => "".into(),
@@ -589,6 +592,7 @@ impl<'a> Interpreter<'a> {
589592
}
590593
}
591594

595+
/// Run a PdlBlock::Data
592596
async fn run_data(&mut self, block: &DataBlock, _context: Context) -> Interpretation {
593597
if self.debug {
594598
eprintln!("Data raw={:?} {:?}", block.raw, block.data);
@@ -599,7 +603,7 @@ impl<'a> Interpreter<'a> {
599603
let result = self.def(&block.def, &self.resultify(&block.data), &block.parser)?;
600604
Ok((result, vec![], PdlBlock::Data(trace)))
601605
} else {
602-
let result = self.def(&block.def, &self.eval_complex(&block.data)?, &block.parser)?;
606+
let result = self.def(&block.def, &self.eval_json(&block.data)?, &block.parser)?;
603607
trace.data = from_str(to_string(&result)?.as_str())?;
604608
Ok((result, vec![], PdlBlock::Data(trace)))
605609
}
@@ -786,6 +790,7 @@ impl<'a> Interpreter<'a> {
786790
))
787791
}
788792

793+
/// Run a PdlBlock::Array
789794
async fn run_array(&mut self, block: &ArrayBlock, context: Context) -> Interpretation {
790795
let mut result_items = vec![];
791796
let mut all_messages = vec![];
@@ -807,6 +812,7 @@ impl<'a> Interpreter<'a> {
807812
))
808813
}
809814

815+
/// Run a PdlBlock::Message
810816
async fn run_message(&mut self, block: &MessageBlock, context: Context) -> Interpretation {
811817
let (content_result, content_messages, content_trace) =
812818
self.run(&block.content, context).await?;
@@ -865,6 +871,7 @@ pub fn run_sync(program: &PdlBlock, cwd: Option<PathBuf>, debug: bool) -> Interp
865871
.map_err(|err| Box::<dyn Error>::from(err.to_string()))
866872
}
867873

874+
/// Read in a file from disk and parse it as a PDL program
868875
fn parse_file(path: &PathBuf) -> Result<PdlBlock, PdlError> {
869876
from_reader(File::open(path)?)
870877
.map_err(|err| Box::<dyn Error + Send + Sync>::from(err.to_string()))

0 commit comments

Comments
 (0)