-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
138 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mod prelude; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} | ||
|