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 20, 2024
2 parents 83c404e + 738c9fe commit d75e62c
Show file tree
Hide file tree
Showing 11 changed files with 337 additions and 77 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 @@ -22,3 +22,12 @@ msgstr "操作符{}不支持类型{}"

msgid "The number of data of vm stack is not correct, should have {} data"
msgstr "虚拟机栈中数据数量不正确,期望有{}个数据"

msgid "{} is unmatched"
msgstr "{}未匹配"

msgid "ZeroDivisionError"
msgstr "除零错误"

msgid "{} is divided by zero"
msgstr "{}被零除"
14 changes: 12 additions & 2 deletions rust/src/base/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@ const EXIT_FAILURE: i32 = 1;

pub const SYNTAX_ERROR: &str = "SyntaxError";
pub const OPERATOR_ERROR: &str = "OperatorError";
pub const VM_ERROR:&str = "VmError";
pub const VM_ERROR: &str = "VmError";
pub const ZERO_DIVSION_ERROR: &str = "ZeroDivisionError";

pub const STRING_WITHOUT_END: &str = "this string should be ended with {}";
pub const UNMATCHED_BRACE: &str = "{} is unmatched";
pub const OPERATOR_IS_NOT_SUPPORT: &str = "operator {} is not supported for type {}";
pub const VM_DATA_NUMBER:&str = "The number of data of vm stack is not correct, should have {} data";
pub const VM_DATA_NUMBER: &str =
"The number of data of vm stack is not correct, should have {} data";
pub const ZERO_DIV:&str = "{} is divided by zero";

/// maybe useful when you want to use [should_panic]
pub static SHOULD_PANIC:bool = false;

pub struct ErrorInfo {
pub message: String,
Expand Down Expand Up @@ -38,5 +45,8 @@ pub fn report_error(content: &impl ErrorContent, info: ErrorInfo) {
eprintln!("Error in line {}", content.get_line());
eprintln!("In module {}", content.get_module_name());
eprintln!("{}:{}", gettext(info.errot_type), info.message);
if SHOULD_PANIC {
panic!("SHOULD PANIC var which is used to panic when error is reported is set")
}
exit(EXIT_FAILURE);
}
40 changes: 26 additions & 14 deletions rust/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,24 @@ impl Content {
}
}

pub fn new_line(module_name: &str, line: usize) -> Self {
Self {
module_name: String::from(module_name),
line,
}
}

pub fn add_line(&mut self) {
self.line += 1;
}

pub fn del_line(&mut self) {
self.line -= 1;
}

pub fn set_line(&mut self, line: usize) {
self.line = line;
}
}

impl Option {
Expand All @@ -65,51 +76,48 @@ impl Option {

#[derive(Hash, Eq, PartialEq)]
pub struct Float {
front:i32,
back:i32
front: i32,
back: i32,
}

impl Float {
fn new(front:i32, back:i32) -> Self {
Self {
front,
back
}
fn new(front: i32, back: i32) -> Self {
Self { front, back }
}
}

pub struct ValuePool {
const_ints: hash_map::HashMap<i64, usize>,
const_strings: hash_map::HashMap<String, usize>,
const_floats: hash_map::HashMap<Float, usize>
const_floats: hash_map::HashMap<Float, usize>,
}

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

impl ValuePool {
fn new() -> Self {
let mut ret = Self {
const_ints: hash_map::HashMap::new(),
const_floats: hash_map::HashMap::new(),
const_strings: hash_map::HashMap::new()
const_strings: hash_map::HashMap::new(),
};
ret.add_int(0);
ret.add_int(1);
ret
}

fn add_int(&mut self, val:i64) -> usize {
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 add_string(&mut self, val:String) -> usize {
fn add_string(&mut self, val: String) -> usize {
let len_tmp = self.const_strings.len();
*self.const_strings.entry(val).or_insert(len_tmp)
}

fn add_float(&mut self, val:Float) -> usize {
fn add_float(&mut self, val: Float) -> usize {
let len_tmp = self.const_floats.len();
*self.const_floats.entry(val).or_insert(len_tmp)
}
Expand Down Expand Up @@ -145,6 +153,10 @@ impl StringSource {
impl TokenIo for StringSource {
fn unread(&mut self, c: char) {
self.pos -= self.prev_size;
// check if match the right char
if cfg!(debug_assertions) {
assert_eq!(self.text[self.pos..].chars().next().unwrap(), c);
}
}

fn read(&mut self) -> char {
Expand Down
Loading

0 comments on commit d75e62c

Please sign in to comment.