Skip to content

Commit

Permalink
用于保命的commit
Browse files Browse the repository at this point in the history
  • Loading branch information
limuy2022 committed Feb 1, 2024
1 parent 8debe05 commit 7f3c27e
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 15 deletions.
9 changes: 9 additions & 0 deletions rust/locales/zh_CN/LC_MESSAGES/trans.po
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,12 @@ msgstr "类型错误"
msgid "Type {} and {} are not the same"
msgstr "类型{}和{}不同"

msgid "ArgumentError"
msgstr "参数错误"

msgid "expect {}.But given {}"
msgstr "期望{}.但是输入{}"

msgid "Expect type {}.But given type {}"
msgstr "期望类型{}.但是传入类型是{}"

3 changes: 3 additions & 0 deletions rust/src/base/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub const ZERO_DIVSION_ERROR: &str = "ZeroDivisionError";
pub const NUMBER_OVER_FLOW: &str = "NumberOverFlowError";
pub const SYMBOL_ERROR: &str = "SymbolError";
pub const TYPE_ERROR: &str = "TypeError";
pub const ARGUMENT_ERROR: &str = "ArgumentError";

pub const STRING_WITHOUT_END: &str = "this string should be ended with {}";
pub const UNMATCHED_BRACE: &str = "{} is unmatched";
Expand All @@ -25,6 +26,8 @@ pub const IN_MODULE: &str = "In module {}";
pub const SYMBOL_NOT_FOUND: &str = "Symbol {} not found";
pub const SYMBOL_REDEFINED: &str = "Symbol {} redefined";
pub const TYPE_NOT_THE_SAME: &str = "Type {} and {} are not the same";
pub const ARGU_NUMBER: &str = "expect {}.But given {}";
pub const EXPECT_TYPE: &str = "Expect type {}.But given type {}";

#[derive(Debug)]
pub struct ErrorInfo {
Expand Down
100 changes: 99 additions & 1 deletion rust/src/base/stdlib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,99 @@
pub const STDLIB_LIST: Vec<String> = vec![];
use std::{collections::{HashMap, HashSet}, sync::Mutex};
use lazy_static::lazy_static;
use crate::{base::error::{ARGUMENT_ERROR, ARGU_NUMBER, EXPECT_TYPE, SYNTAX_ERROR}, compiler::scope::{Type, TypeAllowNull}, tvm::DynaData};

use super::{codegen::Opcode, error::{ErrorInfo, RunResult}};

type StdlibFunc = fn(DynaData) -> RunResult<()>;

#[derive(Hash, PartialEq, Eq)]
pub struct RustFunction {
pub name: String,
pub buildin_id: usize,
pub ptr : StdlibFunc,
pub argvs_type: Vec<Type>,
pub return_type: TypeAllowNull
}

trait Function {
fn gen_code(&self) -> Opcode;
fn check_argvs(&self, argvs:Vec<Type>) -> Result<(), ErrorInfo> ;
fn get_return_type(&self) -> &TypeAllowNull;
}

impl Function for RustFunction {
fn gen_code(&self) -> Opcode {
return Opcode::CallNative;
}

fn check_argvs(&self, argvs:Vec<Type>) -> Result<(), ErrorInfo> {
if argvs.len() != self.argvs_type.len() {
return Err(ErrorInfo::new(
gettextrs::gettext!(ARGU_NUMBER, self.argvs_type.len(), argvs.len()),
gettextrs::gettext(ARGUMENT_ERROR)
))
}
for i in 0..self.argvs_type.len() {
if argvs[i] != self.argvs_type[i] {
return Err(ErrorInfo::new(
gettextrs::gettext!(EXPECT_TYPE, self.argvs_type[i].origin_name, argvs[i].origin_name),
gettextrs::gettext(ARGUMENT_ERROR)
))
}
}
Ok(())
}

fn get_return_type(&self) -> &TypeAllowNull {
&self.return_type
}
}

pub struct RustClass {
pub name: String,
pub members: HashMap<String, String>,
pub functions: HashSet<RustFunction>
}

lazy_static!{
static ref STD_FUNC_ID: Mutex<usize> = Mutex::new(0);
}

impl RustFunction {
pub fn new(name: String, ptr: StdlibFunc, argvs_type: Vec<Type>, return_type: TypeAllowNull) -> RustFunction {
let mut lock = STD_FUNC_ID.lock().unwrap();
let tmp = *lock;
*lock += 1;
RustFunction { name, buildin_id:tmp, ptr , return_type, argvs_type}
}
}

pub struct StdlibNode {
pub name: String,
pub sons: HashMap<String, StdlibNode>,
pub functions: HashSet<RustFunction>
}

impl StdlibNode {
pub fn new(name: String) -> StdlibNode {
StdlibNode { name, sons: HashMap::new() , functions: HashSet::new()}
}

pub fn add_module(&mut self, name: String) {
self.sons.insert(name.clone(), StdlibNode::new(name));
}

pub fn add_function(&mut self, name: RustFunction) {
self.functions.insert(name);
}
}

pub fn init() -> StdlibNode {
// init stdlib
let mut stdlib = StdlibNode::new("std".to_string());
stdlib
}

lazy_static! {
pub static ref STDLIB_LIST:StdlibNode = init();
}
31 changes: 19 additions & 12 deletions rust/src/compiler/scope.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use lazy_static::lazy_static;
use std::{cell::RefCell, collections::HashMap, fmt::Display, rc::Rc};

use crate::base::stdlib::{StdlibNode, STDLIB_LIST};
use super::{BOOL_ID_POS, FLOAT_ID_POS, INT_ID_POS, STR_ID_POS};

lazy_static! {
Expand All @@ -13,14 +13,14 @@ lazy_static! {
];
}

#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum TypeAllowNull {
Yes(Type),
No,
}

/// Manager of function
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Function {
args_type: Vec<usize>,
return_type: TypeAllowNull,
Expand All @@ -37,31 +37,33 @@ impl Function {
pub fn add_argv() {}
}

#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Var {
ty: Function,
ty: Type,
}

impl Var {
pub fn new(ty: Function) -> Self {
pub fn new(ty: Type) -> Self {
Self { ty }
}
}

/// Manager of type
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Type {
attr: Vec<Var>,
funcs: Vec<Function>,
pub name: usize,
pub origin_name: String
}

impl Type {
pub fn new(name: usize) -> Self {
pub fn new(name: usize, origin_name: String) -> Self {
Self {
attr: vec![],
funcs: vec![],
name,
origin_name
}
}

Expand All @@ -74,10 +76,10 @@ impl Type {
}
}
lazy_static! {
pub static ref INT_TYPE: Type = Type::new(INT_ID_POS);
pub static ref FLOAT_TYPE: Type = Type::new(INT_ID_POS);
pub static ref STR_TYPE: Type = Type::new(INT_ID_POS);
pub static ref BOOL_TYPE: Type = Type::new(INT_ID_POS);
pub static ref INT_TYPE: Type = Type::new(INT_ID_POS, "int".to_string());
pub static ref FLOAT_TYPE: Type = Type::new(INT_ID_POS, "float".to_string());
pub static ref STR_TYPE: Type = Type::new(INT_ID_POS, "str".to_string());
pub static ref BOOL_TYPE: Type = Type::new(INT_ID_POS, "bool".to_string());
}

pub struct SymScope {
Expand Down Expand Up @@ -114,6 +116,11 @@ impl SymScope {
}
ret
}

/// import the module defined in rust
pub fn import_native_module(&mut self, stdlib:StdlibNode) {

}

pub fn has_sym(&self, id: usize) -> bool {
if self.sym_map.contains_key(&id) {
Expand Down
2 changes: 1 addition & 1 deletion rust/src/tvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod algo;
mod def;
mod function;
mod gc;
mod rustlib;
mod stdlib;
mod types;

use self::types::trcfloat::TrcFloat;
Expand Down
1 change: 0 additions & 1 deletion rust/src/tvm/rustlib.rs

This file was deleted.

1 change: 1 addition & 0 deletions rust/src/tvm/stdlib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod prelude;
6 changes: 6 additions & 0 deletions rust/src/tvm/stdlib/prelude.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use crate::{base::error::RunResult, tvm::DynaData};

pub fn tvm_print(dydata:DynaData) -> RunResult<()> {
Ok(())
}

0 comments on commit 7f3c27e

Please sign in to comment.