Skip to content

Commit

Permalink
bugfix tshell
Browse files Browse the repository at this point in the history
  • Loading branch information
limuy2022 committed Mar 9, 2024
1 parent 844e5b1 commit 87c58f2
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 33 deletions.
1 change: 1 addition & 0 deletions rust/locales/en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ 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"
if_while_accept_bool = "if/while accept only bool type"

[compiler.report]
in_module = "In module %{name}"
Expand Down
1 change: 1 addition & 0 deletions rust/locales/zh-CN.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ unclosed_format = "{未匹配"
[compiler.typerror]
operator_unsupport = "操作符%{0}不支持类型%{1}"
type_not_same = "类型%{0}和%{1}不同"
if_while_accept_bool = "if和while的条件表达式只能是bool类型"

[compiler.report]
error_in_line = "在第%{line}行发生了错误"
Expand Down
1 change: 1 addition & 0 deletions rust/src/base/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ 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";
pub const JUST_ACCEPT_BOOL: &str = "compiler.typerror.if_while_accept_bool";

#[derive(Debug)]
pub struct ErrorInfo {
Expand Down
59 changes: 33 additions & 26 deletions rust/src/compiler/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::base::{
error::*,
stdlib::{get_stdlib, RustFunction, BOOL, CHAR, FLOAT, INT, STR},
};
use crate::compiler::token::TokenType::RightBigBrace;

use super::{scope::*, token::TokenType, InputSource, TokenLex, ValuePool};

Expand Down Expand Up @@ -669,44 +670,57 @@ impl<'a> AstBuilder<'a> {
Ok(())
}

fn if_lex(&mut self) -> RunResult<()> {
fn lex_block(&mut self, end_state: TokenType) -> RunResult<()> {
loop {
let t = self.token_lexer.next_token()?;
if t.tp == end_state {
break;
}
self.token_lexer.next_back(t);
self.statement()?;
}
Ok(())
}

fn lex_condit(&mut self) -> RunResult<()> {
self.expr(false)?;
match self.process_info.stack_type.last() {
None => {
return self.report_error(ErrorInfo::new(t!(EXPECTED_EXPR), t!(SYNTAX_ERROR)));
}
Some(ty) => {
if *ty != self.cache.boolty_id {
return self.report_error(ErrorInfo::new(t!(JUST_ACCEPT_BOOL), t!(TYPE_ERROR)));
}
}
}
Ok(())
}

fn if_lex(&mut self) -> RunResult<()> {
self.lex_condit()?;
self.check_next_token(TokenType::LeftBigBrace)?;
// 最后需要跳转地址
let mut save_jump_opcode_idx = vec![];
loop {
let op_idx = self.staticdata.inst.len();
// 本行是为了跳转到下一个分支
self.add_bycode(Opcode::JumpIfFalse, ARG_WRONG);
loop {
let t = self.token_lexer.next_token()?;
if t.tp == TokenType::RightBigBrace {
break;
}
self.token_lexer.next_back(t);
self.statement()?;
}
self.lex_block(RightBigBrace)?;
self.staticdata.inst[op_idx].operand = self.staticdata.get_last_opcode_id() + 1;
self.add_bycode(Opcode::Jump, ARG_WRONG);
save_jump_opcode_idx.push(self.staticdata.get_last_opcode_id());
let t = self.token_lexer.next_token()?;
if t.tp == TokenType::Else {
let nxt_tok = self.token_lexer.next_token()?;
if nxt_tok.tp == TokenType::If {
self.expr(false)?;
self.lex_condit()?;
self.check_next_token(TokenType::LeftBigBrace)?;
continue;
}
self.token_lexer.next_back(nxt_tok);
self.check_next_token(TokenType::LeftBigBrace)?;
loop {
let t = self.token_lexer.next_token()?;
if t.tp == TokenType::RightBigBrace {
break;
}
self.token_lexer.next_back(t);
self.statement()?;
}
self.lex_block(RightBigBrace)?;
break;
}
save_jump_opcode_idx.pop();
Expand Down Expand Up @@ -775,14 +789,7 @@ impl<'a> AstBuilder<'a> {
}

pub fn generate_code(&mut self) -> RunResult<()> {
loop {
let token = self.token_lexer.next_token()?;
if token.tp == TokenType::EndOfFile {
break;
}
self.token_lexer.next_back(token);
self.statement()?;
}
self.lex_block(TokenType::EndOfFile)?;
Ok(())
}

Expand Down
3 changes: 2 additions & 1 deletion rust/src/compiler/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -759,8 +759,9 @@ impl<'a> TokenLex<'a> {
Err(e)
}

fn clear_error(&mut self) {
pub fn clear_error(&mut self) {
self.braces_check.clear();
self.unget_token.clear();
}

pub fn next_token(&mut self) -> RunResult<Token> {
Expand Down
15 changes: 10 additions & 5 deletions rust/src/tools/tshell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,23 @@ 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();
if line.ends_with('{') {
block += &line;
if line.ends_with("{\n") {
cnt += 1;
}
if line.ends_with('}') {
if line.ends_with("}\n") {
cnt -= 1;
if cnt == 0 {
break;
}
}
block += &line;
}
block
}
Expand All @@ -43,13 +48,13 @@ pub fn tshell() -> RunResult<()> {
io::stdout().flush().unwrap();
let mut line = String::new();
io::stdin().read_line(&mut line).unwrap();
if line.ends_with('{') {
if line.ends_with("{\n") {
line += &get_block();
continue;
}
let source = Box::new(compiler::StringSource::new(line));
ast.token_lexer.modify_input(source);
ast.clear_inst();
ast.token_lexer.clear_error();
ast.generate_code().unwrap_or_else(|e| {
eprintln!("{}", e);
});
Expand Down
7 changes: 6 additions & 1 deletion rust/src/tvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,14 @@ impl<'a> Vm<'a> {
}
}

pub fn run(&mut self) -> Result<(), RuntimeError> {
pub fn reset(&mut self) {
self.pc = 0;
self.dynadata
.resize_var_store(self.static_data.sym_table_sz);
}

pub fn run(&mut self) -> Result<(), RuntimeError> {
self.reset();
while self.pc < self.static_data.inst.len() {
if self.static_data.has_line_table {
self.run_context
Expand Down

0 comments on commit 87c58f2

Please sign in to comment.