Skip to content
Merged
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
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ default-members = ["rscel"]
resolver = "2"

[workspace.package]
version = "1.0.4"
version = "1.0.5"
edition = "2021"
description = "Cel interpreter in rust"
license = "MIT"
Expand All @@ -18,5 +18,5 @@ lto = false
lto = true

[workspace.dependencies]
chrono = { version = "0.4.38", features = ["serde"] }
serde_json = { version = "1.0.121", features = ["raw_value"] }
chrono = { version = "0.4.39", features = ["serde"] }
serde_json = { version = "1.0.138", features = ["raw_value"] }
4 changes: 2 additions & 2 deletions python/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ crate-type = ["cdylib"]

[dependencies]
rscel = { path = "../rscel" }
pyo3 = { version = "0.23", features = [
pyo3 = { version = "0.23.4", features = [
"py-clone",
"extension-module",
"chrono",
Expand All @@ -21,4 +21,4 @@ serde_json = { workspace = true }
bincode = "1.3.3"

[build-dependencies]
pyo3-build-config = "0.23"
pyo3-build-config = "0.23.4"
62 changes: 62 additions & 0 deletions python/rscel.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from typing import Any, Callable, Tuple


CelBasicType = int | float | str | bool | None
CelArrayType = list[CelBasicType | 'CelArrayType' | 'CelDict']
CelDict = dict[str, 'CelValue']
CelValue = CelDict | CelArrayType | CelBasicType

CelCallable = Callable[[*Tuple[CelValue, ...]], CelValue]

CelBinding = dict[str, CelValue | CelCallable | Any]

def eval(prog: str, binding: CelBinding) -> CelValue:
...

class CelProgram:
def __init__(self):
...

def add_source(self, source: str):
...

def add_serialized_json(self, source: str):
...

def add_serialized_bincode(self, bincode: bytes):
...

def serialize_to_json(self) -> str:
...

def serialize_to_bincode(self) -> bytes:
...

def details_json(self) -> str:
...

class BindContext:
def __init__(self):
...

def bind_param(self, name: str, val: CelValue):
...

def bind_func(self, name: str, val: Callable[[Any, Any]]):
...

def bind(self, name: str, val: Any):
...

class CelContext:
def __init__(self):
...

def add_program_string(self, name: str, source: str):
...

def add_program(self, name: str, prog: "CelProgram"):
...

def exec(self, name: str, bindings: BindContext) -> CelValue:
...
6 changes: 3 additions & 3 deletions rscel-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ readme = "../README.md"
proc-macro = true

[dependencies]
proc-macro2 = "1.0.92"
quote = "1.0.37"
syn = { version = "2.0.91", features = ["full"] }
proc-macro2 = "1.0.93"
quote = "1.0.38"
syn = { version = "2.0.98", features = ["full"] }
21 changes: 11 additions & 10 deletions rscel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,26 @@ license = { workspace = true }
readme = "../README.md"

[features]
default = ["type_prop", "protobuf"]
default = ["type_prop", "protobuf", "neg_index"]
ast_ser = []
debug_output = []
type_prop = []
neg_index = []
protobuf = ["dep:protobuf"]

[build-dependencies]
protobuf-codegen = { version = "3.4.0" }
protoc-bin-vendored = { version = "3.0.0" }
protobuf-codegen = { version = "3.7.1" }
protoc-bin-vendored = { version = "3.1.0" }

[dependencies]
rscel-macro = { path = "../rscel-macro", version = "1.0.4" }
rscel-macro = { path = "../rscel-macro" }
test-case = "3.3.1"
regex = "1.10.5"
serde = { version = "1.0.204", features = ["derive", "rc"] }
serde_with = { version = "3.9.0", features = ["chrono"] }
regex = "1.11.1"
serde = { version = "1.0.217", features = ["derive", "rc"] }
serde_with = { version = "3.12.0", features = ["chrono"] }
serde_json = { workspace = true }
chrono = { workspace = true }
duration-str = "0.11.2"
protobuf = { version = "3.5.0", optional = true }
chrono-tz = "0.9.0"
duration-str = "0.13.0"
protobuf = { version = "3.7.1", optional = true }
chrono-tz = "0.10.1"
num-traits = "0.2.19"
1 change: 1 addition & 0 deletions rscel/examples/dumps_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ impl<'a> AstDumper<'a> {
self.dump_or_node(true_clause, depth + 1);
self.dump_expr_node(false_clause, depth + 1);
}
Expr::Match { .. } => todo!(),
}
}

Expand Down
1 change: 1 addition & 0 deletions rscel/examples/explain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ impl AstDumper {
]
.into_iter(),
)),
Expr::Match { .. } => todo!(),
}
}

Expand Down
143 changes: 45 additions & 98 deletions rscel/src/compiler/compiled_prog.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::{
interp::JmpWhen, program::ProgramDetails, types::CelByteCode, ByteCode, CelError, CelValue,
CelValueDyn, Program,
};
mod preresolved;

use crate::{program::ProgramDetails, types::CelByteCode, ByteCode, CelValue, Program};
pub use preresolved::{PreResolvedByteCode, PreResolvedCodePoint};

#[derive(Debug, Clone)]
pub enum NodeValue {
Bytecode(CelByteCode),
Bytecode(PreResolvedByteCode),
ConstExpr(CelValue),
}

Expand All @@ -19,9 +19,8 @@ pub struct CompiledProg {
macro_rules! compile {
($bytecode:expr, $const_expr:expr, $( $child : ident),+) => {
{
use crate::compiler::compiled_prog::NodeValue;
use crate::compiler::compiled_prog::{NodeValue, PreResolvedByteCode};
use crate::program::ProgramDetails;
use crate::types::CelByteCode;

let mut new_details = ProgramDetails::new();

Expand All @@ -40,7 +39,7 @@ macro_rules! compile {
}
}
($($child,)+) => {
let mut new_bytecode = CelByteCode::new();
let mut new_bytecode = PreResolvedByteCode::new();

$(
new_bytecode.extend($child.into_bytecode().into_iter());
Expand All @@ -61,9 +60,13 @@ macro_rules! compile {
}

impl CompiledProg {
pub fn new(inner: NodeValue, details: ProgramDetails) -> Self {
Self { inner, details }
}

pub fn empty() -> CompiledProg {
CompiledProg {
inner: NodeValue::Bytecode(CelByteCode::new()),
inner: NodeValue::Bytecode(PreResolvedByteCode::new()),
details: ProgramDetails::new(),
}
}
Expand All @@ -77,18 +80,35 @@ impl CompiledProg {

pub fn with_bytecode(bytecode: CelByteCode) -> CompiledProg {
CompiledProg {
inner: NodeValue::Bytecode(bytecode),
inner: NodeValue::Bytecode(bytecode.into()),
details: ProgramDetails::new(),
}
}

pub fn with_code_points(bytecode: Vec<ByteCode>) -> CompiledProg {
pub fn with_code_points(bytecode: Vec<PreResolvedCodePoint>) -> CompiledProg {
CompiledProg {
inner: NodeValue::Bytecode(bytecode.into()),
inner: NodeValue::Bytecode(bytecode.into_iter().collect()),
details: ProgramDetails::new(),
}
}

pub fn details(&self) -> &ProgramDetails {
&self.details
}

pub fn into_parts(self) -> (NodeValue, ProgramDetails) {
(self.inner, self.details)
}

pub fn append_if_bytecode(&mut self, b: impl IntoIterator<Item = PreResolvedCodePoint>) {
match &mut self.inner {
NodeValue::Bytecode(bytecode) => {
bytecode.extend(b);
}
NodeValue::ConstExpr(_) => { /* do nothing */ }
}
}

pub fn with_const(val: CelValue) -> CompiledProg {
CompiledProg {
inner: NodeValue::ConstExpr(val),
Expand Down Expand Up @@ -122,7 +142,7 @@ impl CompiledProg {
.into_iter()
.map(|c| c.inner.into_bytecode().into_iter())
.flatten()
.chain(bytecode.into_iter())
.chain(bytecode.into_iter().map(|b| b.into()))
.collect(),
)
};
Expand All @@ -149,10 +169,13 @@ impl CompiledProg {
},
None => CompiledProg {
inner: NodeValue::Bytecode(
[ByteCode::Push(c1), ByteCode::Push(c2)]
.into_iter()
.chain(bytecode.into_iter())
.collect(),
[
PreResolvedCodePoint::Bytecode(ByteCode::Push(c1)),
PreResolvedCodePoint::Bytecode(ByteCode::Push(c2)),
]
.into_iter()
.chain(bytecode.into_iter().map(|b| b.into()))
.collect(),
),
details: new_details,
},
Expand All @@ -162,7 +185,7 @@ impl CompiledProg {
c1.into_bytecode()
.into_iter()
.chain(c2.into_bytecode().into_iter())
.chain(bytecode.into_iter())
.chain(bytecode.into_iter().map(|b| b.into()))
.collect(),
),
details: new_details,
Expand All @@ -174,7 +197,7 @@ impl CompiledProg {
let mut details = self.details;
details.add_source(source);

Program::new(details, self.inner.into_bytecode())
Program::new(details, self.inner.into_bytecode().resolve())
}

pub fn add_ident(mut self, ident: &str) -> CompiledProg {
Expand Down Expand Up @@ -205,82 +228,6 @@ impl CompiledProg {
r
}

pub fn into_turnary(
mut self,
true_clause: CompiledProg,
false_clause: CompiledProg,
) -> CompiledProg {
self.details.union_from(true_clause.details);
self.details.union_from(false_clause.details);

if let NodeValue::ConstExpr(i) = self.inner {
if i.is_err() {
CompiledProg {
inner: NodeValue::ConstExpr(i),
details: self.details,
}
} else {
if cfg!(feature = "type_prop") {
if i.is_truthy() {
CompiledProg {
inner: true_clause.inner,
details: self.details,
}
} else {
CompiledProg {
inner: false_clause.inner,
details: self.details,
}
}
} else {
if let CelValue::Bool(b) = i {
if b {
CompiledProg {
inner: true_clause.inner,
details: self.details,
}
} else {
CompiledProg {
inner: false_clause.inner,
details: self.details,
}
}
} else {
CompiledProg {
inner: NodeValue::ConstExpr(CelValue::from_err(CelError::Value(
format!("{} cannot be converted to bool", i.as_type()),
))),
details: self.details,
}
}
}
}
} else {
let true_clause_bytecode = true_clause.inner.into_bytecode();
let false_clause_bytecode = false_clause.inner.into_bytecode();
CompiledProg {
inner: NodeValue::Bytecode(
self.inner
.into_bytecode()
.into_iter()
.chain(
[ByteCode::JmpCond {
when: JmpWhen::False,
dist: (true_clause_bytecode.len() as u32) + 1, // +1 to jmp over the next jump
leave_val: false,
}]
.into_iter(),
)
.chain(true_clause_bytecode.into_iter())
.chain([ByteCode::Jmp(false_clause_bytecode.len() as u32)].into_iter())
.chain(false_clause_bytecode.into_iter())
.collect(),
),
details: self.details,
}
}
}

#[inline]
pub fn bytecode_len(&self) -> usize {
match self.inner {
Expand All @@ -289,7 +236,7 @@ impl CompiledProg {
}
}

pub fn into_bytecode(self) -> CelByteCode {
pub fn into_unresolved_bytecode(self) -> PreResolvedByteCode {
self.inner.into_bytecode()
}

Expand All @@ -310,10 +257,10 @@ impl NodeValue {
matches!(*self, NodeValue::ConstExpr(_))
}

pub fn into_bytecode(self) -> CelByteCode {
pub fn into_bytecode(self) -> PreResolvedByteCode {
match self {
NodeValue::Bytecode(b) => b,
NodeValue::ConstExpr(c) => CelByteCode::from_code_point(ByteCode::Push(c)),
NodeValue::ConstExpr(c) => [ByteCode::Push(c)].into_iter().collect(),
}
}
}
Loading
Loading