@@ -5,94 +5,13 @@ extern crate bimap;
55
66//use std::io;
77//use std::mem;
8- use bimap:: BiMap ;
98//use digest::Digest;
109//use sha2::Sha256;
1110//use ripemd160::Ripemd160;
1211
13- struct Compiler {
14- opcode_list : BiMap < & ' static str , i32 > ,
15- opcode_alias_list : BiMap < & ' static str , & ' static str > ,
16- }
17-
18- impl Compiler {
19- fn new ( ) -> Compiler {
20- let mut opcode_list: BiMap < & ' static str , i32 > = BiMap :: new ( ) ;
21- opcode_list. insert ( "OP_0" , 0x00 ) ;
22- opcode_list. insert ( "OP_1NEGATE" , 0x4f ) ;
23- opcode_list. insert ( "OP_1" , 0x51 ) ;
24- opcode_list. insert ( "OP_2" , 0x52 ) ;
25- opcode_list. insert ( "OP_3" , 0x53 ) ;
26- opcode_list. insert ( "OP_4" , 0x54 ) ;
27- opcode_list. insert ( "OP_5" , 0x55 ) ;
28- opcode_list. insert ( "OP_6" , 0x56 ) ;
29- opcode_list. insert ( "OP_7" , 0x57 ) ;
30- opcode_list. insert ( "OP_8" , 0x58 ) ;
31- opcode_list. insert ( "OP_9" , 0x59 ) ;
32- opcode_list. insert ( "OP_10" , 0x5a ) ;
33- opcode_list. insert ( "OP_11" , 0x5b ) ;
34- opcode_list. insert ( "OP_12" , 0x5c ) ;
35- opcode_list. insert ( "OP_13" , 0x5d ) ;
36- opcode_list. insert ( "OP_14" , 0x5e ) ;
37- opcode_list. insert ( "OP_15" , 0x5f ) ;
38- opcode_list. insert ( "OP_16" , 0x60 ) ;
39- opcode_list. insert ( "OP_NOP" , 0x61 ) ;
40- opcode_list. insert ( "OP_DUP" , 0x76 ) ;
41- opcode_list. insert ( "OP_IF" , 0x63 ) ;
42- opcode_list. insert ( "OP_NOTIF" , 0x64 ) ;
43- opcode_list. insert ( "OP_ELSE" , 0x67 ) ;
44- opcode_list. insert ( "OP_ENDIF" , 0x68 ) ;
45- opcode_list. insert ( "OP_HASH160" , 0xa9 ) ;
46-
47- let mut opcode_alias_list: BiMap < & ' static str , & ' static str > = BiMap :: new ( ) ;
48- opcode_alias_list. insert ( "OP_FALSE" , "OP_0" ) ;
49- opcode_alias_list. insert ( "OP_TRUE" , "OP_1" ) ;
50-
51- return Compiler {
52- opcode_list : opcode_list,
53- opcode_alias_list : opcode_alias_list,
54- } ;
55- }
56- fn compile_single ( & self , code : & str ) -> i32 {
57- let alias_opcode: & str = match self . opcode_alias_list . get_by_left ( & code) {
58- Some ( & value) => value,
59- None => code,
60- } ;
61- let hex: i32 = match self . opcode_list . get_by_left ( & alias_opcode) {
62- Some ( & value) => value,
63- None => panic ! ( "[Compiler] opcode not found." ) ,
64- } ;
65- return hex;
66- }
67- fn compile ( & self , codes : Vec < & str > ) -> Vec < i32 > {
68- let mut bytecode: Vec < i32 > = vec ! [ ] ;
69-
70- for code in codes {
71- let hex = self . compile_single ( code) ;
72- bytecode. push ( hex) ;
73- }
12+ mod compiler;
13+ use crate :: compiler:: * ;
7414
75- return bytecode;
76- }
77- fn uncompile_single ( & self , hex : & i32 ) -> & str {
78- let code: & str = match self . opcode_list . get_by_right ( & hex) {
79- Some ( & value) => value,
80- None => panic ! ( "[Compiler] opcode not found." ) ,
81- } ;
82- return code;
83- }
84- fn uncompile ( & self , hexs : & Vec < i32 > ) -> Vec < & str > {
85- let mut codes: Vec < & str > = vec ! [ ] ;
86-
87- for hex in hexs {
88- let code = self . uncompile_single ( & hex) ;
89- codes. push ( code) ;
90- }
91-
92- return codes;
93- }
94-
95- }
9615struct VM < ' borrow_code_lifetime > {
9716 stack : & ' borrow_code_lifetime mut Vec < i32 > ,
9817 codes : & ' borrow_code_lifetime Vec < i32 > ,
@@ -128,7 +47,7 @@ impl<'borrow_code_lifetime> VM<'borrow_code_lifetime> {
12847 break ;
12948 }
13049 }
131- self . dump ( ) ;
50+ // self.dump();
13251 }
13352 fn step ( & mut self ) -> i32 {
13453
@@ -253,5 +172,6 @@ fn main() {
253172 let mut vm = VM :: new ( & bytecode, & mut stack, 0 ) ;
254173 vm. dump ( ) ;
255174 vm. run ( ) ;
175+ vm. dump ( ) ;
256176
257177}
0 commit comments