From ea0ec7997da91f76066eb1cd3727040d0768668a Mon Sep 17 00:00:00 2001 From: bnewbold Date: Tue, 19 Apr 2016 00:03:10 -0400 Subject: rust: start work on minimal.rs --- .gitignore | 1 + minimal.rs | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 minimal.rs diff --git a/.gitignore b/.gitignore index 893ac99..492ef67 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,7 @@ build/ _build/ src/build/ *.log +minimal-rs # Don't ignore this file itself !.gitignore diff --git a/minimal.rs b/minimal.rs new file mode 100644 index 0000000..1db7405 --- /dev/null +++ b/minimal.rs @@ -0,0 +1,87 @@ + +// A partial Scheme implementation in Rust +// Build with: rustc minimal.rs -o minimal-rust + +use std::io; +use std::io::Write; + +// There doesn't seem to be a symbole or quote type in Rust, so i'm going to use strings and +// vectors + +// "list of tokens" implemented as a vector of String. +// AST implemented as nested tuples (immutable). + +//let const SCHEME_BUILTINS = ("lambda", "quote", "cond", "else", "cons", "car", "cdr", "null?", "eq?", "atom?", "zero?", "number?", "+", "-", "*", "/"); + +#[allow(dead_code)] +enum SchemeAtom<'a> { + SchemeNull, + SchemeTrue, + SchemeFalse, + SchemeNum(f64), + SchemeBuiltin(&'a str), + SchemeStr(&'a str), +} + +enum SchemeExpr<'a> { + SchemeAtom(SchemeAtom<'a>), + SchemeList(Vec>), +} + +/////////////////////////////////// + +fn is_zero(n: f64) -> bool { + return n == 0.; +} + +/////////////////////////////////// + +fn scheme_tokenize(s: &String) -> Result, &'static str> { + let ret = vec![s]; + return Ok(ret); +} + +fn scheme_parse_num(s: &String) -> Result { + let num = 0.; + return Ok(num); +} + +fn scheme_parse_sexpr(sexpr: Vec<&String>) -> Result { + let ret = sexpr.into_iter().map(|el| SchemeExpr::SchemeAtom(SchemeAtom::SchemeStr(el))).collect(); + return Ok(SchemeExpr::SchemeList(ret)) +} + +fn scheme_parse(tokens: SchemeExpr) -> Result { + return Ok(SchemeExpr::SchemeAtom(SchemeAtom::SchemeNull)); +} + +fn scheme_eval(ast: SchemeExpr) -> Result { + return Ok(SchemeExpr::SchemeAtom(SchemeAtom::SchemeNull)); +} + +fn scheme_repr(ast: SchemeExpr) -> Result { + return Ok("It Worked!".to_string()); +} + +fn main() { + + let stdin = io::stdin(); + let mut stdout = io::stdout(); + let raw_input = &mut String::new(); + + loop { + stdout.write(b"\nminimal-rust> ").unwrap(); + stdout.flush().unwrap(); + raw_input.clear(); + stdin.read_line(raw_input).unwrap(); + if raw_input.len() == 0 { + stdout.write(b"\nCiao!\n").unwrap(); + return; + } + let tokens = scheme_tokenize(raw_input).unwrap(); + let sexpr = scheme_parse_sexpr(tokens).unwrap(); + let ast = scheme_parse(sexpr).unwrap(); + let resp = scheme_eval(ast).unwrap(); + println!("{}", scheme_repr(resp).unwrap()); + } +} -- cgit v1.2.3