aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbnewbold <bnewbold@robocracy.org>2016-04-20 00:45:34 -0400
committerbnewbold <bnewbold@robocracy.org>2016-04-20 00:45:34 -0400
commit35137a03a8a011bc6b7681916e3de05639b32423 (patch)
tree67f950e2a970dc516cce08cbdb39b265e494ce3c
parent495f307ad1b4c467e7b8f67da13932a09a0f2479 (diff)
downloadspectrum-35137a03a8a011bc6b7681916e3de05639b32423.tar.gz
spectrum-35137a03a8a011bc6b7681916e3de05639b32423.zip
rust: implement scheme_parse_token()
-rw-r--r--minimal.rs30
1 files 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<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> {