diff --git a/samples/helloworld.ks b/samples/helloworld.ks index 432b590..344ce33 100644 --- a/samples/helloworld.ks +++ b/samples/helloworld.ks @@ -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) diff --git a/spec/classes.md b/spec/classes.md index 361a42b..6494a2b 100644 --- a/spec/classes.md +++ b/spec/classes.md @@ -1,7 +1,7 @@ ## 定义结构 逗号可以省略 -首字母必须大写 +首字母建议大写 ``` class MyClass { a diff --git a/spec/primitives.md b/spec/primitives.md index d6cd80a..39ecbf7 100644 --- a/spec/primitives.md +++ b/spec/primitives.md @@ -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 diff --git a/src/intern.rs b/src/intern.rs index 19b1d34..0ee4b67 100644 --- a/src/intern.rs +++ b/src/intern.rs @@ -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 { diff --git a/src/main.rs b/src/main.rs index 8bcaaac..03200da 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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); diff --git a/src/primitive/buf.rs b/src/primitive/buf.rs index dec55fe..1c16fcf 100644 --- a/src/primitive/buf.rs +++ b/src/primitive/buf.rs @@ -2,16 +2,33 @@ use super::*; pub fn method(v:&mut Vec, name:Interned, args:Vec)-> 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, args:Vec)-> 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, args:Vec)-> 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::>()), + n=> v.push(to_u8(n)) }; Litr::Uninit -} \ No newline at end of file +} + +fn splice(v:&mut Vec, args:Vec)-> Litr { + let mut args = args.into_iter(); + let arg0 = next_arg!(args "splice方法至少提供一个参数"); + Litr::Uninit +} diff --git a/src/primitive/iter.rs b/src/primitive/iter.rs index 41d173c..c57610c 100644 --- a/src/primitive/iter.rs +++ b/src/primitive/iter.rs @@ -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()方法") diff --git a/src/primitive/std.rs b/src/primitive/kstd.rs similarity index 100% rename from src/primitive/std.rs rename to src/primitive/kstd.rs diff --git a/src/primitive/list.rs b/src/primitive/list.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/primitive/mod.rs b/src/primitive/mod.rs index a538f47..2674fac 100644 --- a/src/primitive/mod.rs +++ b/src/primitive/mod.rs @@ -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; @@ -29,6 +29,7 @@ fn onclone(v:*mut NativeInstance)-> NativeInstance {unsafe{&*v}.clone()} fn ondrop(_v:*mut NativeInstance) {} static mut CLASSES:Option> = None; + fn new_class(s:&[u8], f:Vec<(Interned, NativeFn)>)-> (Interned, NativeClassDef) { let name = intern(s); (name, NativeClassDef { @@ -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() @@ -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; diff --git a/src/runtime/calc.rs b/src/runtime/calc.rs index e98e243..21a4c7e 100644 --- a/src/runtime/calc.rs +++ b/src/runtime/calc.rs @@ -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); @@ -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) @@ -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 }, @@ -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} @@ -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 { @@ -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) @@ -565,6 +567,7 @@ fn index(mut left:CalcRef, i:CalcRef)-> CalcRef { } } + fn binary(mut this: Scope, left:&Box, right:&Box, op:&Box<[u8]>)-> Litr { use Litr::*; if &**op == b"=" { diff --git a/src/runtime/call.rs b/src/runtime/call.rs index c266b2b..a95ffaf 100644 --- a/src/runtime/call.rs +++ b/src/runtime/call.rs @@ -11,7 +11,6 @@ impl Scope { use Function::*; match exec { Native(f)=> f(args, self), - NativeMethod(f)=> (f.f)(f.bound.clone(), args, self), Local(f)=> { let args = args.into_iter().map(|e|e.own()).collect(); self.call_local(&f, args) @@ -27,70 +26,91 @@ impl Scope { } /// 为a.b()的行为匹配对应方法并调用 - fn call_method(mut self, left:&Expr, right:Interned, args:Vec)-> Litr { - let mut left = self.calc_ref(left); - // 匹配所有可能使用.运算符得到函数的类型(instance, obj) - match &mut *left { - Litr::Inst(inst)=> { - let cannot_access_private = unsafe {(*inst.cls).module} != self.exports; - let cls = unsafe {&*inst.cls}; - - // 先找方法 - let methods = &cls.methods; - for mthd in methods.iter() { - if mthd.name == right { - if !mthd.public && cannot_access_private { - err!("'{}'类型的成员方法'{}'是私有的", cls.name, right) - } - // 为函数绑定self - let mut f = mthd.f.clone(); - f.bound = Some(Box::new(left)); - let args = args.into_iter().map(|v|v.own()).collect(); - return self.call_local(&f, args); - } - } - - // 再找属性 - let props = &cls.props; - for (n, prop) in props.iter().enumerate() { - if prop.name == right { - if !prop.public && cannot_access_private { - err!("'{}'类型的成员属性'{}'是私有的", cls.name, right) - } - return self.call(args, CalcRef::Ref(&mut inst.v[n])); + pub fn call_method(mut self, mut args:Vec, mut targ:CalcRef, name:Interned)-> Litr { + match &mut*targ { + Litr::Bool(v)=> match name.vec() { + b"rev"=> Litr::Bool(!*v), + b"then"=> { + let f = match args.get_mut(0) { + Some(f)=> { + let mut s = CalcRef::uninit(); + std::mem::swap(&mut s,f); + s + }, + None=> return Litr::Uninit + }; + if *v { + self.call(vec![], f) + }else { + Litr::Uninit } } + _=> err!("Bool类型只有'rev'和'then'方法") } - Litr::Ninst(inst)=> { - use crate::native::{NativeInstance, NaitveInstanceRef}; + Litr::Buf(v)=> primitive::buf::method(v, name, args), + _=> err!("没有'{}'方法\n 如果你需要调用属性作为函数,请使用(a.b)()的写法", name) + } + // let mut left = self.calc_ref(left); + // // 匹配所有可能使用.运算符得到函数的类型(instance, obj) + // match &mut *left { + // Litr::Inst(inst)=> { + // let cannot_access_private = unsafe {(*inst.cls).module} != self.exports; + // let cls = unsafe {&*inst.cls}; + + // // 先找方法 + // let methods = &cls.methods; + // for mthd in methods.iter() { + // if mthd.name == right { + // if !mthd.public && cannot_access_private { + // err!("'{}'类型的成员方法'{}'是私有的", cls.name, right) + // } + // // 为函数绑定self + // let mut f = mthd.f.clone(); + // f.bound = Some(Box::new(left)); + // let args = args.into_iter().map(|v|v.own()).collect(); + // return self.call_local(&f, args); + // } + // } + + // // 再找属性 + // let props = &cls.props; + // for (n, prop) in props.iter().enumerate() { + // if prop.name == right { + // if !prop.public && cannot_access_private { + // err!("'{}'类型的成员属性'{}'是私有的", cls.name, right) + // } + // return self.call(args, CalcRef::Ref(&mut inst.v[n])); + // } + // } + // } + // Litr::Ninst(inst)=> { + // use crate::native::{NativeInstance, NaitveInstanceRef}; - let cls = unsafe{&*inst.cls}; - let inst: *mut NativeInstance = inst; - let bound = match left { - CalcRef::Own(v)=> if let Litr::Ninst(inst_own) = v { - NaitveInstanceRef::Own(inst_own) - }else {unreachable!()} - CalcRef::Ref(_)=> NaitveInstanceRef::Ref(inst) - }; - - // 先找方法 - for (name, f) in cls.methods.iter() { - if *name == right { - return f(bound, args, self); - } - } + // let cls = unsafe{&*inst.cls}; + // let inst: *mut NativeInstance = inst; + // let bound = match left { + // CalcRef::Own(v)=> if let Litr::Ninst(inst_own) = v { + // NaitveInstanceRef::Own(inst_own) + // }else {unreachable!()} + // CalcRef::Ref(_)=> NaitveInstanceRef::Ref(inst) + // }; - // 再找属性 - return self.call(args, CalcRef::Own((cls.getter)(inst, right))); - } - Litr::Obj(map)=> return self.call( - args, CalcRef::Ref(map.get_mut(&right).unwrap_or_else(||err!("'{}'不是一个函数", right)))), - Litr::Bool(v)=> err!("Bool没有方法"), - Litr::Buf(v)=> return primitive::buf::method(v, right, args), - _=> () - } + // // 先找方法 + // for (name, f) in cls.methods.iter() { + // if *name == right { + // return f(bound, args, self); + // } + // } - err!("'{}'不是一个方法或函数", right) + // // 再找属性 + // return self.call(args, CalcRef::Own((cls.getter)(inst, right))); + // } + // Litr::Obj(map)=> return self.call( + // args, CalcRef::Ref(map.get_mut(&right).unwrap_or_else(||err!("'{}'不是一个函数", right)))), + // Litr::Bool(v)=> err!("Bool没有方法"), + // Litr::Buf(v)=> return primitive::buf::method(v, right, args), + // _=> () + // } } /// 实际调用一个local function @@ -102,10 +122,8 @@ impl Scope { let arg = args.next().unwrap_or(argdecl.default.clone()); vars.push((argdecl.name, arg)); } - // 如果函数被bind了就用bound值,否则继续沿用上级self - let kself = if let Some(s) = &f.bound { - (&***s) as *const Litr as *mut Litr - }else {self.kself}; + // 如果函数被bound了就用bound值,否则继续沿用上级self + let kself = self.kself; let mut ret = Litr::Uninit; let mut scope = Scope::new(ScopeInner { diff --git a/src/runtime/mod.rs b/src/runtime/mod.rs index 40375fd..579d629 100644 --- a/src/runtime/mod.rs +++ b/src/runtime/mod.rs @@ -261,7 +261,7 @@ pub fn run(s:&Statements)-> RunResult { pub fn top_scope(return_to:*mut Litr, imports:*mut Vec<(Interned, Module)>, exports:*mut LocalMod, kself:*mut Litr)-> Scope { let mut vars = Vec::<(Interned, Litr)>::with_capacity(16); vars.push((intern(b"log"), - Litr::Func(Function::Native(crate::primitive::std::log))) + Litr::Func(Function::Native(crate::primitive::kstd::log))) ); let mut class_uses = crate::primitive::classes(); diff --git a/src/scan/charts.rs b/src/scan/charts.rs index 1ffad31..5b17950 100644 --- a/src/scan/charts.rs +++ b/src/scan/charts.rs @@ -1,7 +1,7 @@ //! 映射表 /// 返回符号优先级 -pub fn prec(x:&[u8])-> u8 { +pub const fn prec(x:&[u8])-> u8 { match x { b"-."|b"-:" => 16, b"is"=> 15, @@ -25,13 +25,14 @@ pub const PREC_UNARY:u8 = 12; /// 转义符表 -pub fn escape(c:u8)-> u8 { +pub const fn escape(c:u8)-> u8 { match c { b'n'=> b'\n', b'r'=> b'\r', b't'=> b'\t', b'\\'=> b'\\', b'0'=> 0, + b'`'=> b'`', _=> 255 } } diff --git a/src/scan/expr.rs b/src/scan/expr.rs index a10b7da..4863d2f 100644 --- a/src/scan/expr.rs +++ b/src/scan/expr.rs @@ -2,63 +2,75 @@ use super::{Scanner, charts}; use super::literal::{ KsType, Litr, LocalFuncRaw }; -use crate::intern::Interned; +use crate::intern::{intern, Interned}; /// 可以出现在任何右值的,expression表达式 #[derive(Debug, Clone)] pub enum Expr { Empty, - // 字面量 + /// 字面量 Literal(Litr), - // 变量 + /// 变量 Variant(Interned), - // self + /// self Kself, - // 未绑定作用域的本地函数 + /// 未绑定作用域的本地函数 LocalDecl (LocalFuncRaw), - // -.运算符 + /// -.运算符 module-.func ModFuncAcc(Interned, Interned), - // -:运算符 + /// -:运算符 module-:Class ModClsAcc (Interned, Interned), - // .运算符 + /// .运算符 a.b Property (Box, Interned), - // ::运算符 + /// ::运算符 Class::static_method ImplAccess(Box, Interned), - // 调用函数 - Call{ + + /// 调用函数 x() + Call { args: Vec, targ: Box }, + + /// 调用方法 x.method() + CallMethod { + args: Vec, + targ: Box, + name: Interned + }, + + /// 索引表达式 Index{ left: Box, i: Box }, - // 创建实例 + + /// 创建实例 NewInst{ cls: Box, val: Vec<(Interned,Expr)> }, - // 列表表达式 + /// 列表表达式 List(Vec), - // 对象表达式 + /// 对象表达式 Obj(Vec<(Interned,Expr)>), - // 一元运算 ! - + /// 一元运算 ! - Unary{ right: Box, op: u8 }, - // 二元运算 + /// 二元运算 Binary{ left: Box, right: Box, op: Box<[u8]> }, + /// is表达式 a is ClassA Is { left: Box, right: Box @@ -106,8 +118,8 @@ impl Scanner<'_> { let right = expr_stack.pop().unwrap(); let left = expr_stack.pop().unwrap(); - - // 如果是模块或类的调用就不用Binary + + // 对于模块访问左右都必须是标识符 macro_rules! impl_access {($op:literal, $ty:ident)=>{{ if last_op == $op { if let Expr::Variant(left) = left { @@ -123,15 +135,6 @@ impl Scanner<'_> { impl_access!(b"-.",ModFuncAcc); impl_access!(b"-:",ModClsAcc); - // 属性表达式 - if last_op == b"." { - if let Expr::Variant(right) = right { - expr_stack.push(Expr::Property(Box::new(left), right )); - continue; - } - self.err("`.`右侧需要一个标识符") - } - // ::表达式 if last_op == b"::" { match right { @@ -144,6 +147,7 @@ impl Scanner<'_> { continue; } + // is表达式 if last_op == b"is" { expr_stack.push(Expr::Is { left: Box::new(left), @@ -174,71 +178,68 @@ impl Scanner<'_> { return expr_stack.pop().unwrap(); } - // 如果用户想用返回语句就直接以此分界 - if op == b":" { - unsafe {(*self.i) -= 1;} - return expr_stack.pop().unwrap(); - } - - // 如果此运算符是括号就代表call - if op == b"(" { - self.next(); - self.spaces(); - let targ = Box::new(expr_stack.pop().unwrap()); - let mut args = Vec::new(); - - // 如果直接遇到右括号则代表无参数传入 - if self.cur() == b')' { + // 对二元运算符的各种情况做处理 + match op { + // 如果用户想用返回语句就直接以此分界 + b":"=> { + unsafe {(*self.i) -= 1;} + return expr_stack.pop().unwrap(); + } + + // 如果此运算符是括号就代表call + b"("=> { self.next(); - expr_stack.push(Expr::Call{ - args, targ - }); + self.spaces(); + let targ = Box::new(expr_stack.pop().unwrap()); + let mut args = parse_input_args(self); + expr_stack.push(Expr::Call { args, targ }); continue; } - loop { - let e = self.expr(); - // 调用参数留空就当作uninit - args.push(if let Expr::Empty = e { - Expr::Literal(Litr::Uninit) - }else {e}); + // 如果是.就说明是属性或者调用方法 + b"."=> { + let left = Box::new(expr_stack.pop().unwrap()); + let name = match self.ident() { + Some(n)=> intern(n), + None=> self.err("'.'右边需要属性名") + }; self.spaces(); - if self.cur() != b',' { - break; + // 属性后直接使用括号就是调用方法 + if self.cur() == b'(' { + self.next(); + let args = parse_input_args(self); + expr_stack.push(Expr::CallMethod { args, targ: left, name }); + }else { + expr_stack.push(Expr::Property(left, name)); } - self.next(); - } - if self.i() >= self.src.len() || self.cur() != b')' { - self.err("未闭合的右括号')'。"); + continue; } - self.next(); - expr_stack.push(Expr::Call{ - args, targ - }); - continue; - } - // 如果此运算符是方括号就代表index - if op == b"[" { - self.next(); - self.spaces(); - let left = Box::new(expr_stack.pop().unwrap()); - let i = Box::new(self.expr()); - if self.i() >= self.src.len() || self.cur() != b']' { - self.err("未闭合的右括号']'。"); + // 如果此运算符是方括号就代表index + b"["=> { + self.next(); + self.spaces(); + let left = Box::new(expr_stack.pop().unwrap()); + let i = Box::new(self.expr()); + if self.i() >= self.src.len() || self.cur() != b']' { + self.err("未闭合的右括号']'。"); + } + self.next(); + expr_stack.push(Expr::Index{ + left, i + }); + continue; } - self.next(); - expr_stack.push(Expr::Index{ - left, i - }); - continue; + _=>() } - + // 将新运算符和它右边的值推进栈 self.spaces(); + // 看看右侧值前有没有一元运算符 let mut una = self.expr_unary(); unary.append(&mut una); + // 在此之前判断有没有括号来提升优先级 if self.cur() == b'(' { let group = self.expr_group(); @@ -247,8 +248,8 @@ impl Scanner<'_> { let litr = self.literal(); expr_stack.push(litr); } - op_stack.push(op); - + + op_stack.push(op); } } @@ -262,7 +263,7 @@ impl Scanner<'_> { self.next(); return Expr::Literal(Litr::Uninit); } - + let expr = self.expr(); self.spaces(); if self.i() >= self.src.len() || self.cur() != b')' { @@ -289,3 +290,31 @@ impl Scanner<'_> { v } } + +/// 解析传入参数 +fn parse_input_args(this:&Scanner)-> Vec { + let mut args = Vec::new(); + // 如果直接遇到右括号则代表无参数传入 + if this.cur() == b')' { + this.next(); + return args; + } + + loop { + let e = this.expr(); + // 调用参数留空就当作uninit + args.push(if let Expr::Empty = e { + Expr::Literal(Litr::Uninit) + }else {e}); + this.spaces(); + if this.cur() != b',' { + break; + } + this.next(); + } + if this.i() >= this.src.len() || this.cur() != b')' { + this.err("未闭合的右括号')'。"); + } + this.next(); + args +} \ No newline at end of file diff --git a/src/scan/literal.rs b/src/scan/literal.rs index 6e0e40b..b0d076e 100644 --- a/src/scan/literal.rs +++ b/src/scan/literal.rs @@ -42,8 +42,7 @@ impl Litr { match *f { Function::Local(_)=> "".to_string(), Function::Extern(_)=> "".to_string(), - Function::Native(_)=> "".to_string(), - Function::NativeMethod(_)=> "".to_string() + Function::Native(_)=> "".to_string() } } Str(s)=> s.clone(), @@ -129,8 +128,6 @@ impl Litr { pub enum Function { // Native模块或Runtime提供的Rust函数 Native(crate::native::NativeFn), - // 绑定了self的原生函数 - NativeMethod(crate::native::BoundNativeMethod), // 脚本定义的本地函数 Local(LocalFunc), // 使用extern语句得到的C函数 @@ -159,16 +156,13 @@ pub struct LocalFunc { pub ptr:*const LocalFuncRaw, /// 来自的作用域 pub scope: Scope, - /// 是否绑定了self (如果直接绑定了字面量将会持有其所有权) - pub bound: Option>, } impl LocalFunc { /// 将本地函数定义和作用域绑定 pub fn new(ptr:*const LocalFuncRaw, scope: Scope)-> Self { LocalFunc{ ptr, - scope, - bound: None + scope } } } @@ -202,7 +196,7 @@ impl Clone for Instance { match opt { Some(cls_f)=> { let f = &mut cls_f.f; - f.bound = Some(Box::new(CalcRef::Own(Litr::Inst(cloned)))); + todo!();// f.bound = Some(Box::new(CalcRef::Own(Litr::Inst(cloned)))); let res = f.scope.call_local(f, vec![]); if let Litr::Inst(v) = res { v @@ -225,7 +219,7 @@ impl Drop for Instance { let f = &mut cls_f.f; // 不要额外调用clone let binding = &mut *std::mem::ManuallyDrop::new(Litr::Inst(Instance { cls: self.cls, v: self.v.clone() })); - f.bound = Some(Box::new(CalcRef::Ref(binding))); + todo!();// f.bound = Some(Box::new(CalcRef::Ref(binding))); f.scope.call_local(f, vec![]); } None=> () @@ -280,14 +274,12 @@ impl Scanner<'_> { i += 1; let mut start = i; // 开始结算的起点 let mut vec = Vec::::new(); - + loop { let c = self.src[i]; match c { b'`' => break, b'\\'=> { - use charts::escape; - // 结算一次 vec.extend_from_slice(&self.src[start..i]); @@ -308,7 +300,7 @@ impl Scanner<'_> { b'\n'=> escape_enter!(), // 非换行符就按转义表转义 _=> { - let escaped = escape(escaper); + let escaped = charts::escape(escaper); if escaped == 255 { self.err(&format!("错误的转义符:{}", String::from_utf8_lossy(&[escaper]))); } @@ -316,20 +308,20 @@ impl Scanner<'_> { i += 1; } } - + // 更新结算起点 start = i; } _=> i += 1 } - if i >= len {self.err("未闭合的`。")} + if i >= len {self.err("未闭合的'`'。")} } // 结算 结算起点到末尾 vec.extend_from_slice(&self.src[start..i]); let str = String::from_utf8(vec) .expect(&format!("字符串含非法字符 解析错误({})",self.line())); - + self.set_i(i + 1); Expr::Literal(Litr::Str(str)) } diff --git a/src/scan/mod.rs b/src/scan/mod.rs index a8fdc20..bd5f54b 100644 --- a/src/scan/mod.rs +++ b/src/scan/mod.rs @@ -117,7 +117,7 @@ impl Scanner<'_> { self.next(); } // 多行 - if nc == b'\'' { + else if nc == b'\'' { self.set_i(next + 1); loop { self.next(); @@ -137,13 +137,10 @@ impl Scanner<'_> { } } // /后面不是注释就直接返回 - return; + else{return;} } - continue; - } - _=> { - break; } + _=> break } } } diff --git a/src/scan/stmt.rs b/src/scan/stmt.rs index 4ae1eff..aa295e9 100644 --- a/src/scan/stmt.rs +++ b/src/scan/stmt.rs @@ -191,6 +191,7 @@ impl Scanner<'_> { if let Expr::Empty = expr { Stmt::Empty }else { + println!("{expr:?}"); Stmt::Expression(expr) } } else {