@@ -52,49 +52,49 @@ impl Compiler {
5252
5353 return bytecode;
5454 }
55- fn uncompile_single ( & self , hex : i32 ) -> & str {
55+ fn uncompile_single ( & self , hex : & i32 ) -> & str {
5656 let code: & str = match self . opcode_list . get_by_right ( & hex) {
5757 Some ( & value) => value,
5858 None => panic ! ( "[Compiler] opcode not found." ) ,
5959 } ;
6060 return code;
6161 }
62- fn uncompile ( & self , hexs : Vec < i32 > ) -> Vec < & str > {
62+ fn uncompile ( & self , hexs : & Vec < i32 > ) -> Vec < & str > {
6363 let mut codes: Vec < & str > = vec ! [ ] ;
6464
6565 for hex in hexs {
66- let code = self . uncompile_single ( hex) ;
66+ let code = self . uncompile_single ( & hex) ;
6767 codes. push ( code) ;
6868 }
6969
7070 return codes;
7171 }
7272
7373}
74- struct VM {
74+ struct VM < ' borrow_code_lifetime > {
7575 stack : Vec < i32 > ,
76- codes : Vec < i32 > ,
76+ codes : & ' borrow_code_lifetime Vec < i32 > ,
7777 pc : usize ,
7878}
7979
80- impl VM {
81- fn new ( codes : Vec < i32 > ) -> VM {
80+ impl < ' borrow_code_lifetime > VM < ' borrow_code_lifetime > {
81+ fn new ( codes : & ' borrow_code_lifetime Vec < i32 > ) -> VM < ' borrow_code_lifetime > {
8282 VM {
8383 codes : codes,
8484 pc : 0 ,
8585 stack : vec ! [ ]
8686 }
8787 }
88- fn dump ( self ) {
88+ fn dump ( & self ) {
8989 println ! ( "pc:{}" , self . pc) ;
9090 print ! ( "stack: [" ) ;
91- for value in self . stack {
91+ for value in & self . stack {
9292 print ! ( "{:#x}, " , value) ;
9393 }
9494 println ! ( "]" ) ;
9595
9696 print ! ( "codes: [" ) ;
97- for code in Compiler :: new ( ) . uncompile ( self . codes ) {
97+ for code in Compiler :: new ( ) . uncompile ( & self . codes ) {
9898 print ! ( "{}, " , code) ;
9999 }
100100 println ! ( "]" ) ;
@@ -133,32 +133,32 @@ impl VM {
133133 self . pc += 1 ;
134134 }
135135 fn op_if ( & mut self ) {
136- let expression = self . codes [ self . pc - 1 ] ;
136+ // let = self.codes[self.pc - 2 ];
137137
138138 let after_if = self . codes . split_at ( self . pc + 1 ) . 1 ;
139- let if_to_endif = after_if. split ( |code| * code == Compiler :: new ( ) . compile_single ( "OP_ENDIF" ) ) . next ( ) . unwrap ( ) ;
139+ let if_to_endif = after_if. rsplitn ( 2 , |code| * code == Compiler :: new ( ) . compile_single ( "OP_ENDIF" ) ) ;
140140
141- for code in after_if {
142- print ! ( "{:#x} " , code) ;
143- }
144141 println ! ( "" ) ;
145- for code in if_to_endif {
146- print ! ( "{:#x } " , code) ;
142+ for code in if_to_endif. last ( ) . unwrap ( ) {
143+ print ! ( "{} " , Compiler :: new ( ) . uncompile_single ( & code) ) ;
147144 }
148145 println ! ( "" ) ;
146+
149147 //let true_vm = VM::new(bytecode);
150148 //let false_vm = VM::new(bytecode);
151149 self . pc += 1 ;
150+
152151 panic ! ( "debug" ) ;
153152 }
154153}
155154
156155fn main ( ) {
157156
158157 let compiler = Compiler :: new ( ) ;
159- let bytecode = compiler. compile ( vec ! [ "OP_1 " , "OP_2 " , "OP_DUP" , "OP_IF" , "OP_1 ", "OP_2 " , "OP_ENDIF" , "OP_1" ] ) ;
158+ let bytecode = compiler. compile ( vec ! [ "OP_IF " , "OP_1 " , "OP_IF" , "OP_2" , "OP_ENDIF ", "OP_3 " , "OP_ENDIF" , "OP_1" ] ) ;
160159
161- let mut vm = VM :: new ( bytecode) ;
160+ let mut vm = VM :: new ( & bytecode) ;
161+ vm. dump ( ) ;
162162 vm. run ( ) ;
163163
164164 vm. dump ( ) ;
0 commit comments