Skip to content

Commit

Permalink
feat: add parse() and rparse() functions
Browse files Browse the repository at this point in the history
linux-china committed Apr 26, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 6183634 commit 552b6c1
Showing 8 changed files with 101 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/builtins.rs
Original file line number Diff line number Diff line change
@@ -51,6 +51,8 @@ pub enum Function {
Fend,
Trim,
Truncate,
Parse,
RegexParse,
Strtonum,
FormatBytes,
ToBytes,
@@ -404,6 +406,8 @@ static_map!(
["bf_contains", Function::BloomFilterContains],
["local_ip", Function::LocalIp],
["truncate", Function::Truncate],
["parse", Function::Parse],
["rparse", Function::RegexParse],
["strtonum", Function::Strtonum],
["format_bytes", Function::FormatBytes],
["to_bytes", Function::ToBytes],
@@ -690,6 +694,8 @@ impl Function {
Fend => (smallvec![Str], Str),
Url | Path | SemVer => (smallvec![Str], MapStrStr),
Pairs => (smallvec![Str,Str,Str], MapStrStr),
Parse => (smallvec![Str, Str], MapStrStr),
RegexParse => (smallvec![Str, Str], MapIntStr),
Record => (smallvec![Str], MapStrStr),
Message => (smallvec![Str], MapStrStr),
DataUrl => (smallvec![Str], MapStrStr),
@@ -797,6 +803,7 @@ impl Function {
AppendIfMissing | PrependIfMissing | RemoveIfEnd | RemoveIfBegin => 2,
Pairs => 3,
LastPart => 2,
Parse | RegexParse => 2,
Record | Message => 1,
Quote | DoubleQuote => 1,
VarDump => 1,
@@ -923,6 +930,18 @@ impl Function {
val: BaseTy::Int,
}.abs())
}
RegexParse => {
Ok(Map {
key: BaseTy::Int,
val: BaseTy::Str,
}.abs())
}
Parse => {
Ok(Map {
key: BaseTy::Str,
val: BaseTy::Str,
}.abs())
}
Shlex | Func | Tuple | ParseArray => {
Ok(Map {
key: BaseTy::Int,
12 changes: 12 additions & 0 deletions src/bytecode.rs
Original file line number Diff line number Diff line change
@@ -312,6 +312,8 @@ pub(crate) enum Instr<'a> {
Trim(Reg<Str<'a>>, Reg<Str<'a>>, Reg<Str<'a>>),
Escape(Reg<Str<'a>>, Reg<Str<'a>>, Reg<Str<'a>>),
Truncate(Reg<Str<'a>>, Reg<Str<'a>>, Reg<Int>, Reg<Str<'a>>),
Parse(Reg<runtime::StrMap<'a, Str<'a>>>, Reg<Str<'a>>, Reg<Str<'a>>),
RegexParse(Reg<runtime::IntMap<Str<'a>>>, Reg<Str<'a>>, Reg<Str<'a>>),
Strtonum(Reg<Float>, Reg<Str<'a>>),
FormatBytes(Reg<Str<'a>>, Reg<Int>),
ToBytes(Reg<Int>, Reg<Str<'a>>),
@@ -999,6 +1001,16 @@ impl<'a> Instr<'a> {
len.accum(&mut f);
place_holder.accum(&mut f);
}
Parse(dst, text, template ) => {
dst.accum(&mut f);
text.accum(&mut f);
template.accum(&mut f);
}
RegexParse(dst, text, template ) => {
dst.accum(&mut f);
text.accum(&mut f);
template.accum(&mut f);
}
Strtonum(dst, text ) => {
dst.accum(&mut f);
text.accum(&mut f);
16 changes: 16 additions & 0 deletions src/codegen/intrinsics.rs
Original file line number Diff line number Diff line change
@@ -198,6 +198,8 @@ pub(crate) fn register_all(cg: &mut impl Backend) -> Result<()> {
[ReadOnly] encrypt(str_ref_ty, str_ref_ty, str_ref_ty, str_ref_ty) -> str_ty;
[ReadOnly] decrypt(str_ref_ty, str_ref_ty, str_ref_ty, str_ref_ty) -> str_ty;
[ReadOnly] url(str_ref_ty) -> map_ty;
[ReadOnly] parse(str_ref_ty,str_ref_ty) -> map_ty;
[ReadOnly] rparse(str_ref_ty,str_ref_ty) -> map_ty;
[ReadOnly] record(str_ref_ty) -> map_ty;
[ReadOnly] message(str_ref_ty) -> map_ty;
[ReadOnly] pairs(str_ref_ty, str_ref_ty, str_ref_ty) -> map_ty;
@@ -1139,6 +1141,20 @@ pub(crate) unsafe extern "C" fn truncate(src: *mut U128, len: Int, place_holder:
mem::transmute::<Str, U128>(res)
}

pub(crate) unsafe extern "C" fn parse(text: *mut U128, template: *mut U128) -> *mut c_void {
let text = &*(text as *mut Str);
let template = &*(template as *mut Str);
let res = string_util::parse(text.as_str(), template.as_str());
mem::transmute::<StrMap<Str>, *mut c_void>(res)
}

pub(crate) unsafe extern "C" fn rparse(text: *mut U128, template: *mut U128) -> *mut c_void {
let text = &*(text as *mut Str);
let template = &*(template as *mut Str);
let res = string_util::rparse(text.as_str(), template.as_str());
mem::transmute::<IntMap<Str>, *mut c_void>(res)
}

pub(crate) unsafe extern "C" fn kv_get(namespace: *mut U128, key: *mut U128) -> U128 {
let namespace = &*(namespace as *mut Str);
let key = &*(key as *mut Str);
12 changes: 12 additions & 0 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
@@ -1286,6 +1286,18 @@ pub(crate) trait CodeGenerator: Backend {
let resv = self.call_intrinsic(intrinsic!(truncate), &mut [src, len, place_holder])?;
self.bind_val(dst.reflect(),resv)
}
Parse(dst,text, template) => {
let text = self.get_val(text.reflect())?;
let template = self.get_val(template.reflect())?;
let resv = self.call_intrinsic(intrinsic!(parse), &mut [text, template])?;
self.bind_val(dst.reflect(),resv)
}
RegexParse(dst,text, template) => {
let text = self.get_val(text.reflect())?;
let template = self.get_val(template.reflect())?;
let resv = self.call_intrinsic(intrinsic!(rparse), &mut [text, template])?;
self.bind_val(dst.reflect(),resv)
}
KvGet(dst,namespace, key) => {
let namespace = self.get_val(namespace.reflect())?;
let key = self.get_val(key.reflect())?;
18 changes: 18 additions & 0 deletions src/compile.rs
Original file line number Diff line number Diff line change
@@ -1890,6 +1890,24 @@ impl<'a, 'b> View<'a, 'b> {
))
}
}
Parse => {
if res_reg != UNUSED {
self.pushl(LL::Parse(
res_reg.into(),
conv_regs[0].into(),
conv_regs[1].into(),
))
}
}
RegexParse => {
if res_reg != UNUSED {
self.pushl(LL::RegexParse(
res_reg.into(),
conv_regs[0].into(),
conv_regs[1].into(),
))
}
}
Strtonum => {
if res_reg != UNUSED {
self.pushl(LL::Strtonum(
8 changes: 8 additions & 0 deletions src/dataflow.rs
Original file line number Diff line number Diff line change
@@ -361,6 +361,14 @@ pub(crate) mod boilerplate {
f(dst.into(), Some(len.into()));
f(dst.into(), Some(place_holder.into()));
}
Parse(dst, text, template) => {
f(dst.into(), Some(text.into()));
f(dst.into(), Some(template.into()));
}
RegexParse(dst, text, template) => {
f(dst.into(), Some(text.into()));
f(dst.into(), Some(template.into()));
}
Strtonum(dst, text) => {
f(dst.into(), Some(text.into()));
}
2 changes: 2 additions & 0 deletions src/display.rs
Original file line number Diff line number Diff line change
@@ -181,6 +181,8 @@ impl Display for Function {
Fend => write!(f, "fend"),
Trim => write!(f, "trim"),
Truncate => write!(f, "truncate"),
Parse => write!(f, "parse"),
RegexParse => write!(f, "rparse"),
Strtonum => write!(f, "strtonum"),
FormatBytes => write!(f, "format_bytes"),
ToBytes => write!(f, "to_bytes"),
14 changes: 14 additions & 0 deletions src/interp.rs
Original file line number Diff line number Diff line change
@@ -835,6 +835,20 @@ impl<'a, LR: LineReader> Interp<'a, LR> {
let dst = *dst;
*self.get_mut(dst) = res;
}
Parse(dst, text, template) => {
let text = index(&self.strs, text);
let template = index(&self.strs, template);
let res = runtime::string_util::parse(text.as_str(), template.as_str());
let dst = *dst;
*self.get_mut(dst) = res;
}
RegexParse(dst, text, template) => {
let text = index(&self.strs, text);
let template = index(&self.strs, template);
let res = runtime::string_util::rparse(text.as_str(), template.as_str());
let dst = *dst;
*self.get_mut(dst) = res;
}
Record(dst, src) => {
let src = index(&self.strs, src);
let res = runtime::string_util::record(src.as_str());

0 comments on commit 552b6c1

Please sign in to comment.