Skip to content

Instantly share code, notes, and snippets.

@mfekadu
Last active March 6, 2019 03:43
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 mfekadu/caaaad3fa2232a8eb24e4a6af28c8411 to your computer and use it in GitHub Desktop.
Save mfekadu/caaaad3fa2232a8eb24e4a6af28c8411 to your computer and use it in GitHub Desktop.
ZHRL programming language implemented in Rust for fun (also Elixir implementation here ... https://github.com/mfekadu/ZHLR3-elixir)
// https://gist.github.com/cogas/9e600cc0546b038b8efbdce6ff0f8d4d
// ... BTree in rust (recursive enum)
// Language struct definitions
// v means "value"
// #[derive(Debug)] // Rust told me to put this here
#[derive(Debug, Clone)]
enum ExprC {
IdC { v: &'static str },
NumC { v: usize },
StringC { v: &'static str },
// Rust is weird about "recursive enum"
// https://www.reddit.com/r/rust/comments/5bo09v/recursive_enums/
IfC { tst: Box<ExprC>, thn: Box<ExprC>, els: Box<ExprC> },
// LamC { v: usize },
// AppC { v: usize }
}
// Value struct definitions
// struct NumV { v: usize }
// struct BoolV { v: bool }
// struct StringV { v: &'static str }
// struct CloV { params: usize }
// struct PrimV { v: usize }
fn main() {
let foo: &'static str = "32";
let i = ExprC::IdC{v: foo};
let n = ExprC::NumC{v: 32};
let s = ExprC::StringC{v: foo};
let num_box = Box::new(ExprC::NumC{v: 32});
// boxes must be cloned for some reason ...
let c = ExprC::IfC{tst: num_box.clone(), thn: num_box.clone(), els: num_box.clone()};
fn interp(expr: ExprC) /* -> usize */ {
match expr {
ExprC::NumC{v} => { println!("interp match NumC with v = {}", v); }
ExprC::IdC{v} => { println!("interp match IdC with v = {}", v); }
ExprC::StringC{v} => { println!("interp match StringC with v = {}", v); }
// ExprC::LamC{} => { println!("match LamC"); }
ExprC::IfC{tst, thn, els} => { println!("interp match IfC with tst | thn | els = {:?} | {:?} | {:?}", tst,thn,els); }
// ExprC::AppC{} => { println!("match AppC"); }
_ => { println!("match the rest"); }
}
/* 33 */
}
println!("Hello World!"); println!();
println!("NumC = {:?}", n);
interp(n); println!();
println!("IdC = {:?}", i);
interp(i); println!();
println!("StringC = {:?}", s);
interp(s); println!();
println!("IfC = {:?}", c);
interp(c); println!();
// println!("IfC.tst = {:?}", c.tst);
// println!("IfC.thn = {:?}", c.thn);
// println!("IfC.els = {:?}", c.els);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment