Skip to content

Commit

Permalink
hello world代码解析正确
Browse files Browse the repository at this point in the history
  • Loading branch information
limuy2022 committed Feb 1, 2024
1 parent 15dd4e8 commit b4b9be7
Show file tree
Hide file tree
Showing 11 changed files with 225 additions and 64 deletions.
29 changes: 23 additions & 6 deletions rust/Cargo.lock

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

2 changes: 1 addition & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ colored = "2.1.0"
downcast-rs = "1.2.0"
lazy_static = "1.4.0"
num-bigint = "0.4.4"
reqwest = { version = "0.11.23", features = ["json", "multipart"] }
reqwest = { version = "0.11.24", features = ["json", "multipart"] }
tokio = { version = "1.35.1", features = ["full"] }
llvm-sys = "170.0.1"

Expand Down
74 changes: 46 additions & 28 deletions rust/src/base/stdlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@ use crate::{
compiler::scope::{Type, TypeAllowNull},
tvm::{stdlib::prelude::*, DynaData},
};
use downcast_rs::{impl_downcast, Downcast};
use lazy_static::lazy_static;
use std::{
collections::{HashMap, HashSet},
sync::Mutex,
sync::{Arc, Mutex},
};

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

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

#[derive(Hash, PartialEq, Eq, Clone, Debug)]
pub struct RustFunction {
Expand All @@ -25,11 +23,32 @@ pub struct RustFunction {
pub return_type: TypeAllowNull,
}

pub trait FunctionInterface {
pub trait FunctionClone {
fn clone_box(&self) -> Box<dyn FunctionInterface>;
}

impl<T> FunctionClone for T
where
T: 'static + FunctionInterface + Clone,
{
fn clone_box(&self) -> Box<dyn FunctionInterface> {
Box::new(self.clone())
}
}

pub trait FunctionInterface: Downcast + FunctionClone {
fn check_argvs(&self, argvs: Vec<Type>) -> Result<(), ErrorInfo>;
fn get_return_type(&self) -> &TypeAllowNull;
}

impl Clone for Box<dyn FunctionInterface> {
fn clone(&self) -> Self {
self.clone_box()
}
}

impl_downcast!(FunctionInterface);

impl FunctionInterface for RustFunction {
fn check_argvs(&self, argvs: Vec<Type>) -> Result<(), ErrorInfo> {
if argvs.len() != self.argvs_type.len() {
Expand Down Expand Up @@ -60,9 +79,7 @@ pub struct RustClass {
pub functions: HashSet<RustFunction>,
}

lazy_static! {
static ref STD_FUNC_ID: Mutex<usize> = Mutex::new(0);
}
pub static mut STD_FUNC_TABLE: Vec<StdlibFunc> = vec![];

impl RustFunction {
pub fn new(
Expand All @@ -71,23 +88,23 @@ impl RustFunction {
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,
unsafe {
STD_FUNC_TABLE.push(ptr);
RustFunction {
name,
buildin_id: STD_FUNC_TABLE.len() - 1,
ptr,
return_type,
argvs_type,
}
}
}
}

#[derive(Debug, Clone)]
pub struct StdlibNode {
pub name: String,
pub sons: HashMap<String, StdlibNode>,
pub sons: HashMap<String, Arc<Mutex<StdlibNode>>>,
pub functions: HashSet<RustFunction>,
}

Expand All @@ -100,31 +117,32 @@ impl StdlibNode {
}
}

pub fn add_module(&mut self, name: String) -> StdlibNode {
let ret = StdlibNode::new(name.clone());
self.sons.insert(name, ret.clone());
pub fn add_module(&mut self, name: String) -> Arc<Mutex<StdlibNode>> {
let ret = Arc::new(Mutex::new(StdlibNode::new(name.clone())));
self.sons.insert(name.clone(), ret.clone());
ret
}

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

pub fn get_module<T: Iterator<Item = String>>(&self, mut path: T) -> Option<&StdlibNode> {
pub fn get_module<T: Iterator<Item = String>>(&self, mut path: T) -> Option<StdlibNode> {
let item = path.next();
if item.is_none() {
return Some(self);
return Some(self.clone());
}
let item = item.unwrap();
return self.sons.get(&item).unwrap().get_module(path);
let lock = self.sons.get(&item).unwrap().lock().unwrap();
return lock.get_module(path);
}
}

pub fn init() -> StdlibNode {
// init stdlib
let mut stdlib = StdlibNode::new("std".to_string());
let mut prelude = stdlib.add_module("prelude".to_string());
prelude.add_function(RustFunction::new(
let prelude = stdlib.add_module("prelude".to_string());
prelude.lock().unwrap().add_function(RustFunction::new(
"print".to_string(),
tvm_print,
vec![Type::Any],
Expand Down
6 changes: 3 additions & 3 deletions rust/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ impl TokenIo for FileSource {
pub struct Compiler {
// to support read from stdin and file
input: Box<dyn TokenIo<Item = char>>,
const_pool: ValuePool,
pub const_pool: ValuePool,
option: Option,
content: Content,
}
Expand Down Expand Up @@ -352,11 +352,11 @@ impl Compiler {
}
}

pub fn lex(&mut self) -> RunResult<()> {
pub fn lex(&mut self) -> RunResult<StaticData> {
let token_lexer = TokenLex::new(self);
let mut ast_builder = ast::AstBuilder::new(token_lexer);
ast_builder.generate_code()?;
Ok(())
Ok(ast_builder.return_static_data())
}
}

Expand Down
Loading

0 comments on commit b4b9be7

Please sign in to comment.