Skip to content

Commit

Permalink
fix: 新增了call_method逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
Bylx666 committed Mar 21, 2024
1 parent f8ac65f commit 65f8c2c
Show file tree
Hide file tree
Showing 18 changed files with 287 additions and 200 deletions.
19 changes: 10 additions & 9 deletions samples/helloworld.ks
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@

//mod D:\code\rs\key-native\target\debug\key_native.dll> m
mod D:\code\rs\key-lang\samples\testmod.ks> m;
//mod D:\code\rs\key-lang\samples\testmod.ks> m;

let a(m) {
:m
class a {
a,
.t(v) {
log(v,t)
}
}
let o = {
a:a, b:a
}
let t = m-:Sample::new()
t.a = o
log(t.a.a(200))

let a = '{ff aa da}';
a.push(29u)
log(a)
2 changes: 1 addition & 1 deletion spec/classes.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## 定义结构

逗号可以省略
首字母必须大写
首字母建议大写
```
class MyClass {
a
Expand Down
10 changes: 8 additions & 2 deletions spec/primitives.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
## bool

唯一属性opposite
唯一属性rev

true.opposite == !true
true.rev == !true

方法rev, then

then会转达内部函数的返回值
false.rev().then(||:20) == 20

false.rev == false.rev() == true

## Func

Expand Down
2 changes: 1 addition & 1 deletion src/intern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct Interned {
p: *const Box<[u8]>
}
impl Interned {
pub fn vec(&self)-> &[u8] {
pub const fn vec(&self)-> &[u8] {
unsafe{&**self.p}
}
pub fn str(&self)-> String {
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn main()-> ExitCode {

intern::init();
let scanned = scan::scan(&fs::read("D:\\code\\rs\\key-lang\\samples\\helloworld.ks").unwrap());
println!("{scanned:?}");
// println!("{scanned:?}");
let exit = runtime::run(&scanned);
if let scan::literal::Litr::Int(code) = exit.returned {
return ExitCode::from(code as u8);
Expand Down
29 changes: 23 additions & 6 deletions src/primitive/buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,33 @@ use super::*;

pub fn method(v:&mut Vec<u8>, name:Interned, args:Vec<CalcRef>)-> Litr {
match name.vec() {
b"push"=> push(v, args),
b"splice"=> splice(v, args),
_=> err!("s")
_=> err!("Buf没有{}方法",name)
}
}

pub fn splice(v:&mut Vec<u8>, args:Vec<CalcRef>)-> Litr {
const fn to_u8(v:&Litr)-> u8 {
match v {
Litr::Int(n)=> *n as u8,
Litr::Uint(n)=> *n as u8,
_=> 0
}
}

pub fn push(v:&mut Vec<u8>, args:Vec<CalcRef>)-> Litr {
let mut args = args.into_iter();
let arg0 = match args.next() {
Some(v)=> v,
None=> err!("splice方法至少提供一个参数")
match &*next_arg!(args "'push'方法需要一个数字,列表或数组作为参数") {
Litr::Buf(right)=> v.extend_from_slice(right),
Litr::List(right)=> v.extend_from_slice(
&right.iter().map(|litr|to_u8(litr)).collect::<Vec<u8>>()),
n=> v.push(to_u8(n))
};
Litr::Uninit
}
}

fn splice(v:&mut Vec<u8>, args:Vec<CalcRef>)-> Litr {
let mut args = args.into_iter();
let arg0 = next_arg!(args "splice方法至少提供一个参数");
Litr::Uninit
}
2 changes: 1 addition & 1 deletion src/primitive/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Iterator for LitrIterator<'_> {
self.n = match next_f {
Some(f)=> {
let mut f = Box::new(f.f.clone());
f.bound = Some(Box::new(CalcRef::Ref(self.v)));
todo!();// f.bound = Some(Box::new(CalcRef::Ref(self.v)));
Box::into_raw(f) as usize
},
None=> panic!("迭代class需要定义.@next()方法")
Expand Down
File renamed without changes.
Empty file added src/primitive/list.rs
Empty file.
24 changes: 23 additions & 1 deletion src/primitive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::runtime::{calc::CalcRef, Class, err};
use crate::scan::literal::Litr;
use crate::intern::{Interned, intern};

pub mod std;
pub mod kstd;

pub mod buf;
pub mod int;
Expand All @@ -29,6 +29,7 @@ fn onclone(v:*mut NativeInstance)-> NativeInstance {unsafe{&*v}.clone()}
fn ondrop(_v:*mut NativeInstance) {}

static mut CLASSES:Option<Vec<(Interned, NativeClassDef)>> = None;

fn new_class(s:&[u8], f:Vec<(Interned, NativeFn)>)-> (Interned, NativeClassDef) {
let name = intern(s);
(name, NativeClassDef {
Expand All @@ -40,6 +41,7 @@ fn new_class(s:&[u8], f:Vec<(Interned, NativeFn)>)-> (Interned, NativeClassDef)
next, onclone, ondrop
})
}

pub fn classes()-> Vec<(Interned, Class)> {unsafe {
if let Some(cls) = &mut CLASSES {
cls.iter_mut().map(|(name, f)|(*name, Class::Native(f))).collect()
Expand All @@ -51,3 +53,23 @@ pub fn classes()-> Vec<(Interned, Class)> {unsafe {
classes()
}
}}

macro_rules! next_arg {
($args:ident $($err:literal)+)=> {
match $args.next() {
Some(v)=> v,
None=> err!($($err,)+)
}
};
($args:ident $t:ty:$e:ident:$($t_err:literal)+; $($err:literal)+)=> {
match $args.next() {
Some(v)=> match v {
Litr::$t(v)=> v,
_=> err!($($t_err,)+)
},
None=> err!($($err,)+)
}
}
}

pub(super) use next_arg;
21 changes: 12 additions & 9 deletions src/runtime/calc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,17 @@ impl Scope {
pub fn calc(mut self,e:&Expr)-> Litr {
match e {
Expr::Call { args, targ }=> {
// 先捕获方法调用
// if let Expr::Property(left, right) = targ {
// self.call_
// }
let targ = self.calc_ref(targ);
let args = args.iter().map(|v|self.calc_ref(v)).collect();
self.call(args, targ)
},

Expr::CallMethod { args, targ, name }=> {
let targ = self.calc_ref(targ);
let args = args.iter().map(|v|self.calc_ref(v)).collect();
self.call_method(args, targ, *name)
},

Expr::Index { left, i }=> {
let left = self.calc_ref(left);
let i = self.calc_ref(i);
Expand Down Expand Up @@ -406,7 +408,7 @@ fn expr_set(mut this: Scope, left: &Expr, right: Litr) {
match opt {
Some(f)=> {
let f = &mut f.f;
f.bound = Some(Box::new(CalcRef::Ref(left)));
todo!();// f.bound = Some(Box::new(CalcRef::Ref(left)));
f.scope.call_local(f, vec![i.own(), right]);
}
None=> err!("为'{}'实例索引赋值需要定义`@index_set`方法", cls.name)
Expand Down Expand Up @@ -468,7 +470,7 @@ fn get_prop(this:Scope, mut from:CalcRef, find:Interned)-> Litr {

// 以下都是对基本类型的getter行为
Litr::Bool(v)=> match find.vec() {
b"opposite"=> Litr::Bool(!*v),
b"rev"=> Litr::Bool(!*v),
_=> Litr::Uninit
},

Expand All @@ -483,8 +485,7 @@ fn get_prop(this:Scope, mut from:CalcRef, find:Interned)-> Litr {
match f {
Function::Local(_)=> Litr::Str("local".to_owned()),
Function::Extern(_)=> Litr::Str("extern".to_owned()),
Function::Native(_)=> Litr::Str("native".to_owned()),
Function::NativeMethod(_)=> unreachable!()
Function::Native(_)=> Litr::Str("native".to_owned())
}
}else {Litr::Uninit}

Expand All @@ -506,6 +507,7 @@ fn get_prop(this:Scope, mut from:CalcRef, find:Interned)-> Litr {
}
}


fn index(mut left:CalcRef, i:CalcRef)-> CalcRef {
// 先判断Obj
if let Litr::Obj(map) = &mut *left {
Expand All @@ -525,7 +527,7 @@ fn index(mut left:CalcRef, i:CalcRef)-> CalcRef {
let opt = cls.methods.iter_mut().find(|v|v.name == fname);
if let Some(f) = opt {
let f = &mut f.f;
f.bound = Some(Box::new(left));
todo!();// f.bound = Some(Box::new(left));
return CalcRef::Own(f.scope.call_local(f, vec![i.own()]));
}
err!("读取'{}'实例索引需要定义`@index_get`方法", cls.name)
Expand Down Expand Up @@ -565,6 +567,7 @@ fn index(mut left:CalcRef, i:CalcRef)-> CalcRef {
}
}


fn binary(mut this: Scope, left:&Box<Expr>, right:&Box<Expr>, op:&Box<[u8]>)-> Litr {
use Litr::*;
if &**op == b"=" {
Expand Down
Loading

0 comments on commit 65f8c2c

Please sign in to comment.