1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//! 负责和解释器交互的部分
//! 
//! 此模块定义Key语言中原生模块可能用到的类型

use std::collections::HashMap;
use crate::Instance;

static mut INTERN:fn(&[u8])-> Ident = |_|unsafe{std::mem::transmute(1usize)};
/// 将字符串缓存为指针(和Key解释器用一个缓存池)
pub fn intern(s:&[u8])-> Ident {
  unsafe{ INTERN(s) }
}

pub static mut _KEY_LANG_PANIC:fn(&str)-> ! = |s|panic!("{}",s);
/// 用Key解释器的报错
#[macro_export]
macro_rules! kpanic {($($arg:tt)*)=> {
  unsafe{::key_native::key::_KEY_LANG_PANIC(&format!($($arg)*))}
}}

/// premain函数接收的函数表
#[repr(C)]
struct PreMain {
  intern: fn(&[u8])-> Ident,
  err: fn(&str)->!,
  find_var: fn(Scope, Ident)-> Option<LitrRef>,
}

#[no_mangle]
extern fn premain(module: &PreMain) {
  unsafe {
    INTERN = module.intern;
    _KEY_LANG_PANIC = module.err;
    FIND_VAR = module.find_var;
  }
}

/// 一个合法的标识符, 可以理解为字符串的指针
#[derive(Debug, Clone, Copy)]
pub struct Ident {
  pub p: &'static Box<[u8]>
}
impl Ident {
  /// 将ident作为字符串
  pub fn str(&self)-> String {
    String::from_utf8_lossy(&self.p).into_owned()
  }
  /// 获得ident的slice
  pub fn slice(&self)-> &[u8] {
    self
  }
}
impl std::ops::Deref for Ident {
  type Target = [u8];
  fn deref(&self) -> &Self::Target {
    &*self.p
  }
}
impl std::fmt::Display for Ident {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.write_str(&self.str())
  }
}

pub(super) static mut FIND_VAR:fn(Scope, Ident)-> Option<LitrRef> = |_,_|None;

#[derive(Debug, Clone, Copy)]
pub struct Scope(*mut ());
impl Scope {
  pub fn find_var(self, s:&str)-> Option<LitrRef> {
    unsafe{FIND_VAR(self, intern(s.as_bytes()))}
  }
}

#[derive(Debug, Clone)]
pub enum Litr {
  Uninit,

  Int    (isize),
  Uint   (usize),
  Float  (f64),
  Bool   (bool),

  Func   (Function), 
  Str    (String),
  Buf    (Vec<u8>),
  List   (Vec<Litr>),
  Obj    (HashMap<Ident, Litr>),
  Inst   (()),
  Ninst  (Instance),
  Sym    (Symbol)
}

/// 函数枚举
#[derive(Debug, Clone)]
pub enum Function {
  Local(Box<()>),
  Extern(Box<()>),
  Native(fn(Vec<Litr>)-> Litr)
}

#[derive(Debug, Clone)]
pub enum Symbol {
  IterEnd,
  Reserved
}
impl Symbol {
  pub fn iter_end()-> Litr {
    Litr::Sym(Symbol::IterEnd)
  }
}

/// 可能是引用的Litr
pub enum LitrRef {
  Ref(*mut Litr),
  Own(Litr)
}
impl LitrRef {
  /// 消耗CalcRef返回内部值
  pub fn own(self)-> Litr {
    match self {
      LitrRef::Ref(p)=> unsafe {(*p).clone()}
      LitrRef::Own(v)=> v
    }
  }
}
impl std::ops::Deref for LitrRef {
  type Target = Litr;
  fn deref(&self) -> &Self::Target {
    match self {
      LitrRef::Ref(p)=> unsafe{&**p},
      LitrRef::Own(b)=> b
    }
  }
}
impl std::ops::DerefMut for LitrRef {
  fn deref_mut(&mut self) -> &mut Self::Target {
    match self {
      LitrRef::Ref(p)=> unsafe{&mut **p},
      LitrRef::Own(b)=> b
    }
  }
}