@@ -72,24 +72,24 @@ impl Compiler {
7272
7373}
7474struct VM < ' borrow_code_lifetime > {
75- stack : Vec < i32 > ,
75+ stack : & ' borrow_code_lifetime mut Vec < i32 > ,
7676 codes : & ' borrow_code_lifetime Vec < i32 > ,
7777 pc : usize ,
7878}
7979
8080impl < ' borrow_code_lifetime > VM < ' borrow_code_lifetime > {
81- fn new ( codes : & ' borrow_code_lifetime Vec < i32 > ) -> VM < ' borrow_code_lifetime > {
81+ fn new ( codes : & ' borrow_code_lifetime Vec < i32 > , stack : & ' borrow_code_lifetime mut Vec < i32 > ) -> VM < ' borrow_code_lifetime > {
8282 VM {
8383 codes : codes,
8484 pc : 0 ,
85- stack : vec ! [ ]
85+ stack : stack
8686 }
8787 }
8888 fn dump ( & self ) {
8989 println ! ( "pc:{}" , self . pc) ;
9090 print ! ( "stack: [" ) ;
91- for value in & self . stack {
92- print ! ( "{:#x}, " , value) ;
91+ for value in & mut self . stack . iter ( ) {
92+ print ! ( "{:#x}, " , value) ;
9393 }
9494 println ! ( "]" ) ;
9595
@@ -134,26 +134,54 @@ impl<'borrow_code_lifetime> VM<'borrow_code_lifetime> {
134134 }
135135 fn op_if ( & mut self ) {
136136 //let = self.codes[self.pc - 2];
137+ let mut if_code: Vec < i32 > = vec ! [ ] ;
138+ let mut else_code: Vec < i32 > = vec ! [ ] ;
137139
138140 let after_if = self . codes . split_at ( self . pc + 1 ) . 1 ;
139- let codes_in_if_to_endif = after_if. rsplitn ( 2 , |code| * code == Compiler :: new ( ) . compile_single ( "OP_ENDIF" ) ) ;
141+ let codes_in_if_to_endif = after_if. rsplitn ( 2 , |code| * code == Compiler :: new ( ) . compile_single ( "OP_ENDIF" ) ) . last ( ) . unwrap ( ) ;
140142
141143 {
142144 let mut count_op_if = 0 ;
143145 let mut count_op_endif = 0 ;
144- for code in codes_in_if_to_endif {
145- if code == Compiler :: new ( ) . compile_single ( "OP_IF" ) {
146+ let mut codes_in_if_to_endif = codes_in_if_to_endif. iter ( ) ;
147+ loop {
148+ let code = codes_in_if_to_endif. next ( ) . unwrap ( ) ;
149+ if * code == Compiler :: new ( ) . compile_single ( "OP_IF" ) {
146150 count_op_if+=1 ;
147- } else if code == Compiler :: new ( ) . compile_single ( "OP_ENDIF" ) {
151+ } else if * code == Compiler :: new ( ) . compile_single ( "OP_ENDIF" ) {
148152 count_op_endif+=1 ;
153+ } else if * code == Compiler :: new ( ) . compile_single ( "OP_ELSE" ) {
154+ if count_op_if == count_op_endif {
155+ break ;
156+ }
149157 }
158+ if_code. push ( * code) ;
159+ }
160+ for code in codes_in_if_to_endif {
161+ else_code. push ( * code) ;
150162 }
151163 }
152164
153- //codes_in_if_to_endif.last().unwrap()
165+ print ! ( "if_code: [" ) ;
166+ for code in & if_code {
167+ print ! ( "{} " , Compiler :: new( ) . uncompile_single( & code) ) ;
168+ }
169+ println ! ( "]" ) ;
170+ print ! ( "else_code: [" ) ;
171+ for code in & else_code {
172+ print ! ( "{} " , Compiler :: new( ) . uncompile_single( & code) ) ;
173+ }
174+ println ! ( "]" ) ;
154175
155- //let if_vm = VM::new(bytecode);
156- //let else_vm = VM::new(bytecode);
176+ {
177+ let mut if_vm = VM :: new ( & if_code, self . stack ) ;
178+ if_vm. run ( ) ;
179+ }
180+
181+ {
182+ let mut else_vm = VM :: new ( & else_code, self . stack ) ;
183+ else_vm. run ( ) ;
184+ }
157185 self . pc += 1 ;
158186
159187 panic ! ( "debug" ) ;
@@ -163,11 +191,12 @@ impl<'borrow_code_lifetime> VM<'borrow_code_lifetime> {
163191fn main ( ) {
164192
165193 let compiler = Compiler :: new ( ) ;
166- let bytecode = compiler. compile ( vec ! [ "OP_IF" , "OP_1" , "OP_IF" , "OP_2" , " OP_ENDIF", "OP_3" , "OP_ENDIF" , "OP_1" ] ) ;
194+ 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" ] ) ;
167195
168- let mut vm = VM :: new ( & bytecode) ;
196+ let mut stack: Vec < i32 > = vec ! [ ] ;
197+ let mut vm = VM :: new ( & bytecode, & mut stack) ;
169198 vm. dump ( ) ;
170199 vm. run ( ) ;
171200
172- vm. dump ( ) ;
201+ // vm.dump();
173202}
0 commit comments