Basic infrastructure parses
Also got rid of EOF, don't need it
This commit is contained in:
parent
b62ef43f07
commit
bd1c455dc8
@ -1,4 +1,5 @@
|
||||
use tokenizer::Token;
|
||||
use tokenizer::Kw;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ASTNode {
|
||||
@ -20,7 +21,8 @@ pub struct Prototype {
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Expression {
|
||||
Literal(f64),
|
||||
StringLiteral(String),
|
||||
Number(f64),
|
||||
Variable(String),
|
||||
BinExp(String, Box<Expression>, Box<Expression>),
|
||||
Call(String, Vec<Expression>),
|
||||
@ -49,17 +51,61 @@ impl ParseError {
|
||||
declaraion := Fn prototype expression
|
||||
prototype := identifier LParen (Ident Comma?)* RParen
|
||||
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
|
||||
call_expr := identifier LParen (expression Comma ?)* RParen
|
||||
op := '+', '-', etc.
|
||||
*/
|
||||
|
||||
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();
|
||||
|
||||
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())))
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,5 @@
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Token {
|
||||
EOF,
|
||||
Newline,
|
||||
Semicolon,
|
||||
LParen,
|
||||
@ -125,8 +124,6 @@ pub fn tokenize(input: &str) -> Option<Vec<Token>> {
|
||||
tokens.push(cur_tok);
|
||||
}
|
||||
|
||||
tokens.push(EOF);
|
||||
|
||||
Some(tokens)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user