diff options
author | bnewbold <bnewbold@robocracy.org> | 2016-04-19 22:38:33 -0400 |
---|---|---|
committer | bnewbold <bnewbold@robocracy.org> | 2016-04-19 22:38:54 -0400 |
commit | e46023e0bd31d37ae25556aa13095df85813da9f (patch) | |
tree | 153f5c91fce7358aec244dbf9d21bbe3c0c0f54d | |
parent | 18ed2090ec383fa3e15365ebad90b6a5af919b60 (diff) | |
download | spectrum-e46023e0bd31d37ae25556aa13095df85813da9f.tar.gz spectrum-e46023e0bd31d37ae25556aa13095df85813da9f.zip |
rust: first (broken) implementation of tokenizer
Gets the type signatures right though... switch from &String to &str in
a lot of places.
-rw-r--r-- | minimal.rs | 24 |
1 files changed, 21 insertions, 3 deletions
@@ -32,8 +32,25 @@ fn is_zero(n: f64) -> bool { /////////////////////////////////// -fn scheme_tokenize(s: &String) -> Result<Vec<&String>, &'static str> { - let ret = vec![s]; +//let sep = ('(', ')'); +//let ws = (' ', '\t', '\n'); +// TODO: this doesn't handle strings properly. Eg: +// (quote "this ) will ( fail") +fn scheme_tokenize<'a>(raw_str: &'a str) -> Result<Vec<&'a str>, &'static str> { + let mut ret = Vec::<&str>::new(); + for s in raw_str.split_whitespace() { + if s.len() > 1 && s.starts_with('(') { + let (paren, el) = s.split_at(1); + ret.push("("); + ret.push(el); + } else if s.len() > 1 && s.ends_with(')') { + let (el, paren) = s.split_at(s.len() - 1); + ret.push(el); + ret.push(")"); + } else if s.len() > 0 { + ret.push(s); + } + } return Ok(ret); } @@ -42,7 +59,7 @@ fn scheme_parse_num(s: &String) -> Result<f64, &'static str> { return Ok(num); } -fn scheme_parse_sexpr<'a>(sexpr: &Vec<&'a String>) -> Result<SchemeExpr<'a>, &'static str> { +fn scheme_parse_sexpr<'a>(sexpr: &Vec<&'a str>) -> Result<SchemeExpr<'a>, &'static str> { let ret = sexpr.into_iter().map(|el| SchemeExpr::SchemeStr(el)).collect(); return Ok(SchemeExpr::SchemeList(ret)) } @@ -87,6 +104,7 @@ fn main() { return; } let tokens = scheme_tokenize(&raw_input).unwrap(); + println!("Tokens: {}", tokens.join(", ")); let sexpr = scheme_parse_sexpr(&tokens).unwrap(); let ast = scheme_parse(&sexpr).unwrap(); let resp = scheme_eval(&ast).unwrap(); |