From bd1c455dc85f3f2cce05a694e6819464cf06b384 Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 11 Jan 2016 02:32:41 -0800 Subject: [PATCH] Basic infrastructure parses Also got rid of EOF, don't need it --- src/parser.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++---- src/tokenizer.rs | 3 --- 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index b68436f..346911c 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,4 +1,5 @@ use tokenizer::Token; +use tokenizer::Kw; #[derive(Debug, Clone)] pub enum ASTNode { @@ -20,7 +21,8 @@ pub struct Prototype { #[derive(Debug, Clone)] pub enum Expression { - Literal(f64), + StringLiteral(String), + Number(f64), Variable(String), BinExp(String, Box, Box), Call(String, Vec), @@ -49,17 +51,61 @@ impl ParseError { declaraion := Fn prototype expression prototype := identifier LParen (Ident Comma?)* RParen expression := primary_expression (op primary_expression)* - primary_expression := Identifier | Number | call_expr | paren_expr + primary_expression := Variable | Number | String | call_expr | paren_expr paren_expr := LParen expression RParen call_expr := identifier LParen (expression Comma ?)* RParen op := '+', '-', etc. */ pub fn parse(tokens: &[Token], parsed_tree: &[ASTNode]) -> ParseResult { + use tokenizer::Token::*; + + let mut rest: Vec = tokens.to_vec(); + rest.reverse(); - let rest = tokens.to_vec().reverse(); let mut ast = parsed_tree.to_vec(); - ParseError::new("Parsing not implemented") + loop { + let cur_tok = match rest.last() { + Some(t) => t.clone(), + None => break + }; + + let result: ParseResult = match cur_tok { + Newline | Semicolon => { rest.pop(); continue}, + _ => parse_statement(&mut rest) + }; + + match result { + Ok(node) => ast.push(node), + Err(err) => return Err(err) + } + } + + Ok(ast) +} + +fn parse_statement(tokens: &mut Vec) -> ParseResult { + use tokenizer::Token::*; + let cur_tok: Token = tokens.last().unwrap().clone(); + let result: ASTNode = match cur_tok { + Keyword(Kw::Fn) => try!(parse_declaration(tokens)), + _ => try!(parse_expression(tokens)) + }; + + Ok(result) +} + +fn parse_declaration(tokens: &mut Vec) -> ParseResult { + use tokenizer::Token::*; + tokens.pop(); + tokens.pop(); + Ok(ASTNode::ExprNode(Expression::StringLiteral("Declaration".to_string()))) +} + +fn parse_expression(tokens: &mut Vec) -> ParseResult { + use tokenizer::Token::*; + tokens.pop(); + Ok(ASTNode::ExprNode(Expression::StringLiteral("Expr".to_string()))) } diff --git a/src/tokenizer.rs b/src/tokenizer.rs index 5d52ac4..0f42cd6 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -1,6 +1,5 @@ #[derive(Debug, Clone, PartialEq)] pub enum Token { - EOF, Newline, Semicolon, LParen, @@ -125,8 +124,6 @@ pub fn tokenize(input: &str) -> Option> { tokens.push(cur_tok); } - tokens.push(EOF); - Some(tokens) }