Move definition around

This commit is contained in:
greg 2015-07-22 03:45:42 -07:00
parent 5213dd327f
commit 25f5188d8c

View File

@ -20,24 +20,13 @@ pub enum ParseResult {
type Tokens<'a> = Peekable<Iter<'a,Token>>;
/* expect calls .next() and thus advances the token list */
macro_rules! expect {
($tok:expr, $tokens:expr) => ( if !expect_token($tok, $tokens) {
return ParseResult::Err(format!("Expected {:?}", $tok));
})
}
pub fn parse(input: Vec<Token>) -> ParseResult {
let mut tokens: Tokens = input.iter().peekable();
if let ParseResult::Ok(ast) = let_expression(&mut tokens) {
expect!(EOF, &mut tokens);
return ParseResult::Ok(ast);
}
return ParseResult::Err("Bad parse".to_string());
}
fn expect_token(tok: Token, tokens: &mut Tokens) -> bool {
if let Some(n) = tokens.next() {
let next = (*n).clone();
@ -54,9 +43,22 @@ fn expect_token(tok: Token, tokens: &mut Tokens) -> bool {
}
}
return false;
false
}
pub fn parse(input: Vec<Token>) -> ParseResult {
let mut tokens: Tokens = input.iter().peekable();
if let ParseResult::Ok(ast) = let_expression(&mut tokens) {
expect!(EOF, &mut tokens);
return ParseResult::Ok(ast);
}
return ParseResult::Err("Bad parse".to_string());
}
fn let_expression(input: &mut Tokens) -> ParseResult {
expect!(Identifier("let".to_string()), input);
if let Some(&Identifier(ref name)) = input.next() {