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

Commit d1d0cc9

Browse files
committed
splut compiler.rs
1 parent 9b08d1f commit d1d0cc9

File tree

2 files changed

+89
-84
lines changed

2 files changed

+89
-84
lines changed

src/compiler.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
use bimap::BiMap;
2+
3+
pub struct Compiler {
4+
opcode_list: BiMap<&'static str, i32>,
5+
opcode_alias_list: BiMap<&'static str, &'static str>,
6+
}
7+
8+
impl Compiler {
9+
pub fn new() -> Compiler {
10+
let mut opcode_list: BiMap<&'static str, i32> = BiMap::new();
11+
opcode_list.insert("OP_0", 0x00);
12+
opcode_list.insert("OP_1NEGATE", 0x4f);
13+
opcode_list.insert("OP_1", 0x51);
14+
opcode_list.insert("OP_2", 0x52);
15+
opcode_list.insert("OP_3", 0x53);
16+
opcode_list.insert("OP_4", 0x54);
17+
opcode_list.insert("OP_5", 0x55);
18+
opcode_list.insert("OP_6", 0x56);
19+
opcode_list.insert("OP_7", 0x57);
20+
opcode_list.insert("OP_8", 0x58);
21+
opcode_list.insert("OP_9", 0x59);
22+
opcode_list.insert("OP_10", 0x5a);
23+
opcode_list.insert("OP_11", 0x5b);
24+
opcode_list.insert("OP_12", 0x5c);
25+
opcode_list.insert("OP_13", 0x5d);
26+
opcode_list.insert("OP_14", 0x5e);
27+
opcode_list.insert("OP_15", 0x5f);
28+
opcode_list.insert("OP_16", 0x60);
29+
opcode_list.insert("OP_NOP", 0x61);
30+
opcode_list.insert("OP_DUP", 0x76);
31+
opcode_list.insert("OP_IF", 0x63);
32+
opcode_list.insert("OP_NOTIF", 0x64);
33+
opcode_list.insert("OP_ELSE", 0x67);
34+
opcode_list.insert("OP_ENDIF", 0x68);
35+
opcode_list.insert("OP_HASH160", 0xa9);
36+
37+
let mut opcode_alias_list: BiMap<&'static str, &'static str> = BiMap::new();
38+
opcode_alias_list.insert("OP_FALSE", "OP_0");
39+
opcode_alias_list.insert("OP_TRUE", "OP_1");
40+
41+
return Compiler {
42+
opcode_list: opcode_list,
43+
opcode_alias_list: opcode_alias_list,
44+
};
45+
}
46+
pub fn compile_single(&self, code: &str) -> i32 {
47+
let alias_opcode: &str = match self.opcode_alias_list.get_by_left(&code) {
48+
Some(&value) => value,
49+
None => code,
50+
};
51+
let hex: i32 = match self.opcode_list.get_by_left(&alias_opcode) {
52+
Some(&value) => value,
53+
None => panic!("[Compiler] opcode not found."),
54+
};
55+
return hex;
56+
}
57+
pub fn compile(&self, codes: Vec<&str>) -> Vec<i32> {
58+
let mut bytecode: Vec<i32> = vec![];
59+
60+
for code in codes {
61+
let hex = self.compile_single(code);
62+
bytecode.push(hex);
63+
}
64+
65+
return bytecode;
66+
}
67+
pub fn uncompile_single(&self, hex: &i32) -> &str {
68+
let code: &str = match self.opcode_list.get_by_right(&hex) {
69+
Some(&value) => value,
70+
None => panic!("[Compiler] opcode not found."),
71+
};
72+
return code;
73+
}
74+
pub fn uncompile(&self, hexs: &Vec<i32>) -> Vec<&str> {
75+
let mut codes: Vec<&str> = vec![];
76+
77+
for hex in hexs {
78+
let code = self.uncompile_single(&hex);
79+
codes.push(code);
80+
}
81+
82+
return codes;
83+
}
84+
85+
}

src/main.rs

Lines changed: 4 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
}
9615
struct 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

Comments
 (0)