schala/src/parser.rs

66 lines
1.5 KiB
Rust

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<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>;
#[derive(Debug)]
pub struct ParseError {
pub msg: String
}
impl ParseError {
fn new<T>(msg: &str) -> ParseResult<T> {
Err(ParseError { msg: msg.to_string() })
}
}
/* 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.
*/
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")
}