mirror of
https://github.com/zaphar/ucg.git
synced 2025-07-22 18:19:54 -04:00
DEV: Regex binary expressions work.
This commit is contained in:
parent
c19a51424f
commit
2bfb1ee6fe
@ -90,6 +90,7 @@ pub enum Hook {
|
||||
Out,
|
||||
Assert,
|
||||
Convert,
|
||||
Regex,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
|
@ -18,8 +18,10 @@ use std::io::Read;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::rc::Rc;
|
||||
|
||||
use regex::Regex;
|
||||
|
||||
use super::cache;
|
||||
use super::Value::{P, C, F};
|
||||
use super::Value::{C, F, P};
|
||||
use super::VM;
|
||||
use super::{Composite, Error, Hook, Primitive, Value};
|
||||
use crate::build::AssertCollector;
|
||||
@ -72,6 +74,7 @@ impl Builtins {
|
||||
Hook::Map => self.map(path, stack),
|
||||
Hook::Filter => self.filter(path, stack),
|
||||
Hook::Reduce => self.reduce(path, stack),
|
||||
Hook::Regex => self.regex(stack),
|
||||
}
|
||||
}
|
||||
|
||||
@ -351,6 +354,35 @@ impl Builtins {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn regex(&self, stack: &mut Vec<Rc<Value>>) -> Result<(), Error> {
|
||||
// 1. get left side (string)
|
||||
let left_str = if let Some(val) = stack.pop() {
|
||||
if let &P(Str(ref s)) = val.as_ref() {
|
||||
s.clone()
|
||||
} else {
|
||||
return dbg!(Err(Error {}));
|
||||
}
|
||||
} else {
|
||||
return dbg!(Err(Error {}));
|
||||
};
|
||||
|
||||
// 2. get right side (string)
|
||||
let right_str = if let Some(val) = stack.pop() {
|
||||
if let &P(Str(ref s)) = val.as_ref() {
|
||||
s.clone()
|
||||
} else {
|
||||
return dbg!(Err(Error {}));
|
||||
}
|
||||
} else {
|
||||
return dbg!(Err(Error {}));
|
||||
};
|
||||
|
||||
// 3. compare via regex
|
||||
let rex = Regex::new(&right_str)?;
|
||||
stack.push(Rc::new(P(Bool(rex.find(&left_str).is_some()))));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn reduce<P: AsRef<Path>>(&self, path: P, stack: &mut Vec<Rc<Value>>) -> Result<(), Error> {
|
||||
// get the list from the stack
|
||||
let list = if let Some(list) = stack.pop() {
|
||||
|
@ -556,6 +556,7 @@ fn simple_binary_expr() {
|
||||
"2>1;" => P(Bool(true)),
|
||||
"2<1;" => P(Bool(false)),
|
||||
"1!=1;" => P(Bool(false)),
|
||||
"\"foo\" ~ \"bar\";" => P(Bool(false)),
|
||||
//"true && false;" => P(Bool(false)),
|
||||
)
|
||||
}
|
||||
|
@ -12,9 +12,9 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
use crate::ast::{BinaryExprType, Expression, Statement, Value};
|
||||
use crate::build::opcode::Op;
|
||||
use crate::build::opcode::Primitive;
|
||||
use crate::build::opcode::Value::{C, F, M, P, T};
|
||||
use crate::build::opcode::{Hook, Op};
|
||||
|
||||
pub struct AST();
|
||||
|
||||
@ -82,9 +82,14 @@ impl AST {
|
||||
ops.push(Op::Equal);
|
||||
ops.push(Op::Not);
|
||||
}
|
||||
BinaryExprType::REMatch
|
||||
| BinaryExprType::NotREMatch
|
||||
| BinaryExprType::IN
|
||||
BinaryExprType::REMatch => {
|
||||
ops.push(Op::Runtime(Hook::Regex));
|
||||
}
|
||||
BinaryExprType::NotREMatch => {
|
||||
ops.push(Op::Runtime(Hook::Regex));
|
||||
ops.push(Op::Not);
|
||||
}
|
||||
BinaryExprType::IN
|
||||
| BinaryExprType::IS
|
||||
| BinaryExprType::Mod
|
||||
| BinaryExprType::OR
|
||||
|
@ -278,7 +278,7 @@ impl<'a> VM {
|
||||
}
|
||||
return Err(dbg!(Error {}));
|
||||
}
|
||||
|
||||
|
||||
fn op_equal(&mut self) -> Result<(), Error> {
|
||||
let left = self.pop()?;
|
||||
let right = self.pop()?;
|
||||
|
Loading…
x
Reference in New Issue
Block a user