From a613fa73e54e8362d49c569f699b0f1dfbd66d6c Mon Sep 17 00:00:00 2001 From: greg Date: Sun, 10 Jan 2016 01:15:34 -0800 Subject: [PATCH] Basic parsing framework --- src/main.rs | 12 ++++++++---- src/parser.rs | 51 ++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 54 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index aeb31cb..bbce48a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,7 +11,7 @@ use simplerepl::{REPL, ReplState}; use tokenizer::tokenize; mod tokenizer; -use parser::parse; +use parser::{parse, ParseResult}; mod parser; fn main() { @@ -64,10 +64,14 @@ fn repl_handler(input: &str, state: &mut InterpreterState) -> String { println!("Tokens: {:?}", tokens); } + let ast = match parse(&tokens, &[]) { + Ok(ast) => ast, + Err(err) => return err.msg + }; + if state.show_parse { - println!("not implemented") + println!("AST: {:?}", ast); } - let ast = parse(&tokens); - format!("{:?}", tokens) + format!("{:?}", ast) } diff --git a/src/parser.rs b/src/parser.rs index 5d523e0..1fed2c7 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,11 +1,52 @@ use tokenizer::Token; +#[derive(Debug, Clone)] +pub enum ASTNode { + ExprNode(Expression), + FuncNode(Function), +} + +#[derive(Debug, Clone)] +pub struct Function { + pub prototype: Prototype, + pub body: Expression +} + +#[derive(Debug, Clone)] +pub struct Prototype { + pub name: String, + pub args: Vec +} + +#[derive(Debug, Clone)] +pub enum Expression { + Literal(f64), + Variable(String), + BinExp(String, Box, Box), + Call(String, Vec), +} + +pub type AST = Vec; + +//TODO make this support incomplete parses +pub type ParseResult = Result; + #[derive(Debug)] -pub struct ParseResult { - msg: i32 +pub struct ParseError { + pub msg: String } -pub fn parse(tokens: &[Token]) -> ParseResult { - - ParseResult { msg: 0 } +impl ParseError { + fn new(msg: &str) -> ParseResult { + Err(ParseError { msg: msg.to_string() }) + } } + +pub fn parse(tokens: &[Token], parsed_tree: &[ASTNode]) -> ParseResult { + + let rest = tokens.to_vec().reverse(); + let mut ast = parsed_tree.to_vec(); + + ParseError::new("Parsing not implemented") +} +