Skip to content

Commit

Permalink
加入类型系统
Browse files Browse the repository at this point in the history
  • Loading branch information
limuy2022 committed Jan 31, 2024
1 parent ce015c2 commit 8debe05
Show file tree
Hide file tree
Showing 15 changed files with 392 additions and 110 deletions.
33 changes: 33 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 @@ -19,6 +19,7 @@ lazy_static = "1.4.0"
num-bigint = "0.4.4"
reqwest = { version = "0.11.23", features = ["json", "multipart"] }
tokio = { version = "1.35.1", features = ["full"] }
llvm-sys = "170.0.1"

[profile.release]
panic = "abort"
14 changes: 14 additions & 0 deletions rust/docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,17 @@ func add<T>(T a, T b) -> T {
return a + b;
}
```

## module

You can import by the following ways:

```go
import "a.b.c"
```

Or:

```go
import "a/b/c"
```
Binary file modified rust/locales/zh_CN/LC_MESSAGES/trans.mo
Binary file not shown.
6 changes: 6 additions & 0 deletions rust/locales/zh_CN/LC_MESSAGES/trans.po
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,9 @@ msgstr "符号{}重定义"
msgid "Only support Linux.Users on other platforms should update program by hand"
msgstr "仅支持在linux上直接更新文件.其他操作系统用户请自行更新程序"

msgid "TypeError"
msgstr "类型错误"

msgid "Type {} and {} are not the same"
msgstr "类型{}和{}不同"

1 change: 1 addition & 0 deletions rust/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ pub mod codegen;
pub mod ctrc;
pub mod error;
pub mod func;
pub mod stdlib;
pub mod utils;
2 changes: 2 additions & 0 deletions rust/src/base/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ pub enum Opcode {
Empty,
// a = -a
SelfNegative,
// call native func
CallNative,
}

impl Display for Opcode {
Expand Down
45 changes: 45 additions & 0 deletions rust/src/base/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub const VM_ERROR: &str = "VmError";
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 STRING_WITHOUT_END: &str = "this string should be ended with {}";
pub const UNMATCHED_BRACE: &str = "{} is unmatched";
Expand All @@ -23,6 +24,7 @@ pub const ERROR_IN_LINE: &str = "Error in line {}";
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";

#[derive(Debug)]
pub struct ErrorInfo {
Expand Down Expand Up @@ -77,4 +79,47 @@ impl RuntimeError {
}
}

#[derive(Debug)]
pub struct LightFakeError {}

impl Error for LightFakeError {}

impl Display for LightFakeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "")
}
}

impl LightFakeError {
pub fn new() -> LightFakeError {
LightFakeError {}
}
}

pub struct LightFakeContent {}

impl ErrorContent for LightFakeError {
fn get_module_name(&self) -> &str {
""
}
fn get_line(&self) -> usize {
0
}
}

impl LightFakeContent {
pub fn new() -> LightFakeContent {
LightFakeContent {}
}
}

impl From<LightFakeError> for RuntimeError {
fn from(_: LightFakeError) -> RuntimeError {
RuntimeError::new(
Box::new(LightFakeError::new()),
ErrorInfo::new("".to_string(), "".to_string()),
)
}
}

pub type RunResult<T> = Result<T, RuntimeError>;
1 change: 1 addition & 0 deletions rust/src/base/stdlib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub const STDLIB_LIST: Vec<String> = vec![];
10 changes: 9 additions & 1 deletion rust/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::fmt::Display;
use std::io::BufRead;
use std::{fs, io, vec};

#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum InputSource {
File(String),
StringInternal,
Expand Down Expand Up @@ -133,6 +133,10 @@ pub struct ValuePool {

const INT_VAL_POOL_ZERO: usize = 0;
const INT_VAL_POOL_ONE: usize = 1;
const INT_ID_POS: usize = 0;
const FLOAT_ID_POS: usize = 1;
const BOOL_ID_POS: usize = 2;
const STR_ID_POS: usize = 3;

macro_rules! gen_add_funcs {
($($func_name:ident => ($const_pool:ident, $id_pool:ident, $type:ty)),*) => {
Expand Down Expand Up @@ -164,6 +168,10 @@ impl ValuePool {
};
ret.add_int(0);
ret.add_int(1);
ret.add_id("int".to_string());
ret.add_id("float".to_string());
ret.add_id("bool".to_string());
ret.add_id("str".to_string());
ret
}

Expand Down
Loading

0 comments on commit 8debe05

Please sign in to comment.