2016-01-22 02:55:07 -08:00
|
|
|
use std::fmt;
|
2017-01-23 03:03:12 -08:00
|
|
|
use schala_lang::tokenizer::{Token, Kw, OpTok};
|
|
|
|
use schala_lang::tokenizer::Token::*;
|
2016-12-31 03:35:46 -08:00
|
|
|
use std::collections::VecDeque;
|
2017-01-04 04:18:55 -08:00
|
|
|
use std::rc::Rc;
|
2017-01-06 05:18:52 -08:00
|
|
|
use std::convert::From;
|
2015-12-24 22:01:59 -08:00
|
|
|
|
2016-12-29 02:04:03 -08:00
|
|
|
// Grammar
|
|
|
|
// program := (statement delimiter ?)*
|
|
|
|
// delimiter := Newline | Semicolon
|
|
|
|
// statement := declaration | expression
|
2017-01-11 20:18:17 -08:00
|
|
|
// declaration := FN prototype LCurlyBrace (statement)* RCurlyBrace
|
2016-12-29 02:04:03 -08:00
|
|
|
// prototype := identifier LParen identlist RParen
|
|
|
|
// identlist := Ident (Comma Ident)* | e
|
|
|
|
// exprlist := Expression (Comma Expression)* | e
|
|
|
|
//
|
|
|
|
// expression := primary_expression (op primary_expression)*
|
2017-01-19 19:44:43 -08:00
|
|
|
// primary_expression := number_expr | String | identifier_expr | paren_expr | conditional_expr |
|
2017-01-11 20:18:17 -08:00
|
|
|
// while_expr | lambda_expr
|
2017-01-19 19:44:43 -08:00
|
|
|
// number_expr := (PLUS | MINUS ) number_expr | Number
|
2016-12-29 02:04:03 -08:00
|
|
|
// identifier_expr := call_expression | Variable
|
2017-01-12 02:58:36 -08:00
|
|
|
// call_expr := Identifier LParen exprlist RParen
|
2017-01-09 20:33:08 -08:00
|
|
|
// while_expr := WHILE primary_expression LCurlyBrace (expression delimiter)* RCurlyBrace
|
2016-12-29 02:04:03 -08:00
|
|
|
// paren_expr := LParen expression RParen
|
2017-01-09 20:33:08 -08:00
|
|
|
// conditional_expr := IF expression LCurlyBrace (expression delimiter)* RCurlyBrace (LCurlyBrace (expresion delimiter)* RCurlyBrace)?
|
2017-01-11 20:18:17 -08:00
|
|
|
// lambda_expr := FN LParen identlist RParen LCurlyBrace (expression delimiter)* RCurlyBrace
|
2017-01-12 02:58:36 -08:00
|
|
|
// lambda_call
|
|
|
|
// lambda_call := ε | LParen exprlist RParen
|
2016-12-29 02:04:03 -08:00
|
|
|
// op := '+', '-', etc.
|
|
|
|
//
|
2016-01-12 03:29:28 -08:00
|
|
|
|
2017-01-03 19:10:48 -08:00
|
|
|
pub type AST = Vec<Statement>;
|
|
|
|
|
2016-01-10 01:15:34 -08:00
|
|
|
#[derive(Debug, Clone)]
|
2017-01-03 02:45:36 -08:00
|
|
|
pub enum Statement {
|
2016-01-10 01:15:34 -08:00
|
|
|
ExprNode(Expression),
|
2017-01-02 18:41:46 -08:00
|
|
|
FuncDefNode(Function),
|
2016-01-10 01:15:34 -08:00
|
|
|
}
|
|
|
|
|
2017-01-03 19:10:48 -08:00
|
|
|
impl fmt::Display for Statement {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
use self::Statement::*;
|
|
|
|
match *self {
|
|
|
|
ExprNode(ref expr) => write!(f, "{}", expr),
|
|
|
|
FuncDefNode(_) => write!(f, "UNIMPLEMENTED"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-10 01:15:34 -08:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Function {
|
|
|
|
pub prototype: Prototype,
|
2017-01-03 02:45:36 -08:00
|
|
|
pub body: Vec<Statement>,
|
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 {
|
2017-01-04 04:18:55 -08:00
|
|
|
pub name: Rc<String>,
|
|
|
|
pub parameters: Vec<Rc<String>>,
|
2016-01-10 01:15:34 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum Expression {
|
2016-01-21 19:10:21 -08:00
|
|
|
Null,
|
2017-01-04 04:18:55 -08:00
|
|
|
StringLiteral(Rc<String>),
|
2016-01-11 02:32:41 -08:00
|
|
|
Number(f64),
|
2017-01-04 04:18:55 -08:00
|
|
|
Variable(Rc<String>),
|
2017-01-13 20:05:17 -08:00
|
|
|
BinExp(BinOp, Box<Expression>, Box<Expression>),
|
2017-01-12 02:58:36 -08:00
|
|
|
Call(Callable, Vec<Expression>),
|
2016-12-31 03:35:46 -08:00
|
|
|
Conditional(Box<Expression>, Box<Expression>, Option<Box<Expression>>),
|
2017-01-02 18:41:46 -08:00
|
|
|
Lambda(Function),
|
2016-12-31 03:35:46 -08:00
|
|
|
Block(VecDeque<Expression>),
|
2017-01-03 03:19:52 -08:00
|
|
|
While(Box<Expression>, Vec<Expression>),
|
2016-01-10 01:15:34 -08:00
|
|
|
}
|
|
|
|
|
2017-01-12 02:58:36 -08:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub enum Callable {
|
|
|
|
NamedFunction(Rc<String>),
|
|
|
|
Lambda(Function),
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:55:07 -08:00
|
|
|
impl fmt::Display for Expression {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
use self::Expression::*;
|
2017-01-02 22:17:21 -08:00
|
|
|
match *self {
|
|
|
|
Null => write!(f, "null"),
|
|
|
|
StringLiteral(ref s) => write!(f, "\"{}\"", s),
|
|
|
|
Number(n) => write!(f, "{}", n),
|
|
|
|
Lambda(Function { prototype: Prototype { ref name, ref parameters, .. }, .. }) => {
|
2017-01-02 22:15:14 -08:00
|
|
|
write!(f, "«function: {}, {} arg(s)»", name, parameters.len())
|
|
|
|
}
|
2016-01-22 02:55:07 -08:00
|
|
|
_ => write!(f, "UNIMPLEMENTED"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-06 05:18:52 -08:00
|
|
|
#[derive(Debug, Clone)]
|
2017-01-13 20:05:17 -08:00
|
|
|
pub enum BinOp {
|
2017-01-06 05:18:52 -08:00
|
|
|
Add,
|
|
|
|
AddAssign,
|
|
|
|
Sub,
|
|
|
|
SubAssign,
|
|
|
|
Mul,
|
|
|
|
MulAssign,
|
|
|
|
Div,
|
|
|
|
DivAssign,
|
|
|
|
Mod,
|
|
|
|
Less,
|
|
|
|
LessEq,
|
|
|
|
Greater,
|
|
|
|
GreaterEq,
|
|
|
|
Equal,
|
|
|
|
Assign,
|
|
|
|
Custom(String),
|
|
|
|
}
|
|
|
|
|
2017-01-13 20:05:17 -08:00
|
|
|
impl From<OpTok> for BinOp {
|
|
|
|
fn from(token: OpTok) -> BinOp {
|
|
|
|
use self::BinOp::*;
|
|
|
|
match &token.0[..] {
|
2017-01-06 05:18:52 -08:00
|
|
|
"+" => Add,
|
|
|
|
"+=" => AddAssign,
|
|
|
|
"-" => Sub,
|
|
|
|
"-=" => SubAssign,
|
|
|
|
"*" => Mul,
|
|
|
|
"*=" => MulAssign,
|
|
|
|
"/" => Div,
|
|
|
|
"/=" => DivAssign,
|
|
|
|
"%" => Mod,
|
|
|
|
"<" => Less,
|
|
|
|
"<=" => LessEq,
|
|
|
|
">" => Greater,
|
|
|
|
">=" => GreaterEq,
|
|
|
|
"==" => Equal,
|
|
|
|
"=" => Assign,
|
2017-01-13 20:05:17 -08:00
|
|
|
op => Custom(op.to_string()),
|
2017-01-06 05:18:52 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-15 03:27:24 -08:00
|
|
|
type Precedence = u8;
|
|
|
|
|
2016-12-29 02:04:03 -08:00
|
|
|
// TODO make this support incomplete parses
|
2016-01-10 01:15:34 -08:00
|
|
|
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 {
|
2016-12-29 02:04:03 -08:00
|
|
|
pub msg: String,
|
2016-12-31 17:02:49 -08:00
|
|
|
pub remaining_tokens: Vec<Token>,
|
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> {
|
2017-01-01 19:45:27 -08:00
|
|
|
Err(ParseError {
|
|
|
|
msg: msg.to_string(),
|
|
|
|
remaining_tokens: vec![],
|
|
|
|
})
|
2016-01-10 01:15:34 -08:00
|
|
|
}
|
|
|
|
}
|
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-12-29 02:04:03 -08:00
|
|
|
fn next(&mut self) -> Option<Token> {
|
2016-01-12 03:26:28 -08:00
|
|
|
self.tokens.pop()
|
2016-01-12 01:58:12 -08:00
|
|
|
}
|
2016-01-15 03:27:24 -08:00
|
|
|
|
2017-01-06 04:56:00 -08:00
|
|
|
fn get_precedence(&self, op: &OpTok) -> Precedence {
|
2017-01-01 18:29:09 -08:00
|
|
|
match &op.0[..] {
|
2016-01-15 03:27:24 -08:00
|
|
|
"+" => 10,
|
|
|
|
"-" => 10,
|
|
|
|
"*" => 20,
|
|
|
|
"/" => 20,
|
|
|
|
"%" => 20,
|
2017-01-06 00:58:47 -08:00
|
|
|
"==" => 40,
|
2017-01-05 18:10:57 -08:00
|
|
|
"=" | "+=" | "-=" | "*=" | "/=" => 1,
|
2017-01-03 19:10:48 -08:00
|
|
|
">" | ">=" | "<" | "<=" => 30,
|
2016-01-15 03:27:24 -08:00
|
|
|
_ => 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) => {
|
2017-01-04 19:30:44 -08:00
|
|
|
let err = format!("Expected `{:?}` but got `{:?}`", stringify!($token), x);
|
2016-01-17 00:59:31 -08:00
|
|
|
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},
|
2017-01-04 19:30:44 -08:00
|
|
|
Some(x) => return ParseError::result_from_str(&format!("Expected identifier, but got {:?}", x)),
|
|
|
|
None => return ParseError::result_from_str("Expected identifier, but got end of input"),
|
2016-01-16 20:23:43 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-09 20:41:42 -08:00
|
|
|
macro_rules! skip_whitespace {
|
|
|
|
($_self: expr) => {
|
|
|
|
loop {
|
|
|
|
match $_self.peek() {
|
|
|
|
Some(ref t) if is_delimiter(t) => {
|
|
|
|
$_self.next();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
_ => break,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-05 02:20:23 -08:00
|
|
|
macro_rules! delimiter_block {
|
2017-01-09 20:41:42 -08:00
|
|
|
($_self: expr, $try_parse: ident, $($break_pattern: pat)|+) => {
|
2017-01-05 02:20:23 -08:00
|
|
|
{
|
|
|
|
let mut acc = Vec::new();
|
|
|
|
loop {
|
|
|
|
match $_self.peek() {
|
|
|
|
None => break,
|
|
|
|
Some(ref t) if is_delimiter(t) => { $_self.next(); continue; },
|
2017-01-05 02:51:41 -08:00
|
|
|
$($break_pattern)|+ => break,
|
2017-01-05 02:20:23 -08:00
|
|
|
_ => {
|
|
|
|
let a = try!($_self.$try_parse());
|
|
|
|
acc.push(a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
acc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-12 04:04:14 -08:00
|
|
|
fn is_delimiter(token: &Token) -> bool {
|
|
|
|
match *token {
|
|
|
|
Newline | Semicolon => true,
|
2016-12-29 02:04:03 -08:00
|
|
|
_ => false,
|
2016-01-12 04:04:14 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2017-01-03 02:45:36 -08:00
|
|
|
let result: ParseResult<Statement> = match self.peek() {
|
2017-01-04 19:32:06 -08:00
|
|
|
Some(ref t) if is_delimiter(t) => {
|
2016-12-29 02:04:03 -08:00
|
|
|
self.next();
|
|
|
|
continue;
|
|
|
|
}
|
2016-01-17 00:50:23 -08:00
|
|
|
Some(_) => self.statement(),
|
|
|
|
None => break,
|
2016-01-12 01:58:12 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
match result {
|
|
|
|
Ok(node) => ast.push(node),
|
2016-12-31 17:02:49 -08:00
|
|
|
Err(mut err) => {
|
|
|
|
err.remaining_tokens = self.tokens.clone();
|
|
|
|
err.remaining_tokens.reverse();
|
2017-01-01 19:45:27 -08:00
|
|
|
return Err(err);
|
2016-12-31 17:02:49 -08:00
|
|
|
}
|
2016-01-12 01:58:12 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(ast)
|
|
|
|
}
|
2016-01-11 02:32:41 -08:00
|
|
|
|
2017-01-03 02:45:36 -08:00
|
|
|
fn statement(&mut self) -> ParseResult<Statement> {
|
|
|
|
let node: Statement = match self.peek() {
|
2016-01-17 00:50:23 -08:00
|
|
|
Some(Keyword(Kw::Fn)) => try!(self.declaration()),
|
2017-01-03 02:45:36 -08:00
|
|
|
Some(_) => Statement::ExprNode(try!(self.expression())),
|
2017-01-04 19:30:44 -08:00
|
|
|
None => panic!("Unexpected end of tokens"),
|
2016-01-12 01:58:12 -08:00
|
|
|
};
|
|
|
|
Ok(node)
|
|
|
|
}
|
2016-01-12 00:58:21 -08:00
|
|
|
|
2017-01-03 02:45:36 -08:00
|
|
|
fn declaration(&mut self) -> ParseResult<Statement> {
|
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());
|
2017-01-09 20:33:08 -08:00
|
|
|
expect!(self, LCurlyBrace);
|
2017-01-05 01:58:22 -08:00
|
|
|
let body = try!(self.body());
|
2017-01-09 20:33:08 -08:00
|
|
|
expect!(self, RCurlyBrace);
|
2017-01-03 02:45:36 -08:00
|
|
|
Ok(Statement::FuncDefNode(Function {
|
2016-12-29 02:04:03 -08:00
|
|
|
prototype: prototype,
|
|
|
|
body: body,
|
|
|
|
}))
|
2016-01-12 01:58:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn prototype(&mut self) -> ParseResult<Prototype> {
|
2017-01-05 01:58:22 -08:00
|
|
|
let name = expect_identifier!(self);
|
2016-01-17 00:59:31 -08:00
|
|
|
expect!(self, LParen);
|
2017-01-05 01:58:22 -08:00
|
|
|
let parameters = try!(self.identlist());
|
2016-01-17 00:59:31 -08:00
|
|
|
expect!(self, RParen);
|
2016-12-29 02:04:03 -08:00
|
|
|
Ok(Prototype {
|
|
|
|
name: name,
|
|
|
|
parameters: parameters,
|
|
|
|
})
|
2016-01-12 01:58:12 -08:00
|
|
|
}
|
|
|
|
|
2017-01-04 04:18:55 -08:00
|
|
|
fn identlist(&mut self) -> ParseResult<Vec<Rc<String>>> {
|
2017-01-05 01:58:22 -08:00
|
|
|
let mut args = Vec::new();
|
2016-01-17 01:17:54 -08:00
|
|
|
while let Some(Identifier(name)) = self.peek() {
|
2017-01-04 04:18:55 -08:00
|
|
|
args.push(name.clone());
|
2016-01-17 01:17:54 -08:00
|
|
|
self.next();
|
2017-01-05 01:58:22 -08:00
|
|
|
match self.peek() {
|
|
|
|
Some(Comma) => {self.next();},
|
|
|
|
_ => 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>> {
|
2017-01-05 01:58:22 -08:00
|
|
|
let mut exprs = Vec::new();
|
2016-01-16 20:23:43 -08:00
|
|
|
loop {
|
|
|
|
if let Some(RParen) = self.peek() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
let exp = try!(self.expression());
|
2017-01-05 01:58:22 -08:00
|
|
|
exprs.push(exp);
|
|
|
|
match self.peek() {
|
|
|
|
Some(Comma) => {self.next();},
|
|
|
|
_ => break,
|
2016-01-16 20:23:43 -08:00
|
|
|
}
|
|
|
|
}
|
2017-01-05 01:58:22 -08:00
|
|
|
Ok(exprs)
|
2016-01-16 20:23:43 -08:00
|
|
|
}
|
|
|
|
|
2017-01-03 02:45:36 -08:00
|
|
|
fn body(&mut self) -> ParseResult<Vec<Statement>> {
|
2017-01-05 02:20:23 -08:00
|
|
|
let statements = delimiter_block!(
|
|
|
|
self,
|
|
|
|
statement,
|
2017-01-09 20:33:08 -08:00
|
|
|
Some(RCurlyBrace)
|
2017-01-05 02:20:23 -08:00
|
|
|
);
|
2017-01-03 02:11:59 -08:00
|
|
|
Ok(statements)
|
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-12-29 02:04:03 -08:00
|
|
|
fn precedence_expr(&mut self,
|
|
|
|
mut lhs: Expression,
|
|
|
|
min_precedence: u8)
|
|
|
|
-> ParseResult<Expression> {
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-13 20:05:17 -08:00
|
|
|
lhs = Expression::BinExp(op.into(), Box::new(lhs), Box::new(rhs));
|
2016-01-16 15:14:20 -08:00
|
|
|
}
|
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> {
|
2016-01-17 00:50:23 -08:00
|
|
|
Ok(match self.peek() {
|
2016-12-29 02:04:03 -08:00
|
|
|
Some(Keyword(Kw::Null)) => {
|
|
|
|
self.next();
|
|
|
|
Expression::Null
|
|
|
|
}
|
2017-01-19 19:44:43 -08:00
|
|
|
Some(NumLiteral(_)) => try!(self.number_expression()),
|
|
|
|
Some(Operator(OpTok(ref a))) if **a == "+" || **a == "-" => try!(self.number_expression()),
|
2016-12-29 02:04:03 -08:00
|
|
|
Some(StrLiteral(s)) => {
|
|
|
|
self.next();
|
|
|
|
Expression::StringLiteral(s)
|
|
|
|
}
|
2016-12-30 18:23:28 -08:00
|
|
|
Some(Keyword(Kw::If)) => try!(self.conditional_expr()),
|
2017-01-03 03:19:52 -08:00
|
|
|
Some(Keyword(Kw::While)) => try!(self.while_expr()),
|
2016-12-29 02:04:03 -08:00
|
|
|
Some(Identifier(_)) => try!(self.identifier_expr()),
|
|
|
|
Some(Token::LParen) => try!(self.paren_expr()),
|
2017-01-11 20:18:17 -08:00
|
|
|
Some(Keyword(Kw::Fn)) => try!(self.lambda_expr()),
|
2016-12-30 18:23:28 -08:00
|
|
|
Some(e) => {
|
2017-01-01 19:45:27 -08:00
|
|
|
return ParseError::result_from_str(&format!("Expected primary expression, got \
|
|
|
|
{:?}",
|
|
|
|
e));
|
2016-12-30 18:23:28 -08:00
|
|
|
}
|
2016-12-29 02:04:03 -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
|
|
|
|
2017-01-19 19:44:43 -08:00
|
|
|
fn number_expression(&mut self) -> ParseResult<Expression> {
|
|
|
|
let mut multiplier = 1;
|
|
|
|
loop {
|
|
|
|
match self.peek() {
|
|
|
|
Some(NumLiteral(n)) => {
|
|
|
|
self.next();
|
|
|
|
return Ok(Expression::Number(n * multiplier as f64));
|
|
|
|
}
|
|
|
|
Some(Operator(OpTok(ref a))) if **a == "+" => {
|
|
|
|
self.next();
|
|
|
|
}
|
|
|
|
Some(Operator(OpTok(ref a))) if **a == "-" => {
|
|
|
|
multiplier *= -1;
|
|
|
|
self.next();
|
|
|
|
}
|
|
|
|
Some(e) => {
|
|
|
|
return ParseError::result_from_str(
|
|
|
|
&format!("Expected +, - or number, got {:?}", e));
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
return ParseError::result_from_str(
|
|
|
|
&format!("Expected +, - or number, got EoI"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-11 20:18:17 -08:00
|
|
|
fn lambda_expr(&mut self) -> ParseResult<Expression> {
|
|
|
|
use self::Expression::*;
|
|
|
|
expect!(self, Keyword(Kw::Fn));
|
|
|
|
skip_whitespace!(self);
|
|
|
|
expect!(self, LParen);
|
|
|
|
let parameters = try!(self.identlist());
|
|
|
|
expect!(self, RParen);
|
|
|
|
skip_whitespace!(self);
|
|
|
|
expect!(self, LCurlyBrace);
|
|
|
|
let body = try!(self.body());
|
|
|
|
expect!(self, RCurlyBrace);
|
|
|
|
|
|
|
|
let prototype = Prototype {
|
|
|
|
name: Rc::new("a lambda yo!".to_string()),
|
|
|
|
parameters: parameters,
|
|
|
|
};
|
|
|
|
|
|
|
|
let function = Function {
|
|
|
|
prototype: prototype,
|
|
|
|
body: body,
|
|
|
|
};
|
2017-01-12 02:58:36 -08:00
|
|
|
|
|
|
|
match self.peek() {
|
|
|
|
Some(LParen) => {
|
|
|
|
let args = try!(self.call_expr());
|
|
|
|
Ok(Call(Callable::Lambda(function), args))
|
|
|
|
},
|
|
|
|
_ => Ok(Lambda(function))
|
|
|
|
}
|
2017-01-11 20:18:17 -08:00
|
|
|
}
|
|
|
|
|
2017-01-03 03:19:52 -08:00
|
|
|
fn while_expr(&mut self) -> ParseResult<Expression> {
|
|
|
|
use self::Expression::*;
|
|
|
|
expect!(self, Keyword(Kw::While));
|
|
|
|
let test = try!(self.expression());
|
2017-01-09 20:33:08 -08:00
|
|
|
expect!(self, LCurlyBrace);
|
2017-01-05 02:53:41 -08:00
|
|
|
let body = delimiter_block!(
|
|
|
|
self,
|
|
|
|
expression,
|
2017-01-09 20:33:08 -08:00
|
|
|
Some(RCurlyBrace)
|
2017-01-05 02:53:41 -08:00
|
|
|
);
|
2017-01-09 20:33:08 -08:00
|
|
|
expect!(self, RCurlyBrace);
|
2017-01-03 03:19:52 -08:00
|
|
|
Ok(While(Box::new(test), body))
|
|
|
|
}
|
|
|
|
|
2016-12-30 18:23:28 -08:00
|
|
|
fn conditional_expr(&mut self) -> ParseResult<Expression> {
|
2016-12-31 03:35:46 -08:00
|
|
|
use self::Expression::*;
|
2016-12-30 18:23:28 -08:00
|
|
|
expect!(self, Keyword(Kw::If));
|
|
|
|
let test = try!(self.expression());
|
2017-01-09 20:41:42 -08:00
|
|
|
skip_whitespace!(self);
|
2017-01-09 20:33:08 -08:00
|
|
|
expect!(self, LCurlyBrace);
|
2017-01-09 20:41:42 -08:00
|
|
|
skip_whitespace!(self);
|
2017-01-05 02:51:41 -08:00
|
|
|
let then_block = delimiter_block!(
|
|
|
|
self,
|
|
|
|
expression,
|
2017-01-09 20:33:08 -08:00
|
|
|
Some(RCurlyBrace)
|
2017-01-05 02:51:41 -08:00
|
|
|
);
|
2017-01-09 20:33:08 -08:00
|
|
|
expect!(self, RCurlyBrace);
|
2017-01-09 20:41:42 -08:00
|
|
|
skip_whitespace!(self);
|
2016-12-30 18:23:28 -08:00
|
|
|
let else_block = if let Some(Keyword(Kw::Else)) = self.peek() {
|
|
|
|
self.next();
|
2017-01-09 20:41:42 -08:00
|
|
|
skip_whitespace!(self);
|
2017-01-09 20:33:08 -08:00
|
|
|
expect!(self, LCurlyBrace);
|
2017-01-05 02:51:41 -08:00
|
|
|
let else_exprs = delimiter_block!(
|
|
|
|
self,
|
|
|
|
expression,
|
2017-01-09 20:33:08 -08:00
|
|
|
Some(RCurlyBrace)
|
2017-01-05 02:51:41 -08:00
|
|
|
);
|
2016-12-30 18:27:34 -08:00
|
|
|
Some(else_exprs)
|
2016-12-30 18:23:28 -08:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2017-01-09 20:33:08 -08:00
|
|
|
expect!(self, RCurlyBrace);
|
2017-01-01 19:45:27 -08:00
|
|
|
Ok(Conditional(Box::new(test),
|
2017-01-05 02:51:41 -08:00
|
|
|
Box::new(Block(VecDeque::from(then_block))),
|
|
|
|
else_block.map(|list| Box::new(Block(VecDeque::from(list))))))
|
2016-12-30 18:23:28 -08:00
|
|
|
}
|
|
|
|
|
2016-01-16 20:23:43 -08:00
|
|
|
fn identifier_expr(&mut self) -> ParseResult<Expression> {
|
|
|
|
let name = expect_identifier!(self);
|
|
|
|
let expr = match self.peek() {
|
|
|
|
Some(LParen) => {
|
|
|
|
let args = try!(self.call_expr());
|
2017-01-12 02:58:36 -08:00
|
|
|
Expression::Call(Callable::NamedFunction(name), args)
|
2016-12-29 02:04:03 -08:00
|
|
|
}
|
|
|
|
__ => Expression::Variable(name),
|
2016-01-16 20:23:43 -08:00
|
|
|
};
|
|
|
|
Ok(expr)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call_expr(&mut self) -> ParseResult<Vec<Expression>> {
|
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
|
|
|
}
|
|
|
|
|
2017-01-03 02:45:36 -08:00
|
|
|
pub fn parse(tokens: &[Token], _parsed_tree: &[Statement]) -> ParseResult<AST> {
|
2016-01-12 01:58:12 -08:00
|
|
|
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::*;
|
2017-01-03 02:45:36 -08:00
|
|
|
use super::Statement::*;
|
|
|
|
use super::Expression::*;
|
2016-01-16 15:14:20 -08:00
|
|
|
|
|
|
|
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]
|
2017-01-11 20:18:17 -08:00
|
|
|
fn function_parse_test() {
|
2016-01-17 00:08:46 -08:00
|
|
|
use super::Function;
|
|
|
|
parsetest!(
|
2017-01-09 20:33:08 -08:00
|
|
|
"fn a() { 1 + 2 }",
|
2017-01-02 18:41:46 -08:00
|
|
|
&[FuncDefNode(Function {prototype: Prototype { ref name, ref parameters }, ref body})],
|
2017-01-03 02:11:59 -08:00
|
|
|
match &body[..] { &[ExprNode(BinExp(_, box Number(1.0), box Number(2.0)))] => true, _ => false }
|
2017-01-05 02:36:28 -08:00
|
|
|
&& **name == "a" && match ¶meters[..] { &[] => true, _ => false }
|
2016-01-17 00:08:46 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
parsetest!(
|
2017-01-09 20:33:08 -08:00
|
|
|
"fn a(x,y){ 1 + 2 }",
|
2017-01-02 18:41:46 -08:00
|
|
|
&[FuncDefNode(Function {prototype: Prototype { ref name, ref parameters }, ref body})],
|
2017-01-03 02:11:59 -08:00
|
|
|
match &body[..] { &[ExprNode(BinExp(_, box Number(1.0), box Number(2.0)))] => true, _ => false }
|
2017-01-05 02:36:28 -08:00
|
|
|
&& **name == "a" && *parameters[0] == "x" && *parameters[1] == "y" && parameters.len() == 2
|
2016-01-17 00:08:46 -08:00
|
|
|
);
|
2017-01-11 20:18:17 -08:00
|
|
|
|
|
|
|
let t3 = "fn (x) { x + 2 }";
|
|
|
|
let tokens3 = tokenizer::tokenize(t3).unwrap();
|
|
|
|
assert!(parse(&tokens3, &[]).is_err());
|
2016-01-17 00:08:46 -08:00
|
|
|
}
|
|
|
|
|
2016-01-16 15:14:20 -08:00
|
|
|
#[test]
|
2016-01-16 16:23:26 -08:00
|
|
|
fn expression_parse_test() {
|
2017-01-05 02:36:28 -08:00
|
|
|
parsetest!("a", &[ExprNode(Variable(ref s))], **s == "a");
|
2016-01-16 16:22:00 -08:00
|
|
|
parsetest!("a + b",
|
2017-01-16 01:10:06 -08:00
|
|
|
&[ExprNode(BinExp(BinOp::Add, box Variable(ref a), box Variable(ref b)))],
|
2017-01-13 20:05:17 -08:00
|
|
|
**a == "a" && **b == "b");
|
2016-01-16 16:26:52 -08:00
|
|
|
parsetest!("a + b * c",
|
2017-01-16 01:10:06 -08:00
|
|
|
&[ExprNode(BinExp(BinOp::Add, box Variable(ref a), box BinExp(BinOp::Mul, box Variable(ref b), box Variable(ref c))))],
|
2017-01-13 20:05:17 -08:00
|
|
|
**a == "a" && **b == "b" && **c == "c");
|
2016-01-16 16:26:52 -08:00
|
|
|
parsetest!("a * b + c",
|
2017-01-16 01:10:06 -08:00
|
|
|
&[ExprNode(BinExp(BinOp::Add, box BinExp(BinOp::Mul, box Variable(ref a), box Variable(ref b)), box Variable(ref c)))],
|
2017-01-13 20:05:17 -08:00
|
|
|
**a == "a" && **b == "b" && **c == "c");
|
2016-01-16 17:00:54 -08:00
|
|
|
parsetest!("(a + b) * c",
|
2017-01-16 01:10:06 -08:00
|
|
|
&[ExprNode(BinExp(BinOp::Mul, box BinExp(BinOp::Add, box Variable(ref a), box Variable(ref b)), box Variable(ref c)))],
|
2017-01-13 20:05:17 -08:00
|
|
|
**a == "a" && **b == "b" && **c == "c");
|
2016-01-16 15:14:20 -08:00
|
|
|
}
|
2016-12-31 04:00:30 -08:00
|
|
|
|
|
|
|
#[test]
|
2017-01-11 20:18:17 -08:00
|
|
|
fn lambda_parse_test() {
|
|
|
|
use tokenizer;
|
|
|
|
let t1 = "(fn(x) { x + 2 })";
|
|
|
|
let tokens1 = tokenizer::tokenize(t1).unwrap();
|
|
|
|
match parse(&tokens1, &[]).unwrap()[..] {
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
let t2 = "fn(x) { x + 2 }";
|
|
|
|
let tokens2 = tokenizer::tokenize(t2).unwrap();
|
|
|
|
assert!(parse(&tokens2, &[]).is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-12-31 04:00:30 -08:00
|
|
|
fn conditional_parse_test() {
|
|
|
|
use tokenizer;
|
2017-01-09 20:33:08 -08:00
|
|
|
let t1 = "if null { 20 } else { 40 }";
|
2016-12-31 04:00:30 -08:00
|
|
|
let tokens = tokenizer::tokenize(t1).unwrap();
|
|
|
|
match parse(&tokens, &[]).unwrap()[..] {
|
2017-01-02 16:55:26 -08:00
|
|
|
[ExprNode(Conditional(box Null, box Block(_), Some(box Block(_))))] => (),
|
|
|
|
_ => panic!(),
|
2016-12-31 04:00:30 -08:00
|
|
|
}
|
2017-01-03 01:21:20 -08:00
|
|
|
|
2017-01-09 20:41:42 -08:00
|
|
|
let t2 = r"
|
|
|
|
if null {
|
|
|
|
20
|
|
|
|
} else {
|
|
|
|
40
|
|
|
|
}
|
|
|
|
";
|
2017-01-03 01:21:20 -08:00
|
|
|
let tokens2 = tokenizer::tokenize(t2).unwrap();
|
|
|
|
match parse(&tokens2, &[]).unwrap()[..] {
|
|
|
|
[ExprNode(Conditional(box Null, box Block(_), Some(box Block(_))))] => (),
|
|
|
|
_ => panic!(),
|
|
|
|
}
|
2017-01-09 20:41:42 -08:00
|
|
|
|
|
|
|
let t2 = r"
|
|
|
|
if null {
|
|
|
|
20 } else
|
|
|
|
{
|
|
|
|
40
|
|
|
|
}
|
|
|
|
";
|
|
|
|
let tokens3 = tokenizer::tokenize(t2).unwrap();
|
|
|
|
match parse(&tokens3, &[]).unwrap()[..] {
|
|
|
|
[ExprNode(Conditional(box Null, box Block(_), Some(box Block(_))))] => (),
|
|
|
|
_ => panic!(),
|
|
|
|
}
|
2016-12-31 04:00:30 -08:00
|
|
|
}
|
2016-01-16 15:14:20 -08:00
|
|
|
}
|