Skip to content

Commit

Permalink
var define feature added
Browse files Browse the repository at this point in the history
  • Loading branch information
limuy2022 committed Mar 5, 2024
1 parent 49ccda5 commit da8c169
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
4 changes: 2 additions & 2 deletions rust/src/compiler/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ impl<'a> AstBuilder<'a> {
.borrow_mut()
.add_var(sym_idx, var_type);
self.staticdata
.update_sym_table_sz(self.self_scope.as_ref().borrow().get_scope_last_idx());
.update_sym_table_sz(self.self_scope.as_ref().borrow().get_var_table_sz());
self.add_bycode(
if var_type == self.cache.intty_id {
Opcode::StoreInt
Expand Down Expand Up @@ -715,7 +715,7 @@ mod tests {
assert_eq!(
t.staticdata.inst,
vec![
Inst::new(Opcode::LoadInt, INT_VAL_POOL_ONE),
Inst::new(Opcode::LoadInt, 2),
Inst::new(Opcode::StoreInt, 0)
],
)
Expand Down
4 changes: 4 additions & 0 deletions rust/src/compiler/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,10 @@ impl SymScope {
self.scope_sym_id
}

pub fn get_var_table_sz(&self) -> usize {
self.vars_id
}

pub fn get_class(&self, classid: usize) -> Option<Type> {
// 在标准库界限内
if classid < get_stdclass_end() {
Expand Down
18 changes: 16 additions & 2 deletions rust/src/tvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ mod gc;
pub mod stdlib;
mod types;

use std::ptr;

use self::{
gc::GcMgr,
types::{
Expand Down Expand Up @@ -61,7 +63,13 @@ impl<'a> DynaData<'a> {
}

pub fn resize_var_store(&mut self, cap: usize) {
self.var_store.reserve(cap);
// 宁浪费不放过
self.var_store.resize(cap, 0 as *mut TrcInt);
self.int_store.resize(cap, 0);
self.float_store.resize(cap, 0.0);
self.str_store.resize(cap, ptr::null_mut());
self.bool_store.resize(cap, false);
self.char_store.resize(cap, '0');
}
}

Expand Down Expand Up @@ -593,8 +601,14 @@ mod tests {
}

#[test]
fn test_vm() {
fn test_stdfunc() {
let mut vm = gen_test_env(r#"print("{},{},{},{}", 1, "h", 'p', true)"#);
vm.run().unwrap()
}

#[test]
fn test_var_define() {
let mut vm = gen_test_env(r#"a:=10"#);
vm.run().unwrap()
}
}

0 comments on commit da8c169

Please sign in to comment.