Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ waymark-prometheus-exporter-bringup = { path = "crates/lib/prometheus-exporter-b
waymark-proto = { path = "crates/lib/proto" }
waymark-runloop = { path = "crates/lib/runloop" }
waymark-runner = { path = "crates/lib/runner" }
waymark-runner-eval-core = { path = "crates/lib/runner-eval-core" }
waymark-runner-execution-core = { path = "crates/lib/runner-execution-core" }
waymark-runner-executor-core = { path = "crates/lib/runner-executor-core" }
waymark-runner-expr = { path = "crates/lib/runner-expr" }
waymark-runner-expr-eval = { path = "crates/lib/runner-expr-eval" }
waymark-runner-expr-fmt = { path = "crates/lib/runner-expr-fmt" }
waymark-runner-pure-expr-eval = { path = "crates/lib/runner-pure-expr-eval" }
waymark-runner-retry-policy = { path = "crates/lib/runner-retry-policy" }
waymark-runner-state = { path = "crates/lib/runner-state" }
waymark-scheduler-backend = { path = "crates/lib/scheduler-backend" }
Expand Down
13 changes: 13 additions & 0 deletions crates/lib/runner-eval-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "waymark-runner-eval-core"
edition = "2024"
version.workspace = true
publish.workspace = true

[dependencies]
waymark-ir-conversions = { workspace = true }
waymark-proto = { workspace = true }
waymark-runner-expr = { workspace = true }

serde_json = { workspace = true }
thiserror = { workspace = true }
71 changes: 71 additions & 0 deletions crates/lib/runner-eval-core/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#[derive(Debug, thiserror::Error)]
pub enum BinaryOpError {
#[error("missing left")]
LeftMissing,

#[error("missing right")]
RightMissing,
}

#[derive(Debug, thiserror::Error)]
pub enum UnaryOpError {
#[error("missing operand")]
Missing,
}

#[derive(Debug, thiserror::Error)]
pub enum DictEntryError {
#[error("missing key")]
MissingKey,

#[error("missing value")]
MissingValue,
}

#[derive(Debug, thiserror::Error)]
pub enum IndexAccessError {
#[error("missing object")]
MissingObject,

#[error("missing index")]
MissingIndex,
}

#[derive(Debug, thiserror::Error)]
pub enum DotAccessError {
#[error("missing object")]
MissingObject,
}

#[derive(Debug, thiserror::Error)]
pub enum SpreadError {
#[error("collection missing")]
CollectionMissing,

#[error("action missing")]
ActionMissing,
}

#[derive(Debug, thiserror::Error)]
pub enum ExprToValueError<QueueActionCall> {
#[error("queue action call: {0}")]
QueueActionCall(#[source] QueueActionCall),

#[error("binary op: {0}")]
BinaryOp(#[from] BinaryOpError),

#[error("unary op: {0}")]
UnaryOp(#[from] UnaryOpError),

#[error("dict entry: {0}")]
DictEntry(#[from] DictEntryError),

#[error("index access: {0}")]
IndexAccess(#[from] IndexAccessError),

#[error("dot access: {0}")]
DotAccess(#[from] DotAccessError),

#[error("spread: {0}")]
Spread(#[from] SpreadError),
}
77 changes: 77 additions & 0 deletions crates/lib/runner-eval-core/src/fold.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use waymark_proto::ast as ir;

use crate::util::is_truthy;

/// Try to fold a literal binary operation to a concrete value.
///
/// Example:
/// - (1, 2, BINARY_OP_ADD) -> 3
pub fn literal_binary(
op: i32,
left: &serde_json::Value,
right: &serde_json::Value,
) -> Option<serde_json::Value> {
match ir::BinaryOperator::try_from(op).ok() {
Some(ir::BinaryOperator::BinaryOpAdd) => {
if let (Some(left), Some(right)) = (left.as_i64(), right.as_i64()) {
return Some(serde_json::Value::Number((left + right).into()));
}
if let (Some(left), Some(right)) = (left.as_f64(), right.as_f64()) {
return serde_json::Number::from_f64(left + right).map(serde_json::Value::Number);
}
if let (Some(left), Some(right)) = (left.as_str(), right.as_str()) {
return Some(serde_json::Value::String(format!("{left}{right}")));
}
None
}
Some(ir::BinaryOperator::BinaryOpSub) => {
if let (Some(left), Some(right)) = (left.as_f64(), right.as_f64()) {
return serde_json::Number::from_f64(left - right).map(serde_json::Value::Number);
}
None
}
Some(ir::BinaryOperator::BinaryOpMul) => {
if let (Some(left), Some(right)) = (left.as_f64(), right.as_f64()) {
return serde_json::Number::from_f64(left * right).map(serde_json::Value::Number);
}
None
}
Some(ir::BinaryOperator::BinaryOpDiv) => {
if let (Some(left), Some(right)) = (left.as_f64(), right.as_f64()) {
return serde_json::Number::from_f64(left / right).map(serde_json::Value::Number);
}
None
}
Some(ir::BinaryOperator::BinaryOpFloorDiv) => {
if let (Some(left), Some(right)) = (left.as_f64(), right.as_f64()) {
if right == 0.0 {
return None;
}
let value = (left / right).floor();
return serde_json::Number::from_f64(value).map(serde_json::Value::Number);
}
None
}
Some(ir::BinaryOperator::BinaryOpMod) => {
if let (Some(left), Some(right)) = (left.as_f64(), right.as_f64()) {
return serde_json::Number::from_f64(left % right).map(serde_json::Value::Number);
}
None
}
_ => None,
}
}

/// Try to fold a literal unary operation to a concrete value.
///
/// Example:
/// - (UNARY_OP_NEG, 4) -> -4
pub fn literal_unary(op: i32, operand: &serde_json::Value) -> Option<serde_json::Value> {
match ir::UnaryOperator::try_from(op).ok() {
Some(ir::UnaryOperator::UnaryOpNeg) => operand
.as_f64()
.and_then(|value| serde_json::Number::from_f64(-value).map(serde_json::Value::Number)),
Some(ir::UnaryOperator::UnaryOpNot) => Some(serde_json::Value::Bool(!is_truthy(operand))),
_ => None,
}
}
Loading
Loading