2015-12-25 02:03:11 -08:00
|
|
|
use tokenizer::Token;
|
2016-01-11 02:32:41 -08:00
|
|
|
use tokenizer::Kw;
|
2015-12-24 22:01:59 -08:00
|
|
|
|
2016-01-10 01:15:34 -08:00
|
|
|
#[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<String>
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum Expression {
|
2016-01-11 02:32:41 -08:00
|
|
|
StringLiteral(String),
|
|
|
|
Number(f64),
|
2016-01-10 01:15:34 -08:00
|
|
|
Variable(String),
|
|
|
|
BinExp(String, Box<Expression>, Box<Expression>),
|
|
|
|
Call(String, Vec<Expression>),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type AST = Vec<ASTNode>;
|
|
|
|
|
|
|
|
//TODO make this support incomplete parses
|
|
|
|
pub type ParseResult<T> = Result<T, ParseError>;
|
|
|
|
|
2015-12-24 22:01:59 -08:00
|
|
|
#[derive(Debug)]
|
2016-01-10 01:15:34 -08:00
|
|
|
pub struct ParseError {
|
|
|
|
pub msg: String
|
2015-12-30 15:09:31 -08:00
|
|
|
}
|
|
|
|
|
2016-01-10 01:15:34 -08:00
|
|
|
impl ParseError {
|
|
|
|
fn new<T>(msg: &str) -> ParseResult<T> {
|
|
|
|
Err(ParseError { msg: msg.to_string() })
|
|
|
|
}
|
|
|
|
}
|
2015-12-25 02:03:11 -08:00
|
|
|
|
2016-01-11 02:03:03 -08:00
|
|
|
/* Grammar
|
|
|
|
program := (statement delimiter ?)*
|
|
|
|
delimiter := Newline | Semicolon
|
|
|
|
statement := declaration | expression
|
|
|
|
declaraion := Fn prototype expression
|
|
|
|
prototype := identifier LParen (Ident Comma?)* RParen
|
|
|
|
expression := primary_expression (op primary_expression)*
|
2016-01-11 02:32:41 -08:00
|
|
|
primary_expression := Variable | Number | String | call_expr | paren_expr
|
2016-01-11 02:03:03 -08:00
|
|
|
paren_expr := LParen expression RParen
|
|
|
|
call_expr := identifier LParen (expression Comma ?)* RParen
|
|
|
|
op := '+', '-', etc.
|
|
|
|
*/
|
|
|
|
|
2016-01-10 01:15:34 -08:00
|
|
|
pub fn parse(tokens: &[Token], parsed_tree: &[ASTNode]) -> ParseResult<AST> {
|
2016-01-11 02:32:41 -08:00
|
|
|
use tokenizer::Token::*;
|
|
|
|
|
|
|
|
let mut rest: Vec<Token> = tokens.to_vec();
|
|
|
|
rest.reverse();
|
2016-01-10 01:15:34 -08:00
|
|
|
|
|
|
|
let mut ast = parsed_tree.to_vec();
|
|
|
|
|
2016-01-11 02:32:41 -08:00
|
|
|
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())))
|
2015-12-25 02:03:11 -08:00
|
|
|
}
|
2016-01-10 01:15:34 -08:00
|
|
|
|