Skip to content

Commit 5b31fc5

Browse files
committed
Start on bytecode decoding stuff
1 parent 9a7ba3c commit 5b31fc5

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

seax_svm/src/bytecode/mod.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
use std::io::Read;
2+
3+
use super::SVMCell::*;
4+
use super::Inst::*;
5+
use super::Inst;
6+
7+
#[unstable(feature="decode")]
8+
pub struct Decoder<'a, R: 'a> {
9+
source: &'a mut R,
10+
read: usize
11+
}
12+
13+
14+
#[unstable(feature="decode")]
15+
impl<'a, R> Decoder<'a, R> where R: Read {
16+
17+
#[unstable(feature="decode")]
18+
pub fn new(src: &'a mut R) -> Decoder<'a, R>{
19+
Decoder {
20+
source: src,
21+
read: 0
22+
}
23+
}
24+
25+
#[unstable(feature="decode")]
26+
fn decode_inst(byte: &u8) -> Result<Inst, String> {
27+
match *byte {
28+
0xA0u8 => Ok(NIL),
29+
0xA1u8 => Ok(LDC),
30+
0xA2u8 => Ok(LD),
31+
0xA3u8 => Ok(LDF),
32+
0xA4u8 => Ok(AP),
33+
0xA5u8 => Ok(JOIN),
34+
0xA6u8 => Ok(RAP),
35+
0xA7u8 => Ok(RET),
36+
0xA8u8 => Ok(DUM),
37+
0xA9u8 => Ok(SEL),
38+
0xAAu8 => Ok(ADD),
39+
0xABu8 => Ok(SUB),
40+
0xACu8 => Ok(MUL),
41+
0xADu8 => Ok(DIV),
42+
0xAFu8 => Ok(FDIV),
43+
0xB0u8 => Ok(MOD),
44+
0xB1u8 => Ok(EQ),
45+
0xB2u8 => Ok(GT),
46+
0xB3u8 => Ok(GTE),
47+
0xB4u8 => Ok(LT),
48+
0xB5u8 => Ok(LTE),
49+
0xB6u8 => Ok(ATOM),
50+
0xB7u8 => Ok(NULL),
51+
0xB8u8 => Ok(READC),
52+
0xB9u8 => Ok(WRITEC),
53+
0xBAu8 => Ok(APCC),
54+
0xC0u8 => Ok(CONS),
55+
0xCDu8 => Ok(CDR),
56+
0xCAu8 => Ok(CAR),
57+
b if b < 0xA0u8 => Err(String::from("byte too low")),
58+
b if b > 0xB9u8 && b < 0xC0u8 => Err(format!("reserved byte {:?}", b)),
59+
b if b > 0xCAu8 => Err(String::from("byte too high")),
60+
_ => panic!("Got a byte that was weird")
61+
}
62+
}
63+
64+
/// Returns the number of bytes read by the decoder
65+
#[unstable(feature="decode")]
66+
pub fn read(&self) -> usize {
67+
self.read
68+
}
69+
70+
71+
}

seax_svm/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#![feature(staged_api)]
66
#![staged_api]
77

8-
98
#[cfg(test)]
109
extern crate quickcheck;
1110

@@ -29,6 +28,12 @@ pub mod slist;
2928
#[stable(feature="vm_core", since="0.1.2")]
3029
pub mod cell;
3130

31+
/// Bytecode
32+
///
33+
/// Contains code for encoding and decoding Seax bytecode files.
34+
#[unstable(feature="bytecode")]
35+
pub mod bytecode;
36+
3237
#[cfg(test)]
3338
mod tests;
3439

0 commit comments

Comments
 (0)