2016-01-22 02:55:07 -08:00
|
|
|
use std::fmt;
|
2016-01-15 03:27:24 -08:00
|
|
|
use tokenizer::{Token, Kw, Op};
|
2015-12-24 22:01:59 -08:00
|
|
|
|
2016-01-12 03:29:28 -08:00
|
|
|
/* Grammar
|
|
|
|
program := (statement delimiter ?)*
|
|
|
|
delimiter := Newline | Semicolon
|
|
|
|
statement := declaration | expression
|
|
|
|
declaraion := Fn prototype (statement)* End
|
|
|
|
prototype := identifier LParen identlist RParen
|
|
|
|
identlist := Ident (Comma Ident)* | e
|
2016-01-16 17:00:54 -08:00
|
|
|
exprlist := Expression (Comma Expression)* | e
|
2016-01-12 03:29:28 -08:00
|
|
|
|
|
|
|
expression := primary_expression (op primary_expression)*
|
2016-01-16 20:23:43 -08:00
|
|
|
primary_expression := Number | String | identifier_expr | paren_expr
|
|
|
|
identifier_expr := call_expression | Variable
|
2016-01-12 03:29:28 -08:00
|
|
|
paren_expr := LParen expression RParen
|
2016-01-16 20:23:43 -08:00
|
|
|
call_expr := Identifier LParen exprlist RParen
|
2016-01-12 03:29:28 -08:00
|
|
|
op := '+', '-', etc.
|
|
|
|
*/
|
|
|
|
|
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,
|
2016-01-12 03:29:03 -08:00
|
|
|
pub body: Vec<Expression>,
|
2016-01-10 01:15:34 -08:00
|
|
|
}
|
|
|
|
|
2016-01-17 00:08:46 -08:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2016-01-10 01:15:34 -08:00
|
|
|
pub struct Prototype {
|
|
|
|
pub name: String,
|
2016-01-24 01:03:57 -08:00
|
|
|
pub parameters: Vec<String>
|
2016-01-10 01:15:34 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum Expression {
|
2016-01-21 19:10:21 -08:00
|
|
|
Null,
|
2016-01-11 02:32:41 -08:00
|
|
|
StringLiteral(String),
|
|
|
|
Number(f64),
|
2016-01-10 01:15:34 -08:00
|
|
|
Variable(String),
|
|
|
|
BinExp(String, Box<Expression>, Box<Expression>),
|
|
|
|
Call(String, Vec<Expression>),
|
2016-01-26 02:57:09 -08:00
|
|
|
Conditional(Box<Expression>, Box<Expression>, Option<Box<Expression>>),
|
2016-01-10 01:15:34 -08:00
|
|
|
}
|
|
|
|
|
2016-01-22 02:55:07 -08:00
|
|
|
impl fmt::Display for ASTNode {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
use self::ASTNode::*;
|
|
|
|
match self {
|
|
|
|
&ExprNode(ref expr) => write!(f, "{}", expr),
|
|
|
|
&FuncNode(_) => write!(f, "UNIMPLEMENTED"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Expression {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
use self::Expression::*;
|
|
|
|
match self {
|
|
|
|
&Null => write!(f, "null"),
|
|
|
|
&StringLiteral(ref s) => write!(f, "\"{}\"", s),
|
|
|
|
&Number(n) => write!(f, "{}", n),
|
|
|
|
_ => write!(f, "UNIMPLEMENTED"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-10 01:15:34 -08:00
|
|
|
pub type AST = Vec<ASTNode>;
|
|
|
|
|
2016-01-15 03:27:24 -08:00
|
|
|
type Precedence = u8;
|
|
|
|
|
2016-01-10 01:15:34 -08:00
|
|
|
//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 {
|
2016-01-12 00:58:21 -08:00
|
|
|
fn result_from_str<T>(msg: &str) -> ParseResult<T> {
|
2016-01-10 01:15:34 -08:00
|
|
|
Err(ParseError { msg: msg.to_string() })
|
|
|
|
}
|
|
|
|
}
|
2015-12-25 02:03:11 -08:00
|
|
|
|
2016-01-12 01:58:12 -08:00
|
|
|
struct Parser {
|
|
|
|
tokens: Vec<Token>,
|
|
|
|
}
|
2016-01-11 02:32:41 -08:00
|
|
|
|
2016-01-12 01:58:12 -08:00
|
|
|
impl Parser {
|
|
|
|
fn initialize(tokens: &[Token]) -> Parser {
|
|
|
|
let mut tokens = tokens.to_vec();
|
|
|
|
tokens.reverse();
|
|
|
|
Parser { tokens: tokens }
|
|
|
|
}
|
2016-01-11 02:32:41 -08:00
|
|
|
|
2016-01-16 15:14:20 -08:00
|
|
|
fn peek(&self) -> Option<Token> {
|
2016-01-12 01:58:12 -08:00
|
|
|
self.tokens.last().map(|x| x.clone())
|
2016-01-11 02:32:41 -08:00
|
|
|
}
|
|
|
|
|
2016-01-12 03:26:28 -08:00
|
|
|
fn next(&mut self) -> Option<Token>{
|
|
|
|
self.tokens.pop()
|
2016-01-12 01:58:12 -08:00
|
|
|
}
|
2016-01-15 03:27:24 -08:00
|
|
|
|
2016-01-16 15:14:20 -08:00
|
|
|
fn get_precedence(&self, op: &Op) -> Precedence {
|
2016-01-15 03:27:24 -08:00
|
|
|
match &op.repr[..] {
|
|
|
|
"+" => 10,
|
|
|
|
"-" => 10,
|
|
|
|
"*" => 20,
|
|
|
|
"/" => 20,
|
|
|
|
"%" => 20,
|
|
|
|
_ => 255,
|
|
|
|
}
|
|
|
|
}
|
2016-01-11 02:32:41 -08:00
|
|
|
}
|
|
|
|
|
2016-01-12 00:58:21 -08:00
|
|
|
macro_rules! expect {
|
2016-01-17 00:59:31 -08:00
|
|
|
($self_:expr, $token:pat) => {
|
2016-01-12 01:58:12 -08:00
|
|
|
match $self_.peek() {
|
|
|
|
Some($token) => {$self_.next();},
|
2016-01-17 00:59:31 -08:00
|
|
|
Some(x) => {
|
|
|
|
let err = format!("Expected `{:?}` but got `{:?}`", stringify!($token), x); //TODO implement Display for token
|
|
|
|
return ParseError::result_from_str(&err)
|
|
|
|
},
|
|
|
|
None => {
|
|
|
|
let err = format!("Expected `{:?}` but got end of input", stringify!($token));
|
|
|
|
return ParseError::result_from_str(&err) //TODO make this not require 2 stringifications
|
|
|
|
}
|
|
|
|
|
2016-01-12 01:58:12 -08:00
|
|
|
}
|
2016-01-12 00:58:21 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-16 20:23:43 -08:00
|
|
|
macro_rules! expect_identifier {
|
|
|
|
($self_:expr) => {
|
|
|
|
match $self_.peek() {
|
|
|
|
Some(Identifier(s)) => {$self_.next(); s},
|
|
|
|
_ => return ParseError::result_from_str("Expected Identifier")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-12 04:04:14 -08:00
|
|
|
fn is_delimiter(token: &Token) -> bool {
|
|
|
|
use tokenizer::Token::*;
|
|
|
|
match *token {
|
|
|
|
Newline | Semicolon => true,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-12 01:58:12 -08:00
|
|
|
impl Parser {
|
|
|
|
fn program(&mut self) -> ParseResult<AST> {
|
|
|
|
let mut ast = Vec::new(); //TODO have this come from previously-parsed tree
|
|
|
|
loop {
|
2016-01-17 00:50:23 -08:00
|
|
|
let result: ParseResult<ASTNode> = match self.peek() {
|
|
|
|
Some(ref t) if is_delimiter(&t) => { self.next(); continue},
|
|
|
|
Some(_) => self.statement(),
|
|
|
|
None => break,
|
2016-01-12 01:58:12 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
match result {
|
|
|
|
Ok(node) => ast.push(node),
|
|
|
|
Err(err) => return Err(err)
|
|
|
|
}
|
|
|
|
}
|
2016-01-11 02:32:41 -08:00
|
|
|
|
2016-01-12 01:58:12 -08:00
|
|
|
Ok(ast)
|
|
|
|
}
|
2016-01-11 02:32:41 -08:00
|
|
|
|
2016-01-12 01:58:12 -08:00
|
|
|
fn statement(&mut self) -> ParseResult<ASTNode> {
|
|
|
|
use tokenizer::Token::*;
|
2016-01-17 00:50:23 -08:00
|
|
|
let node: ASTNode = match self.peek() {
|
|
|
|
Some(Keyword(Kw::Fn)) => try!(self.declaration()),
|
|
|
|
Some(_) => ASTNode::ExprNode(try!(self.expression())),
|
|
|
|
None => panic!("unexpected end of tokens"),
|
2016-01-12 01:58:12 -08:00
|
|
|
};
|
2016-01-12 00:58:21 -08:00
|
|
|
|
2016-01-12 01:58:12 -08:00
|
|
|
Ok(node)
|
|
|
|
}
|
2016-01-12 00:58:21 -08:00
|
|
|
|
2016-01-12 01:58:12 -08:00
|
|
|
fn declaration(&mut self) -> ParseResult<ASTNode> {
|
|
|
|
use tokenizer::Token::*;
|
2016-01-17 00:59:31 -08:00
|
|
|
expect!(self, Keyword(Kw::Fn));
|
2016-01-12 01:58:12 -08:00
|
|
|
let prototype = try!(self.prototype());
|
2016-01-12 03:29:03 -08:00
|
|
|
let body: Vec<Expression> = try!(self.body());
|
2016-01-17 00:59:31 -08:00
|
|
|
expect!(self, Keyword(Kw::End));
|
2016-01-12 03:29:03 -08:00
|
|
|
Ok(ASTNode::FuncNode(Function { prototype: prototype, body: body } ))
|
2016-01-12 01:58:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn prototype(&mut self) -> ParseResult<Prototype> {
|
|
|
|
use tokenizer::Token::*;
|
2016-01-16 20:23:43 -08:00
|
|
|
let name: String = expect_identifier!(self);
|
2016-01-17 00:59:31 -08:00
|
|
|
expect!(self, LParen);
|
2016-01-24 01:03:57 -08:00
|
|
|
let parameters: Vec<String> = try!(self.identlist());
|
2016-01-17 00:59:31 -08:00
|
|
|
expect!(self, RParen);
|
2016-01-24 01:03:57 -08:00
|
|
|
Ok(Prototype {name: name, parameters: parameters})
|
2016-01-12 01:58:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn identlist(&mut self) -> ParseResult<Vec<String>> {
|
|
|
|
use tokenizer::Token::*;
|
|
|
|
let mut args: Vec<String> = Vec::new();
|
2016-01-17 01:17:54 -08:00
|
|
|
while let Some(Identifier(name)) = self.peek() {
|
|
|
|
args.push(name);
|
|
|
|
self.next();
|
|
|
|
if let Some(Comma) = self.peek() {
|
|
|
|
self.next();
|
|
|
|
} else {
|
|
|
|
break;
|
2016-01-12 01:58:12 -08:00
|
|
|
}
|
2016-01-12 00:58:21 -08:00
|
|
|
}
|
2016-01-12 01:58:12 -08:00
|
|
|
Ok(args)
|
2016-01-12 00:58:21 -08:00
|
|
|
}
|
|
|
|
|
2016-01-16 20:23:43 -08:00
|
|
|
fn exprlist(&mut self) -> ParseResult<Vec<Expression>> {
|
|
|
|
use tokenizer::Token::*;
|
|
|
|
let mut args: Vec<Expression> = Vec::new();
|
|
|
|
loop {
|
|
|
|
if let Some(RParen) = self.peek() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
let exp = try!(self.expression());
|
|
|
|
args.push(exp);
|
|
|
|
if let Some(Comma) = self.peek() {
|
|
|
|
self.next();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(args)
|
|
|
|
}
|
|
|
|
|
2016-01-12 03:29:03 -08:00
|
|
|
fn body(&mut self) -> ParseResult<Vec<Expression>> {
|
2016-01-12 01:58:12 -08:00
|
|
|
use tokenizer::Token::*;
|
2016-01-12 04:04:14 -08:00
|
|
|
let mut exprs = Vec::new();
|
|
|
|
loop {
|
|
|
|
match self.peek() {
|
|
|
|
Some(ref t) if is_delimiter(t) => { self.next(); continue},
|
|
|
|
Some(Keyword(Kw::End)) => break,
|
|
|
|
_ => {
|
|
|
|
let expr = try!(self.expression());
|
|
|
|
exprs.push(expr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(exprs)
|
2016-01-12 01:58:12 -08:00
|
|
|
}
|
2016-01-12 00:58:21 -08:00
|
|
|
|
2016-01-12 04:04:14 -08:00
|
|
|
fn expression(&mut self) -> ParseResult<Expression> {
|
2016-01-15 03:27:24 -08:00
|
|
|
let lhs: Expression = try!(self.primary_expression());
|
|
|
|
self.precedence_expr(lhs, 0)
|
|
|
|
}
|
|
|
|
|
2016-01-16 15:14:20 -08:00
|
|
|
fn precedence_expr(&mut self, mut lhs: Expression, min_precedence: u8) -> ParseResult<Expression> {
|
2016-01-15 03:27:24 -08:00
|
|
|
use tokenizer::Token::*;
|
2016-01-16 15:14:20 -08:00
|
|
|
while let Some(Operator(op)) = self.peek() {
|
|
|
|
let precedence = self.get_precedence(&op);
|
|
|
|
if precedence < min_precedence {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
self.next();
|
|
|
|
let mut rhs = try!(self.primary_expression());
|
|
|
|
while let Some(Operator(ref op)) = self.peek() {
|
|
|
|
if self.get_precedence(op) > precedence {
|
|
|
|
let new_prec = self.get_precedence(op);
|
|
|
|
rhs = try!(self.precedence_expr(rhs, new_prec));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lhs = Expression::BinExp(op.repr, Box::new(lhs), Box::new(rhs));
|
|
|
|
}
|
|
|
|
|
2016-01-15 03:27:24 -08:00
|
|
|
Ok(lhs)
|
2016-01-13 23:29:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn primary_expression(&mut self) -> ParseResult<Expression> {
|
|
|
|
use tokenizer::Token::*;
|
2016-01-17 00:50:23 -08:00
|
|
|
Ok(match self.peek() {
|
2016-01-21 19:10:21 -08:00
|
|
|
Some(Keyword(Kw::Null)) => { self.next(); Expression::Null },
|
2016-01-13 23:29:18 -08:00
|
|
|
Some(NumLiteral(n)) => { self.next(); Expression::Number(n) },
|
|
|
|
Some(StrLiteral(s)) => { self.next(); Expression::StringLiteral(s) },
|
2016-01-17 00:16:01 -08:00
|
|
|
Some(Identifier(_)) => { try!(self.identifier_expr()) },
|
2016-01-16 15:14:20 -08:00
|
|
|
Some(Token::LParen) => { try!(self.paren_expr()) }
|
2016-01-17 00:16:01 -08:00
|
|
|
Some(_) => return ParseError::result_from_str("Expected primary expression"),
|
2016-01-15 01:04:54 -08:00
|
|
|
None => return ParseError::result_from_str("Expected primary expression received EoI")
|
2016-01-17 00:50:23 -08:00
|
|
|
})
|
2016-01-12 01:58:12 -08:00
|
|
|
}
|
2016-01-15 01:04:54 -08:00
|
|
|
|
2016-01-16 20:23:43 -08:00
|
|
|
fn identifier_expr(&mut self) -> ParseResult<Expression> {
|
|
|
|
use tokenizer::Token::*;
|
|
|
|
let name = expect_identifier!(self);
|
|
|
|
let expr = match self.peek() {
|
|
|
|
Some(LParen) => {
|
|
|
|
let args = try!(self.call_expr());
|
|
|
|
Expression::Call(name, args)
|
|
|
|
},
|
|
|
|
__ => Expression::Variable(name)
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(expr)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call_expr(&mut self) -> ParseResult<Vec<Expression>> {
|
2016-01-16 17:00:54 -08:00
|
|
|
use tokenizer::Token::*;
|
2016-01-17 00:59:31 -08:00
|
|
|
expect!(self, LParen);
|
2016-01-17 00:16:01 -08:00
|
|
|
let args: Vec<Expression> = try!(self.exprlist());
|
2016-01-17 00:59:31 -08:00
|
|
|
expect!(self, RParen);
|
2016-01-16 20:23:43 -08:00
|
|
|
Ok(args)
|
2016-01-15 01:04:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn paren_expr(&mut self) -> ParseResult<Expression> {
|
2016-01-17 00:59:31 -08:00
|
|
|
expect!(self, Token::LParen);
|
2016-01-15 01:15:57 -08:00
|
|
|
let expr = try!(self.expression());
|
2016-01-17 00:59:31 -08:00
|
|
|
expect!(self, Token::RParen);
|
2016-01-15 01:15:57 -08:00
|
|
|
Ok(expr)
|
2016-01-15 01:04:54 -08:00
|
|
|
}
|
2016-01-11 02:32:41 -08:00
|
|
|
}
|
|
|
|
|
2016-01-12 01:58:12 -08:00
|
|
|
pub fn parse(tokens: &[Token], _parsed_tree: &[ASTNode]) -> ParseResult<AST> {
|
|
|
|
let mut parser = Parser::initialize(tokens);
|
|
|
|
parser.program()
|
2015-12-25 02:03:11 -08:00
|
|
|
}
|
2016-01-16 15:14:20 -08:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use tokenizer;
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
macro_rules! parsetest {
|
2016-01-16 16:12:58 -08:00
|
|
|
($input:expr, $output:pat, $ifexpr:expr) => {
|
2016-01-16 15:14:20 -08:00
|
|
|
{
|
|
|
|
let tokens = tokenizer::tokenize($input).unwrap();
|
|
|
|
let ast = parse(&tokens, &[]).unwrap();
|
|
|
|
match &ast[..] {
|
2016-01-16 16:12:58 -08:00
|
|
|
$output if $ifexpr => (),
|
2016-01-16 15:14:20 -08:00
|
|
|
x => panic!("Error in parse test, got {:?} instead", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-17 00:08:46 -08:00
|
|
|
#[test]
|
|
|
|
fn call_parse_test() {
|
|
|
|
use super::ASTNode::*;
|
|
|
|
use super::Expression::*;
|
|
|
|
use super::Function;
|
|
|
|
|
|
|
|
parsetest!(
|
|
|
|
"fn a() 1 + 2 end",
|
|
|
|
[FuncNode(Function {prototype: Prototype { name: ref name, args: ref args }, body: ref body})],
|
|
|
|
match &body[..] { [BinExp(_, box Number(1.0), box Number(2.0))] => true, _ => false }
|
|
|
|
&& name == "a" && match &args[..] { [] => true, _ => false }
|
|
|
|
);
|
|
|
|
|
|
|
|
parsetest!(
|
|
|
|
"fn a(x,y) 1 + 2 end",
|
|
|
|
[FuncNode(Function {prototype: Prototype { name: ref name, args: ref args }, body: ref body})],
|
|
|
|
match &body[..] { [BinExp(_, box Number(1.0), box Number(2.0))] => true, _ => false }
|
|
|
|
&& name == "a" && *args == ["x","y"]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-01-16 15:14:20 -08:00
|
|
|
#[test]
|
2016-01-16 16:23:26 -08:00
|
|
|
fn expression_parse_test() {
|
|
|
|
use super::ASTNode::*;
|
|
|
|
use super::Expression::*;
|
2016-01-17 00:08:46 -08:00
|
|
|
parsetest!("a", [ExprNode(Variable(ref s))], s == "a");
|
2016-01-16 16:22:00 -08:00
|
|
|
parsetest!("a + b",
|
2016-01-16 16:23:26 -08:00
|
|
|
[ExprNode(BinExp(ref plus, box Variable(ref a), box Variable(ref b)))],
|
2016-01-16 16:22:00 -08:00
|
|
|
plus == "+" && a == "a" && b == "b");
|
2016-01-16 16:26:52 -08:00
|
|
|
parsetest!("a + b * c",
|
|
|
|
[ExprNode(BinExp(ref plus, box Variable(ref a), box BinExp(ref mul, box Variable(ref b), box Variable(ref c))))],
|
|
|
|
plus == "+" && mul == "*" && a == "a" && b == "b" && c == "c");
|
|
|
|
parsetest!("a * b + c",
|
|
|
|
[ExprNode(BinExp(ref plus, box BinExp(ref mul, box Variable(ref a), box Variable(ref b)), box Variable(ref c)))],
|
|
|
|
plus == "+" && mul == "*" && a == "a" && b == "b" && c == "c");
|
2016-01-16 17:00:54 -08:00
|
|
|
parsetest!("(a + b) * c",
|
|
|
|
[ExprNode(BinExp(ref mul, box BinExp(ref plus, box Variable(ref a), box Variable(ref b)), box Variable(ref c)))],
|
|
|
|
plus == "+" && mul == "*" && a == "a" && b == "b" && c == "c");
|
2016-01-16 15:14:20 -08:00
|
|
|
}
|
|
|
|
}
|