Skip to content

Commit 6896f86

Browse files
authored
Merge branch '1BADragon:main' into main
2 parents 4ffb780 + a43f7c9 commit 6896f86

File tree

12 files changed

+469
-246
lines changed

12 files changed

+469
-246
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ target/
1010
*.pdb
1111

1212
.env/
13+
14+
#mac shit
15+
.DS_Store

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rscel"
3-
version = "0.7.2"
3+
version = "0.8.0"
44
edition = "2021"
55
description = "Cel interpreter in rust"
66
license = "MIT"

src/cel_value.rs

Lines changed: 84 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,62 @@ impl CelValue {
124124
}
125125
}
126126

127+
pub fn int_type() -> CelValue {
128+
CelValue::from_type("int")
129+
}
130+
131+
pub fn uint_type() -> CelValue {
132+
CelValue::from_type("uint")
133+
}
134+
135+
pub fn float_type() -> CelValue {
136+
CelValue::from_type("float")
137+
}
138+
139+
pub fn bool_type() -> CelValue {
140+
CelValue::from_type("bool")
141+
}
142+
143+
pub fn string_type() -> CelValue {
144+
CelValue::from_type("string")
145+
}
146+
147+
pub fn bytes_type() -> CelValue {
148+
CelValue::from_type("bytes")
149+
}
150+
151+
pub fn list_type() -> CelValue {
152+
CelValue::from_type("list")
153+
}
154+
155+
pub fn map_type() -> CelValue {
156+
CelValue::from_type("map")
157+
}
158+
159+
pub fn null_type() -> CelValue {
160+
CelValue::from_type("null")
161+
}
162+
163+
pub fn ident_type() -> CelValue {
164+
CelValue::from_type("ident")
165+
}
166+
167+
pub fn type_type() -> CelValue {
168+
CelValue::from_type("type")
169+
}
170+
171+
pub fn timestamp_type() -> CelValue {
172+
CelValue::from_type("timestamp")
173+
}
174+
175+
pub fn duration_type() -> CelValue {
176+
CelValue::from_type("duration")
177+
}
178+
179+
pub fn bytecode_type() -> CelValue {
180+
CelValue::from_type("bytecode")
181+
}
182+
127183
pub fn is_truthy(&self) -> bool {
128184
match self {
129185
CelValue::Int(i) => *i != 0,
@@ -240,6 +296,8 @@ impl CelValue {
240296
} else if let (CelValue::Duration(l), CelValue::Duration(r)) = (lhs.as_ref(), rhs.as_ref())
241297
{
242298
Ok(CelValue::from_bool(l == r))
299+
} else if let (CelValue::Type(l), CelValue::Type(r)) = (lhs.as_ref(), rhs.as_ref()) {
300+
Ok(CelValue::from_bool(l == r))
243301
} else {
244302
Err(CelError::invalid_op(&format!(
245303
"Invalid op '==' between {:?} and {:?}",
@@ -345,10 +403,20 @@ impl CelValue {
345403
if let CelValue::String(r) = self {
346404
Ok(CelValue::from_bool(m.contains_key(r)))
347405
} else {
348-
return Err(CelError::invalid_op(&format!(
406+
Err(CelError::invalid_op(&format!(
407+
"Op 'in' invalid between {:?} and {:?}",
408+
lhs_type, rhs_type
409+
)))
410+
}
411+
}
412+
CelValue::String(s) => {
413+
if let CelValue::String(r) = self {
414+
Ok(CelValue::from_bool(s.contains(r)))
415+
} else {
416+
Err(CelError::invalid_op(&format!(
349417
"Op 'in' invalid between {:?} and {:?}",
350418
lhs_type, rhs_type
351-
)));
419+
)))
352420
}
353421
}
354422
_ => {
@@ -380,20 +448,20 @@ impl CelValue {
380448

381449
pub fn as_type(&self) -> CelValue {
382450
match self {
383-
CelValue::Int(_) => CelValue::from_type("int"),
384-
CelValue::UInt(_) => CelValue::from_type("uint"),
385-
CelValue::Float(_) => CelValue::from_type("float"),
386-
CelValue::Bool(_) => CelValue::from_type("bool"),
387-
CelValue::String(_) => CelValue::from_type("string"),
388-
CelValue::Bytes(_) => CelValue::from_type("bytes"),
389-
CelValue::List(_) => CelValue::from_type("list"),
390-
CelValue::Map(_) => CelValue::from_type("map"),
391-
CelValue::Null => CelValue::from_type("null_type"),
392-
CelValue::Ident(_) => CelValue::from_type("ident"),
393-
CelValue::Type(_) => CelValue::from_type("type"),
394-
CelValue::TimeStamp(_) => CelValue::from_type("timestamp"),
395-
CelValue::Duration(_) => CelValue::from_type("duration"),
396-
CelValue::ByteCode(_) => CelValue::from_type("bytecode"),
451+
CelValue::Int(_) => CelValue::int_type(),
452+
CelValue::UInt(_) => CelValue::uint_type(),
453+
CelValue::Float(_) => CelValue::float_type(),
454+
CelValue::Bool(_) => CelValue::bool_type(),
455+
CelValue::String(_) => CelValue::string_type(),
456+
CelValue::Bytes(_) => CelValue::bytes_type(),
457+
CelValue::List(_) => CelValue::list_type(),
458+
CelValue::Map(_) => CelValue::map_type(),
459+
CelValue::Null => CelValue::null_type(),
460+
CelValue::Ident(_) => CelValue::ident_type(),
461+
CelValue::Type(_) => CelValue::type_type(),
462+
CelValue::TimeStamp(_) => CelValue::timestamp_type(),
463+
CelValue::Duration(_) => CelValue::duration_type(),
464+
CelValue::ByteCode(_) => CelValue::bytecode_type(),
397465
}
398466
}
399467
}

src/compiler/compiler.rs

Lines changed: 106 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -620,13 +620,6 @@ impl<'l> CelCompiler<'l> {
620620
let start_loc = self.tokenizer.location();
621621

622622
match self.tokenizer.peek()? {
623-
Some(Token::Type) => {
624-
self.tokenizer.next()?;
625-
Ok((
626-
ParseResult::with_bytecode(vec![ByteCode::Push(CelValue::from_ident("type"))]),
627-
AstNode::new(Primary::Type, start_loc, self.tokenizer.location()),
628-
))
629-
}
630623
Some(Token::Ident(val)) => {
631624
self.tokenizer.next()?;
632625
Ok((
@@ -696,7 +689,7 @@ impl<'l> CelCompiler<'l> {
696689
Ok((
697690
ParseResult::with_bytecode(vec![ByteCode::Push(CelValue::from_uint(val))]),
698691
AstNode::new(
699-
Primary::Literal(Literal::Unsigned(val)),
692+
Primary::Literal(LiteralsAndKeywords::UnsignedLit(val)),
700693
start_loc,
701694
self.tokenizer.location(),
702695
),
@@ -707,7 +700,7 @@ impl<'l> CelCompiler<'l> {
707700
Ok((
708701
ParseResult::with_bytecode(vec![ByteCode::Push(CelValue::from_int(val))]),
709702
AstNode::new(
710-
Primary::Literal(Literal::Integer(val)),
703+
Primary::Literal(LiteralsAndKeywords::IntegerLit(val)),
711704
start_loc,
712705
self.tokenizer.location(),
713706
),
@@ -718,7 +711,7 @@ impl<'l> CelCompiler<'l> {
718711
Ok((
719712
ParseResult::with_bytecode(vec![ByteCode::Push(CelValue::from_float(val))]),
720713
AstNode::new(
721-
Primary::Literal(Literal::Floating(val)),
714+
Primary::Literal(LiteralsAndKeywords::FloatingLit(val)),
722715
start_loc,
723716
self.tokenizer.location(),
724717
),
@@ -731,7 +724,7 @@ impl<'l> CelCompiler<'l> {
731724
val.clone(),
732725
))]),
733726
AstNode::new(
734-
Primary::Literal(Literal::String(val)),
727+
Primary::Literal(LiteralsAndKeywords::StringLit(val)),
735728
start_loc,
736729
self.tokenizer.location(),
737730
),
@@ -744,7 +737,7 @@ impl<'l> CelCompiler<'l> {
744737
val.clone(),
745738
))]),
746739
AstNode::new(
747-
Primary::Literal(Literal::ByteString(val)),
740+
Primary::Literal(LiteralsAndKeywords::ByteStringLit(val)),
748741
start_loc,
749742
self.tokenizer.location(),
750743
),
@@ -755,7 +748,7 @@ impl<'l> CelCompiler<'l> {
755748
Ok((
756749
ParseResult::with_bytecode(vec![ByteCode::Push(CelValue::from_bool(val))]),
757750
AstNode::new(
758-
Primary::Literal(Literal::Boolean(val)),
751+
Primary::Literal(LiteralsAndKeywords::BooleanLit(val)),
759752
start_loc,
760753
self.tokenizer.location(),
761754
),
@@ -766,7 +759,106 @@ impl<'l> CelCompiler<'l> {
766759
Ok((
767760
ParseResult::with_bytecode(vec![ByteCode::Push(CelValue::from_null())]),
768761
AstNode::new(
769-
Primary::Literal(Literal::Null),
762+
Primary::Literal(LiteralsAndKeywords::NullLit),
763+
start_loc,
764+
self.tokenizer.location(),
765+
),
766+
))
767+
}
768+
Some(Token::NullType) => {
769+
self.tokenizer.next()?;
770+
Ok((
771+
ParseResult::with_bytecode(vec![ByteCode::Push(CelValue::null_type())]),
772+
AstNode::new(
773+
Primary::Literal(LiteralsAndKeywords::NullType),
774+
start_loc,
775+
self.tokenizer.location(),
776+
),
777+
))
778+
}
779+
Some(Token::Int) => {
780+
self.tokenizer.next()?;
781+
Ok((
782+
ParseResult::with_bytecode(vec![ByteCode::Push(CelValue::int_type())]),
783+
AstNode::new(
784+
Primary::Literal(LiteralsAndKeywords::Int),
785+
start_loc,
786+
self.tokenizer.location(),
787+
),
788+
))
789+
}
790+
Some(Token::Uint) => {
791+
self.tokenizer.next()?;
792+
Ok((
793+
ParseResult::with_bytecode(vec![ByteCode::Push(CelValue::uint_type())]),
794+
AstNode::new(
795+
Primary::Literal(LiteralsAndKeywords::Uint),
796+
start_loc,
797+
self.tokenizer.location(),
798+
),
799+
))
800+
}
801+
Some(Token::Float) => {
802+
self.tokenizer.next()?;
803+
Ok((
804+
ParseResult::with_bytecode(vec![ByteCode::Push(CelValue::float_type())]),
805+
AstNode::new(
806+
Primary::Literal(LiteralsAndKeywords::Float),
807+
start_loc,
808+
self.tokenizer.location(),
809+
),
810+
))
811+
}
812+
Some(Token::Bool) => {
813+
self.tokenizer.next()?;
814+
Ok((
815+
ParseResult::with_bytecode(vec![ByteCode::Push(CelValue::bool_type())]),
816+
AstNode::new(
817+
Primary::Literal(LiteralsAndKeywords::Bool),
818+
start_loc,
819+
self.tokenizer.location(),
820+
),
821+
))
822+
}
823+
Some(Token::Timestamp) => {
824+
self.tokenizer.next()?;
825+
Ok((
826+
ParseResult::with_bytecode(vec![ByteCode::Push(CelValue::timestamp_type())]),
827+
AstNode::new(
828+
Primary::Literal(LiteralsAndKeywords::Timestamp),
829+
start_loc,
830+
self.tokenizer.location(),
831+
),
832+
))
833+
}
834+
Some(Token::Duration) => {
835+
self.tokenizer.next()?;
836+
Ok((
837+
ParseResult::with_bytecode(vec![ByteCode::Push(CelValue::duration_type())]),
838+
AstNode::new(
839+
Primary::Literal(LiteralsAndKeywords::Duration),
840+
start_loc,
841+
self.tokenizer.location(),
842+
),
843+
))
844+
}
845+
Some(Token::String) => {
846+
self.tokenizer.next()?;
847+
Ok((
848+
ParseResult::with_bytecode(vec![ByteCode::Push(CelValue::string_type())]),
849+
AstNode::new(
850+
Primary::Literal(LiteralsAndKeywords::String),
851+
start_loc,
852+
self.tokenizer.location(),
853+
),
854+
))
855+
}
856+
Some(Token::Type) => {
857+
self.tokenizer.next()?;
858+
Ok((
859+
ParseResult::with_bytecode(vec![ByteCode::Push(CelValue::type_type())]),
860+
AstNode::new(
861+
Primary::Literal(LiteralsAndKeywords::Type),
770862
start_loc,
771863
self.tokenizer.location(),
772864
),

src/compiler/grammar.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ pub enum Primary {
141141
Parens(AstNode<Expr>),
142142
ListConstruction(AstNode<ExprList>),
143143
ObjectInit(AstNode<ObjInits>),
144-
Literal(Literal),
144+
Literal(LiteralsAndKeywords),
145145
}
146146

147147
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
@@ -161,13 +161,24 @@ pub struct ObjInits {
161161
}
162162

163163
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
164-
pub enum Literal {
165-
Null,
164+
pub enum LiteralsAndKeywords {
165+
Type,
166166

167-
Integer(i64),
168-
Unsigned(u64),
169-
Floating(f64),
170-
String(String),
171-
ByteString(Vec<u8>),
172-
Boolean(bool),
167+
NullType,
168+
Int,
169+
Uint,
170+
Float,
171+
Bool,
172+
String,
173+
Bytes,
174+
Timestamp,
175+
Duration,
176+
177+
NullLit,
178+
IntegerLit(i64),
179+
UnsignedLit(u64),
180+
FloatingLit(f64),
181+
StringLit(String),
182+
ByteStringLit(Vec<u8>),
183+
BooleanLit(bool),
173184
}

0 commit comments

Comments
 (0)