From aabe6b810f04f75546024cd5ac5ec1c1cc7b2f8b Mon Sep 17 00:00:00 2001 From: bnewbold Date: Wed, 20 Apr 2016 01:05:03 -0400 Subject: rust: add symbol and quote types --- minimal.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/minimal.rs b/minimal.rs index c2f3b02..369b9db 100644 --- a/minimal.rs +++ b/minimal.rs @@ -21,8 +21,10 @@ enum SchemeExpr<'a> { SchemeFalse, SchemeNum(f64), SchemeBuiltin(&'a str), + SchemeSymbol(&'a str), SchemeStr(&'a str), SchemeList(Vec>), + SchemeQuote(Vec>), } /////////////////////////////////// @@ -106,6 +108,11 @@ fn scheme_parse_token(token: &str) -> Result { return Ok(SchemeExpr::SchemeStr(token)); } + // If it's all alphas, must be a symbol + if token.is_alpha() { + return Ok(SchemeExpr::SchemeSymbol(token)); + } + return Err("unparsable token"); } @@ -154,6 +161,7 @@ fn scheme_repr<'a>(ast: &SchemeExpr) -> Result { &SchemeExpr::SchemeNull => Ok("'()".to_string()), &SchemeExpr::SchemeBuiltin(b)=> Ok(b.to_string()), &SchemeExpr::SchemeStr(s)=> Ok(s.to_string()), + &SchemeExpr::SchemeSymbol(s)=> Ok(s.to_string()), &SchemeExpr::SchemeNum(num) => Ok(format!("{}", num).to_string()), &SchemeExpr::SchemeList(ref list) => { let mut ret: String = @@ -162,6 +170,13 @@ fn scheme_repr<'a>(ast: &SchemeExpr) -> Result { ret.push_str(" )"); Ok(ret) }, + &SchemeExpr::SchemeQuote(ref list) => { + let mut ret: String = + list.iter().fold("(quote ".to_string(), + |acc, ref el| acc + " " + &scheme_repr(&el).unwrap()); + ret.push_str(" )"); + Ok(ret) + }, } } -- cgit v1.2.3