From 35137a03a8a011bc6b7681916e3de05639b32423 Mon Sep 17 00:00:00 2001 From: bnewbold Date: Wed, 20 Apr 2016 00:45:34 -0400 Subject: rust: implement scheme_parse_token() --- minimal.rs | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/minimal.rs b/minimal.rs index 57c8268..c2f3b02 100644 --- a/minimal.rs +++ b/minimal.rs @@ -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, &'static str> { } fn scheme_parse_token(token: &str) -> Result { - // 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::() { + 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> { -- cgit v1.2.3