-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1byte_format.rs
290 lines (265 loc) · 8.13 KB
/
1byte_format.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
use std::mem::size_of;
use std::mem::size_of_val;
use std::collections::HashMap;
use Expression::{
// 0-1-2
One, Zero, Two,
// 10 args
A0,A1,A2,//A3,A4,A5,A6,A7,A8,A9,
U0, U3, U9, U15,
// 100 registers
// U0,U1,U2,U3,U4,U5,U6,U7,U8,U9,
// U10,U11,U12,U13,U14,U15,U16,U17,U18,U19,
// U20,U21,U22,U23,U24,U25,U26,U27,U28,U29,
// U30,U31,U32,U33,U34,U35,U36,U37,U38,U39,
// U40,U41,U42,U43,U44,U45,U46,U47,U48,U49,
// U50,U51,U52,U53,U54,U55,U56,U57,U58,U59,
// U60,U61,U62,U63,U64,U65,U66,U67,U68,U69,
// U70,U71,U72,U73,U74,U75,U76,U77,U78,U79,
// U80,U81,U82,U83,U84,U85,U86,U87,U88,U89,
// U90,U91,U92,U93,U94,U95,U96,U97,U98,U99,
// instructions
Function, Call, Rec, Return,
// branch
JNE,
// types
// Integer, String, Double,
// Operators
Add, Sub, //Mul, Div,
};
// enum Test { Add, Sub, Div, Mul, Number, Var,
// JNE,
// Function, Call,
// Rec, Return
// }
#[derive(Clone,Debug)]
enum Expression {
// 0-1-2
One, Zero, Two,
// 10 args mA
A0,A1,A2,//A3,A4,A5,A6,A7,A8,A9,
U0, U3, U9, U15,
// instructions
Function, Call, Rec, Return,
// branch
JNE,
// types
// Integer, String, Double,
// Operators
Add, Sub, //Mul, Div,
}
// const MAX_INSTRUCTIONS:i32 = 40000;
fn eval(
bytecode : &Vec<Expression>,
stack: &mut Vec<i128>)-> i128 {
let mut function_call = 0;
let mut instruction_count = 0;
let blength:i128 = bytecode.len() as i128;
let mut pointer:i128 = 0;
let mut rstack: Vec<i128> = Vec::new();
let mut vmap: HashMap<i128,i128> = HashMap::new();
let mut fmap: HashMap<i128, i128> = HashMap::new();
let mut vbackup: Vec<HashMap<i128,i128>> = Vec::new();
// println!("{} - block#{} [eval] ==== START ====", &instruction_count, &pointer);
loop {
if pointer < blength && pointer != -1 {
// if instruction_count > MAX_INSTRUCTIONS {return -1}
// println!("{} - block#{} stacks[{}]: ", &instruction_count, &pointer, &stack.len());
execute(
&mut vmap, &mut fmap, &bytecode[pointer as usize],
stack, &mut rstack, &mut vbackup, &mut pointer,
&mut function_call);
pointer += 1;
instruction_count += 1;
}
else if stack.len() > 0 {
// println!("function calls/instructions: {}/{}",
// &function_call,&instruction_count);
let result = stack.pop().unwrap();
return result;
} else {
return 0
}
}
}
/// execute expression by vmap && fmap
fn execute(
vmap: &mut HashMap<i128, i128>,
fmap: &mut HashMap<i128, i128>,
exp: &Expression,
stack: &mut Vec<i128>,
rstack: &mut Vec<i128>,
vbackup: &mut Vec<HashMap<i128, i128>>,
pointer: &mut i128,
function_call: &mut i128) {
match exp {
Two => stack.push(2),
One => stack.push(1),
Zero => stack.push(0),
A0 => stack.push(*vmap.get(&0).unwrap()),
A1 => stack.push(*vmap.get(&1).unwrap()),
A2 => stack.push(*vmap.get(&2).unwrap()),
U0 => stack.push(0),
U3 => stack.push(3),
U9 => stack.push(8),
U15 => stack.push(14),
Sub => {
let x = stack.pop().unwrap();
let y = stack.pop().unwrap();
// println!("[sub] {} - {}", &x, &y);
stack.push(x-y);
},
Add => {
let x = stack.pop().unwrap();
let y = stack.pop().unwrap();
// println!("[add] {} + {}", &x, &y);
stack.push(x+y);
},
JNE => {
// print!("\n[not_equal] ");
let label = stack.pop().unwrap();
let x = stack.pop().unwrap();
let y = stack.pop().unwrap();
if x != y {
*pointer = label;
}
// print!("{} != {} > jump #{}\n", &x, &y, &label);
},
Return => {
// Restore vmap
if vbackup.len() > 0 {
*vmap = vbackup.pop().unwrap();
//
// Restore pointer to where it was called
let callback = rstack.pop().unwrap();
// println!("[return] @{} callback > #{}", &pointer, callback);
*pointer = callback;
} else {
// println!("[return]\n");
}
},
Function => {
// print!("[func]");
//
let fid = stack.pop().unwrap();
// Cache into fmap if new:
if !fmap.contains_key(&fid) {
let ptr = pointer.clone() + 1;
fmap.insert(fid, ptr);
// print!(" new#{:#02x}@ptr={} ", &fid, &ptr);
}
// print!("(");
// Restore args if vmap exist.
if vmap.len() > 0 {
vbackup.push(vmap.clone());
}
// args amount
let mut index = 0;
let n = stack.pop().unwrap();
loop {
if index < n {
let v = stack.pop().unwrap();
// print!(" {} ", &v);
vmap.insert(index, v);
index += 1;
} else {
break;
}
}
// counting loop
*function_call += 1;
// print!(")");
// println!();
},
// [rec]: will be faster than call for ignoring function lookup.
Rec => {
// println!("[rec]\n");
rstack.push(*pointer);
*pointer = -1;
},
Call => {
let id = stack.pop().unwrap();
// println!("[call] f#{} @{}\n", &id, &pointer);
let f = &*fmap.get(&id).unwrap();
// .expect(&format!("function not found: {}", id));
// save pointer, so we can return from a call.
rstack.push(*pointer);
*pointer = f-1;
}
}
}
fn main() {
//
// Version fib (n)
//
// let bytecode = vec![
// // fib (n, a, b)
// // 3 args, 0x0 index, function()
// One, U0, Function, // 0
// // n != 0 ?
// Zero, // 1
// A0, // 2
// U9,JNE, // 3
// Zero, // 4
// Return, // 5
// // n != 1 ?
// One, // 6
// A0, // 7
// U15,JNE, // 8
// One, // 9
// Return, // 10
// // f1 = f(n - 1)
// One, // 11
// A0, // 12
// Sub, // 13
// Rec,
// // f2 = (n - 2)
// Two, // 11
// A0, // 12
// Sub, // 13
// Rec, //
// // f1 + f2
// Add,
// Return,
// ];
//
// Version fib (n,a,b)
//
let bytecode = vec![
// fib (n, a, b)
// 3 args, 0x0 index, function()
U3, U0, Function, // 0
// n != 0 ?
Zero, // 1
A0, // 2
U9,JNE, // 3
A1, // 4
Return, // 5
// n != 1 ?
One, // 6
A0, // 7
U15,JNE, // 8
A2, // 9
Return, // 10
// f1 = (a + b)
A1, // 11
A2, // 12
Add, // 13
// f2 = b
A2, // 14
// f3 = n - 1
One, //
A0, //
Sub, //
// f(f3. f2. f1)
Rec,
// U0, Call, //
];
// println!("Expression enum = {:?} bytes", size_of::<Expression>());
// println!("Total bytecode = {} items / {} bytes", &bytecode.len() ,size_of_val(&bytecode));
let mut new_stack:Vec<i128> = vec![1, 0, 100]; // b a n f => f( n a b)
// let mut new_stack:Vec<i128> = vec![30]; // b a n f => f( n a b)
// let result =
eval(&bytecode, &mut new_stack);
// println!("result = {}", &result);
}