Created
August 27, 2014 10:01
-
-
Save aarroyoc/5015aa4ace1309371ce3 to your computer and use it in GitHub Desktop.
Crea tu lenguaje de programación en Rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn execute(&mut self, execbyte: u8) -> () { | |
if self.push { | |
self.push(execbyte); | |
self.push=false; | |
}else{ | |
let op: Option<Instruction> = FromPrimitive::from_u8(execbyte); | |
match op{ | |
None => { | |
println!("Unknown instruction, skipping..."); | |
}, | |
Some(bc) => { | |
match bc{ | |
INTEGER => { | |
self.push=true; | |
}, | |
ADD => { | |
let a=self.pop() as int; | |
let b=self.pop() as int; | |
let c=a+b; | |
self.push(c as u8); | |
}, | |
SHOWINTEGER => { | |
println!("Integer value {}",self.pop() as int); | |
}, | |
SHOWVERSION => { | |
println!("PerinVM v0.1.0"); | |
}, | |
EXITVM => { | |
println!("Exit VM"); | |
}, | |
STRING => { | |
println!("Unsupported instruction 'STRING' "); | |
} | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[deriving(FromPrimitive)] | |
enum Instruction { | |
INTEGER = 0x00, | |
STRING = 0x01, | |
ADD = 0x02, | |
SHOWINTEGER = 0x0A, | |
SHOWVERSION = 0x0E, | |
EXITVM = 0x0F | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub fn interpreter(&mut self,bytecode: &'static str) -> (){ | |
for execbyte in bytecode.chars() { | |
self.execute(execbyte as u8); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn push(&mut self, value: u8) -> (){ | |
self.stack.push(value); | |
} | |
fn pop(&mut self) -> u8{ | |
let a: Option<u8>=self.stack.pop(); | |
match a{ | |
None => { | |
println!("Failed to pop"); | |
0 | |
}, | |
Some(result) => { | |
result | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment