Skip to content

Commit

Permalink
fix: 修缮整数和Buf的实例方法行为
Browse files Browse the repository at this point in the history
  • Loading branch information
Bylx666 committed Apr 13, 2024
1 parent 3b35a24 commit 36a53d8
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 91 deletions.
11 changes: 2 additions & 9 deletions samples/helloworld.ks
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@

mod D:\code\rs\tst\target\debug\tstlib.dll> m;
let delay = m-.async_delay;
//mod D:\code\rs\tst\target\debug\tstlib.dll> m;

// 未使用fall, 因此不会阻塞
log(delay(233, 2000)); // 打印 Planet { Builtin }

// 使用fall
log(delay(233, 1000).fall()); // 1秒后打印233

// 由于未使用wait_inc, 第一个delay还未完成时程序就退出了
log(18446744073709551615u.int);
108 changes: 40 additions & 68 deletions src/primitive/buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ use super::*;

pub fn method(v:&mut Vec<u8>, scope:Scope, name:Interned, args:Vec<CalcRef>)-> Litr {
match name.vec() {
// binary edit
b"read"=> read(v, args),
b"read_float"=> read_float(v, args),
b"write"=> write(v, args),

// edit
b"push"=> push(v, args),
b"push_front"=> push_front(v, args),
Expand All @@ -22,13 +27,9 @@ pub fn method(v:&mut Vec<u8>, scope:Scope, name:Interned, args:Vec<CalcRef>)-> L
b"fill_clone"=> fill_clone(v, args),
b"rotate"=> rotate(v, args),
b"rev"=> rev(v),
b"concat"=> concat(v, args),
b"concat_clone"=> concat_clone(v, args),

// binary edit
b"read"=> read(v, args),
b"read_float"=> read(v, args),
b"write"=> write(v, args),
b"rev_clone"=> rev_clone(v),
b"slice"=> slice(v, args),
b"slice_clone"=> slice_clone(v, args),

// iter
b"for_each"=> for_each(v, args, scope),
Expand All @@ -37,17 +38,13 @@ pub fn method(v:&mut Vec<u8>, scope:Scope, name:Interned, args:Vec<CalcRef>)-> L
b"fold"=> fold(v, args, scope),
b"dedup"=> dedup(v, args, scope),
b"sort"=> sort(v, args, scope),
b"replace"=> replace(v, args),
b"replace_clone"=> replace_clone(v, args),

// part
b"filter"=> filter(v, args, scope),
b"filter_clone"=> filter_clone(v, args, scope),
b"slice"=> slice(v, args),
b"slice_clone"=> slice_clone(v, args),
b"part"=> part(v, args, scope),

// find
b"replace"=> replace(v, args),
b"replace_clone"=> replace_clone(v, args),
b"includes"=> includes(v, args),
b"index_of"=> index_of(v, args, scope),
b"r_index_of"=> r_index_of(v, args, scope),
Expand All @@ -59,10 +56,10 @@ pub fn method(v:&mut Vec<u8>, scope:Scope, name:Interned, args:Vec<CalcRef>)-> L
b"as_utf8"=> as_utf8(v),
b"as_utf16"=> as_utf16(v),
b"to_list"=> Litr::List(v.iter().map(|n|Litr::Uint(*n as usize)).collect()),
b"join"=> join(v, args),

b"last"=> last(v),
b"expand"=> expand(v, args),
b"join"=> join(v, args),
_=> panic!("Buf没有{}方法",name)
}
}
Expand All @@ -81,6 +78,11 @@ const fn to_usize(v:&Litr)-> usize {
_=> 0
}
}
const fn to_str(s:&[u8])-> &str {
// SAFETY: 本方法使用std::str::pattern的实现,
// 其底层依赖字节比较 并不检查字符边界
unsafe {std::str::from_utf8_unchecked(s)}
}

/// 推进数组或者单数字
fn push(v:&mut Vec<u8>, args:Vec<CalcRef>)-> Litr {
Expand Down Expand Up @@ -199,7 +201,7 @@ fn pop(v:&mut Vec<u8>, args:Vec<CalcRef>)-> Litr {
Litr::Int(n)=> *n as usize,
_=> panic!("buf.pop的参数必须为整数")
};
if at >= v.len() {
if at > v.len() {
panic!("分界线索引{at}大于数组长度{}", v.len());
}

Expand All @@ -220,7 +222,7 @@ fn pop_front(v:&mut Vec<u8>, args:Vec<CalcRef>)-> Litr {
Litr::Int(n)=> *n as usize,
_=> panic!("buf.pop_front的参数必须为整数")
};
if at >= v.len() {
if at > v.len() {
panic!("分界线索引{at}大于数组长度{}", v.len());
}

Expand All @@ -238,6 +240,12 @@ fn rev(v:&mut Vec<u8>)-> Litr {
v.reverse();
Litr::Uninit
}
/// rev
fn rev_clone(v:&mut Vec<u8>)-> Litr {
let mut v = v.clone();
v.reverse();
Litr::Buf(v)
}

/// 将当前数组按函数过滤
fn filter(v:&mut Vec<u8>, args:Vec<CalcRef>, scope:Scope)-> Litr {
Expand Down Expand Up @@ -486,43 +494,6 @@ fn rotate(v:&mut Vec<u8>, args:Vec<CalcRef>)-> Litr {
Litr::Uninit
}

/// 将另一个Buf连接到自己后面
fn concat(v:&mut Vec<u8>, args:Vec<CalcRef>)-> Litr {
let other_tmp;
let other = match &**args.get(0).expect("buf.concat需要传入另一个Buf或数组") {
Litr::List(b)=> {
other_tmp = b.iter().map(|n|to_u8(n)).collect();
&other_tmp
}
Litr::Buf(b)=> b,
n=> {
v.push(to_u8(n));
return Litr::Uninit;
}
};
v.extend_from_slice(other);
Litr::Uninit
}

/// concat复制版本
fn concat_clone(v:&mut Vec<u8>, args:Vec<CalcRef>)-> Litr {
let mut v = v.clone();
let other_tmp;
let other = match &**args.get(0).expect("buf.concat需要传入另一个Buf或数组") {
Litr::List(b)=> {
other_tmp = b.iter().map(|n|to_u8(n)).collect();
&other_tmp
}
Litr::Buf(b)=> b,
n=> {
v.push(to_u8(n));
return Litr::Uninit;
}
};
v.extend_from_slice(other);
Litr::Buf(v)
}

/// 将十六进制数以字符的格式渲染, 传入一个分隔符
fn join(v:&mut Vec<u8>, args:Vec<CalcRef>)-> Litr {
if v.len()==0 {return Litr::Str(String::new());}
Expand Down Expand Up @@ -582,10 +553,16 @@ fn slice_clone(v:&mut Vec<u8>, args:Vec<CalcRef>)-> Litr {

/// 是否存在一个数
fn includes(v:&mut Vec<u8>, args:Vec<CalcRef>)-> Litr {
let find = to_u8(args.get(0).expect("buf.includes需要知道你要找啥"));
Litr::Bool(match v.iter().find(|&&n|n==find) {
Some(_)=> true,
None=> false
Litr::Bool(match &**args.get(0).expect("buf.includes需要知道你要找啥") {
Litr::Str(s)=> to_str(v).contains(s),
Litr::Buf(s)=> to_str(v).contains(to_str(s)),
n=> {
let find = to_u8(n);
match v.iter().find(|&&n|n==find) {
Some(_)=> true,
None=> false
}
}
})
}

Expand Down Expand Up @@ -682,7 +659,7 @@ fn read(v:&mut Vec<u8>, args:Vec<CalcRef>)-> Litr {
);

// 访问溢出时直接返回0
if sz / 8 + index >= v.len() {
if sz / 8 + index > v.len() {
return Litr::Uint(0);
}

Expand All @@ -709,10 +686,10 @@ fn read(v:&mut Vec<u8>, args:Vec<CalcRef>)-> Litr {
/// 在指定偏移读取一个Float, 第二个参数取决于机器的大小端, true不一定指大端序
fn read_float(v:&mut Vec<u8>, args:Vec<CalcRef>)-> Litr {
let index = args.get(0).map_or(0, |n|to_usize(n));
if index + 8 >= v.len() {
if index + 8 > v.len() {
return Litr::Float(0.0);
}
let big_endian = args.get(2).map_or(false, |n|
let big_endian = args.get(1).map_or(false, |n|
match &**n {
Litr::Bool(b)=> *b,
_=> false
Expand Down Expand Up @@ -744,22 +721,17 @@ fn as_utf16(v:&mut Vec<u8>)-> Litr {

/// 替换的内部方法
fn _replace(v:&mut Vec<u8>, args:Vec<CalcRef>)-> String {
const fn to_str(s:&[u8])-> &str {
// SAFETY: 本方法使用std::str::pattern的实现,
// 其底层依赖字节比较 并不检查字符边界
unsafe {std::str::from_utf8_unchecked(s)}
}
let s = to_str(v);

let from = match &** args.get(0).expect("str.replace需要一个搜索Buf") {
let from = match &** args.get(0).expect("buf.replace需要一个搜索Buf") {
Litr::Str(s)=> s,
Litr::Buf(s)=> to_str(s),
_=> panic!("str.replace第一个参数必须是Buf或Str")
_=> panic!("buf.replace第一个参数必须是Buf或Str")
};
let to = args.get(1).map_or("", |n| match &**n {
Litr::Str(s)=> s,
Litr::Buf(s)=> to_str(s),
_=> panic!("str.replace第二个参数必须是Buf或Str")
_=> panic!("buf.replace第二个参数必须是Buf或Str")
});

if let Some(n) = args.get(2) {
Expand Down
33 changes: 19 additions & 14 deletions src/primitive/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,25 @@ pub fn method_int(n:isize, name:Interned, args:Vec<CalcRef>)-> Litr {
args.get(0).map_or(0, |n|$deal(n))
}}
match name.vec() {
b"abs"=> Litr::Uint(n.unsigned_abs()),
b"popcnt"=> Litr::Uint(n.count_ones() as _),
b"rev"=> Litr::Int(n.swap_bytes()),
b"leading"=> Litr::Uint(n.leading_zeros() as _),
b"ending"=> Litr::Uint(n.trailing_zeros() as _),
b"pow"=> args.get(0).map_or(Litr::Int(1), |val|match &**val{
Litr::Uint(r)=> Litr::Int(n.pow(*r as _)),
Litr::Int(r)=> Litr::Int(n.pow(*r as _)),
Litr::Float(r)=> Litr::Float((n as f64).powf(*r)),
_=> Litr::Int(1)
}),
b"sqrt"=> Litr::Int(n.pow(2)),
b"log"=> Litr::Uint(n.ilog(get_arg0!(to_isize)) as _),
b"log2"=> Litr::Uint(n.ilog2() as _),
b"log10"=> Litr::Uint(n.ilog10() as _),

b"abs"=> Litr::Uint(n.unsigned_abs()),
b"as_buf"=> Litr::Buf(n.to_ne_bytes().to_vec()),
b"to_oct"=> Litr::Str(format!("{:o}", n)),
b"to_str"=> Litr::Str(n.to_string()),
b"to_oct"=> Litr::Str(format!("{:o}", n)),
b"to_hex"=> Litr::Str(format!("{:X}", n)),

b"min"=> Litr::Int(n.min(get_arg0!(to_isize))),
b"max"=> Litr::Int(n.max(get_arg0!(to_isize))),
b"rev"=> Litr::Int(n.swap_bytes()),
_=> panic!("{}上没有{}方法","Int",name)
}
}
Expand All @@ -59,26 +57,33 @@ pub fn method_uint(n:usize, name:Interned, args:Vec<CalcRef>)-> Litr {
args.get(0).map_or(0, |n|$deal(n))
}}
match name.vec() {
b"popcnt"=> Litr::Uint(n.count_ones() as _),
b"rev"=> Litr::Uint(n.swap_bytes()),
b"leading"=> Litr::Uint(n.leading_zeros() as _),
b"ending"=> Litr::Uint(n.trailing_zeros() as _),
b"pow"=> args.get(0).map_or(Litr::Uint(1), |val|match &**val{
Litr::Uint(r)=> Litr::Uint(n.pow(*r as _)),
Litr::Int(r)=> Litr::Uint(n.pow(*r as _)),
Litr::Float(r)=> Litr::Float((n as f64).powf(*r)),
_=> Litr::Uint(1)
}),
b"sqrt"=> Litr::Uint(n.pow(2)),
b"log"=> Litr::Uint(n.ilog(get_arg0!(to_usize)) as _),
b"log2"=> Litr::Uint(n.ilog2() as _),
b"log10"=> Litr::Uint(n.ilog10() as _),

b"as_buf"=> Litr::Buf(n.to_ne_bytes().to_vec()),
b"rotate_left"=> Litr::Uint(n.rotate_left(get_arg0!(to_u32))),
b"rotate_right"=> Litr::Uint(n.rotate_right(get_arg0!(to_u32))),
b"as8"=> Litr::Buf(vec![n as u8]),
b"as16"=> Litr::Buf(unsafe{std::mem::transmute::<_,[u8;2]>(n as u16)}.to_vec()),
b"as32"=> Litr::Buf(unsafe{std::mem::transmute::<_,[u8;4]>(n as u32)}.to_vec()),
b"as64"=> Litr::Buf(n.to_ne_bytes().to_vec()),
b"to_oct"=> Litr::Str(format!("{:o}", n)),
b"to_str"=> Litr::Str(n.to_string()),
b"to_hex"=> Litr::Str(format!("{:X}", n)),

// bin
b"popcnt"=> Litr::Uint(n.count_ones() as _),
b"rev"=> Litr::Uint(n.swap_bytes()),
b"leading"=> Litr::Uint(n.leading_zeros() as _),
b"ending"=> Litr::Uint(n.trailing_zeros() as _),
b"rotate_left"=> Litr::Uint(n.rotate_left(get_arg0!(to_u32))),
b"rotate_right"=> Litr::Uint(n.rotate_right(get_arg0!(to_u32))),

b"min"=> Litr::Uint(n.min(get_arg0!(to_usize))),
b"max"=> Litr::Uint(n.max(get_arg0!(to_usize))),
_=> panic!("{}上没有{}方法","Uint",name)
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/calc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,8 @@ fn binary(mut this: Scope, left:&Box<Expr>, right:&Box<Expr>, op:&Box<[u8]>)-> L
(Float(l),Float(r))=> Float(l $op r),
(Float(l),Int(r))=> Float(l $op *r as f64),
(Int(l),Float(r))=> Float(*l as f64 $op r),
(Float(l),Uint(r))=> Float(l $op *r as f64),
(Uint(l),Float(r))=> Float(*l as f64 $op r),
(l,r)=> panic!("{}运算无法应用于{:?}和{:?}", stringify!($op), l, r)
}
}};
Expand Down

0 comments on commit 36a53d8

Please sign in to comment.