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),
BinOp(Box<AST>, Box<AST>, Box<AST>),
Binding(String, Box<AST>),
Statements(Vec<AST>)
Statements(Vec<AST>),
IfStatement(Box<AST>, Box<AST>, Option<Box<AST>>)
}
pub enum ParseResult {
@ -120,9 +121,28 @@ fn let_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 {
let next = input.next();
if let Some(&Identifier(ref value)) = next {

View File

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