Skip to content

Commit

Permalink
fix brace bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
limuy2022 committed Mar 9, 2024
1 parent 87c58f2 commit 0119112
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 43 deletions.
6 changes: 1 addition & 5 deletions rust/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ pub mod token;

use self::{ast::AstBuilder, token::TokenLex};
use crate::{
base::{
codegen::{ConstPool, StaticData},
error::*,
},
base::{codegen::ConstPool, error::*},
cfg,
};
use rust_i18n::t;
Expand All @@ -21,7 +18,6 @@ use std::{
fs,
io::{self, BufRead},
process::exit,
sync::OnceLock,
vec,
};

Expand Down
26 changes: 17 additions & 9 deletions rust/src/compiler/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,23 @@ enum NumValue {
FloatVal(Float),
}

impl<'a> Iterator for TokenLex<'a> {
type Item = Token;

fn next(&mut self) -> std::option::Option<Self::Item> {
match self.next_token() {
Ok(v) => {
if v.tp == TokenType::EndOfFile {
None
} else {
Some(v)
}
}
Err(_) => None,
}
}
}

impl<'a> TokenLex<'a> {
pub fn new(compiler_data: &'a mut Compiler) -> TokenLex<'a> {
TokenLex {
Expand Down Expand Up @@ -861,15 +878,6 @@ impl<'a> TokenLex<'a> {
}
}

impl Drop for TokenLex<'_> {
fn drop(&mut self) {
// check the braces stack
if let Err(e) = self.check() {
panic!("{}", e);
}
}
}

#[cfg(test)]
mod tests {
use std::{collections::HashSet, fmt::Debug, hash::Hash};
Expand Down
73 changes: 44 additions & 29 deletions rust/src/tools/tshell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,10 @@ use std::io::{self, Write};

use crate::{
base::{codegen::StaticData, error::RunResult},
compiler,
compiler::{self, token::TokenType},
tvm::Vm,
};

fn get_block() -> String {
let mut block = String::new();
let mut cnt = 1;
loop {
for i in 0..cnt {
print!(" ");
}
print!("...");
io::stdout().flush().unwrap();
let mut line = String::new();
io::stdin().read_line(&mut line).unwrap();
block += &line;
if line.ends_with("{\n") {
cnt += 1;
}
if line.ends_with("}\n") {
cnt -= 1;
if cnt == 0 {
break;
}
}
}
block
}

pub fn tshell() -> RunResult<()> {
println!("{}\n\n", t!("tshell.welcome").bold());
let mut compiler = compiler::Compiler::new_string_compiler(
Expand All @@ -46,10 +21,50 @@ pub fn tshell() -> RunResult<()> {
loop {
print!("tshell>");
io::stdout().flush().unwrap();

let mut line = String::new();
io::stdin().read_line(&mut line).unwrap();
if line.ends_with("{\n") {
line += &get_block();
let mut cnt = 0;
// 此处要引入compiler的词法分析器来解析大括号和小括号
let mut braces_lexer = compiler::Compiler::new_string_compiler(
compiler::Option::new(false, compiler::InputSource::StringInternal),
"",
);
let mut check_lexer = braces_lexer.get_token_lex();
let mut flag = true;
loop {
for _ in 0..cnt {
print!("....");
}
io::stdout().flush().unwrap();

let mut tmp = String::new();
io::stdin().read_line(&mut tmp).unwrap();
line += &tmp;
check_lexer.modify_input(Box::new(compiler::StringSource::new(tmp)));
loop {
let i = match check_lexer.next_token() {
Ok(i) => i,
Err(e) => {
eprintln!("{}", e);
flag = false;
break;
}
};
if i.tp == TokenType::EndOfFile {
break;
}
if i.tp == compiler::token::TokenType::LeftBigBrace {
cnt += 1;
} else if i.tp == compiler::token::TokenType::RightBigBrace {
cnt -= 1;
}
}
if cnt == 0 {
break;
}
}
if !flag {
continue;
}
let source = Box::new(compiler::StringSource::new(line));
ast.token_lexer.modify_input(source);
Expand Down

0 comments on commit 0119112

Please sign in to comment.