Basic infrastructure parses

Also got rid of EOF, don't need it
This commit is contained in:
greg 2016-01-11 02:32:41 -08:00
parent b62ef43f07
commit bd1c455dc8
2 changed files with 50 additions and 7 deletions

View File

@ -1,4 +1,5 @@
use tokenizer::Token; use tokenizer::Token;
use tokenizer::Kw;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum ASTNode { pub enum ASTNode {
@ -20,7 +21,8 @@ pub struct Prototype {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum Expression { pub enum Expression {
Literal(f64), StringLiteral(String),
Number(f64),
Variable(String), Variable(String),
BinExp(String, Box<Expression>, Box<Expression>), BinExp(String, Box<Expression>, Box<Expression>),
Call(String, Vec<Expression>), Call(String, Vec<Expression>),
@ -49,17 +51,61 @@ impl ParseError {
declaraion := Fn prototype expression declaraion := Fn prototype expression
prototype := identifier LParen (Ident Comma?)* RParen prototype := identifier LParen (Ident Comma?)* RParen
expression := primary_expression (op primary_expression)* 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 paren_expr := LParen expression RParen
call_expr := identifier LParen (expression Comma ?)* RParen call_expr := identifier LParen (expression Comma ?)* RParen
op := '+', '-', etc. op := '+', '-', etc.
*/ */
pub fn parse(tokens: &[Token], parsed_tree: &[ASTNode]) -> ParseResult<AST> { pub fn parse(tokens: &[Token], parsed_tree: &[ASTNode]) -> ParseResult<AST> {
use tokenizer::Token::*;
let mut rest: Vec<Token> = tokens.to_vec();
rest.reverse();
let rest = tokens.to_vec().reverse();
let mut ast = parsed_tree.to_vec(); 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<ASTNode> = 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<Token>) -> ParseResult<ASTNode> {
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<Token>) -> ParseResult<ASTNode> {
use tokenizer::Token::*;
tokens.pop();
tokens.pop();
Ok(ASTNode::ExprNode(Expression::StringLiteral("Declaration".to_string())))
}
fn parse_expression(tokens: &mut Vec<Token>) -> ParseResult<ASTNode> {
use tokenizer::Token::*;
tokens.pop();
Ok(ASTNode::ExprNode(Expression::StringLiteral("Expr".to_string())))
} }

View File

@ -1,6 +1,5 @@
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum Token { pub enum Token {
EOF,
Newline, Newline,
Semicolon, Semicolon,
LParen, LParen,
@ -125,8 +124,6 @@ pub fn tokenize(input: &str) -> Option<Vec<Token>> {
tokens.push(cur_tok); tokens.push(cur_tok);
} }
tokens.push(EOF);
Some(tokens) Some(tokens)
} }