Starting to do if statement parsing

This commit is contained in:
greg 2015-07-24 17:59:24 -07:00
parent 169e662049
commit edf100b583
2 changed files with 26 additions and 6 deletions

View File

@ -11,7 +11,8 @@ pub enum AST {
Number(f64), Number(f64),
BinOp(Box<AST>, Box<AST>, Box<AST>), BinOp(Box<AST>, Box<AST>, Box<AST>),
Binding(String, Box<AST>), Binding(String, Box<AST>),
Statements(Vec<AST>) Statements(Vec<AST>),
IfStatement(Box<AST>, Box<AST>, Option<Box<AST>>)
} }
pub enum ParseResult { pub enum ParseResult {
@ -120,8 +121,27 @@ fn let_expression(input: &mut Tokens) -> ParseResult {
} }
fn expression(input: &mut Tokens) -> ParseResult { fn expression(input: &mut Tokens) -> ParseResult {
ParseResult::Err("dame".to_string()) let lookahead = input.peek().map(|i| i.clone());
println!("{:?}", lookahead);
match lookahead {
Some(&Keyword(Kw::If)) => {
input.next();
ParseResult::Ok( AST::IfStatement(
Box::new(AST::Number(1.0)),
Box::new(AST::Number(2.0)),
None))
},
_ => ParseResult::Err("error in expression()".to_string())
} }
}
/*
fn if_expression(input: &mut Tokens) -> ParseResult {
}
*/
fn rhs(input: &mut Tokens) -> ParseResult { fn rhs(input: &mut Tokens) -> ParseResult {
let next = input.next(); let next = input.next();

View File

@ -92,9 +92,9 @@ pub fn tokenize(input: &str) -> Vec<Token> {
} }
fn handle_identifier(identifier: String) -> Token { fn handle_identifier(identifier: String) -> Token {
if identifier == "let" { match &identifier[..] {
return Token::Keyword(Kw::Let); "let" => Token::Keyword(Kw::Let),
"if" => Token::Keyword(Kw::If),
_ => Token::Identifier(identifier)
} }
return Token::Identifier(identifier);
} }