Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
limuy2022 committed Jan 21, 2024
1 parent f6cde84 commit 5c2f170
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 31 deletions.
27 changes: 27 additions & 0 deletions rust/docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ Here are the operator support
|>>|bit right shift|
|!|not|

Obviously,operators like ```+=``` is supported,too.

But,something should be noticed is that you cannot use logical operators for ```int``` or anything else,just for bool.

So,code like this cannot be compiled successfully:

```rust
a := 1
if a {
println(a)
}
```

Ok,just like others language,but there is an important difference.
you cannot use the different types of values to calaulate

Expand Down Expand Up @@ -124,3 +137,17 @@ the second is use ```/**/```,this kind can cross the line,like:
hello world!
*/
```

## Data structures for Trc

Std lib provide many kinds of data structures for Trc.Here is the list:

|Structure|
|:---|
|St table|
|suffix automaton|
|ac automaton|
|list|
|forward list|
|stack|
|deque|
8 changes: 5 additions & 3 deletions rust/src/compiler.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! 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;
mod token;

use self::token::TokenLex;
Expand Down Expand Up @@ -235,7 +236,7 @@ pub struct Compiler {
}

impl Compiler {
fn new(option: Option) -> Self {
pub fn new(option: Option) -> Self {
match option.inputsource {
InputSource::File(ref filename) => {
let f = std::fs::File::open(filename);
Expand All @@ -252,7 +253,7 @@ impl Compiler {
}
}

fn new_string_compiler(option: Option, source: &str) -> Self {
pub fn new_string_compiler(option: Option, source: &str) -> Self {
Compiler {
input: Box::new(StringSource::new(String::from(source))),
const_pool: ValuePool::new(),
Expand All @@ -261,7 +262,8 @@ impl Compiler {
}
}

fn lex(&mut self) {
pub fn lex(&mut self) {
let token_lexer = TokenLex::new(self);
let ast_builder = ast::AstBuilder::new(token_lexer);
}
}
11 changes: 11 additions & 0 deletions rust/src/compiler/ast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use super::TokenLex;

pub struct AstBuilder<'a> {
token_lexer: TokenLex<'a>,
}

impl<'a> AstBuilder<'a> {
pub fn new(token_lexer: TokenLex<'a>) -> Self {
AstBuilder { token_lexer }
}
}
82 changes: 57 additions & 25 deletions rust/src/compiler/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,21 @@ enum TokenType {
// %
Mod,
// //
ExactDivision,
ExactDiv,
// ~
BitNot,
// <<
BitLeftShift,
// >>
BitRightShift,
// &
BitAnd,
// |
BitOr,
// ^
Xor,
// **
Power,
// +=
SelfAdd,
// -=
Expand All @@ -44,10 +58,20 @@ enum TokenType {
SelfExtraDiv,
// %=
SelfMod,
// **
Power,
// **=
SelfPower,
// ~=
SelfBitNot,
// <<=
SelfBitLeftShift,
// >>=
SelfBitRightShift,
// &=
SelfBitAnd,
// |=
SelfBitOr,
// ^=
SelfXor,
IntValue,
StringValue,
FloatValue,
Expand All @@ -71,6 +95,10 @@ enum TokenType {
GreaterEqual,
// !
Not,
// ||
Or,
// &&
And,
EndOfLine,
}

Expand Down Expand Up @@ -134,13 +162,14 @@ macro_rules! self_symbol {
}

macro_rules! double_symbol {
($before_sym:expr, $before_self_sym:expr, $matched_sym:expr, $matched_self_sym:expr, matched_char:expr, $sself:expr) => {{
($before_sym:expr, $before_self_sym:expr, $matched_sym:expr, $matched_self_sym:expr, $matched_char:expr, $sself:expr) => {{
let c = $sself.compiler_data.input.read();
if c == $matched_char {
return self_symbol!($matched_sym, $matched_self_sym, self);
self_symbol!($matched_sym, $matched_self_sym, $sself)
} else {
$sself.compiler_data.input.unread(c);
self_symbol!($before_sym, $before_self_sym, $sself)
}
self.compiler_data.input.unread(c);
return self_symbol!($before_sym, $before_self_sym, self);
}};
}

Expand Down Expand Up @@ -229,29 +258,32 @@ impl TokenLex<'_> {
}
'+' => self_symbol!(TokenType::Add, TokenType::SelfAdd, self),
'-' => self_symbol!(TokenType::Sub, TokenType::SelfSub, self),
'*' => {
let c = self.compiler_data.input.read();
if c == '*' {
self_symbol!(TokenType::Power, TokenType::SelfPower, self)
} else {
self.compiler_data.input.unread(c);
self_symbol!(TokenType::Mul, TokenType::SelfMul, self)
}
}
'*' => double_symbol!(
TokenType::Mul,
TokenType::SelfMul,
TokenType::Power,
TokenType::SelfPower,
'*',
self
),
'%' => self_symbol!(TokenType::Mod, TokenType::SelfMod, self),
'/' => {
let c = self.compiler_data.input.read();
if c == '=' {
Token::new(TokenType::SelfDiv, None)
} else {
self.compiler_data.input.unread(c);
Token::new(TokenType::Div, None)
}
}
'/' => double_symbol!(
TokenType::Div,
TokenType::SelfDiv,
TokenType::ExactDiv,
TokenType::SelfExtraDiv,
'/',
self
),
'=' => binary_symbol!(TokenType::Assign, TokenType::Equal, '=', self),
'!' => binary_symbol!(TokenType::Not, TokenType::NotEqual, '=', self),
'>' => binary_symbol!(TokenType::Greater, TokenType::GreaterEqual, '=', self),
'<' => binary_symbol!(TokenType::Less, TokenType::LessEqual, '=', self),
'~' => Token::new(TokenType::BitNot, None),
'^' => Token::new(TokenType::Xor, None),
'|' => {
binary_symbol!(TokenType::Or, TokenType::BitOr, '|', self)
}
_ => panic!("Not a symbol.Compiler error"),
})
}
Expand Down
5 changes: 4 additions & 1 deletion rust/src/tools/compile_tool.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use crate::compiler;

pub fn compile(opt: compiler::Option) {}
pub fn compile(opt: compiler::Option) {
let mut compiler = compiler::Compiler::new(opt);
compiler.lex();
}
4 changes: 4 additions & 0 deletions rust/src/tvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ enum Opcode {
BitNot,
BitAnd,
BitOr,
BitLeftShift,
BitRightShift,
// change the option code index
Goto,
// return from a function
Expand Down Expand Up @@ -223,6 +225,8 @@ impl<'a> Vm<'a> {
Opcode::BitAnd => binary_opcode!(bit_and, self),
Opcode::BitOr => binary_opcode!(bit_or, self),
Opcode::BitNot => unary_opcode!(bit_not, self),
Opcode::BitLeftShift => binary_opcode!(bit_left_shift, self),
Opcode::BitRightShift => binary_opcode!(bit_right_shift, self),
}
self.pc += 1;
}
Expand Down
4 changes: 3 additions & 1 deletion rust/src/tvm/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ pub trait TrcObj: Downcast + std::fmt::Display {
power => "**",
bit_and => "&",
bit_or => "|",
xor => "~"
xor => "~",
bit_left_shift => "<<",
bit_right_shift => ">>"
);

fn not(&self) -> TypeError {
Expand Down
4 changes: 3 additions & 1 deletion rust/src/tvm/types/trcint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ impl TrcObj for TrcInt {
sub => -, "-", TrcInt, TrcInt,
mul => *, "*", TrcInt, TrcInt,
bit_and => &, "&", TrcInt, TrcInt,
bit_or => |, "|", TrcInt, TrcInt
bit_or => |, "|", TrcInt, TrcInt,
bit_left_shift => <<, "<<", TrcInt, TrcInt,
bit_right_shift => >>, ">>", TrcInt, TrcInt
);

impl_oper!(div, div_int, "/", TrcInt, TrcFloat, ?);
Expand Down

0 comments on commit 5c2f170

Please sign in to comment.