diff options
author | bnewbold <bnewbold@robocracy.org> | 2016-04-20 00:45:34 -0400 |
---|---|---|
committer | bnewbold <bnewbold@robocracy.org> | 2016-04-20 00:45:34 -0400 |
commit | 35137a03a8a011bc6b7681916e3de05639b32423 (patch) | |
tree | 67f950e2a970dc516cce08cbdb39b265e494ce3c | |
parent | 495f307ad1b4c467e7b8f67da13932a09a0f2479 (diff) | |
download | spectrum-35137a03a8a011bc6b7681916e3de05639b32423.tar.gz spectrum-35137a03a8a011bc6b7681916e3de05639b32423.zip |
rust: implement scheme_parse_token()
-rw-r--r-- | minimal.rs | 30 |
1 files changed, 27 insertions, 3 deletions
@@ -11,7 +11,8 @@ use std::io::Write; // "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?", "+", "-", "*", "/"); +const SCHEME_BUILTINS: [&'static str; 16] = ["lambda", "quote", "cond", "else", "cons", "car", "cdr", + "null?", "eq?", "atom?", "zero?", "number?", "+", "-", "*", "/"]; #[allow(dead_code)] enum SchemeExpr<'a> { @@ -81,8 +82,31 @@ fn scheme_tokenize<'a>(raw_str: &'a str) -> Result<Vec<&'a str>, &'static str> { } fn scheme_parse_token(token: &str) -> Result<SchemeExpr, &'static str> { - // XXX: implement me - return Ok(SchemeExpr::SchemeNull); + + // First match on easy stuff + match token { + "#t" => return Ok(SchemeExpr::SchemeTrue), + "#f" => return Ok(SchemeExpr::SchemeFalse), + _ => () + } + + // Is it a builtin? + if SCHEME_BUILTINS.contains(&token) { + return Ok(SchemeExpr::SchemeBuiltin(token)); + } + + // Try to parse as a number + match token.parse::<f64>() { + Ok(x) => return Ok(SchemeExpr::SchemeNum(x)), + Err(_) => () + } + + // Is it a string? + if token.starts_with("\"") && token.ends_with("\"") { + return Ok(SchemeExpr::SchemeStr(token)); + } + + return Err("unparsable token"); } fn scheme_parse<'a>(tokens: &Vec<&'a str>, depth: u32) -> Result<(SchemeExpr<'a>, usize), &'static str> { |