Skip to content

Commit

Permalink
弃用lazy_static
Browse files Browse the repository at this point in the history
  • Loading branch information
limuy2022 committed Feb 7, 2024
1 parent f36d1bb commit 8a0dbf2
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 46 deletions.
1 change: 0 additions & 1 deletion rust/Cargo.lock

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

1 change: 0 additions & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ clap = { version = "4.4.18", features = ["derive"] }
gettext-rs = "0.7.0"
colored = "2.1.0"
downcast-rs = "1.2.0"
lazy_static = "1.4.0"
num-bigint = "0.4.4"
llvm-sys = "170.0.1"
derive = { path = "./derive" }
Expand Down
2 changes: 1 addition & 1 deletion rust/derive/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn process_function_def(sig: &mut Signature) -> (Vec<Stmt>, Vec<TypePath>, T
};
// println!("argv:{:#?}", path);
if path.path.segments[0].ident == "any" {
args_type_required.push(parse_str("RustClass").unwrap());
args_type_required.push(parse_str("AnyType").unwrap());
new_stmts.push(
parse_str::<Stmt>(&format!(
"let mut {} = dydata.obj_stack.pop().unwrap();",
Expand Down
2 changes: 2 additions & 0 deletions rust/src/base/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ pub enum Opcode {
Empty,
// a = -a
SelfNegative,
SelfNegativeInt,
SelfNegativeFloat,
// call native func
CallNative,
}
Expand Down
19 changes: 12 additions & 7 deletions rust/src/base/stdlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ use crate::{
tvm::DynaData,
};
use downcast_rs::{impl_downcast, Downcast};
use lazy_static::lazy_static;
use std::sync::OnceLock;
use std::{
collections::HashMap,
fmt::{Debug, Display},
};

type StdlibFunc = fn(&mut DynaData) -> RuntimeResult<()>;
const ANY_TYPE_ID: usize = 0;

#[derive(Clone, Debug)]
pub struct IOType {
Expand Down Expand Up @@ -168,10 +168,6 @@ impl RustClass {
pub fn add_attr(&mut self, name: impl Into<String>, attr: Var) {
self.members.insert(name.into(), attr);
}

pub fn export_info() -> usize {
0
}
}

impl ClassInterface for RustClass {
Expand Down Expand Up @@ -284,8 +280,17 @@ impl Stdlib {
}
}

lazy_static! {
pub static ref ANY_TYPE: RustClass = RustClass::new("any", HashMap::new(), None, None, 0);
pub fn get_any_type() -> &'static RustClass {
static ANY_TYPE: OnceLock<RustClass> = OnceLock::new();
ANY_TYPE.get_or_init(|| RustClass::new("any", HashMap::new(), None, None, ANY_TYPE_ID))
}

pub struct AnyType {}

impl AnyType {
pub fn export_info() -> usize {
ANY_TYPE_ID
}
}

/// 获取到标准库的类的个数,从而区分标准库和用户自定义的类
Expand Down
13 changes: 1 addition & 12 deletions rust/src/compiler/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,7 @@ use crate::base::stdlib::{
get_stdclass_end, get_stdlib, ClassInterface, FunctionInterface, IOType, Stdlib,
STD_CLASS_TABLE,
};
use lazy_static::lazy_static;
use std::{borrow::Borrow, cell::RefCell, collections::HashMap, fmt::Display, rc::Rc};

lazy_static! {
static ref VAR_TYPE: Vec<String> = vec![
"int".to_string(),
"float".to_string(),
"str".to_string(),
"bool".to_string(),
"bigint".to_string(),
];
}
use std::{cell::RefCell, collections::HashMap, fmt::Display, rc::Rc};

#[derive(Clone, Debug)]
pub enum TypeAllowNull {
Expand Down
50 changes: 29 additions & 21 deletions rust/src/compiler/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use crate::{
hash_map,
};
use gettextrs::gettext;
use lazy_static::lazy_static;
use std::{collections::HashMap, fmt::Display, process::exit};
use std::{collections::HashMap, fmt::Display, process::exit, sync::OnceLock};

#[derive(PartialEq, Debug, Clone, Hash, Eq)]
pub enum TokenType {
Expand Down Expand Up @@ -255,23 +254,32 @@ macro_rules! check_braces_match {
}}
}

lazy_static! {
static ref KEYWORDS: HashMap<String, TokenType> = hash_map![
"while".to_string() => TokenType::While,
"for".to_string() => TokenType::For,
"if".to_string() => TokenType::If,
"else".to_string() => TokenType::Else,
"class".to_string() => TokenType::Class,
"func".to_string() => TokenType::Func,
"match".to_string() => TokenType::Match,
"return".to_string() => TokenType::Return,
"import".to_string() => TokenType::Import
];
static ref RADIX_TO_PREFIX: HashMap<usize, &'static str> = hash_map![
2 => "0b",
8 => "0o",
16 => "0x"
];
fn get_keywords() -> &'static HashMap<String, TokenType> {
static KEYWORDS: OnceLock<HashMap<String, TokenType>> = OnceLock::new();
KEYWORDS.get_or_init(|| {
hash_map![
"while".to_string() => TokenType::While,
"for".to_string() => TokenType::For,
"if".to_string() => TokenType::If,
"else".to_string() => TokenType::Else,
"class".to_string() => TokenType::Class,
"func".to_string() => TokenType::Func,
"match".to_string() => TokenType::Match,
"return".to_string() => TokenType::Return,
"import".to_string() => TokenType::Import
]
})
}

fn get_redix_to_prefix() -> &'static HashMap<usize, &'static str> {
static RADIX_TO_PREFIX: OnceLock<HashMap<usize, &'static str>> = OnceLock::new();
RADIX_TO_PREFIX.get_or_init(|| {
hash_map![
2 => "0b",
8 => "0o",
16 => "0x"
]
})
}

enum NumValue {
Expand Down Expand Up @@ -318,7 +326,7 @@ impl TokenLex<'_> {
break;
}
}
let tmp = KEYWORDS.get(&retname);
let tmp = get_keywords().get(&retname);
match tmp {
Some(val) => Token::new((*val).clone(), None),
None => Token::new(
Expand Down Expand Up @@ -544,7 +552,7 @@ impl TokenLex<'_> {
return Err(RuntimeError::new(
Box::new(self.compiler_data.content.clone()),
ErrorInfo::new(
gettext!(PREFIX_FOR_FLOAT, RADIX_TO_PREFIX[&(radix as usize)]),
gettext!(PREFIX_FOR_FLOAT, get_redix_to_prefix()[&(radix as usize)]),
gettext(SYNTAX_ERROR),
),
));
Expand Down
12 changes: 12 additions & 0 deletions rust/src/tvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,18 @@ impl<'a> Vm<'a> {
.obj_stack
.push(Box::new(TrcInt::new(first._value >> second._value)));
}
codegen::Opcode::SelfNegativeInt => {
let first = self.impl_unary_opcode::<TrcInt>()?;
self.dynadata
.obj_stack
.push(Box::new(TrcInt::new(-first._value)));
}
codegen::Opcode::SelfNegativeFloat => {
let first = self.impl_unary_opcode::<TrcFloat>()?;
self.dynadata
.obj_stack
.push(Box::new(TrcFloat::new(-first._value)));
}
}
self.pc += 1;
}
Expand Down
5 changes: 2 additions & 3 deletions rust/src/tvm/stdlib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::base::stdlib::{new_class_id, Stdlib, ANY_TYPE, STD_CLASS_TABLE};
use crate::base::stdlib::{get_any_type, new_class_id, Stdlib, STD_CLASS_TABLE};
use derive::def_module;

pub mod algo;
Expand All @@ -10,8 +10,7 @@ def_module!(module_name = std, submodules = [prelude, ds, algo]);
pub fn import_stdlib() -> Stdlib {
let _newid = new_class_id();
unsafe {
STD_CLASS_TABLE.push(ANY_TYPE.clone());
STD_CLASS_TABLE.push(get_any_type().clone());
}

init()
}

0 comments on commit 8a0dbf2

Please sign in to comment.