Skip to content

Commit

Permalink
feat: add func() function
Browse files Browse the repository at this point in the history
  • Loading branch information
linux-china committed Mar 12, 2024
1 parent 466dee7 commit 1498443
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ pub enum Function {
DataUrl,
DateTime,
Shlex,
Func,
FromJson,
ToJson,
VarDump,
Expand Down Expand Up @@ -338,6 +339,7 @@ static_map!(
["path", Function::Path],
["datetime", Function::DateTime],
["shlex", Function::Shlex],
["func", Function::Func],
["http_get", Function::HttpGet],
["http_post", Function::HttpPost],
["s3_get", Function::S3Get],
Expand Down Expand Up @@ -650,6 +652,7 @@ impl Function {
DataUrl => (smallvec![Str], MapStrStr),
DateTime => (smallvec![Str], MapStrInt),
Shlex => (smallvec![Str], MapIntStr),
Func => (smallvec![Str], MapIntStr),
HttpGet => (smallvec![Str, MapStrStr], MapStrStr),
HttpPost => (smallvec![Str, MapStrStr, Str ], MapStrStr),
S3Get => (smallvec![Str, Str], Str),
Expand Down Expand Up @@ -730,7 +733,7 @@ impl Function {
Whoami | Os | OsFamily | Arch | Pwd | UserHome => 0,
Exit | ToUpper | ToLower | Clear | Srand | System | HexToInt | ToInt | EscapeCSV
| EscapeTSV | Close | Length | ReadErr | ReadErrCmd | Nextline | NextlineCmd
| Uuid | SnowFlake | Fend | Url | SemVer | Path | DataUrl | DateTime | Shlex | ToJson | FromJson | ToCsv | FromCsv | TypeOfVariable | IsArray | Unop(_) => 1,
| Uuid | SnowFlake | Fend | Url | SemVer | Path | DataUrl | DateTime | Shlex | Func | ToJson | FromJson | ToCsv | FromCsv | TypeOfVariable | IsArray | Unop(_) => 1,
SetFI | SubstrIndex | Match | Setcol | Binop(_) => 2,
JoinCSV | JoinTSV | Delete | Contains => 2,
DefaultIfEmpty => 2,
Expand Down Expand Up @@ -840,7 +843,7 @@ impl Function {
val: BaseTy::Int
}.abs())
}
Shlex => {
Shlex | Func => {
Ok(Map {
key: BaseTy::Int,
val: BaseTy::Str
Expand Down
5 changes: 5 additions & 0 deletions src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ pub(crate) enum Instr<'a> {
DataUrl(Reg<runtime::StrMap<'a, Str<'a>>>, Reg<Str<'a>>),
DateTime(Reg<runtime::StrMap<'a, Int>>, Reg<Str<'a>>),
Shlex(Reg<runtime::IntMap<Str<'a>>>, Reg<Str<'a>>),
Func(Reg<runtime::IntMap<Str<'a>>>, Reg<Str<'a>>),
Uniq(Reg<runtime::IntMap<Str<'a>>>, Reg<runtime::IntMap<Str<'a>>>, Reg<Str<'a>>),
TypeOfArray(Reg<Str<'a>>),
TypeOfNumber(Reg<Str<'a>>),
Expand Down Expand Up @@ -669,6 +670,10 @@ impl<'a> Instr<'a> {
dst.accum(&mut f);
text.accum(&mut f);
}
Func(dst, text) => {
dst.accum(&mut f);
text.accum(&mut f);
}
HttpGet(dst, url,headers) => {
dst.accum(&mut f);
url.accum(&mut f);
Expand Down
9 changes: 8 additions & 1 deletion src/codegen/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::runtime::{self, printf::{printf, FormatArg}, splitter::{
batch::{ByteReader, CSVReader, WhitespaceOffsets},
chunk::{ChunkProducer, OffsetChunk},
regex::RegexSplitter,
}, ChainedReader, FileRead, Float, Int, IntMap, Line, LineReader, RegexCache, Str, StrMap, math_util};
}, ChainedReader, FileRead, Float, Int, IntMap, Line, LineReader, RegexCache, Str, StrMap, math_util, string_util};
use crate::{
builtins::Variable,
common::{CancelSignal, Cleanup, FileSpec, Notification, Result},
Expand Down Expand Up @@ -193,6 +193,7 @@ pub(crate) fn register_all(cg: &mut impl Backend) -> Result<()> {
[ReadOnly] data_url(str_ref_ty) -> map_ty;
[ReadOnly] datetime(str_ref_ty) -> map_ty;
[ReadOnly] shlex(str_ref_ty) -> map_ty;
[ReadOnly] func(str_ref_ty) -> map_ty;
[ReadOnly] sqlite_query(str_ref_ty, str_ref_ty) -> map_ty;
[ReadOnly] sqlite_execute(str_ref_ty, str_ref_ty) -> int_ty;
[ReadOnly] mysql_query(str_ref_ty, str_ref_ty) -> map_ty;
Expand Down Expand Up @@ -1304,6 +1305,12 @@ pub(crate) unsafe extern "C" fn shlex(text: *mut U128) -> *mut c_void {
mem::transmute::<IntMap<Str>, *mut c_void>(res)
}

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

pub(crate) unsafe extern "C" fn sqlite_query(db_path: *mut U128, sql: *mut U128) -> *mut c_void {
let db_path = &*(db_path as *mut Str);
let sql = &*(sql as *mut Str);
Expand Down
1 change: 1 addition & 0 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,7 @@ pub(crate) trait CodeGenerator: Backend {
self.bind_val(dst.reflect(), resv)
}
Shlex(dst,text) => self.unop(intrinsic!(shlex), dst, text),
Func(dst,text) => self.unop(intrinsic!(func), dst, text),
SqliteQuery(dst,db_path,sql) => {
let db_path = self.get_val(db_path.reflect())?;
let sql = self.get_val(sql.reflect())?;
Expand Down
5 changes: 5 additions & 0 deletions src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2080,6 +2080,11 @@ impl<'a, 'b> View<'a, 'b> {
self.pushl(LL::Shlex(res_reg.into(), conv_regs[0].into()))
}
}
Func => {
if res_reg != UNUSED {
self.pushl(LL::Func(res_reg.into(), conv_regs[0].into()))
}
}
HttpGet => {
if res_reg != UNUSED {
self.pushl(LL::HttpGet(res_reg.into(), conv_regs[0].into(), conv_regs[1].into()))
Expand Down
1 change: 1 addition & 0 deletions src/dataflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ pub(crate) mod boilerplate {
DataUrl(dst, src) => f(dst.into(), Some(src.into())),
DateTime(dst, timestamp) => f(dst.into(), Some(timestamp.into())),
Shlex(dst, text) => f(dst.into(), Some(text.into())),
Func(dst, text) => f(dst.into(), Some(text.into())),
FromJson(dst, src) => f(dst.into(), Some(src.into())),
MapIntIntToJson(dst, arr) => f(dst.into(), Some(arr.into())),
MapIntFloatToJson(dst, arr) => f(dst.into(), Some(arr.into())),
Expand Down
1 change: 1 addition & 0 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ impl Display for Function {
DataUrl => write!(f, "data_url"),
DateTime => write!(f, "datetime"),
Shlex => write!(f, "shlex"),
Func => write!(f, "func"),
HttpGet => write!(f, "http_get"),
HttpPost => write!(f, "http_post"),
S3Get => write!(f, "s3_get"),
Expand Down
6 changes: 6 additions & 0 deletions src/interp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,12 @@ impl<'a, LR: LineReader> Interp<'a, LR> {
let dst = *dst;
*self.get_mut(dst) = res;
}
Func(dst, text) => {
let text = index(&self.strs, text);
let res = runtime::string_util::func(text.as_str());
let dst = *dst;
*self.get_mut(dst) = res;
}
HttpGet(dst, url, headers) => {
let url = index(&self.strs, url);
let headers = self.get(*headers);
Expand Down

0 comments on commit 1498443

Please sign in to comment.