Skip to content

Commit

Permalink
update deps
Browse files Browse the repository at this point in the history
change gettext-rs to rust-i18n

fix i18n string
  • Loading branch information
limuy2022 committed Feb 23, 2024
1 parent 1205443 commit cf64dea
Show file tree
Hide file tree
Showing 26 changed files with 808 additions and 482 deletions.
504 changes: 402 additions & 102 deletions rust/Cargo.lock

Large diffs are not rendered by default.

17 changes: 15 additions & 2 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ repository = "https://github.com/limuy2022/trc"

[dependencies]
rand = "0.8.5"
clap = { version = "4.5.0", features = ["derive"] }
gettext-rs = "0.7.0"
clap = { version = "4.5.1", features = ["derive"] }
colored = "2.1.0"
downcast-rs = "1.2.0"
num-bigint = "0.4.4"
llvm-sys = "170.0.1"
derive = { path = "./derive" }
libloading = "0.8.1"
stdlib = { path = "./stdlib" }
rust-i18n = "3.0.1"
sys-locale = "0.3.1"

[profile.release]
panic = "abort"
Expand All @@ -29,3 +30,15 @@ strip = true

[workspace]
members = ["derive", ".", "stdlib"]

[package.metadata.i18n]
# The available locales for your application, default: ["en"].
available-locales = ["en", "zh-CN"]

# The default locale, default: "en".
default-locale = "en"

# Path for your translations YAML file, default: "locales".
# This config for let `cargo i18n` command line tool know where to find your translations.
# You must keep this path same as the one you pass to method `rust_i18n::i18n!`.
load-path = "locales"
1 change: 1 addition & 0 deletions rust/examples/print.trc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("{},{}!", "hello", "world")
51 changes: 51 additions & 0 deletions rust/locales/en.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
[tshell]
welcome = "Welcome to tshell.Type help() to get more infomation"

[compiler]
ZeroDivisionError = "ZeroDivisionError"
NumberOverFlowError = "NumberOverFlowError"
ArgumentError = "ArgumentError"
FormatStringError = "FormatStringError"
SymbolError = "SymbolError"
TypeError = "TypeError"
OperatorError = "OperatorError"
VmError = "VmError"
SyntaxError = "SyntaxError"

[compiler.syntaxerror]
str_error = "this string should be ended with \""
unmatched = "%{0} is unmatched"
prefix_float = "Prefix %{0} can be used for float"
unexpected_token = "token %{0} is not expected"
unclosed_comment = "unclosed comment"
char_error = "char should be the format like 'x'."

[compiler.symbolerror]
not_found_sym = "Symbol %{0} not found"
redefined_sym = "Symbol %{0} redefined"

[compiler.argumenterror]
argu_num = "expect %{0}.But given %{1}"
type_unmatched = "Expect type %{0}.But given type %{1}"
void_argu = "argument cannot be 'void'"

[compiler.formatstringerror]
unclosed_format = "unmatched {"

[compiler.typerror]
operator_unsupport = "operator %{0} is not supported for type %{1}"
type_not_same = "Type %{0} and %{1} are not the same"

[compiler.report]
in_module = "In module %{name}"
error_in_line = "Error in line %{line}"

[compiler.numberoverflowerror]
float_over_flow = "Float %{0} is too large to store"

[vm.vmerror]
data_num_error = "The number of data of vm stack is not correct, should have %{0} data"
stack_frame_empty = "frame stack is empty.But running a pop frame opcode"

[vm.zerodivisionerror]
zerodiv = "%{0} is divided by zero"
51 changes: 51 additions & 0 deletions rust/locales/zh-CN.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
[tshell]
welcome = "欢迎使用tshell。输入help()获取更多信息"

[compiler]
NumberOverFlowError = "数值溢出错误"
FormatStringError = "格式化字符串错误"
SymbolError = "符号错误"
TypeError = "类型错误"
ZeroDivisionError = "除零错误"
VmError = "虚拟机错误"
ArgumentError = "参数错误"
SyntaxError = "语法错误"
OperatorError = "操作符错误"

[compiler.syntaxerror]
unmatched = "%{0}未匹配"
char_error = "字符应当以类似'x'的格式书写."
str_error = "这个字符串应当以\"结束"
unclosed_comment = "未闭合的注释"
prefix_float = "前缀%{0}不能对浮点数使用"
unexpected_token = "token %{0}不是被期望的"

[compiler.symbolerror]
not_found_sym = "未找到符号%{0}"
redefined_sym = "符号%{0}重定义"

[compiler.argumenterror]
void_argu = "参数不能为void"
type_unmatched = "期望类型%{0}.但是传入类型是%{1}"
argu_num = "期望%{0}个参数.但是输入%{1}个参数"

[compiler.formatstringerror]
unclosed_format = "{未匹配"

[compiler.typerror]
operator_unsupport = "操作符%{0}不支持类型%{1}"
type_not_same = "类型%{0}和%{1}不同"

[compiler.report]
error_in_line = "错误在第%{line}行"
in_module = "在模块%{name}中"

[compiler.numberoverflowerror]
float_over_flow = "浮点数%{0}超过了储存范围"

[vm.vmerror]
data_num_error = "虚拟机栈中数据数量不正确,期望有%{0}个数据"
stack_frame_empty = "帧栈为空,但运行了pop frame指令"

[vm.zerodivisionerror]
zerodiv = "%{0}被零除"
Binary file removed rust/locales/zh_CN/LC_MESSAGES/trans.mo
Binary file not shown.
94 changes: 0 additions & 94 deletions rust/locales/zh_CN/LC_MESSAGES/trans.po

This file was deleted.

21 changes: 0 additions & 21 deletions rust/script/gen_locales.py

This file was deleted.

4 changes: 3 additions & 1 deletion rust/src/base/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,12 @@ pub enum VmStackType {
Object,
}

pub type InstSet = Vec<Inst>;

#[derive(Default)]
pub struct StaticData {
pub constpool: ConstPool,
pub inst: Vec<Inst>,
pub inst: InstSet,
pub funcs: Vec<func::Func>,
pub sym_table_sz: usize,
pub line_table: Vec<usize>,
Expand Down
68 changes: 34 additions & 34 deletions rust/src/base/error.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
use colored::Colorize;
use gettextrs::gettext;
use rust_i18n::t;
use std::error::Error;
use std::fmt::{Debug, Display};

pub const SYNTAX_ERROR: &str = "SyntaxError";
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 TYPE_ERROR: &str = "TypeError";
pub const ARGUMENT_ERROR: &str = "ArgumentError";
pub const FORMAT_STR_ERROR: &str = "FormatStringError";

pub const STRING_WITHOUT_END: &str = "this string should be ended with {}";
pub const CHAR_FORMAT: &str = "char should be the format like 'x'.";
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_FRAME_EMPTY: &str = "frame stack is empty.But running a pop frame opcode";
pub const ZERO_DIV: &str = "{} is divided by zero";
pub const PREFIX_FOR_FLOAT: &str = "Prefix {} can be used for float";
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";
pub const TYPE_NOT_THE_SAME: &str = "Type {} and {} are not the same";
pub const ARGU_NUMBER: &str = "expect {}.But given {}";
pub const EXPECT_TYPE: &str = "Expect type {}.But given type {}";
pub const UNCLODED_COMMENT: &str = "unclosed comment";
pub const UNCLOSED_FORMAT: &str = "{ is not matched.";
pub const SYNTAX_ERROR: &str = "compiler.SyntaxError";
pub const OPERATOR_ERROR: &str = "compiler.OperatorError";
pub const VM_ERROR: &str = "compiler.VmError";
pub const ZERO_DIVSION_ERROR: &str = "compiler.ZeroDivisionError";
pub const NUMBER_OVER_FLOW: &str = "compiler.NumberOverFlowError";
pub const SYMBOL_ERROR: &str = "compiler.SymbolError";
pub const TYPE_ERROR: &str = "compiler.TypeError";
pub const ARGUMENT_ERROR: &str = "compiler.ArgumentError";
pub const FORMAT_STR_ERROR: &str = "compiler.FormatStringError";

pub const STRING_WITHOUT_END: &str = "compiler.syntaxerror.str_error";
pub const CHAR_FORMAT: &str = "compiler.syntaxerror.char_error";
pub const UNMATCHED_BRACE: &str = "compiler.syntaxerror.unmatched";
pub const OPERATOR_IS_NOT_SUPPORT: &str = "compiler.typerror.operator_unsupport";
pub const VM_DATA_NUMBER: &str = "vm.vmerror.data_num_error";
pub const VM_FRAME_EMPTY: &str = "vm.vmerror.stack_frame_empty";
pub const ZERO_DIV: &str = "vm.zerodivisionerror.zerodiv";
pub const PREFIX_FOR_FLOAT: &str = "compiler.syntaxerror.prefix_float";
pub const FLOAT_OVER_FLOW: &str = "compiler.numberoverflowerror.float_over_flow";
pub const UNEXPECTED_TOKEN: &str = "compiler.syntaxerror.unexpected_token";
pub const ERROR_IN_LINE: &str = "compiler.report.error_in_line";
pub const IN_MODULE: &str = "compiler.report.in_module";
pub const SYMBOL_NOT_FOUND: &str = "compiler.symbolerror.not_found_sym";
pub const SYMBOL_REDEFINED: &str = "compiler.symbolerror.redefined_sym";
pub const TYPE_NOT_THE_SAME: &str = "compiler.typerror.type_not_same";
pub const ARGU_NUMBER: &str = "compiler.argumenterror.argu_num";
pub const EXPECT_TYPE: &str = "compiler.argumenterror.type_unmatched";
pub const UNCLODED_COMMENT: &str = "compiler.syntaxerror.unclosed_comment";
pub const UNCLOSED_FORMAT: &str = "compiler.formatstringerror.unclosed_format";
pub const ARGUMENT_CANNOT_BE_VOID: &str = "compiler.argumenterror.void_argu";

#[derive(Debug)]
pub struct ErrorInfo {
Expand Down Expand Up @@ -72,9 +72,9 @@ impl Display for RuntimeError {
r#"{}
{}
{}:{}"#,
gettext!(ERROR_IN_LINE, self.content.get_line()),
gettext!(IN_MODULE, self.content.get_module_name()),
gettext(self.info.error_type.clone()).red(),
t!(ERROR_IN_LINE, line = self.content.get_line()),
t!(IN_MODULE, name = self.content.get_module_name()),
self.info.error_type.clone().red(),
self.info.message.red()
);
write!(f, "{}", s)
Expand Down
9 changes: 5 additions & 4 deletions rust/src/base/stdlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
tvm::DynaData,
};
use downcast_rs::{impl_downcast, Downcast};
use rust_i18n::t;
use std::sync::OnceLock;
use std::{
collections::HashMap,
Expand Down Expand Up @@ -45,8 +46,8 @@ impl IOType {
pub fn check_argvs(&self, argvs: Vec<usize>) -> 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),
t!(ARGU_NUMBER, "0" = self.argvs_type.len(), "1" = argvs.len()),
t!(ARGUMENT_ERROR),
));
}
for i in argvs.iter().enumerate().take(self.argvs_type.len()) {
Expand All @@ -55,8 +56,8 @@ impl IOType {
}
if self.argvs_type[i.0] != *i.1 {
return Err(ErrorInfo::new(
gettextrs::gettext!(EXPECT_TYPE, self.argvs_type[i.0], i.1),
gettextrs::gettext(ARGUMENT_ERROR),
t!(EXPECT_TYPE, "0" = self.argvs_type[i.0], "1" = i.1),
t!(ARGUMENT_ERROR),
));
}
}
Expand Down
Loading

0 comments on commit cf64dea

Please sign in to comment.