2015-12-25 02:03:11 -08:00
|
|
|
use tokenizer::Token;
|
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 {
|
|
|
|
Literal(f64),
|
|
|
|
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)*
|
|
|
|
primary_expression := Identifier | Number | call_expr | paren_expr
|
|
|
|
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> {
|
|
|
|
|
|
|
|
let rest = tokens.to_vec().reverse();
|
|
|
|
let mut ast = parsed_tree.to_vec();
|
|
|
|
|
|
|
|
ParseError::new("Parsing not implemented")
|
2015-12-25 02:03:11 -08:00
|
|
|
}
|
2016-01-10 01:15:34 -08:00
|
|
|
|