Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
limuy2022 committed Jan 29, 2024
2 parents 28c6ca0 + c2e29f2 commit 1c01235
Show file tree
Hide file tree
Showing 24 changed files with 292 additions and 180 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ there are two ways to use it:

They have their own adventages.You can choose by your preference.

The c++ version is the first version of trc.But after I uograded it to cpp 20 stardand.it bacame hard to compile and develop.And there is a lot of trouble codes and bugs in it.So I want to stop to develop it until the cpp 20 standard is more stable.

The rust version is the second version of trc.It is under development now.It is designed better.

I don't know which will be the main version.Maybe both?

## International

Trc supports many different kinds of languages.We use GNU gettext.So it dont't need change the language by hand.
Expand Down
29 changes: 0 additions & 29 deletions cpp/doc/developer/dirs.md

This file was deleted.

24 changes: 0 additions & 24 deletions cpp/doc/developer/system/Compiler/BNF.txt

This file was deleted.

9 changes: 0 additions & 9 deletions cpp/doc/developer/system/Compiler/Compiler.md

This file was deleted.

17 changes: 0 additions & 17 deletions cpp/doc/developer/system/TVM.md

This file was deleted.

24 changes: 0 additions & 24 deletions cpp/doc/developer/system/Trc.md

This file was deleted.

9 changes: 0 additions & 9 deletions cpp/doc/use/ESC.md

This file was deleted.

5 changes: 0 additions & 5 deletions cpp/doc/use/questions.md

This file was deleted.

8 changes: 0 additions & 8 deletions cpp/doc/use/style tool.md

This file was deleted.

6 changes: 2 additions & 4 deletions rust/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# A new instance of Trc language
# The rust version of Trc language

## using language is 100% rust

Rust's modern and safe features make me very intesested in it and decided to learn it by developing this project.

So this is just an experimental project and I will keep develop c++ version and rust version(maybe more kinds of compilers developing by other languages will be added in future)
As a modern language,rust provide us a lot of useful features and I enjoy myself in it.So this project is developed by pure rust.

## Build

Expand Down
3 changes: 2 additions & 1 deletion rust/docs/developer/EBNF.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ statements : statements statement
statement :
ID := expr
ID(argvs)
item : ID | int | float | string
opt_argvs: argvs | empty
argvs : argvs , argv | argv
argv : expr
expr : expr + term | expr - term
term : term * factor | term / factor
factor : (expr) | ID
factor : (expr) | item
3 changes: 2 additions & 1 deletion rust/docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,10 @@ hello world!
## the var of trc

First,we support the UTF-8 with your var name.So you can define your var like this:

```go
你好:=90
```
```

## Data structures for Trc

Expand Down
Binary file modified rust/locales/zh_CN/LC_MESSAGES/trans.mo
Binary file not shown.
10 changes: 10 additions & 0 deletions rust/locales/zh_CN/LC_MESSAGES/trans.po
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,13 @@ msgstr "在模块{}中"
msgid "Error in line {}"
msgstr "错误在第{}行"

msgid "SymbolError"
msgstr "符号错误"

msgid "Symbol {} not found"
msgstr "未找到符号{}"

msgid "Symbol {} redefined"
msgstr "符号{}重定义"


33 changes: 32 additions & 1 deletion rust/src/base/codegen.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use super::func;
use core::cmp::max;
use std::fmt::Display;

#[derive(Debug)]
pub enum Opcode {
Add,
Sub,
Expand All @@ -23,14 +26,30 @@ pub enum Opcode {
BitOr,
BitLeftShift,
BitRightShift,
// change the option code index
// change pc counter
Goto,
// return from a function
PopFrame,
// create a frame to hold the function
NewFrame,
// Load a int from const pool
LoadInt,
// Load a float from const pool
LoadFloat,
// Load a string from const pool
LoadString,
// Load a bigint from const pool
LoadBigInt,
// Load a local var to the stack
LoadLocal,
// Store a local var
StoreLocal,
}

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

pub struct ConstPool {
Expand All @@ -54,10 +73,17 @@ pub struct Inst {
pub operand: usize,
}

impl Inst {
pub fn new(opcode: Opcode, operand: usize) -> Self {
Self { opcode, operand }
}
}

pub struct StaticData {
pub constpool: ConstPool,
pub inst: Vec<Inst>,
pub funcs: Vec<func::Func>,
pub sym_table_sz: usize,
}

impl StaticData {
Expand All @@ -66,6 +92,11 @@ impl StaticData {
constpool: ConstPool::new(),
inst: vec![],
funcs: vec![],
sym_table_sz: 0,
}
}

pub fn update_sym_table_sz(&mut self, newsz: usize) {
self.sym_table_sz = max(self.sym_table_sz, newsz);
}
}
3 changes: 3 additions & 0 deletions rust/src/base/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub const OPERATOR_ERROR: &str = "OperatorError";
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 STRING_WITHOUT_END: &str = "this string should be ended with {}";
pub const UNMATCHED_BRACE: &str = "{} is unmatched";
Expand All @@ -20,6 +21,8 @@ pub const FLOAT_OVER_FLOW: &str = "Float {} is too large to store";
pub const UNEXPECTED_TOKEN: &str = "token {} is not expected";
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";

#[derive(Debug)]
pub struct ErrorInfo {
Expand Down
61 changes: 36 additions & 25 deletions rust/src/compiler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! reference iterator:https://stackoverflow.com/questions/43952104/how-can-i-store-a-chars-iterator-in-the-same-struct-as-the-string-it-is-iteratin
//! reference float hash map:https://www.soinside.com/question/tUJxYmevbVSHZYe2C2AK5o
//! reference iterator:<https://stackoverflow.com/questions/43952104/how-can-i-store-a-chars-iterator-in-the-same-struct-as-the-string-it-is-iteratin>
//! reference float hash map:<https://www.soinside.com/question/tUJxYmevbVSHZYe2C2AK5o>
mod ast;
pub mod scope;
mod token;

use self::token::TokenLex;
Expand Down Expand Up @@ -76,7 +77,7 @@ impl Option {
}
}

#[derive(Hash, Eq, PartialEq, Clone)]
#[derive(Hash, Eq, PartialEq, Clone, Debug)]
pub struct Float {
front: u32,
back: u32,
Expand Down Expand Up @@ -116,11 +117,30 @@ pub struct ValuePool {
const_floats: Pool<Float>,
name_pool: Pool<String>,
const_big_int: Pool<String>,
id_int: Vec<i64>,
id_float: Vec<Float>,
id_str: Vec<String>,
id_name: Vec<String>,
}

const INT_VAL_POOL_ZERO: usize = 0;
const INT_VAL_POOL_ONE: usize = 1;

macro_rules! gen_add_funcs {
($($func_name:ident => ($const_pool:ident, $id_pool:ident, $type:ty)),*) => {
$(
fn $func_name(&mut self, val: $type) -> usize {
let len_tmp = self.$const_pool.len();
let ret = *self.$const_pool.entry(val.clone()).or_insert(len_tmp);
if len_tmp != self.$const_pool.len() {
self.$id_pool.push(val);
}
ret
}
)*
};
}

impl ValuePool {
fn new() -> Self {
let mut ret = Self {
Expand All @@ -129,34 +149,22 @@ impl ValuePool {
const_strings: HashMap::new(),
name_pool: HashMap::new(),
const_big_int: HashMap::new(),
id_int: vec![],
id_float: vec![],
id_str: vec![],
id_name: vec![],
};
ret.add_int(0);
ret.add_int(1);
ret
}

fn add_int(&mut self, val: i64) -> usize {
let len_tmp = self.const_ints.len();
*self.const_ints.entry(val).or_insert(len_tmp)
}

fn string_get(pool: &mut Pool<String>, str: String) -> usize {
let len_tmp = pool.len();
*pool.entry(str).or_insert(len_tmp)
}

fn add_string(&mut self, val: String) -> usize {
Self::string_get(&mut self.const_strings, val)
}

fn add_float(&mut self, val: Float) -> usize {
let len_tmp = self.const_floats.len();
*self.const_floats.entry(val).or_insert(len_tmp)
}

fn add_id(&mut self, val: String) -> usize {
Self::string_get(&mut self.name_pool, val)
}
gen_add_funcs!(
add_int => (const_ints, id_int, i64),
add_float => (const_floats, id_float, Float),
add_string => (const_strings, id_str, String),
add_id => (name_pool, id_name, String)
);

fn store_val_to_vm(&mut self) -> ConstPool {
let mut ret = ConstPool::new();
Expand Down Expand Up @@ -385,5 +393,8 @@ mod tests {
assert_eq!(pool.add_string(String::from("value")), 0);
assert_eq!(pool.add_string(String::from("value")), 0);
assert_eq!(pool.add_string(String::from("vale")), 1);
assert_eq!(pool.id_int[0], 0);
assert_eq!(pool.id_float[0], Float::new(9, 0));
assert_eq!(pool.id_str[1], "vale");
}
}
Loading

0 comments on commit 1c01235

Please sign in to comment.