diff options
author | Dominick Allen <dominick.allen1989@gmail.com> | 2020-06-26 21:56:34 -0500 |
---|---|---|
committer | Dominick Allen <dominick.allen1989@gmail.com> | 2020-06-26 21:56:34 -0500 |
commit | 2a62f88ca03d5db406756525b5a6e24eae027eab (patch) | |
tree | cb8baa698388926ce54decc3a272e4f700864566 /src | |
parent | ad9f7e6a689c7fe42444cbe22ecdf1de8d092d5b (diff) |
Rename parse to tokenize, add Environment.
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/environment.rs | 15 | ||||
-rw-r--r-- | src/lib/tokenize.rs (renamed from src/lib/parse.rs) | 20 |
2 files changed, 25 insertions, 10 deletions
diff --git a/src/lib/environment.rs b/src/lib/environment.rs new file mode 100644 index 0000000..ef00e13 --- /dev/null +++ b/src/lib/environment.rs @@ -0,0 +1,15 @@ +use std::collections::HashMap; + +use super::sexpr::SExpr; + +pub struct Environment { + env: HashMap<String, SExpr> +} + +impl Environment { + pub fn new() -> Environment { + Environment { + env: HashMap::new() + } + } +} diff --git a/src/lib/parse.rs b/src/lib/tokenize.rs index 047b374..384b04f 100644 --- a/src/lib/parse.rs +++ b/src/lib/tokenize.rs @@ -1,7 +1,7 @@ use super::types::Type; use super::types::Number; use super::types::Op; -use super::sexpr::SExpr; +use super::types::SEXP; pub type MaybeToken = (Option<Result<Token, String>>, usize); @@ -134,7 +134,7 @@ fn analyze2(expr: &str) -> MaybeToken { (Some(Ok(Token::Value(Type::Bool(true)))), 4) } else if word == "false" { (Some(Ok(Token::Value(Type::Bool(false)))), 5) - } else if let Ok(op) = word.parse::<Op>() { + } else if let Ok(op) = word.tokenize::<Op>() { (Some(Ok(Token::Value(Type::Operator(op)))), word.len()) } else if c.is_alphabetic() { (Some(Ok(Token::Value(Type::Symbol(word.to_string())))), word.len()) @@ -170,7 +170,7 @@ pub fn is_paren(expr: &str) -> MaybeToken { pub fn is_op(expr: &str) -> MaybeToken { let word = make_word(expr); - match word.parse::<Op>() { + match word.tokenize::<Op>() { Ok(op) => (Some(Ok(Token::Value(Type::Operator(op)))), word.len()), _ => (None, 0) } @@ -236,7 +236,7 @@ pub fn is_number(expr: &str) -> MaybeToken { pub fn is_int(word: &str) -> MaybeToken { //let word = make_word(expr); - match word.parse::<isize>() { + match word.tokenize::<isize>() { Ok(x) => (Some(Ok(Token::Value(Type::Number(Number::Int(x))))), word.len()), _ => (None, 0) } @@ -244,13 +244,13 @@ pub fn is_int(word: &str) -> MaybeToken { pub fn is_float(word: &str) -> MaybeToken { //let word = make_word(expr); - match word.parse::<f32>() { + match word.tokenize::<f32>() { Ok(x) => (Some(Ok(Token::Value(Type::Number(Number::Float(x))))), word.len()), _ => (None, 0) } } -pub fn parse(expr: &str) -> Result<SExpr, String> { +pub fn tokenize(expr: &str) -> Result<SEXP, String> { let mut tokenstream = TokenStream::default(expr); match tokenstream.peek() { Some(Ok(Token::LParen)) => { @@ -258,13 +258,13 @@ pub fn parse(expr: &str) -> Result<SExpr, String> { descend(&mut tokenstream) }, Some(Ok(Token::RParen)) => Err("Malformed expression".to_string()), - Some(Ok(Token::Value(x))) => Ok(SExpr::Atom(x)), + Some(Ok(Token::Value(x))) => Ok(SEXP::Atom(x)), Some(Err(f)) => Err(f), None => Err("Empty expression".to_string()) } } -pub fn descend(tokenstream: &mut TokenStream) -> Result<SExpr, String> { +pub fn descend(tokenstream: &mut TokenStream) -> Result<SEXP, String> { let mut sexp = Vec::new(); loop { let token = match tokenstream.next() { @@ -286,11 +286,11 @@ pub fn descend(tokenstream: &mut TokenStream) -> Result<SExpr, String> { break; }, Token::Value(atom) => { - sexp.push(SExpr::Atom(atom)); + sexp.push(SEXP::Atom(atom)); continue; } } } - Ok(SExpr::Sexpr(sexp)) + Ok(SEXP::Sexpr(sexp)) } |