More parsing work
This commit is contained in:
parent
674f70a428
commit
0999cbe28e
@ -10,7 +10,7 @@ pub enum ASTNode {
|
|||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Function {
|
pub struct Function {
|
||||||
pub prototype: Prototype,
|
pub prototype: Prototype,
|
||||||
pub body: Expression
|
pub body: Vec<ASTNode>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
@ -48,9 +48,10 @@ impl ParseError {
|
|||||||
program := (statement delimiter ?)*
|
program := (statement delimiter ?)*
|
||||||
delimiter := Newline | Semicolon
|
delimiter := Newline | Semicolon
|
||||||
statement := declaration | expression
|
statement := declaration | expression
|
||||||
declaraion := Fn prototype expression
|
declaraion := Fn prototype (statement)* End
|
||||||
prototype := identifier LParen identlist RParen
|
prototype := identifier LParen identlist RParen
|
||||||
identlist := Ident (Comma Ident)*
|
identlist := Ident (Comma Ident)* | e
|
||||||
|
|
||||||
expression := primary_expression (op primary_expression)*
|
expression := primary_expression (op primary_expression)*
|
||||||
primary_expression := Variable | Number | String | call_expr | paren_expr
|
primary_expression := Variable | Number | String | call_expr | paren_expr
|
||||||
paren_expr := LParen expression RParen
|
paren_expr := LParen expression RParen
|
||||||
@ -73,8 +74,8 @@ impl Parser {
|
|||||||
self.tokens.last().map(|x| x.clone())
|
self.tokens.last().map(|x| x.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn next(&mut self) {
|
fn next(&mut self) -> Option<Token>{
|
||||||
self.tokens.pop();
|
self.tokens.pop()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -127,7 +128,8 @@ impl Parser {
|
|||||||
expect!(self, Fn, "Expected 'fn'");
|
expect!(self, Fn, "Expected 'fn'");
|
||||||
let prototype = try!(self.prototype());
|
let prototype = try!(self.prototype());
|
||||||
let body = try!(self.body());
|
let body = try!(self.body());
|
||||||
Ok(ASTNode::FuncNode(Function { prototype: prototype, body: body}))
|
expect!(self, Keyword(Kw::End), "Expected 'end'");
|
||||||
|
Ok(ASTNode::FuncNode(Function { prototype: prototype, body: vec!(ASTNode::ExprNode(body))} ))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn prototype(&mut self) -> ParseResult<Prototype> {
|
fn prototype(&mut self) -> ParseResult<Prototype> {
|
||||||
@ -172,8 +174,12 @@ impl Parser {
|
|||||||
|
|
||||||
fn expression(&mut self) -> ParseResult<ASTNode> {
|
fn expression(&mut self) -> ParseResult<ASTNode> {
|
||||||
use tokenizer::Token::*;
|
use tokenizer::Token::*;
|
||||||
self.next();
|
let expr: Expression = match self.next() {
|
||||||
Ok(ASTNode::ExprNode(Expression::StringLiteral("Expr".to_string())))
|
Some(Identifier(s)) => Expression::StringLiteral(s),
|
||||||
|
Some(x) => panic!("lol tryinna parse {:?}", x),
|
||||||
|
None => panic!("FUCK")
|
||||||
|
};
|
||||||
|
Ok(ASTNode::ExprNode(expr))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user