Skip to content

Commit 66a1e9e

Browse files
1BADragonCedarMatt
andauthored
v1.0.5 work (#46)
* wip * Added labels to unresolved bytecode * Allow for negative jumps * add labels to turnary * Removed the leave_val argument in jmpwhen * Fixup the test * wip * wip: starting pattern gen * Have basic type matching working * Have OR parsed as pattern * Finished match v1 * Make bad match return null * implement negative index as feature * version bump * update pyi file * fix the tests --------- Co-authored-by: matt <[email protected]>
1 parent e26f787 commit 66a1e9e

27 files changed

+1793
-1075
lines changed

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ default-members = ["rscel"]
44
resolver = "2"
55

66
[workspace.package]
7-
version = "1.0.4"
7+
version = "1.0.5"
88
edition = "2021"
99
description = "Cel interpreter in rust"
1010
license = "MIT"
@@ -18,5 +18,5 @@ lto = false
1818
lto = true
1919

2020
[workspace.dependencies]
21-
chrono = { version = "0.4.38", features = ["serde"] }
22-
serde_json = { version = "1.0.121", features = ["raw_value"] }
21+
chrono = { version = "0.4.39", features = ["serde"] }
22+
serde_json = { version = "1.0.138", features = ["raw_value"] }

python/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ crate-type = ["cdylib"]
1111

1212
[dependencies]
1313
rscel = { path = "../rscel" }
14-
pyo3 = { version = "0.23", features = [
14+
pyo3 = { version = "0.23.4", features = [
1515
"py-clone",
1616
"extension-module",
1717
"chrono",
@@ -21,4 +21,4 @@ serde_json = { workspace = true }
2121
bincode = "1.3.3"
2222

2323
[build-dependencies]
24-
pyo3-build-config = "0.23"
24+
pyo3-build-config = "0.23.4"

python/rscel.pyi

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from typing import Any, Callable, Tuple
2+
3+
4+
CelBasicType = int | float | str | bool | None
5+
CelArrayType = list[CelBasicType | 'CelArrayType' | 'CelDict']
6+
CelDict = dict[str, 'CelValue']
7+
CelValue = CelDict | CelArrayType | CelBasicType
8+
9+
CelCallable = Callable[[*Tuple[CelValue, ...]], CelValue]
10+
11+
CelBinding = dict[str, CelValue | CelCallable | Any]
12+
13+
def eval(prog: str, binding: CelBinding) -> CelValue:
14+
...
15+
16+
class CelProgram:
17+
def __init__(self):
18+
...
19+
20+
def add_source(self, source: str):
21+
...
22+
23+
def add_serialized_json(self, source: str):
24+
...
25+
26+
def add_serialized_bincode(self, bincode: bytes):
27+
...
28+
29+
def serialize_to_json(self) -> str:
30+
...
31+
32+
def serialize_to_bincode(self) -> bytes:
33+
...
34+
35+
def details_json(self) -> str:
36+
...
37+
38+
class BindContext:
39+
def __init__(self):
40+
...
41+
42+
def bind_param(self, name: str, val: CelValue):
43+
...
44+
45+
def bind_func(self, name: str, val: Callable[[Any, Any]]):
46+
...
47+
48+
def bind(self, name: str, val: Any):
49+
...
50+
51+
class CelContext:
52+
def __init__(self):
53+
...
54+
55+
def add_program_string(self, name: str, source: str):
56+
...
57+
58+
def add_program(self, name: str, prog: "CelProgram"):
59+
...
60+
61+
def exec(self, name: str, bindings: BindContext) -> CelValue:
62+
...

rscel-macro/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ readme = "../README.md"
1010
proc-macro = true
1111

1212
[dependencies]
13-
proc-macro2 = "1.0.92"
14-
quote = "1.0.37"
15-
syn = { version = "2.0.91", features = ["full"] }
13+
proc-macro2 = "1.0.93"
14+
quote = "1.0.38"
15+
syn = { version = "2.0.98", features = ["full"] }

rscel/Cargo.toml

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,26 @@ license = { workspace = true }
77
readme = "../README.md"
88

99
[features]
10-
default = ["type_prop", "protobuf"]
10+
default = ["type_prop", "protobuf", "neg_index"]
1111
ast_ser = []
1212
debug_output = []
1313
type_prop = []
14+
neg_index = []
1415
protobuf = ["dep:protobuf"]
1516

1617
[build-dependencies]
17-
protobuf-codegen = { version = "3.4.0" }
18-
protoc-bin-vendored = { version = "3.0.0" }
18+
protobuf-codegen = { version = "3.7.1" }
19+
protoc-bin-vendored = { version = "3.1.0" }
1920

2021
[dependencies]
21-
rscel-macro = { path = "../rscel-macro", version = "1.0.4" }
22+
rscel-macro = { path = "../rscel-macro" }
2223
test-case = "3.3.1"
23-
regex = "1.10.5"
24-
serde = { version = "1.0.204", features = ["derive", "rc"] }
25-
serde_with = { version = "3.9.0", features = ["chrono"] }
24+
regex = "1.11.1"
25+
serde = { version = "1.0.217", features = ["derive", "rc"] }
26+
serde_with = { version = "3.12.0", features = ["chrono"] }
2627
serde_json = { workspace = true }
2728
chrono = { workspace = true }
28-
duration-str = "0.11.2"
29-
protobuf = { version = "3.5.0", optional = true }
30-
chrono-tz = "0.9.0"
29+
duration-str = "0.13.0"
30+
protobuf = { version = "3.7.1", optional = true }
31+
chrono-tz = "0.10.1"
3132
num-traits = "0.2.19"

rscel/examples/dumps_ast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ impl<'a> AstDumper<'a> {
3939
self.dump_or_node(true_clause, depth + 1);
4040
self.dump_expr_node(false_clause, depth + 1);
4141
}
42+
Expr::Match { .. } => todo!(),
4243
}
4344
}
4445

rscel/examples/explain.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ impl AstDumper {
116116
]
117117
.into_iter(),
118118
)),
119+
Expr::Match { .. } => todo!(),
119120
}
120121
}
121122

rscel/src/compiler/compiled_prog.rs

Lines changed: 45 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use crate::{
2-
interp::JmpWhen, program::ProgramDetails, types::CelByteCode, ByteCode, CelError, CelValue,
3-
CelValueDyn, Program,
4-
};
1+
mod preresolved;
2+
3+
use crate::{program::ProgramDetails, types::CelByteCode, ByteCode, CelValue, Program};
4+
pub use preresolved::{PreResolvedByteCode, PreResolvedCodePoint};
55

66
#[derive(Debug, Clone)]
77
pub enum NodeValue {
8-
Bytecode(CelByteCode),
8+
Bytecode(PreResolvedByteCode),
99
ConstExpr(CelValue),
1010
}
1111

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

2625
let mut new_details = ProgramDetails::new();
2726

@@ -40,7 +39,7 @@ macro_rules! compile {
4039
}
4140
}
4241
($($child,)+) => {
43-
let mut new_bytecode = CelByteCode::new();
42+
let mut new_bytecode = PreResolvedByteCode::new();
4443

4544
$(
4645
new_bytecode.extend($child.into_bytecode().into_iter());
@@ -61,9 +60,13 @@ macro_rules! compile {
6160
}
6261

6362
impl CompiledProg {
63+
pub fn new(inner: NodeValue, details: ProgramDetails) -> Self {
64+
Self { inner, details }
65+
}
66+
6467
pub fn empty() -> CompiledProg {
6568
CompiledProg {
66-
inner: NodeValue::Bytecode(CelByteCode::new()),
69+
inner: NodeValue::Bytecode(PreResolvedByteCode::new()),
6770
details: ProgramDetails::new(),
6871
}
6972
}
@@ -77,18 +80,35 @@ impl CompiledProg {
7780

7881
pub fn with_bytecode(bytecode: CelByteCode) -> CompiledProg {
7982
CompiledProg {
80-
inner: NodeValue::Bytecode(bytecode),
83+
inner: NodeValue::Bytecode(bytecode.into()),
8184
details: ProgramDetails::new(),
8285
}
8386
}
8487

85-
pub fn with_code_points(bytecode: Vec<ByteCode>) -> CompiledProg {
88+
pub fn with_code_points(bytecode: Vec<PreResolvedCodePoint>) -> CompiledProg {
8689
CompiledProg {
87-
inner: NodeValue::Bytecode(bytecode.into()),
90+
inner: NodeValue::Bytecode(bytecode.into_iter().collect()),
8891
details: ProgramDetails::new(),
8992
}
9093
}
9194

95+
pub fn details(&self) -> &ProgramDetails {
96+
&self.details
97+
}
98+
99+
pub fn into_parts(self) -> (NodeValue, ProgramDetails) {
100+
(self.inner, self.details)
101+
}
102+
103+
pub fn append_if_bytecode(&mut self, b: impl IntoIterator<Item = PreResolvedCodePoint>) {
104+
match &mut self.inner {
105+
NodeValue::Bytecode(bytecode) => {
106+
bytecode.extend(b);
107+
}
108+
NodeValue::ConstExpr(_) => { /* do nothing */ }
109+
}
110+
}
111+
92112
pub fn with_const(val: CelValue) -> CompiledProg {
93113
CompiledProg {
94114
inner: NodeValue::ConstExpr(val),
@@ -122,7 +142,7 @@ impl CompiledProg {
122142
.into_iter()
123143
.map(|c| c.inner.into_bytecode().into_iter())
124144
.flatten()
125-
.chain(bytecode.into_iter())
145+
.chain(bytecode.into_iter().map(|b| b.into()))
126146
.collect(),
127147
)
128148
};
@@ -149,10 +169,13 @@ impl CompiledProg {
149169
},
150170
None => CompiledProg {
151171
inner: NodeValue::Bytecode(
152-
[ByteCode::Push(c1), ByteCode::Push(c2)]
153-
.into_iter()
154-
.chain(bytecode.into_iter())
155-
.collect(),
172+
[
173+
PreResolvedCodePoint::Bytecode(ByteCode::Push(c1)),
174+
PreResolvedCodePoint::Bytecode(ByteCode::Push(c2)),
175+
]
176+
.into_iter()
177+
.chain(bytecode.into_iter().map(|b| b.into()))
178+
.collect(),
156179
),
157180
details: new_details,
158181
},
@@ -162,7 +185,7 @@ impl CompiledProg {
162185
c1.into_bytecode()
163186
.into_iter()
164187
.chain(c2.into_bytecode().into_iter())
165-
.chain(bytecode.into_iter())
188+
.chain(bytecode.into_iter().map(|b| b.into()))
166189
.collect(),
167190
),
168191
details: new_details,
@@ -174,7 +197,7 @@ impl CompiledProg {
174197
let mut details = self.details;
175198
details.add_source(source);
176199

177-
Program::new(details, self.inner.into_bytecode())
200+
Program::new(details, self.inner.into_bytecode().resolve())
178201
}
179202

180203
pub fn add_ident(mut self, ident: &str) -> CompiledProg {
@@ -205,82 +228,6 @@ impl CompiledProg {
205228
r
206229
}
207230

208-
pub fn into_turnary(
209-
mut self,
210-
true_clause: CompiledProg,
211-
false_clause: CompiledProg,
212-
) -> CompiledProg {
213-
self.details.union_from(true_clause.details);
214-
self.details.union_from(false_clause.details);
215-
216-
if let NodeValue::ConstExpr(i) = self.inner {
217-
if i.is_err() {
218-
CompiledProg {
219-
inner: NodeValue::ConstExpr(i),
220-
details: self.details,
221-
}
222-
} else {
223-
if cfg!(feature = "type_prop") {
224-
if i.is_truthy() {
225-
CompiledProg {
226-
inner: true_clause.inner,
227-
details: self.details,
228-
}
229-
} else {
230-
CompiledProg {
231-
inner: false_clause.inner,
232-
details: self.details,
233-
}
234-
}
235-
} else {
236-
if let CelValue::Bool(b) = i {
237-
if b {
238-
CompiledProg {
239-
inner: true_clause.inner,
240-
details: self.details,
241-
}
242-
} else {
243-
CompiledProg {
244-
inner: false_clause.inner,
245-
details: self.details,
246-
}
247-
}
248-
} else {
249-
CompiledProg {
250-
inner: NodeValue::ConstExpr(CelValue::from_err(CelError::Value(
251-
format!("{} cannot be converted to bool", i.as_type()),
252-
))),
253-
details: self.details,
254-
}
255-
}
256-
}
257-
}
258-
} else {
259-
let true_clause_bytecode = true_clause.inner.into_bytecode();
260-
let false_clause_bytecode = false_clause.inner.into_bytecode();
261-
CompiledProg {
262-
inner: NodeValue::Bytecode(
263-
self.inner
264-
.into_bytecode()
265-
.into_iter()
266-
.chain(
267-
[ByteCode::JmpCond {
268-
when: JmpWhen::False,
269-
dist: (true_clause_bytecode.len() as u32) + 1, // +1 to jmp over the next jump
270-
leave_val: false,
271-
}]
272-
.into_iter(),
273-
)
274-
.chain(true_clause_bytecode.into_iter())
275-
.chain([ByteCode::Jmp(false_clause_bytecode.len() as u32)].into_iter())
276-
.chain(false_clause_bytecode.into_iter())
277-
.collect(),
278-
),
279-
details: self.details,
280-
}
281-
}
282-
}
283-
284231
#[inline]
285232
pub fn bytecode_len(&self) -> usize {
286233
match self.inner {
@@ -289,7 +236,7 @@ impl CompiledProg {
289236
}
290237
}
291238

292-
pub fn into_bytecode(self) -> CelByteCode {
239+
pub fn into_unresolved_bytecode(self) -> PreResolvedByteCode {
293240
self.inner.into_bytecode()
294241
}
295242

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

313-
pub fn into_bytecode(self) -> CelByteCode {
260+
pub fn into_bytecode(self) -> PreResolvedByteCode {
314261
match self {
315262
NodeValue::Bytecode(b) => b,
316-
NodeValue::ConstExpr(c) => CelByteCode::from_code_point(ByteCode::Push(c)),
263+
NodeValue::ConstExpr(c) => [ByteCode::Push(c)].into_iter().collect(),
317264
}
318265
}
319266
}

0 commit comments

Comments
 (0)