Skip to content

Commit

Permalink
add type system
Browse files Browse the repository at this point in the history
  • Loading branch information
limuy2022 committed Feb 6, 2024
1 parent 7f9d095 commit 3863a33
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 129 deletions.
78 changes: 0 additions & 78 deletions cpp/doc/developer/token.ll

This file was deleted.

11 changes: 11 additions & 0 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ reqwest = { version = "0.11.24", features = ["json", "multipart"] }
tokio = { version = "1.36.0", features = ["full"] }
llvm-sys = "170.0.1"
derive = { path = "./derive" }
libloading = "0.8.1"

[profile.release]
panic = "abort"
Expand Down
1 change: 1 addition & 0 deletions rust/derive/src/def_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ pub fn def_impl(content: TokenStream) -> TokenStream {
#(
classes.insert(stringify!(#right_class).to_string(), #left_class::export_info());
#left_class::gen_funcs_info();
#left_class::gen_overrides_info();
)*
#(
submodules.insert(stringify!(#submodules).to_string(), #submodules::init());
Expand Down
10 changes: 7 additions & 3 deletions rust/derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,12 @@ pub fn trc_class(_: TokenStream, input: TokenStream) -> TokenStream {
stringify!(#name),
members,
None,
Self::override_export(),
None,
classid
);
STD_CLASS_TABLE.with(|std| {
std.borrow_mut().push(ret);
});
// let funcs_info = Self::function_export()
// ret.functions = funcs_info;
classid
}

Expand All @@ -143,6 +141,12 @@ pub fn trc_class(_: TokenStream, input: TokenStream) -> TokenStream {
});
}

pub fn gen_overrides_info() {
STD_CLASS_TABLE.with(|std| {
std.borrow_mut()[Self::export_info()].overrides = Self::override_export();
});
}

pub fn export_info() -> usize {
static ID: OnceLock<usize> = OnceLock::new();
*ID.get_or_init(|| {
Expand Down
20 changes: 10 additions & 10 deletions rust/docs/developer/EBNF.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,43 +21,43 @@ argv : expr

expr : expr1 expr_

expr_: || expr_ | empty
expr_: || expr1 expr_ | empty

expr1 : expr2 expr1_

expr1_: && expr1_ | empty
expr1_: && expr2 expr1_ | empty

expr2 : expr3 expr2_

expr2_: == expr2_| != expr2_| < expr2_| > expr2_| <= expr2_| >= expr2_ |empty
expr2_: == expr3 expr2_| != expr3 expr2_| < expr3 expr2_| > expr3 expr2_| <= expr3 expr2_| >= expr3 expr2_ | empty

expr3 : expr4 expr3_

expr3_: | expr3_ | empty
expr3_: | expr4 expr3_ | empty

expr4 : expr5 expr4_

expr4_: ^ expr4_ | empty
expr4_: ^ expr5 expr4_ | empty

expr5 : expr6 expr5_

expr5_: & expr5_ | empty
expr5_: & expr6 expr5_ | empty

expr6: expr7 expr6_

expr6_: << expr6_| >> expr6_ | empty
expr6_: << expr7 expr6_| >> expr7 expr6_ | empty

expr7 : expr8 expr7_

expr7_: + expr7_| - expr7_ | empty
expr7_: + expr8 expr7_| - expr8 expr7_ | empty

expr8 : expr9 expr8_

expr8_: * expr8_| / expr8_| % expr8_| // expr8_ | empty
expr8_: * expr9 expr8_| / expr9 expr8_| % expr8_| // expr9 expr8_ | empty

expr9: factor expr9_

expr9_: ** expr9_ | empty
expr9_: ** factor expr9_ | empty

factor : (expr) | item | - factor | + factor | ! factor | ~ factor

Expand Down
6 changes: 3 additions & 3 deletions rust/src/base/stdlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,14 @@ impl RustClass {
name: impl Into<String>,
members: HashMap<String, Var>,
functions: Option<HashMap<String, RustFunction>>,
overrides: HashMap<TokenType, IOType>,
overrides: Option<HashMap<TokenType, IOType>>,
id: usize,
) -> RustClass {
RustClass {
name: name.into(),
members,
functions: functions.unwrap_or_else(|| HashMap::new()),
overrides,
overrides: overrides.unwrap_or_else(|| HashMap::new()),
id,
}
}
Expand Down Expand Up @@ -283,7 +283,7 @@ impl Stdlib {

lazy_static! {
pub static ref ANY_TYPE: RustClass =
RustClass::new("any", HashMap::new(), None, HashMap::new(), new_class_id());
RustClass::new("any", HashMap::new(), None, None, new_class_id());
pub static ref STDLIB_ROOT: Stdlib = crate::tvm::stdlib::init();
}

Expand Down
57 changes: 26 additions & 31 deletions rust/src/compiler/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,13 @@ macro_rules! try_err {

macro_rules! tmp_expe_function_gen {
($tmpfuncname:ident, $next_item_func:ident, $($accepted_token:path => $add_opcode:path),*) => {
fn $tmpfuncname(&mut self, istry: bool) -> AstError<TypeAllowNull> {
fn $tmpfuncname(&mut self, istry: bool, extend: usize) -> AstError<TypeAllowNull> {
let next_sym = self.token_lexer.next_token()?;
match next_sym.tp {
$($accepted_token => {
let tya = self.$next_item_func(istry)?;
self.add_bycode($add_opcode, NO_ARG);
let tyb = self.$tmpfuncname(istry)?;
if let TypeAllowNull::No = tyb {
return Ok(tya);
}
if let TypeAllowNull::No = tya {
return Ok(tya)
}
let tya = self.$next_item_func(istry)?.unwrap();
// 读取IOType检查
let tya = tya.unwrap();
let tyb = tyb.unwrap();
let func_obj = self.self_scope.as_ref().borrow().get_class(tya).unwrap();
let func_obj = self.self_scope.as_ref().borrow().get_class(extend).unwrap();
let io_check = func_obj.get_override_func($accepted_token);
match io_check {
None => return try_err!(istry,
Expand All @@ -56,15 +46,26 @@ macro_rules! tmp_expe_function_gen {
)
),
Some(v) => {
if let Ok(_) = v.check_argvs(vec![tyb]) {
return Ok(v.return_type.clone());
if let Ok(_) = v.check_argvs(vec![tya]) {}
else {
let func_obja = self.self_scope.as_ref().borrow().get_class(tya).unwrap();
return try_err!(istry,
Box::new(self.token_lexer.compiler_data.content.clone()),
ErrorInfo::new(gettext!(TYPE_NOT_THE_SAME, func_obj.get_name(),
func_obja.get_name()), gettextrs::gettext(TYPE_ERROR)))
}
let func_objb = self.self_scope.as_ref().borrow().get_class(tyb).unwrap();
return try_err!(istry,
Box::new(self.token_lexer.compiler_data.content.clone()),
ErrorInfo::new(gettext!(TYPE_NOT_THE_SAME, func_obj.get_name(),
func_objb.get_name()), gettextrs::gettext(TYPE_ERROR)))

}
}
let io_check = io_check.unwrap();
self.add_bycode($add_opcode, NO_ARG);
let stage_ty = io_check.return_type.unwrap();
let tyb = self.$tmpfuncname(istry, stage_ty)?;
match tyb {
TypeAllowNull::No => {
return Ok(TypeAllowNull::Yes(stage_ty));
}
TypeAllowNull::Yes(_) => {
return Ok(tyb);
}
}
})*
Expand All @@ -83,20 +84,14 @@ macro_rules! expr_gen {
tmp_expe_function_gen!($tmpfuncname, $next_item_func, $($accepted_token => $add_opcode),*);
fn $funcname(&mut self, istry: bool) -> AstError<TypeAllowNull> {
let t1 = self.$next_item_func(istry)?;
let t2 = self.$tmpfuncname(istry)?;
if let TypeAllowNull::No = t2 {
return Ok(t1);
}
if let TypeAllowNull::No = t1 {
return Ok(t1);
}
if t1.unwrap() != t2.unwrap() {
return try_err!(istry,
Box::new(self.token_lexer.compiler_data.content.clone()),
ErrorInfo::new(gettext!(TYPE_NOT_THE_SAME, t1,
t2), gettextrs::gettext(TYPE_ERROR)))
let t2 = self.$tmpfuncname(istry, t1.unwrap())?;
if let TypeAllowNull::No = t2 {
return Ok(t1);
}
Ok(t1)
Ok(t2)
}
};
}
Expand Down
3 changes: 3 additions & 0 deletions rust/src/compiler/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ pub enum TokenType {
Func,
Import,
Return,
// 不会被使用到的自反运算符,仅仅当标识重载运算符使用
SelfNegative,
EndOfFile,
}

Expand Down Expand Up @@ -195,6 +197,7 @@ impl Display for TokenType {
TokenType::Return => "return",
TokenType::SelfAnd => "&&=",
TokenType::SelfOr => "||=",
TokenType::SelfNegative => "-",
};
write!(f, "{}", res)
}
Expand Down
48 changes: 48 additions & 0 deletions rust/src/tvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::base::codegen::{self, StaticData};
use crate::base::stdlib::STD_FUNC_TABLE;
use crate::{base::error::*, cfg};
use gettextrs::gettext;
use libloading::Library;

pub struct DynaData<'a> {
obj_stack: Vec<Box<dyn types::TrcObj>>,
Expand Down Expand Up @@ -63,6 +64,53 @@ impl Content {
}
}

/// Load libc
fn load_libc() -> Option<&'static Library> {
static LIBC: std::sync::OnceLock<Option<Library>> = std::sync::OnceLock::new();
#[cfg(target_os = "linux")]
{
let tmp = LIBC.get_or_init(|| unsafe {
let lib: Result<Library, libloading::Error> = libloading::Library::new("");
match lib {
Err(_) => None,
Ok(val) => Some(val),
}
});
match tmp {
None => None,
Some(val) => Some(&val),
}
}
#[cfg(target_os = "windows")]
{
let tmp = LIBC.get_or_init(|| unsafe {
let lib: Result<Library, libloading::Error> = libloading::Library::new("msvcrt.dll");
match lib {
Err(_) => None,
Ok(val) => Some(val),
}
});
match tmp {
None => None,
Some(val) => Some(&val),
}
}
#[cfg(target_os = "macos")]
{
let tmp = LIBC.get_or_init(|| unsafe {
let lib: Result<Library, libloading::Error> = libloading::Library::new("libc.dylib");
match lib {
Err(_) => None,
Ok(val) => Some(val),
}
});
match tmp {
None => None,
Some(val) => Some(&val),
}
}
}

/// reduce the duplicate code to solve the operator running
macro_rules! operator_opcode {
($trait_used:ident, $sself:expr) => {{
Expand Down
Loading

0 comments on commit 3863a33

Please sign in to comment.