Basic parsing framework

This commit is contained in:
greg 2016-01-10 01:15:34 -08:00
parent 8c473c554e
commit a613fa73e5
2 changed files with 54 additions and 9 deletions

View File

@ -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)
}

View File

@ -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<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 ParseResult {
msg: i32
pub struct ParseError {
pub msg: String
}
pub fn parse(tokens: &[Token]) -> ParseResult {
ParseResult { msg: 0 }
impl ParseError {
fn new<T>(msg: &str) -> ParseResult<T> {
Err(ParseError { msg: msg.to_string() })
}
}
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")
}