Skip to content

Commit

Permalink
finish tshell's function support
Browse files Browse the repository at this point in the history
  • Loading branch information
limuy2022 committed Mar 17, 2024
1 parent 9fb421e commit 5a116e9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
25 changes: 9 additions & 16 deletions src/compiler/ast.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
use std::{
borrow::{Borrow, BorrowMut},
cell::RefCell,
collections::HashMap,
rc::Rc,
};

use rust_i18n::t;

use crate::base::{
codegen::{Inst, Opcode, StaticData, VmStackType, ARG_WRONG, NO_ARG},
error::*,
func,
stdlib::{get_stdlib, ArgsNameTy, IOType, RustFunction, BOOL, CHAR, FLOAT, INT, STR},
};
use crate::compiler::token::TokenType::RightBigBrace;
use rust_i18n::t;
use std::{cell::RefCell, rc::Rc};

use super::{
scope::*,
Expand Down Expand Up @@ -158,6 +151,7 @@ impl<'a> AstBuilder<'a> {
pub fn clear_inst(&mut self) {
self.staticdata.inst.clear();
self.staticdata.function_split = None;
self.first_func = false
}

fn report_error<T>(&self, info: ErrorInfo) -> AstError<T> {
Expand Down Expand Up @@ -963,12 +957,11 @@ impl<'a> AstBuilder<'a> {
}

fn generate_func_in_scope(&mut self) -> AstError<()> {
let tmp = self
.self_scope
.as_ref()
.borrow_mut()
.funcs_temp_store
.clone();
let mut tmp = vec![];
std::mem::swap(
&mut tmp,
&mut self.self_scope.as_ref().borrow_mut().funcs_temp_store,
);
for i in tmp {
let code_begin = self.lex_function(&i.1)?;
self.staticdata.funcs.push(func::Func::new(code_begin))
Expand All @@ -983,7 +976,7 @@ impl<'a> AstBuilder<'a> {
Ok(())
}

fn add_bycode(&mut self, opty: Opcode, opnum: usize) {
pub fn add_bycode(&mut self, opty: Opcode, opnum: usize) {
self.staticdata.inst.push(Inst::new(opty, opnum));
if !self.token_lexer.compiler_data.option.optimize {
// 不生成行号表了
Expand Down
31 changes: 27 additions & 4 deletions src/tools/tshell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use colored::*;
use rust_i18n::t;
use rustyline::{config::Configurer, history::FileHistory, Editor};

use crate::base::codegen::{Opcode, NO_ARG};
use crate::{
base::{codegen::StaticData, error::RunResult},
compiler::{self, token::TokenType},
Expand All @@ -27,6 +28,7 @@ pub fn tshell() -> RunResult<()> {
let mut vm = unsafe { Vm::new(&*(ast.prepare_get_static() as *const StaticData)) };
let mut should_exit = false;
let mut function_list = vec![];
let mut function_defined_num = 0;
'stop_repl: loop {
let mut line = String::new();
let mut cnt = 0;
Expand Down Expand Up @@ -88,11 +90,18 @@ pub fn tshell() -> RunResult<()> {
ast.token_lexer.modify_input(source);
ast.clear_inst();
ast.token_lexer.clear_error();
ast.generate_code().unwrap_or_else(|e| {
if let Err(e) = ast.generate_code() {
eprintln!("{}", e);
});
continue;
}
// 将之前的函数添加进去
ast.staticdata.inst.extend(function_list.clone());
if !function_list.is_empty() {
ast.add_bycode(Opcode::Stop, NO_ARG);
for i in 0..function_defined_num {
ast.staticdata.funcs[i].func_addr += ast.staticdata.get_next_opcode_id();
}
ast.staticdata.inst.extend(function_list.clone());
}
vm.set_static_data(unsafe { &*(ast.prepare_get_static() as *const StaticData) });
match vm.run() {
Ok(_) => {}
Expand All @@ -102,7 +111,21 @@ pub fn tshell() -> RunResult<()> {
}
// 切分之前定义的函数
if let Some(func_split) = ast.staticdata.function_split {
function_list = ast.staticdata.inst[func_split + 1..].to_vec()
function_list = ast.staticdata.inst[func_split + 1..].to_vec();
function_defined_num = ast.staticdata.funcs.len();
for i in &mut ast.staticdata.funcs {
i.func_addr -= func_split + 1;
}
} else {
let mut basis = usize::MAX;
for i in &ast.staticdata.funcs {
if i.func_addr < basis {
basis = i.func_addr;
}
}
for i in &mut ast.staticdata.funcs {
i.func_addr -= basis;
}
}
io::stdout().flush().unwrap();
should_exit = false;
Expand Down

0 comments on commit 5a116e9

Please sign in to comment.