From edf100b583dcd4ff5b15538236b539f4c40e8857 Mon Sep 17 00:00:00 2001 From: greg Date: Fri, 24 Jul 2015 17:59:24 -0700 Subject: [PATCH] Starting to do if statement parsing --- src/parser.rs | 24 ++++++++++++++++++++++-- src/tokenizer.rs | 8 ++++---- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index bed789f..938e9d9 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -11,7 +11,8 @@ pub enum AST { Number(f64), BinOp(Box, Box, Box), Binding(String, Box), - Statements(Vec) + Statements(Vec), + IfStatement(Box, Box, Option>) } 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 { diff --git a/src/tokenizer.rs b/src/tokenizer.rs index dd03a68..28bc4db 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -92,9 +92,9 @@ pub fn tokenize(input: &str) -> Vec { } 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); }