Skip to content

Commit

Permalink
Func::new
Browse files Browse the repository at this point in the history
  • Loading branch information
Bylx666 committed Feb 29, 2024
1 parent a94dabf commit 3459d74
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 11 deletions.
4 changes: 2 additions & 2 deletions samples/helloworld.ks
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
mod D:\code\rs\key-native\target\debug\key_native.dll> m
// mod D:\code\rs\key-lang\samples\testmod.ks> m;

let a = 20;
m-.test()
let f = Func::new(":hh+aa", ["hh", "aa"]);
log(f(20,98))
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ fn main()-> ExitCode {
// 如果不加分号报错会错行,记得提示用户

intern::init();
let scanned = scan::scan(fs::read("D:\\code\\rs\\key-lang\\samples\\helloworld.ks").unwrap());
let scanned = scan::scan(&fs::read("D:\\code\\rs\\key-lang\\samples\\helloworld.ks").unwrap());
println!("{scanned:?}");
let exit = runtime::run(&scanned);
if let scan::literal::Litr::Int(code) = exit.returned {
Expand Down
40 changes: 36 additions & 4 deletions src/primitive/func.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,42 @@
use crate::{
intern::{intern, Interned},
native::NativeFn,
runtime::calc::CalcRef,
scan::literal::Litr
runtime::{calc::CalcRef, err, Scope},
scan::{literal::{ArgDecl, Function, KsType, Litr, LocalFunc, LocalFuncRaw}, stmt::Statements}
};

fn s_new(s:Vec<CalcRef>)-> Litr {
Litr::Bool(false)
pub fn statics()-> Vec<(Interned, NativeFn)> {
vec![
(intern(b"new"), s_new)
]
}

fn s_new(s:Vec<CalcRef>, cx:Scope)-> Litr {
let mut itr = s.into_iter();
let arg1 = itr.next();
let stmts = match &arg1 {
Some(arg)=> crate::scan::scan(match &**arg {
Litr::Str(s)=> s.as_bytes(),
Litr::Buf(b)=> b,
_=> err!("Func::new第一个参数必须是Str或Buf, 用来被解析为函数体")
}),
None=> Statements(Vec::new())
};

let arg2 = itr.next();
let argdecl = match &arg2 {
Some(arg)=> match &**arg {
Litr::List(v)=>
v.iter().map(|v|match v {
Litr::Str(s)=> ArgDecl {default: Litr::Uninit, name: intern(s.as_bytes()), t:KsType::Any},
_=> ArgDecl {default: Litr::Uninit, name: intern(b"#ignored"), t:KsType::Any}
}).collect(),
_=> err!("Func::new第二个参数必须是Str组成的List, 用来定义该函数的参数名")
},
None=> Vec::new()
};

Litr::Func(Function::Local(LocalFunc::new(Box::into_raw(Box::new(
LocalFuncRaw {argdecl, stmts}
)),cx)))
}
3 changes: 2 additions & 1 deletion src/primitive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ pub fn classes()-> Vec<(Interned, Class)> {unsafe {
}else {
let obj_c = new_class(b"Obj", obj::statics());
let sym_c = new_class(b"Sym", sym::statics());
CLASSES = Some(vec![obj_c, sym_c]);
let func_c = new_class(b"Func", func::statics());
CLASSES = Some(vec![obj_c, sym_c, func_c]);
classes()
}
}}
4 changes: 2 additions & 2 deletions src/scan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ use literal::{Litr, KsType, ArgDecl};
use expr::Expr;

/// 将字符扫描为ast
pub fn scan(src: Vec<u8>)-> Statements {
pub fn scan(src: &[u8])-> Statements {
// 已知此处所有变量未泄露
// 为了规避&mut所有权检查,将引用改为指针
let mut i = 0;
let mut line = 1;
let mut sttms = Statements::default();
let mut scanner = Scanner {
src:&*src, i:&mut i, line:&mut line,
src, i:&mut i, line:&mut line,
sttms:&mut sttms as *mut Statements
};
scanner.scan();
Expand Down
2 changes: 1 addition & 1 deletion src/scan/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ impl Scanner<'_> {
let file = std::fs::read(path).unwrap_or_else(|e|self.err(&format!(
"无法找到模块'{}'", path
)));
let mut module = crate::runtime::run(&scan(file)).exports;
let mut module = crate::runtime::run(&scan(&file)).exports;
Stmt::Mod(name, module)
}
_ => self.err("未知模块类型")
Expand Down

0 comments on commit 3459d74

Please sign in to comment.