Skip to content

Commit

Permalink
feat: 移除litr的sym, 将其设定为原生模块, 类型名为Symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
Bylx666 committed Apr 8, 2024
1 parent 4a04238 commit bb10056
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 55 deletions.
10 changes: 7 additions & 3 deletions samples/helloworld.ks
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
mod D:\code\rs\tst\target\debug\tstlib.dll> m;

// 直接调用模块上的公共函数
let a = m-.new();
a.test(); // 打印:
let s = "芙
斯";
for c: s.lines() {
log(c)
}
16 changes: 8 additions & 8 deletions src/primitive/iter.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::cell::UnsafeCell;

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

use super::sym::Symbol;

/// instance类型专用的迭代器
struct InstanceIter<'a> {
f: LocalFunc,
Expand All @@ -15,8 +16,8 @@ impl Iterator for InstanceIter<'_> {
type Item = Litr;
fn next(&mut self) -> Option<Self::Item> {
let r = Scope::call_local_with_self(&self.f, vec![], self.kself);
if let Litr::Sym(s) = &r {
if let Symbol::IterEnd = s {
if let Litr::Ninst(inst) = &r {
if super::sym::is_sym(inst) && inst.v == super::sym::ITER_END {
return None;
}
}
Expand All @@ -33,8 +34,8 @@ impl Iterator for NativeInstanceIter<'_> {
type Item = Litr;
fn next(&mut self) -> Option<Self::Item> {
let r = (self.f)(self.kself);
if let Litr::Sym(s) = &r {
if let Symbol::IterEnd = s {
if let Litr::Ninst(inst) = &r {
if super::sym::is_sym(inst) && inst.v == super::sym::ITER_END {
return None;
}
}
Expand Down Expand Up @@ -69,7 +70,6 @@ impl<'a> LitrIterator<'a> {
Litr::Bool(_) => panic!("Bool无法迭代"),
Litr::Func(_) => panic!("Func无法迭代"),
Litr::Float(_) => panic!("Float无法迭代"),
Litr::Sym(_) => panic!("Sym无法迭代"),
Litr::Uninit => panic!("给uninit迭代?死刑!"),
};
LitrIterator { inner }
Expand Down
3 changes: 1 addition & 2 deletions src/primitive/kstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,13 @@ fn splice(s:&mut String, args:Vec<CalcRef>)-> Litr {
// - statics -
pub fn statics()-> Vec<(Interned, NativeFn)> {
use std::str::Lines;
use sym::Symbol;
unsafe {
// 初始化lines()迭代器类
ITER_LINES = Box::into_raw(Box::new(super::new_iter_class(
b"Str.lines",
|v| {
let itr = v.v as *mut Lines;
(*itr).next().map_or(Litr::Sym(Symbol::IterEnd),
(*itr).next().map_or(sym::iter_end(),
|v|Litr::Str(v.to_string()))
},
|v| {
Expand Down
12 changes: 4 additions & 8 deletions src/primitive/litr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ pub enum Litr {
List (Vec<Litr>),
Obj (HashMap<Interned, Litr>),
Inst (Instance),
Ninst (NativeInstance),
Sym (crate::primitive::sym::Symbol)
Ninst (NativeInstance)
}

impl Litr {
Expand Down Expand Up @@ -107,8 +106,7 @@ impl Litr {
s.push_str(" }");
s
}
Ninst(inst)=> (unsafe { &*inst.cls }.to_str)(inst),
Sym(s)=> super::sym::to_str(s)
Ninst(inst)=> (unsafe { &*inst.cls }.to_str)(inst)
}
}
}
Expand Down Expand Up @@ -211,7 +209,6 @@ pub enum KsType {
Buf,
List,
Obj,
Sym,
Class(Interned)
}
impl KsType {
Expand Down Expand Up @@ -242,7 +239,7 @@ impl KsType {
}
}}
}}
matcher!{Buf Bool Float Func Int Uint List Obj Str Sym}
matcher!{Buf Bool Float Func Int Uint List Obj Str}
}
}

Expand All @@ -256,7 +253,7 @@ impl std::fmt::Debug for KsType {
KsType::Class(n)=> unsafe{ std::str::from_utf8_unchecked(n.vec()) }
}
}}
f.write_str(m!{Any Buf Bool Float Func Int Uint List Obj Str Sym})
f.write_str(m!{Any Buf Bool Float Func Int Uint List Obj Str})
}
}

Expand Down Expand Up @@ -300,7 +297,6 @@ impl PartialOrd for Litr {
match_list(&*l.v, &*r.v)
}else {None}
}
(Sym(l), Sym(r))=> l.partial_cmp(r),
_=> None
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/primitive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ use crate::native::{
use crate::runtime::{calc::CalcRef, Scope, Class};
use crate::intern::{Interned, intern};

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

pub fn ninst_to_str(inst:&NativeInstance)-> String {
format!("{} {{ Builtin }}", &unsafe{&*inst.cls}.name.str())
}

/// 创建一个只有静态方法的原生类
fn new_static_class(s:&[u8], f:Vec<(Interned, NativeFn)>)-> (Interned, NativeClassDef) {
fn new_static_class(s:&[u8], f:Vec<(Interned, NativeFn)>)-> (Interned, *mut NativeClassDef) {
let name = intern(s);
(name, NativeClassDef {
(name, Box::into_raw(Box::new(NativeClassDef {
name,
methods: Vec::new(),
statics: f,
Expand All @@ -43,10 +43,10 @@ fn new_static_class(s:&[u8], f:Vec<(Interned, NativeFn)>)-> (Interned, NativeCla
index_get:|_,_|Litr::Uninit,
index_set:|_,_,_|(),
to_str: ninst_to_str,
next:|_|Litr::Sym(sym::Symbol::IterEnd),
next:|_|sym::iter_end(),
onclone:|v|v.clone(),
ondrop:|_|()
})
})))
}

/// 创建一个只带有迭代器的原生类
Expand Down Expand Up @@ -74,7 +74,7 @@ fn new_iter_class(
/// 返回只含有静态函数的内置类
pub fn classes()-> Vec<(Interned, Class)> {unsafe {
if let Some(cls) = &mut CLASSES {
cls.iter_mut().map(|(name, f)|(*name, Class::Native(f))).collect()
cls.iter().map(|(name, f)|(*name, Class::Native(*f))).collect()
}else {
CLASSES = Some(vec![
new_static_class(b"Buf", buf::statics()),
Expand All @@ -84,7 +84,7 @@ pub fn classes()-> Vec<(Interned, Class)> {unsafe {
new_static_class(b"Uint", int::statics_uint()),
new_static_class(b"Float", float::statics()),
new_static_class(b"Str", kstr::statics()),
new_static_class(b"Sym", sym::statics()),
sym::init(),
new_static_class(b"Func", func::statics()),
]);
classes()
Expand Down
8 changes: 4 additions & 4 deletions src/primitive/obj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::{
intern::{intern, Interned},
native::{NativeClassDef, NativeFn, NativeInstance},
primitive::{litr::Litr, sym::Symbol},
primitive::{litr::Litr, sym},
runtime::{calc::CalcRef, Scope}
};
use std::collections::HashMap;
Expand Down Expand Up @@ -145,7 +145,7 @@ pub fn statics()-> Vec<(Interned, NativeFn)> {
b"Obj.keys",
|v| {
let itr = v.v as *mut Keys<'_, Interned, Litr>;
(*itr).next().map_or(Litr::Sym(Symbol::IterEnd),
(*itr).next().map_or(sym::iter_end(),
|v|Litr::Str(v.str()))
},
|v| {
Expand All @@ -159,7 +159,7 @@ pub fn statics()-> Vec<(Interned, NativeFn)> {
|v| {
let itr = v.v as *mut Values<'_, Interned, Litr>;
(*itr).next()
.map_or(Litr::Sym(Symbol::IterEnd),|v|v.clone())
.map_or(sym::iter_end(),|v|v.clone())
},
|v| {
drop(Box::from_raw(v.v as *mut Values<'_, Interned, Litr>))
Expand All @@ -171,7 +171,7 @@ pub fn statics()-> Vec<(Interned, NativeFn)> {
b"Obj.entries",
|v| {
let itr = v.v as *mut Iter<'_, Interned, Litr>;
(*itr).next().map_or(Litr::Sym(Symbol::IterEnd),|(k,v)|Litr::List(
(*itr).next().map_or(sym::iter_end(),|(k,v)|Litr::List(
vec![Litr::Str(k.str()), v.clone()]
))
},
Expand Down
41 changes: 22 additions & 19 deletions src/primitive/sym.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
use crate::{
runtime::{calc::CalcRef, Scope},
primitive::litr::Litr,
intern::{Interned, intern},
native::NativeFn
intern::{intern, Interned}, native::{NativeClassDef, NativeFn, NativeInstance}, primitive::litr::Litr, runtime::{calc::CalcRef, Scope}
};

#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub enum Symbol {
IterEnd,
Reserved
}
pub const ITER_END:usize = 1;

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

pub fn statics()-> Vec<(Interned, NativeFn)> {
vec![
(intern(b"iter_end"), s_iter_end)
]
pub fn init()-> (Interned, *mut NativeClassDef) {
unsafe {
let s = super::new_static_class(b"Sym",
vec![
(intern(b"iter_end"), |_,_|iter_end())
]
);
SYMBOL_CLASS = s.1;
s
}
}

fn s_iter_end(_:Vec<CalcRef>, _cx:Scope)-> Litr {
Litr::Sym(Symbol::IterEnd)
pub fn is_sym(v:&NativeInstance)-> bool {
unsafe {v.cls == SYMBOL_CLASS}
}
pub fn iter_end()-> Litr {
unsafe{Litr::Ninst(NativeInstance {v: 1, w:0, cls: SYMBOL_CLASS})}
}

pub fn to_str(s:&Symbol)-> String {
let t = match s {
Symbol::IterEnd=> "迭代结束",
Symbol::Reserved=> "未使用"
pub fn to_str(s:&NativeInstance)-> String {
let t = match s.v {
ITER_END=> "迭代结束",
_=> "未知"
};
format!("Sym {{ {} }}", t)
}
2 changes: 1 addition & 1 deletion src/runtime/calc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ impl Scope {
}
}}
matcher!{
Bool Buf Float Func Int List Obj Str Sym Uint
Bool Buf Float Func Int List Obj Str Uint
}
}

Expand Down
1 change: 0 additions & 1 deletion src/runtime/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ impl Scope {
Litr::Float(n)=> primitive::float::method(*n, name, args),
Litr::Str(s)=> primitive::kstr::method(s, self, name, args),
Litr::Func(f)=> primitive::func::method(f, name, self, args),
Litr::Sym(_)=> panic!("Sym没有方法"),
Litr::Uninit=> panic!("uninit没有方法"),
Litr::Inst(inst)=> {
let cannot_access_private = unsafe {(*inst.cls).cx.exports} != self.exports;
Expand Down
3 changes: 1 addition & 2 deletions src/runtime/externer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ pub fn translate(arg:&Litr)-> Result<usize,String> {
List(_)=> Err("列表类型不可作为C指针传递".to_string()),
Obj(_)=> Err("Ks对象不可作为C指针传递".to_string()),
Inst(_)=> Err("Ks实例不可作为C指针传递".to_string()),
Ninst(_)=> Err("Ks原生实例不可作为C指针传递".to_string()),
Sym(_)=> Err("sym不可作为C数字传递".to_string())
Ninst(_)=> Err("Ks原生实例不可作为C指针传递".to_string())
}
}

Expand Down

0 comments on commit bb10056

Please sign in to comment.