Skip to content

Instantly share code, notes, and snippets.

@aarroyoc
Created August 27, 2014 10:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aarroyoc/5015aa4ace1309371ce3 to your computer and use it in GitHub Desktop.
Save aarroyoc/5015aa4ace1309371ce3 to your computer and use it in GitHub Desktop.
Crea tu lenguaje de programación en Rust
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' ");
}
}
}
}
}
}
#[deriving(FromPrimitive)]
enum Instruction {
INTEGER = 0x00,
STRING = 0x01,
ADD = 0x02,
SHOWINTEGER = 0x0A,
SHOWVERSION = 0x0E,
EXITVM = 0x0F
}
pub fn interpreter(&mut self,bytecode: &'static str) -> (){
for execbyte in bytecode.chars() {
self.execute(execbyte as u8);
}
}
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