Skip to content
This repository was archived by the owner on Jan 21, 2023. It is now read-only.

Commit 479ae27

Browse files
committed
if implement more smart
1 parent cda291b commit 479ae27

File tree

1 file changed

+39
-56
lines changed

1 file changed

+39
-56
lines changed

src/main.rs

Lines changed: 39 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ struct VM<'borrow_code_lifetime> {
9191
}
9292

9393
impl<'borrow_code_lifetime> VM<'borrow_code_lifetime> {
94-
fn new(codes: &'borrow_code_lifetime Vec<i32>, stack: &'borrow_code_lifetime mut Vec<i32>) -> VM<'borrow_code_lifetime>{
94+
fn new(codes: &'borrow_code_lifetime Vec<i32>, stack: &'borrow_code_lifetime mut Vec<i32>, pc: usize) -> VM<'borrow_code_lifetime>{
9595
VM {
9696
codes: codes,
97-
pc: 0,
98-
stack: stack
97+
stack: stack,
98+
pc: pc,
9999
}
100100
}
101101
fn dump(&self) {
@@ -114,18 +114,25 @@ impl<'borrow_code_lifetime> VM<'borrow_code_lifetime> {
114114
}
115115
fn run(&mut self) {
116116
while self.codes.len() > self.pc {
117-
self.step();
117+
let halt = self.step();
118+
if halt == 1 {
119+
break;
120+
}
118121
}
119122
}
120-
fn step(&mut self) {
123+
fn step(&mut self) -> i32{
121124
let compiler = Compiler::new();
122-
if self.codes[self.pc] == compiler.compile_single("OP_0") { self.op_pushnumber(0); }
123-
else if self.codes[self.pc] == compiler.compile_single("OP_1") { self.op_pushnumber(1); }
124-
else if self.codes[self.pc] == compiler.compile_single("OP_2") { self.op_pushnumber(2); }
125-
else if self.codes[self.pc] == compiler.compile_single("OP_NOP") { self.op_nop(); }
126-
else if self.codes[self.pc] == compiler.compile_single("OP_DUP") { self.op_dup(); }
127-
else if self.codes[self.pc] == compiler.compile_single("OP_IF") { self.op_if(); }
125+
if self.codes[self.pc] == compiler.compile_single("OP_0") { self.op_pushnumber(0); }
126+
else if self.codes[self.pc] == compiler.compile_single("OP_1") { self.op_pushnumber(1); }
127+
else if self.codes[self.pc] == compiler.compile_single("OP_2") { self.op_pushnumber(2); }
128+
else if self.codes[self.pc] == compiler.compile_single("OP_NOP") { self.op_nop(); }
129+
else if self.codes[self.pc] == compiler.compile_single("OP_DUP") { self.op_dup(); }
130+
else if self.codes[self.pc] == compiler.compile_single("OP_IF") { self.op_if(); }
131+
else if self.codes[self.pc] == compiler.compile_single("OP_ENDIF"){ return 1; }
132+
else if self.codes[self.pc] == compiler.compile_single("OP_ELSE"){ return 1; }
128133
else { panic!("[VM] The opcode is not implemented yet,"); }
134+
135+
return 0;
129136
}
130137
fn op_pushnumber(&mut self, num: i32){
131138
self.stack.push(num);
@@ -146,57 +153,33 @@ impl<'borrow_code_lifetime> VM<'borrow_code_lifetime> {
146153
self.pc += 1;
147154
}
148155
fn op_if(&mut self){
149-
//let = self.codes[self.pc - 2];
150-
let mut if_code: Vec<i32> = vec![];
151-
let mut else_code: Vec<i32> = vec![];
152156

153-
// TODO: replace rspritn to count loop like OP_ELSE
154-
let after_if = self.codes.split_at(self.pc + 1).1;
155-
let codes_in_if_to_endif = after_if.rsplitn(2, |code| *code == Compiler::new().compile_single("OP_ENDIF")).last().unwrap();
156-
157-
{
158-
let mut count_op_if = 0;
159-
let mut count_op_endif = 0;
160-
let mut codes_in_if_to_endif = codes_in_if_to_endif.iter();
161-
loop {
162-
let code = codes_in_if_to_endif.next().unwrap();
163-
if *code == Compiler::new().compile_single("OP_IF") {
164-
count_op_if+=1;
165-
} else if *code == Compiler::new().compile_single("OP_ENDIF") {
166-
count_op_endif+=1;
167-
} else if *code == Compiler::new().compile_single("OP_ELSE") {
168-
if count_op_if == count_op_endif {
169-
break;
170-
}
171-
}
172-
if_code.push(*code);
173-
}
174-
for code in codes_in_if_to_endif {
175-
else_code.push(*code);
157+
self.pc += 1;
158+
159+
let bool = self.stack.pop();
160+
if bool.unwrap() == 0 { // run from OP_ELSE to OP_END
161+
162+
let mut if_vm = VM::new(self.codes, self.stack, self.pc);
163+
if_vm.run();
164+
165+
if self.codes[self.pc] == Compiler::new().compile_single("OP_ELSE") {
166+
if_vm.run_nothing(); // skip from OP_ELSE to OP_ENDIF
176167
}
177-
}
178168

179-
print!("if_code: [");
180-
for code in &if_code {
181-
print!("{} ", Compiler::new().uncompile_single(&code));
182-
}
183-
println!("]");
184-
print!("else_code: [");
185-
for code in &else_code {
186-
print!("{} ", Compiler::new().uncompile_single(&code));
187-
}
188-
println!("]");
169+
} else { // run from OP_IF to OP_ELSE
189170

190-
{
191-
let mut if_vm = VM::new(&if_code, self.stack);
171+
let mut if_vm = VM::new(self.codes, self.stack, self.pc);
192172
if_vm.run();
193-
}
194173

195-
{
196-
let mut else_vm = VM::new(&else_code, self.stack);
197-
else_vm.run();
174+
if self.codes[self.pc] == Compiler::new().compile_single("OP_ELSE") {
175+
let mut if_vm = VM::new(self.codes, self.stack, self.pc);
176+
if_vm.run_nothing(); // skip from OP_IF to OP_ELSE
177+
}
198178
}
199-
self.pc += 1;
179+
180+
//let = self.codes[self.pc - 2];
181+
let mut if_code: Vec<i32> = vec![];
182+
let mut else_code: Vec<i32> = vec![];
200183

201184
panic!("debug");
202185
}
@@ -208,7 +191,7 @@ fn main() {
208191
let bytecode = compiler.compile(vec!["OP_IF", "OP_1", "OP_IF","OP_2", "OP_ELSE", "OP_1", "OP_ENDIF", "OP_ELSE", "OP_3", "OP_ENDIF", "OP_1"]);
209192

210193
let mut stack: Vec<i32> = vec![];
211-
let mut vm = VM::new(&bytecode, &mut stack);
194+
let mut vm = VM::new(&bytecode, &mut stack,0);
212195
vm.dump();
213196
vm.run();
214197

0 commit comments

Comments
 (0)