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