Skip to content

Commit

Permalink
fix: 肃清unused
Browse files Browse the repository at this point in the history
feat: 可省略ks后缀
feat: 报错栈追踪
fix: 模块路径将以自己所在文件夹为起点搜索
fix: 模块外调用出错可以追踪文件名
fix: 模块外的本地函数体丢失
  • Loading branch information
Bylx666 committed Apr 30, 2024
1 parent eaa0634 commit 51abb17
Show file tree
Hide file tree
Showing 26 changed files with 232 additions and 209 deletions.
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ linker = "rust-lld"
rustflags = ["-C", "linker-flavor=ld.lld"]

[target.x86_64-pc-windows-msvc]
rustflags = ["-C", "link-arg=./.cargo/icon.RES"]
[target.aarch64-pc-windows-msvc]
rustflags = ["-C", "link-arg=./.cargo/icon.RES"]
4 changes: 1 addition & 3 deletions src/c.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! 所有跨平台相关的函数都在这

use std::ptr::NonNull;

#[cfg(windows)]
mod dl {
extern {
Expand Down Expand Up @@ -47,7 +45,7 @@ impl Clib {
}
/// 从动态库中寻找一个函数
pub fn get(&self, sym:&[u8])-> Option<*const ()> {
let mut s = [sym,&[0]].concat();
let s = [sym,&[0]].concat();
unsafe {
let v = dlsym(self.0, s.as_ptr());
if v.is_null() {
Expand Down
6 changes: 3 additions & 3 deletions src/intern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ impl Interned {
pub fn str(&self)-> String {
String::from_utf8_lossy(self.vec()).into_owned()
}
pub const fn ptr(&self)-> *const Vec<u8> {
self.p as *const Vec<u8>
}
// pub const fn ptr(&self)-> *const Vec<u8> {
// self.p as *const Vec<u8>
// }
}

impl std::fmt::Debug for Interned {
Expand Down
28 changes: 18 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![allow(unused)]
#![feature(hash_set_entry)]

use std::{fs, collections::HashMap, mem::transmute, hash::{BuildHasher, Hash}, vec};
use std::fs;
use std::process::ExitCode;

mod intern;
Expand All @@ -24,11 +23,11 @@ static mut GLOBAL_OPTIONS:GlobalOptions = GlobalOptions {

/// 标志目前走到的行号
static mut LINE:usize = 1;
/// 用于标记报错文件
static mut PLACE:String = String::new();
/// 用于标记目前文件路径 在模块导入搜索时使用此作为搜索目录
static mut FILE_PATH:&str = "";

/// 标志解释器的版本
static VERSION:usize = 100006;
static VERSION:usize = 100062;

/// 解释器发行者(用于区分主版本和魔改版)
///
Expand All @@ -47,7 +46,7 @@ fn main()-> ExitCode {
let mut args = std::env::args();
args.next();
let path = if let Some(s) = args.next() {
utils::to_absolute_path(s)
utils::to_absolute_path(s).leak()
}else {
println!("> Key Lang\n version: {}\n by: {}", VERSION, DISTRIBUTION);
return ExitCode::SUCCESS;
Expand All @@ -62,25 +61,34 @@ fn main()-> ExitCode {
}

// 自定义报错
unsafe {PLACE = path.clone()}
unsafe {FILE_PATH = path}
std::panic::set_hook(Box::new(|inf| {
use crate::utils::date;
let line = unsafe{LINE};
let place = unsafe{&*PLACE};
let place = unsafe{&*FILE_PATH};
let s = if let Some(mes) = inf.payload().downcast_ref::<&'static str>() {
mes
}else if let Some(mes) = inf.payload().downcast_ref::<String>() {
mes
}else{"错误"};
println!("\n> {}\n {}:第{}行\n\n> Key Script CopyLeft by Subkey\n {}\n", s, place, line, date());

let stack = unsafe{
let mut s = String::new();
use std::fmt::Write;
for n in runtime::call::CALL_STACK.iter().rev() {
let _ = s.write_fmt(format_args!("\n {} at {}:{}",n.fname,n.file,n.line));
}
s
};
println!("\n> {}\n {}:第{}行{}\n\n> Key Script CopyLeft by {}\n {}", s, place, line, stack, DISTRIBUTION, date());
}));

// 运行并返回
let scanned = scan::scan(&fs::read(&path).unwrap_or_else(|e|
panic!("无法读取'{}': {}", path, e)));
if unsafe{GLOBAL_OPTIONS.print_ast} {println!("{scanned:?}")}

let exit = runtime::run(&scanned);
let exit = runtime::run(&scanned, path);

// 如果原生模块调用了wait_inc就堵住当前线程
unsafe {
Expand Down
3 changes: 1 addition & 2 deletions src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ use crate::{
litr::{Instance, Litr},
planet
},
runtime::{outlive::{self, LocalFunc}, Variant},
scan::stmt::LocalMod
runtime::{outlive::{self, LocalFunc}, Variant}
};
use crate::runtime::{calc::CalcRef, Scope};

Expand Down
17 changes: 9 additions & 8 deletions src/primitive/buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,10 @@ fn insert(v:&mut Vec<u8>, args:Vec<CalcRef>)-> Litr {

match &**args.next().expect("buf.insert需要传入第二个参数:整数,列表或数组作为插入内容") {
Litr::Buf(b)=> {
v.splice(index..index, b.iter().copied()).collect::<Vec<_>>();
let _ = v.splice(index..index, b.iter().copied()).collect::<Vec<_>>();
},
Litr::List(b)=> {
v.splice(index..index, b.iter()
let _ = v.splice(index..index, b.iter()
.map(|n|to_u8(n))).collect::<Vec<_>>();
}
n=> v.insert(index, to_u8(n))
Expand Down Expand Up @@ -418,17 +418,18 @@ fn join(v:&mut Vec<u8>, args:Vec<CalcRef>)-> Litr {
}else {""};

use std::fmt::Write;
const WRITE_ERR: &str = "buf.join写入错误";
let mut s = String::new();
s.write_fmt(format_args!("{:02X}",v[0]));
s.write_fmt(format_args!("{:02X}",v[0])).expect(WRITE_ERR);
for n in &v[1..] {
s.write_fmt(format_args!("{sep}{n:02X}"));
s.write_fmt(format_args!("{sep}{n:02X}")).expect(WRITE_ERR);
}
Litr::Str(s)
}

/// 嘎嘎复制和计算, 将整个数组折叠成一个值
fn fold(v:&mut Vec<u8>, args:Vec<CalcRef>, scope:Scope)-> Litr {
let mut init = args.get(0).expect("buf.fold需要一个初始值").clone().own();
let init = args.get(0).expect("buf.fold需要一个初始值").clone().own();
let f = match &**args.get(1).expect("buf.fold需要第二个参数的函数来处理数据") {
Litr::Func(f)=> f,
_=> panic!("buf.fold第二个参数只能是函数")
Expand Down Expand Up @@ -653,14 +654,14 @@ pub fn statics()-> Vec<(Interned, NativeFn)> {
}

/// 创建n长度的Buf
fn s_new(args:Vec<CalcRef>, cx:Scope)-> Litr {
fn s_new(args:Vec<CalcRef>, _cx:Scope)-> Litr {
// 如果传入了大小就按大小分配
if let Some(n) = args.get(0) {
let n = to_usize(n);

unsafe {
let layout = std::alloc::Layout::from_size_align_unchecked(n, 1);
let alc = unsafe {std::alloc::alloc_zeroed(layout)};
let alc = std::alloc::alloc_zeroed(layout);
Litr::Buf(Vec::from_raw_parts(alc, n, n))
}
}else {
Expand All @@ -676,7 +677,7 @@ fn s_new_uninit(args:Vec<CalcRef>, _cx:Scope)-> Litr {

unsafe {
let layout = std::alloc::Layout::from_size_align_unchecked(n, 1);
let alc = unsafe {std::alloc::alloc(layout)};
let alc = std::alloc::alloc(layout);
Litr::Buf(Vec::from_raw_parts(alc, n, n))
}
}else {
Expand Down
10 changes: 5 additions & 5 deletions src/primitive/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn method(f:&Function, name:Interned, cx: Scope, args:Vec<CalcRef>)-> Litr {
}

/// 传入self并调用
pub fn kcall(f:&Function, mut args:Vec<CalcRef>, mut cx:Scope)-> Litr {
pub fn kcall(f:&Function, mut args:Vec<CalcRef>, cx:Scope)-> Litr {
assert!(args.len()>=1, "func.call必须传入一个值作为self");
let trans_args = args.split_off(1);
let mut kself = args.pop().unwrap();
Expand All @@ -32,7 +32,7 @@ pub fn kcall(f:&Function, mut args:Vec<CalcRef>, mut cx:Scope)-> Litr {
}

/// 复制一个函数,但上下文在当前作用域
pub fn clone_here(f:&Function, mut args:Vec<CalcRef>, mut cx:Scope)-> Litr {
pub fn clone_here(f:&Function, _args:Vec<CalcRef>, cx:Scope)-> Litr {
Litr::Func(match f {
Function::Local(f)=> Function::Local(LocalFunc::new(f.ptr, cx)),
// 如果不是local就正常调用
Expand All @@ -41,7 +41,7 @@ pub fn clone_here(f:&Function, mut args:Vec<CalcRef>, mut cx:Scope)-> Litr {
}

/// 复制一个函数,但上下文在当前作用域
pub fn call_here(f:&Function, mut args:Vec<CalcRef>, mut cx:Scope)-> Litr {
pub fn call_here(f:&Function, mut args:Vec<CalcRef>, cx:Scope)-> Litr {
assert!(args.len()>=1, "func.call_here必须传入一个值作为self");
let trans_args = args.split_off(1);
let mut kself = args.pop().unwrap();
Expand All @@ -57,7 +57,7 @@ pub fn call_here(f:&Function, mut args:Vec<CalcRef>, mut cx:Scope)-> Litr {
}

/// 复制一个函数,但上下文在该模块的顶级作用域
pub fn clone_top(f:&Function, mut args:Vec<CalcRef>, mut cx:Scope)-> Litr {
pub fn clone_top(f:&Function, _args:Vec<CalcRef>, mut cx:Scope)-> Litr {
// 获取顶级作用域
while let Some(s) = &cx.parent {
cx = s.clone()
Expand Down Expand Up @@ -124,6 +124,6 @@ fn s_new(mut s:Vec<CalcRef>, cx:Scope)-> Litr {
}

Litr::Func(Function::Local(LocalFunc::new(Box::into_raw(Box::new(
LocalFuncRaw {argdecl:LocalFuncRawArg::Normal(argdecl), stmts}
LocalFuncRaw {argdecl:LocalFuncRawArg::Normal(argdecl), stmts, name: intern(b"unnamed")}
)),cx)))
}
8 changes: 3 additions & 5 deletions src/primitive/iter.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use std::cell::UnsafeCell;

use crate::{
intern::intern,
native::{NativeInstance, NativeMethod},
native::NativeInstance,
primitive::litr::{Litr, LocalFunc},
runtime::{calc::CalcRef, Scope}
runtime::Scope
};

/// instance类型专用的迭代器
Expand Down Expand Up @@ -56,7 +54,7 @@ impl<'a> LitrIterator<'a> {
Litr::List(v)=> Box::new(v.iter().cloned()),
Litr::Inst(inst)=> {
let f = & unsafe{&*inst.cls}.methods.iter()
.find(|f|f.name == intern(b"@next"))
.find(|f|f.f.name == intern(b"@next"))
.expect("迭代class需要定义'.@next()'方法").f;
let f = LocalFunc::new(f, unsafe{&*inst.cls}.cx);
Box::new(InstanceIter { f, kself:v })
Expand Down
12 changes: 5 additions & 7 deletions src/primitive/kstd.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! 定义顶级作用域的函数

use crate::intern::{Interned,intern};
use crate::intern::intern;
use crate::primitive::litr::{Litr, Function};
use crate::runtime::{calc::CalcRef, Scope, Variant};

Expand Down Expand Up @@ -47,17 +47,15 @@ fn run_ks(args:Vec<CalcRef>, mut cx:Scope)-> Litr {

unsafe {
// 将报错位置写为evil 并保存原先的报错数据
let mut place = std::mem::take(&mut crate::PLACE);
let mut file_dir = std::mem::take(&mut crate::FILE_PATH);
let line = crate::LINE;
crate::PLACE = format!("run_ks: {}({})", place, line);
crate::FILE_PATH = "run_ks";
crate::LINE = 1;

// 解析并运行
let scanned = crate::scan::scan(s);
for (l, sm) in &scanned.v {
unsafe{
crate::LINE = *l;
}
crate::LINE = *l;
cx.evil(sm);
// 如果evil到return或break就在这停下
if cx.ended {
Expand All @@ -66,7 +64,7 @@ fn run_ks(args:Vec<CalcRef>, mut cx:Scope)-> Litr {
}

// 还原报错信息
crate::PLACE = std::mem::take(&mut place);
crate::FILE_PATH = std::mem::take(&mut file_dir);
crate::LINE = line;
}
Litr::Uninit
Expand Down
2 changes: 1 addition & 1 deletion src/primitive/kstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn to_usize(n:&Litr)-> usize {

static mut ITER_LINES: *mut NativeClassDef = std::ptr::null_mut();

pub fn method(s:&mut String, scope:Scope, name:Interned, args:Vec<CalcRef>)-> Litr {
pub fn method(s:&mut String, _scope:Scope, name:Interned, args:Vec<CalcRef>)-> Litr {
macro_rules! get_arg0 {
// 解析为usize
(usize)=> {
Expand Down
29 changes: 10 additions & 19 deletions src/primitive/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,6 @@ fn map_clone(v:&mut Vec<Litr>, args:Vec<CalcRef>, scope:Scope)-> Litr {
.map(|a| scope.call(vec![CalcRef::Ref(a)], f) ).collect())
}

/// map_clone的原地版本
fn map(v:&mut Vec<Litr>, args:Vec<CalcRef>, scope:Scope)-> Litr {
let f = match &**args.get(0).expect("list.map需要一个函数作为参数") {
Litr::Func(f)=> f,
_=> panic!("list.map第一个参数只能传函数")
};
*v = v.iter_mut().map(|a| scope.call(vec![CalcRef::Ref(a)], f)).collect();
Litr::Uninit
}

/// 从末尾切去一个, 可传一个数字作为切去数量
fn pop(v:&mut Vec<Litr>, args:Vec<CalcRef>)-> Litr {
if let Some(arg0) = args.get(0) {
Expand Down Expand Up @@ -234,10 +224,10 @@ fn insert_many(v:&mut Vec<Litr>, args:Vec<CalcRef>)-> Litr {

match &**args.get(1).expect("list.insert_many需要传入第二个参数作为插入内容") {
Litr::Buf(b)=> {
v.splice(index..index, b.iter().map(|n|Litr::Uint(*n as usize))).collect::<Vec<_>>();
let _ = v.splice(index..index, b.iter().map(|n|Litr::Uint(*n as usize))).collect::<Vec<_>>();
},
Litr::List(b)=> {
v.splice(index..index, b.iter()
let _ = v.splice(index..index, b.iter()
.map(|n|n.clone())).collect::<Vec<_>>();
}
_=> panic!("list.insert_many第二个参数必须是List或Buf")
Expand Down Expand Up @@ -349,13 +339,14 @@ fn join(v:&mut Vec<Litr>, args:Vec<CalcRef>)-> Litr {
}else {""};

use std::fmt::Write;
const WRITE_ERR: &str = "list.join错误";
let mut res = String::new();
res.write_fmt(format_args!("{}", v[0].str()));
res.write_fmt(format_args!("{}", v[0].str())).expect(WRITE_ERR);
for n in &mut v[1..] {
if let Litr::Str(s) = n {
res.write_fmt(format_args!("{sep}{}",s));
res.write_fmt(format_args!("{sep}{}",s)).expect(WRITE_ERR);
}else {
res.write_fmt(format_args!("{sep}{}",n.str()));
res.write_fmt(format_args!("{sep}{}",n.str())).expect(WRITE_ERR);
}
}
Litr::Str(res)
Expand All @@ -364,7 +355,7 @@ fn join(v:&mut Vec<Litr>, args:Vec<CalcRef>)-> Litr {
/// 嘎嘎复制和计算, 将整个数组折叠成一个值
fn fold(v:&mut Vec<Litr>, args:Vec<CalcRef>, scope:Scope)-> Litr {
let mut args = args.into_iter();
let mut init = args.next().expect("list.fold需要一个初始值").clone().own();
let init = args.next().expect("list.fold需要一个初始值").clone().own();
let f_ = args.next().expect("list.fold需要第二个参数的函数来处理数据");
let f = match &*f_ {
Litr::Func(f)=> f,
Expand Down Expand Up @@ -397,7 +388,7 @@ fn includes(v:&mut Vec<Litr>, args:Vec<CalcRef>)-> Litr {
}

/// 找数组中第一个所指数字, 也可以传函数来自定义判断
fn index_of(v:&mut Vec<Litr>, args:Vec<CalcRef>, scope:Scope)-> Litr {
fn index_of(v:&mut Vec<Litr>, args:Vec<CalcRef>, _cx:Scope)-> Litr {
let find = &**args.get(0).expect("list.index_of需要传入一个值");
let res = v.iter().position(|n|n==find);
match res {
Expand All @@ -407,11 +398,11 @@ fn index_of(v:&mut Vec<Litr>, args:Vec<CalcRef>, scope:Scope)-> Litr {
}

/// index_of反向版
fn r_index_of(v:&mut Vec<Litr>, args:Vec<CalcRef>, scope:Scope)-> Litr {
fn r_index_of(v:&mut Vec<Litr>, args:Vec<CalcRef>, _cx:Scope)-> Litr {
let find = &**args.get(0).expect("list.r_index_of需要传入一个值");
let res = v.iter().rev().position(|n|n==find);
match res {
Some(n)=> Litr::Uint((v.len() - n - 1)),
Some(n)=> Litr::Uint(v.len() - n - 1),
None=> Litr::Uninit
}
}
Expand Down
Loading

3 comments on commit 51abb17

@twhice
Copy link

@twhice twhice commented on 51abb17 Apr 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

代码还是没有格式化,clippy仍然给出大约250个warn,miri仍然无法测试。好了,你撕下了你的遮羞布,删掉了那一行allow,但是没有解决任何问题,并且留下了更多不符合社区规范的代码

@twhice
Copy link

@twhice twhice commented on 51abb17 Apr 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我哪配让你看我的pr啊?自我高潮的你毫无长进,你真以为你解决了问题?乐

@twhice
Copy link

@twhice twhice commented on 51abb17 Apr 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我之前的建议接不接受也无所谓了,反正你根本就没有看过一眼我改了哪些地方,也没有去了解我说的哪些东西。现在皆大欢喜,你很快乐,继续在这坨最精致的屎上雕花

Please sign in to comment.