2021-06-06 17:09:45 -05:00
|
|
|
//! AbleScript Parser
|
|
|
|
//!
|
|
|
|
//! Type of this parser is recursive descent
|
|
|
|
|
|
|
|
use logos::{Lexer, Logos};
|
|
|
|
|
|
|
|
use crate::ast::*;
|
|
|
|
use crate::error::{Error, ErrorKind};
|
|
|
|
use crate::lexer::Token;
|
|
|
|
use crate::variables::Value;
|
|
|
|
|
|
|
|
/// Parser structure which holds lexer and metadata
|
|
|
|
///
|
|
|
|
/// Make one using [`Parser::new`] function
|
|
|
|
pub struct Parser<'source> {
|
|
|
|
lexer: Lexer<'source, Token>,
|
2021-06-07 15:21:21 -05:00
|
|
|
tdark: bool,
|
2021-06-06 17:09:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'source> Parser<'source> {
|
|
|
|
/// Create a new parser from source code
|
|
|
|
pub fn new(source: &'source str) -> Self {
|
|
|
|
Self {
|
|
|
|
lexer: Token::lexer(source),
|
2021-06-07 15:21:21 -05:00
|
|
|
tdark: false,
|
2021-06-06 17:09:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Start parsing tokens
|
|
|
|
///
|
|
|
|
/// Loops trough lexer, parses statements, returns AST
|
2022-04-01 18:34:25 -05:00
|
|
|
pub fn init(&mut self) -> Result<Block, Error> {
|
2021-06-06 17:09:45 -05:00
|
|
|
let mut ast = vec![];
|
|
|
|
while let Some(token) = self.lexer.next() {
|
2021-06-07 15:21:21 -05:00
|
|
|
match token {
|
|
|
|
// Ignore comments
|
|
|
|
Token::Comment => continue,
|
|
|
|
|
|
|
|
// T-Dark block (replace `lang` with `script`)
|
2022-04-01 18:34:25 -05:00
|
|
|
Token::TDark => ast.extend(self.tdark_flow()?),
|
2021-06-07 15:21:21 -05:00
|
|
|
token => ast.push(self.parse(token)?),
|
|
|
|
}
|
2021-06-06 17:09:45 -05:00
|
|
|
}
|
|
|
|
Ok(ast)
|
|
|
|
}
|
|
|
|
|
2021-07-15 14:39:01 -05:00
|
|
|
/// Get next item
|
|
|
|
///
|
|
|
|
/// If EOF, return Error instead of None
|
|
|
|
fn checked_next(&mut self) -> Result<Token, Error> {
|
2021-12-14 14:10:58 -06:00
|
|
|
loop {
|
|
|
|
match self
|
|
|
|
.lexer
|
|
|
|
.next()
|
|
|
|
.ok_or_else(|| Error::unexpected_eof(self.lexer.span().start))?
|
|
|
|
{
|
|
|
|
Token::Comment => (),
|
|
|
|
token => break Ok(token),
|
|
|
|
}
|
|
|
|
}
|
2021-07-15 14:39:01 -05:00
|
|
|
}
|
|
|
|
|
2021-06-06 17:09:45 -05:00
|
|
|
/// Parse a token
|
|
|
|
///
|
|
|
|
/// This function will route to corresponding flow functions
|
|
|
|
/// which may advance the lexer iterator
|
2022-04-01 18:34:25 -05:00
|
|
|
fn parse(&mut self, token: Token) -> Result<Spanned<Stmt>, Error> {
|
2021-06-06 17:09:45 -05:00
|
|
|
let start = self.lexer.span().start;
|
|
|
|
|
|
|
|
match token {
|
2022-04-01 18:22:46 -05:00
|
|
|
Token::If => Ok(Spanned::new(self.if_flow()?, start..self.lexer.span().end)),
|
|
|
|
Token::Functio => Ok(Spanned::new(
|
2021-06-06 17:09:45 -05:00
|
|
|
self.functio_flow()?,
|
|
|
|
start..self.lexer.span().end,
|
|
|
|
)),
|
2022-04-01 18:22:46 -05:00
|
|
|
Token::Bff => Ok(Spanned::new(self.bff_flow()?, start..self.lexer.span().end)),
|
|
|
|
Token::Var => Ok(Spanned::new(self.var_flow()?, start..self.lexer.span().end)),
|
2022-04-01 18:34:25 -05:00
|
|
|
Token::Melo => Ok(Spanned::new(
|
|
|
|
self.melo_flow()?,
|
|
|
|
start..self.lexer.span().end,
|
|
|
|
)),
|
|
|
|
Token::Loop => Ok(Spanned::new(
|
|
|
|
self.loop_flow()?,
|
|
|
|
start..self.lexer.span().end,
|
|
|
|
)),
|
2022-04-01 18:22:46 -05:00
|
|
|
Token::Break => Ok(Spanned::new(
|
2022-04-01 18:34:25 -05:00
|
|
|
self.semicolon_terminated(Stmt::Break)?,
|
2021-06-07 04:00:06 -05:00
|
|
|
start..self.lexer.span().end,
|
|
|
|
)),
|
2022-04-01 18:22:46 -05:00
|
|
|
Token::HopBack => Ok(Spanned::new(
|
2022-04-01 18:34:25 -05:00
|
|
|
self.semicolon_terminated(Stmt::HopBack)?,
|
2021-06-07 04:00:06 -05:00
|
|
|
start..self.lexer.span().end,
|
|
|
|
)),
|
2022-04-01 18:22:46 -05:00
|
|
|
Token::Rlyeh => Ok(Spanned::new(
|
2022-04-01 18:34:25 -05:00
|
|
|
self.semicolon_terminated(Stmt::Rlyeh)?,
|
2021-06-07 04:07:50 -05:00
|
|
|
start..self.lexer.span().end,
|
|
|
|
)),
|
2022-04-01 18:22:46 -05:00
|
|
|
Token::Rickroll => Ok(Spanned::new(
|
2022-04-01 18:34:25 -05:00
|
|
|
self.semicolon_terminated(Stmt::Rickroll)?,
|
2021-06-16 10:35:06 -05:00
|
|
|
start..self.lexer.span().end,
|
2021-06-13 00:24:09 -05:00
|
|
|
)),
|
2021-06-07 04:00:06 -05:00
|
|
|
|
|
|
|
Token::Identifier(_)
|
|
|
|
| Token::String(_)
|
|
|
|
| Token::Integer(_)
|
2022-04-08 10:39:14 -05:00
|
|
|
| Token::Aint
|
2021-07-27 05:09:36 -05:00
|
|
|
| Token::LeftBracket
|
2022-04-01 18:22:46 -05:00
|
|
|
| Token::LeftParen => Ok(Spanned::new(
|
2021-06-07 04:00:06 -05:00
|
|
|
self.value_flow(token)?,
|
|
|
|
start..self.lexer.span().end,
|
|
|
|
)),
|
|
|
|
|
2021-06-06 17:09:45 -05:00
|
|
|
t => Err(Error {
|
|
|
|
kind: ErrorKind::UnexpectedToken(t),
|
|
|
|
span: start..self.lexer.span().end,
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-07 04:00:06 -05:00
|
|
|
/// Require statement to be semicolon terminated
|
|
|
|
///
|
|
|
|
/// Utility function for short statements
|
2022-04-01 18:34:25 -05:00
|
|
|
fn semicolon_terminated(&mut self, stmt_kind: Stmt) -> Result<Stmt, Error> {
|
2021-06-07 04:00:06 -05:00
|
|
|
self.require(Token::Semicolon)?;
|
|
|
|
Ok(stmt_kind)
|
|
|
|
}
|
|
|
|
|
2021-06-06 17:09:45 -05:00
|
|
|
/// Require next item to be equal with expected one
|
2022-03-19 20:20:55 -05:00
|
|
|
fn require(&mut self, required: Token) -> Result<(), Error> {
|
|
|
|
match self.checked_next()? {
|
|
|
|
t if t == required => Ok(()),
|
|
|
|
t => Err(Error::new(ErrorKind::UnexpectedToken(t), self.lexer.span())),
|
2021-06-06 17:09:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get an Identifier
|
2022-04-01 18:34:25 -05:00
|
|
|
fn get_ident(&mut self) -> Result<Spanned<String>, Error> {
|
2021-07-15 14:39:01 -05:00
|
|
|
match self.checked_next()? {
|
2022-04-01 18:34:25 -05:00
|
|
|
Token::Identifier(ident) => Ok(Spanned::new(
|
|
|
|
if self.tdark {
|
2021-10-04 16:00:18 -05:00
|
|
|
ident.replace("lang", "script")
|
2021-06-07 15:21:21 -05:00
|
|
|
} else {
|
2021-10-04 16:00:18 -05:00
|
|
|
ident
|
2021-06-07 15:21:21 -05:00
|
|
|
},
|
2022-04-01 18:34:25 -05:00
|
|
|
self.lexer.span(),
|
|
|
|
)),
|
2021-06-06 17:09:45 -05:00
|
|
|
t => Err(Error::new(ErrorKind::UnexpectedToken(t), self.lexer.span())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse an expression
|
|
|
|
///
|
|
|
|
/// AbleScript strongly separates expressions from statements.
|
|
|
|
/// Expressions do not have any side effects and the are
|
|
|
|
/// only mathematial and logical operations or values.
|
2022-04-01 18:34:25 -05:00
|
|
|
fn parse_expr(
|
|
|
|
&mut self,
|
|
|
|
token: Token,
|
|
|
|
buf: &mut Option<Spanned<Expr>>,
|
|
|
|
) -> Result<Spanned<Expr>, Error> {
|
2021-06-07 15:58:28 -05:00
|
|
|
let start = match buf {
|
|
|
|
Some(e) => e.span.start,
|
|
|
|
None => self.lexer.span().start,
|
|
|
|
};
|
2021-06-06 17:09:45 -05:00
|
|
|
|
|
|
|
match token {
|
|
|
|
// Values
|
2022-04-01 18:22:46 -05:00
|
|
|
Token::Identifier(i) => Ok(Spanned::new(
|
2022-04-01 18:34:25 -05:00
|
|
|
Expr::Variable(if self.tdark {
|
2021-06-07 15:21:21 -05:00
|
|
|
i.replace("lang", "script")
|
|
|
|
} else {
|
|
|
|
i
|
|
|
|
}),
|
2021-06-06 17:09:45 -05:00
|
|
|
start..self.lexer.span().end,
|
|
|
|
)),
|
2022-04-01 18:22:46 -05:00
|
|
|
Token::Integer(i) => Ok(Spanned::new(
|
2022-04-01 18:34:25 -05:00
|
|
|
Expr::Literal(Value::Int(i)),
|
2021-06-06 17:09:45 -05:00
|
|
|
start..self.lexer.span().end,
|
|
|
|
)),
|
2022-04-01 18:22:46 -05:00
|
|
|
Token::String(s) => Ok(Spanned::new(
|
2022-04-01 18:34:25 -05:00
|
|
|
Expr::Literal(Value::Str(if self.tdark {
|
2021-06-07 15:21:21 -05:00
|
|
|
s.replace("lang", "script")
|
|
|
|
} else {
|
|
|
|
s
|
|
|
|
})),
|
2021-06-06 17:09:45 -05:00
|
|
|
start..self.lexer.span().end,
|
|
|
|
)),
|
2022-03-19 20:20:55 -05:00
|
|
|
|
2021-07-27 05:09:36 -05:00
|
|
|
Token::LeftBracket => match buf.take() {
|
2022-04-01 18:22:46 -05:00
|
|
|
Some(buf) => Ok(Spanned::new(
|
2022-01-22 13:37:44 -06:00
|
|
|
self.index_flow(buf)?,
|
2021-07-27 05:09:36 -05:00
|
|
|
start..self.lexer.span().end,
|
|
|
|
)),
|
2022-04-01 18:34:25 -05:00
|
|
|
None => Ok(Spanned::new(
|
|
|
|
self.cart_flow()?,
|
|
|
|
start..self.lexer.span().end,
|
|
|
|
)),
|
2021-07-27 05:09:36 -05:00
|
|
|
},
|
|
|
|
|
2021-06-06 17:09:45 -05:00
|
|
|
// Operations
|
2021-06-07 02:17:18 -05:00
|
|
|
Token::Plus
|
|
|
|
| Token::Minus
|
|
|
|
| Token::Star
|
|
|
|
| Token::FwdSlash
|
|
|
|
| Token::EqualEqual
|
|
|
|
| Token::NotEqual
|
|
|
|
| Token::LessThan
|
2022-04-01 18:22:46 -05:00
|
|
|
| Token::GreaterThan => Ok(Spanned::new(
|
2021-06-07 04:00:06 -05:00
|
|
|
self.binop_flow(
|
|
|
|
BinOpKind::from_token(token).map_err(|e| Error::new(e, self.lexer.span()))?,
|
2021-06-07 02:17:18 -05:00
|
|
|
buf,
|
|
|
|
)?,
|
2021-06-06 17:09:45 -05:00
|
|
|
start..self.lexer.span().end,
|
|
|
|
)),
|
|
|
|
|
2022-04-08 10:39:14 -05:00
|
|
|
Token::Aint => Ok(Spanned::new(
|
2021-06-06 17:09:45 -05:00
|
|
|
{
|
2021-07-15 14:39:01 -05:00
|
|
|
let next = self.checked_next()?;
|
2022-04-08 10:39:14 -05:00
|
|
|
Expr::Aint(Box::new(self.parse_expr(next, buf)?))
|
2021-06-06 17:09:45 -05:00
|
|
|
},
|
|
|
|
start..self.lexer.span().end,
|
|
|
|
)),
|
2021-07-27 05:09:36 -05:00
|
|
|
|
2021-06-06 17:09:45 -05:00
|
|
|
Token::LeftParen => self.expr_flow(Token::RightParen),
|
2021-06-11 11:31:25 -05:00
|
|
|
t => Err(Error::new(ErrorKind::UnexpectedToken(t), self.lexer.span())),
|
2021-06-06 17:09:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-27 05:14:11 -05:00
|
|
|
/// Flow for creating carts
|
2022-04-01 18:34:25 -05:00
|
|
|
fn cart_flow(&mut self) -> Result<Expr, Error> {
|
2021-07-29 11:17:08 -05:00
|
|
|
let mut cart = vec![];
|
2021-07-29 11:58:11 -05:00
|
|
|
let mut buf = None;
|
|
|
|
|
|
|
|
match self.checked_next()? {
|
2021-07-29 12:17:10 -05:00
|
|
|
Token::RightBracket => (),
|
|
|
|
t => {
|
|
|
|
buf = Some(self.parse_expr(t, &mut buf)?);
|
|
|
|
'cart: loop {
|
|
|
|
let value = loop {
|
|
|
|
match self.checked_next()? {
|
|
|
|
Token::Arrow => break buf.take(),
|
|
|
|
t => buf = Some(self.parse_expr(t, &mut buf)?),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.ok_or_else(|| {
|
|
|
|
Error::new(ErrorKind::UnexpectedToken(Token::Arrow), self.lexer.span())
|
|
|
|
})?;
|
2021-07-29 11:58:11 -05:00
|
|
|
|
2021-07-29 12:17:10 -05:00
|
|
|
let key = loop {
|
|
|
|
match self.checked_next()? {
|
|
|
|
Token::RightBracket => {
|
|
|
|
cart.push((
|
|
|
|
value,
|
|
|
|
buf.take().ok_or_else(|| {
|
|
|
|
Error::unexpected_eof(self.lexer.span().start)
|
|
|
|
})?,
|
|
|
|
));
|
2021-07-29 11:58:11 -05:00
|
|
|
|
2021-07-29 12:17:10 -05:00
|
|
|
break 'cart;
|
|
|
|
}
|
|
|
|
Token::Comma => break buf.take(),
|
|
|
|
t => buf = Some(self.parse_expr(t, &mut buf)?),
|
|
|
|
}
|
2021-07-29 11:58:11 -05:00
|
|
|
}
|
2021-07-29 12:17:10 -05:00
|
|
|
.ok_or_else(|| Error::unexpected_eof(self.lexer.span().start))?;
|
|
|
|
|
|
|
|
cart.push((value, key));
|
2021-07-29 11:58:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-29 11:17:08 -05:00
|
|
|
|
2022-04-01 18:34:25 -05:00
|
|
|
Ok(Expr::Cart(cart))
|
2021-07-27 05:14:11 -05:00
|
|
|
}
|
|
|
|
|
2022-01-22 13:37:44 -06:00
|
|
|
/// Flow for indexing operations
|
|
|
|
///
|
|
|
|
/// Indexing with empty index resolves to length of expression, else it indexes
|
2022-04-01 18:34:25 -05:00
|
|
|
fn index_flow(&mut self, expr: Spanned<Expr>) -> Result<Expr, Error> {
|
2022-01-22 13:37:44 -06:00
|
|
|
let mut buf = None;
|
|
|
|
Ok(loop {
|
|
|
|
match self.checked_next()? {
|
|
|
|
Token::RightBracket => match buf {
|
|
|
|
Some(index) => {
|
2022-04-01 18:34:25 -05:00
|
|
|
break Expr::Index {
|
2022-01-22 13:37:44 -06:00
|
|
|
expr: Box::new(expr),
|
|
|
|
index: Box::new(index),
|
|
|
|
}
|
|
|
|
}
|
2022-04-01 18:34:25 -05:00
|
|
|
None => break Expr::Len(Box::new(expr)),
|
2022-01-22 13:37:44 -06:00
|
|
|
},
|
|
|
|
token => buf = Some(self.parse_expr(token, &mut buf)?),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-06 17:09:45 -05:00
|
|
|
/// Flow for operators
|
|
|
|
///
|
|
|
|
/// Generates operation from LHS buffer and next expression as RHS
|
|
|
|
///
|
|
|
|
/// This is unaware of precedence, as AbleScript do not have it
|
2022-04-01 18:34:25 -05:00
|
|
|
fn binop_flow(
|
|
|
|
&mut self,
|
|
|
|
kind: BinOpKind,
|
|
|
|
lhs: &mut Option<Spanned<Expr>>,
|
|
|
|
) -> Result<Expr, Error> {
|
|
|
|
Ok(Expr::BinOp {
|
2021-06-06 17:09:45 -05:00
|
|
|
lhs: Box::new(
|
|
|
|
lhs.take()
|
2021-06-16 10:35:06 -05:00
|
|
|
.ok_or_else(|| Error::new(ErrorKind::MissingLhs, self.lexer.span()))?,
|
2021-06-06 17:09:45 -05:00
|
|
|
),
|
|
|
|
rhs: {
|
2021-06-07 20:03:26 -05:00
|
|
|
let next = self
|
|
|
|
.lexer
|
|
|
|
.next()
|
2021-06-16 10:35:06 -05:00
|
|
|
.ok_or_else(|| Error::unexpected_eof(self.lexer.span().start))?;
|
2021-06-06 17:09:45 -05:00
|
|
|
Box::new(self.parse_expr(next, &mut None)?)
|
|
|
|
},
|
|
|
|
kind,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse expressions until terminate token
|
2022-04-01 18:34:25 -05:00
|
|
|
fn expr_flow(&mut self, terminate: Token) -> Result<Spanned<Expr>, Error> {
|
2021-06-06 17:09:45 -05:00
|
|
|
let mut buf = None;
|
|
|
|
Ok(loop {
|
2021-07-15 14:39:01 -05:00
|
|
|
match self.checked_next()? {
|
2021-06-07 04:07:50 -05:00
|
|
|
t if t == terminate => {
|
2021-06-16 10:35:06 -05:00
|
|
|
break buf.take().ok_or_else(|| {
|
|
|
|
Error::new(ErrorKind::UnexpectedToken(t), self.lexer.span())
|
|
|
|
})?
|
2021-06-07 04:07:50 -05:00
|
|
|
}
|
2021-06-06 17:09:45 -05:00
|
|
|
t => buf = Some(self.parse_expr(t, &mut buf)?),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse a list of statements between curly braces
|
2021-06-07 04:00:06 -05:00
|
|
|
fn get_block(&mut self) -> Result<Block, Error> {
|
2021-06-06 17:09:45 -05:00
|
|
|
self.require(Token::LeftCurly)?;
|
|
|
|
let mut block = vec![];
|
|
|
|
|
|
|
|
loop {
|
2021-07-15 14:39:01 -05:00
|
|
|
match self.checked_next()? {
|
2021-06-06 17:09:45 -05:00
|
|
|
Token::RightCurly => break,
|
2022-04-01 18:34:25 -05:00
|
|
|
Token::TDark => block.extend(self.tdark_flow()?),
|
2021-06-06 17:09:45 -05:00
|
|
|
t => block.push(self.parse(t)?),
|
|
|
|
}
|
|
|
|
}
|
2022-04-01 18:34:25 -05:00
|
|
|
Ok(block)
|
2021-06-06 17:09:45 -05:00
|
|
|
}
|
|
|
|
|
2021-12-14 16:16:19 -06:00
|
|
|
/// Parse T-Dark block
|
|
|
|
fn tdark_flow(&mut self) -> Result<Block, Error> {
|
|
|
|
self.tdark = true;
|
|
|
|
let block = self.get_block();
|
|
|
|
self.tdark = false;
|
|
|
|
block
|
|
|
|
}
|
|
|
|
|
2021-06-07 04:00:06 -05:00
|
|
|
/// If Statement parser gets any kind of value (Identifier or Literal)
|
|
|
|
/// It cannot parse it as it do not parse expressions. Instead of it it
|
|
|
|
/// will parse it to function call or print statement.
|
2022-04-01 18:34:25 -05:00
|
|
|
fn value_flow(&mut self, init: Token) -> Result<Stmt, Error> {
|
2021-06-07 04:00:06 -05:00
|
|
|
let mut buf = Some(self.parse_expr(init, &mut None)?);
|
|
|
|
let r = loop {
|
2021-07-15 14:39:01 -05:00
|
|
|
match self.checked_next()? {
|
2021-06-11 09:59:40 -05:00
|
|
|
// Print to stdout
|
2021-06-07 04:07:50 -05:00
|
|
|
Token::Print => {
|
2022-04-01 18:34:25 -05:00
|
|
|
let stmt = Stmt::Print(buf.take().ok_or_else(|| {
|
2021-06-16 10:35:06 -05:00
|
|
|
Error::new(ErrorKind::UnexpectedToken(Token::Print), self.lexer.span())
|
|
|
|
})?);
|
2021-10-23 16:20:45 -05:00
|
|
|
break self.semicolon_terminated(stmt)?;
|
2021-06-07 04:07:50 -05:00
|
|
|
}
|
2021-06-11 09:59:40 -05:00
|
|
|
|
|
|
|
// Functio call
|
2021-06-07 04:00:06 -05:00
|
|
|
Token::LeftParen => {
|
2021-08-01 11:36:09 -05:00
|
|
|
break self.functio_call_flow(buf.take().ok_or_else(|| {
|
|
|
|
Error::new(
|
|
|
|
ErrorKind::UnexpectedToken(Token::LeftParen),
|
|
|
|
self.lexer.span(),
|
|
|
|
)
|
|
|
|
})?)?;
|
2021-06-07 04:00:06 -05:00
|
|
|
}
|
2021-06-11 09:59:40 -05:00
|
|
|
|
|
|
|
// Variable Assignment
|
|
|
|
Token::Equal => {
|
2021-10-21 13:51:24 -05:00
|
|
|
if let Some(Ok(assignable)) = buf.take().map(Assignable::from_expr) {
|
2022-04-01 18:34:25 -05:00
|
|
|
break Stmt::Assign {
|
2021-10-12 15:14:20 -05:00
|
|
|
assignable,
|
2021-06-11 09:59:40 -05:00
|
|
|
value: self.expr_flow(Token::Semicolon)?,
|
|
|
|
};
|
2021-10-12 15:14:20 -05:00
|
|
|
} else {
|
|
|
|
return Err(Error::new(
|
|
|
|
ErrorKind::UnexpectedToken(Token::Equal),
|
|
|
|
self.lexer.span(),
|
|
|
|
));
|
2021-06-11 09:59:40 -05:00
|
|
|
}
|
|
|
|
}
|
2021-06-18 13:28:53 -05:00
|
|
|
|
|
|
|
// Read input
|
|
|
|
Token::Read => {
|
2021-10-23 14:53:21 -05:00
|
|
|
if let Some(Ok(assignable)) = buf.take().map(Assignable::from_expr) {
|
|
|
|
self.require(Token::Semicolon)?;
|
2022-04-01 18:34:25 -05:00
|
|
|
break Stmt::Read(assignable);
|
2021-10-23 14:53:21 -05:00
|
|
|
} else {
|
|
|
|
return Err(Error::new(
|
|
|
|
ErrorKind::UnexpectedToken(Token::Read),
|
|
|
|
self.lexer.span(),
|
|
|
|
));
|
2021-06-18 13:28:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-07 04:00:06 -05:00
|
|
|
t => buf = Some(self.parse_expr(t, &mut buf)?),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(r)
|
|
|
|
}
|
|
|
|
|
2021-06-06 17:09:45 -05:00
|
|
|
/// Parse If flow
|
|
|
|
///
|
|
|
|
/// Consists of condition and block, there is no else
|
2022-04-01 18:34:25 -05:00
|
|
|
fn if_flow(&mut self) -> Result<Stmt, Error> {
|
2021-06-06 17:09:45 -05:00
|
|
|
self.require(Token::LeftParen)?;
|
|
|
|
|
|
|
|
let cond = self.expr_flow(Token::RightParen)?;
|
2021-06-07 02:17:18 -05:00
|
|
|
|
2021-06-07 04:00:06 -05:00
|
|
|
let body = self.get_block()?;
|
2021-06-06 17:09:45 -05:00
|
|
|
|
2022-04-01 18:34:25 -05:00
|
|
|
Ok(Stmt::If { cond, body })
|
2021-06-06 17:09:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse functio flow
|
|
|
|
///
|
2021-10-04 16:00:18 -05:00
|
|
|
/// functio $ident (a, b, c) { ... }
|
2022-04-01 18:34:25 -05:00
|
|
|
fn functio_flow(&mut self) -> Result<Stmt, Error> {
|
2021-10-04 16:00:18 -05:00
|
|
|
let ident = self.get_ident()?;
|
2021-06-06 17:09:45 -05:00
|
|
|
|
|
|
|
self.require(Token::LeftParen)?;
|
|
|
|
|
2021-06-12 22:07:58 -05:00
|
|
|
let mut params = vec![];
|
2021-06-06 17:09:45 -05:00
|
|
|
loop {
|
2021-07-15 14:39:01 -05:00
|
|
|
match self.checked_next()? {
|
2021-06-06 17:09:45 -05:00
|
|
|
Token::RightParen => break,
|
|
|
|
Token::Identifier(i) => {
|
2022-04-01 18:34:25 -05:00
|
|
|
params.push(Spanned::new(i, self.lexer.span()));
|
2021-06-06 17:09:45 -05:00
|
|
|
|
2021-06-07 04:07:50 -05:00
|
|
|
// Require comma (next) or right paren (end) after identifier
|
2021-07-15 14:39:01 -05:00
|
|
|
match self.checked_next()? {
|
2021-06-06 17:09:45 -05:00
|
|
|
Token::Comma => continue,
|
|
|
|
Token::RightParen => break,
|
|
|
|
t => {
|
|
|
|
return Err(Error::new(
|
|
|
|
ErrorKind::UnexpectedToken(t),
|
|
|
|
self.lexer.span(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t => return Err(Error::new(ErrorKind::UnexpectedToken(t), self.lexer.span())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-07 04:00:06 -05:00
|
|
|
let body = self.get_block()?;
|
2021-06-06 17:09:45 -05:00
|
|
|
|
2022-04-01 18:34:25 -05:00
|
|
|
Ok(Stmt::Functio {
|
2021-10-12 15:14:20 -05:00
|
|
|
ident,
|
|
|
|
params,
|
|
|
|
body,
|
|
|
|
})
|
2021-06-06 17:09:45 -05:00
|
|
|
}
|
2021-06-07 04:00:06 -05:00
|
|
|
|
2021-06-11 10:52:47 -05:00
|
|
|
/// Parse BF function declaration
|
|
|
|
///
|
2021-10-04 16:00:18 -05:00
|
|
|
/// `bff $ident ([tapelen]) { ... }`
|
2022-04-01 18:34:25 -05:00
|
|
|
fn bff_flow(&mut self) -> Result<Stmt, Error> {
|
2021-10-04 16:00:18 -05:00
|
|
|
let ident = self.get_ident()?;
|
2021-06-11 10:52:47 -05:00
|
|
|
|
2021-07-15 14:39:01 -05:00
|
|
|
let tape_len = match self.checked_next()? {
|
2021-06-11 10:52:47 -05:00
|
|
|
Token::LeftParen => {
|
|
|
|
let len = Some(self.expr_flow(Token::RightParen)?);
|
|
|
|
self.require(Token::LeftCurly)?;
|
|
|
|
len
|
|
|
|
}
|
|
|
|
Token::LeftCurly => None,
|
2021-06-11 11:34:34 -05:00
|
|
|
token => {
|
|
|
|
return Err(Error::new(
|
|
|
|
ErrorKind::UnexpectedToken(token),
|
|
|
|
self.lexer.span(),
|
|
|
|
))
|
|
|
|
}
|
2021-06-11 10:52:47 -05:00
|
|
|
};
|
|
|
|
|
2021-06-11 11:10:11 -05:00
|
|
|
let mut code: Vec<u8> = vec![];
|
2021-06-11 10:52:47 -05:00
|
|
|
loop {
|
2021-07-15 14:39:01 -05:00
|
|
|
match self.checked_next()? {
|
2021-06-11 11:10:11 -05:00
|
|
|
Token::Plus
|
|
|
|
| Token::Minus
|
|
|
|
| Token::Comma
|
|
|
|
| Token::LeftBracket
|
|
|
|
| Token::RightBracket
|
|
|
|
| Token::LessThan
|
|
|
|
| Token::GreaterThan => code.push(self.lexer.slice().as_bytes()[0]),
|
|
|
|
Token::RightCurly => break,
|
|
|
|
_ => (),
|
|
|
|
}
|
2021-06-11 10:52:47 -05:00
|
|
|
}
|
|
|
|
|
2022-04-01 18:34:25 -05:00
|
|
|
Ok(Stmt::BfFunctio {
|
2021-10-04 16:00:18 -05:00
|
|
|
ident,
|
2021-06-11 10:52:47 -05:00
|
|
|
tape_len,
|
|
|
|
code,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-07 04:00:06 -05:00
|
|
|
/// Parse functio call flow
|
2022-04-01 18:34:25 -05:00
|
|
|
fn functio_call_flow(&mut self, expr: Spanned<Expr>) -> Result<Stmt, Error> {
|
2021-06-07 04:00:06 -05:00
|
|
|
let mut args = vec![];
|
|
|
|
let mut buf = None;
|
|
|
|
loop {
|
2021-07-15 14:39:01 -05:00
|
|
|
match self.checked_next()? {
|
2021-06-07 04:07:50 -05:00
|
|
|
// End of argument list
|
2021-06-07 04:00:06 -05:00
|
|
|
Token::RightParen => {
|
|
|
|
if let Some(expr) = buf.take() {
|
|
|
|
args.push(expr)
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2021-06-07 04:07:50 -05:00
|
|
|
|
|
|
|
// Next argument
|
2021-06-07 04:00:06 -05:00
|
|
|
Token::Comma => match buf.take() {
|
|
|
|
Some(expr) => args.push(expr),
|
2021-06-07 04:07:50 -05:00
|
|
|
// Comma alone
|
2021-06-07 04:00:06 -05:00
|
|
|
None => {
|
|
|
|
return Err(Error::new(
|
|
|
|
ErrorKind::UnexpectedToken(Token::Comma),
|
|
|
|
self.lexer.span(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
t => buf = Some(self.parse_expr(t, &mut buf)?),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-11 09:59:40 -05:00
|
|
|
self.require(Token::Semicolon)?;
|
2022-04-01 18:34:25 -05:00
|
|
|
Ok(Stmt::Call { expr, args })
|
2021-06-07 04:00:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse variable declaration
|
2022-04-01 18:34:25 -05:00
|
|
|
fn var_flow(&mut self) -> Result<Stmt, Error> {
|
2021-10-04 16:00:18 -05:00
|
|
|
let ident = self.get_ident()?;
|
2021-07-15 14:39:01 -05:00
|
|
|
let init = match self.checked_next()? {
|
2021-06-07 04:00:06 -05:00
|
|
|
Token::Equal => Some(self.expr_flow(Token::Semicolon)?),
|
|
|
|
Token::Semicolon => None,
|
|
|
|
t => return Err(Error::new(ErrorKind::UnexpectedToken(t), self.lexer.span())),
|
|
|
|
};
|
|
|
|
|
2022-04-01 18:34:25 -05:00
|
|
|
Ok(Stmt::Var { ident, init })
|
2021-06-07 04:00:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse Melo flow
|
2022-04-01 18:34:25 -05:00
|
|
|
fn melo_flow(&mut self) -> Result<Stmt, Error> {
|
2021-10-04 16:00:18 -05:00
|
|
|
let ident = self.get_ident()?;
|
2022-04-01 18:34:25 -05:00
|
|
|
self.semicolon_terminated(Stmt::Melo(ident))
|
2021-06-07 04:00:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse loop flow
|
|
|
|
///
|
|
|
|
/// `loop` is an infinite loop, no condition, only body
|
2022-04-01 18:34:25 -05:00
|
|
|
fn loop_flow(&mut self) -> Result<Stmt, Error> {
|
|
|
|
Ok(Stmt::Loop {
|
2021-06-07 04:00:06 -05:00
|
|
|
body: self.get_block()?,
|
|
|
|
})
|
|
|
|
}
|
2021-06-06 17:09:45 -05:00
|
|
|
}
|
2021-06-07 15:58:28 -05:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple_math() {
|
2022-02-22 15:49:56 -06:00
|
|
|
let code = "1 * (a + 3) / 666 print;";
|
2022-04-01 18:34:25 -05:00
|
|
|
let expected = &[Spanned {
|
|
|
|
item: Stmt::Print(Spanned {
|
|
|
|
item: Expr::BinOp {
|
|
|
|
lhs: Box::new(Spanned {
|
|
|
|
item: Expr::BinOp {
|
|
|
|
lhs: Box::new(Spanned {
|
|
|
|
item: Expr::Literal(Value::Int(1)),
|
2021-06-07 15:58:28 -05:00
|
|
|
span: 0..1,
|
|
|
|
}),
|
2022-04-01 18:34:25 -05:00
|
|
|
rhs: Box::new(Spanned {
|
|
|
|
item: Expr::BinOp {
|
|
|
|
lhs: Box::new(Spanned {
|
|
|
|
item: Expr::Variable("a".to_owned()),
|
2021-06-07 15:58:28 -05:00
|
|
|
span: 5..6,
|
|
|
|
}),
|
2022-04-01 18:34:25 -05:00
|
|
|
rhs: Box::new(Spanned {
|
|
|
|
item: Expr::Literal(Value::Int(3)),
|
2021-06-07 15:58:28 -05:00
|
|
|
span: 9..10,
|
|
|
|
}),
|
|
|
|
kind: BinOpKind::Add,
|
|
|
|
},
|
|
|
|
span: 5..10,
|
|
|
|
}),
|
|
|
|
kind: BinOpKind::Multiply,
|
|
|
|
},
|
|
|
|
span: 0..11,
|
|
|
|
}),
|
2022-04-01 18:34:25 -05:00
|
|
|
rhs: Box::new(Spanned {
|
|
|
|
item: Expr::Literal(Value::Int(666)),
|
2021-06-07 15:58:28 -05:00
|
|
|
span: 14..17,
|
|
|
|
}),
|
|
|
|
kind: BinOpKind::Divide,
|
|
|
|
},
|
|
|
|
span: 0..17,
|
|
|
|
}),
|
|
|
|
span: 0..24,
|
|
|
|
}];
|
|
|
|
|
|
|
|
let ast = Parser::new(code).init().unwrap();
|
|
|
|
assert_eq!(ast, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn variable_declaration() {
|
2022-02-22 15:49:56 -06:00
|
|
|
let code = "var a = 42;";
|
2022-04-01 18:34:25 -05:00
|
|
|
let expected = &[Spanned {
|
|
|
|
item: Stmt::Var {
|
|
|
|
ident: Spanned {
|
|
|
|
item: "a".to_owned(),
|
2021-06-07 15:58:28 -05:00
|
|
|
span: 4..5,
|
|
|
|
},
|
2022-04-01 18:34:25 -05:00
|
|
|
init: Some(Spanned {
|
|
|
|
item: Expr::Literal(Value::Int(42)),
|
2021-06-07 15:58:28 -05:00
|
|
|
span: 8..10,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
span: 0..11,
|
|
|
|
}];
|
|
|
|
|
|
|
|
let ast = Parser::new(code).init().unwrap();
|
|
|
|
assert_eq!(ast, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn if_flow() {
|
2022-02-22 15:49:56 -06:00
|
|
|
let code = "if (a == always) { /*Buy Able products!*/ print; }";
|
2022-04-01 18:34:25 -05:00
|
|
|
let expected = &[Spanned {
|
|
|
|
item: Stmt::If {
|
|
|
|
cond: Spanned {
|
|
|
|
item: Expr::BinOp {
|
|
|
|
lhs: Box::new(Spanned {
|
|
|
|
item: Expr::Variable("a".to_owned()),
|
2021-06-07 15:58:28 -05:00
|
|
|
span: 4..5,
|
|
|
|
}),
|
2022-04-01 18:34:25 -05:00
|
|
|
rhs: Box::new(Spanned {
|
|
|
|
item: Expr::Variable("always".to_owned()),
|
2021-06-07 15:58:28 -05:00
|
|
|
span: 9..15,
|
|
|
|
}),
|
|
|
|
kind: BinOpKind::Equal,
|
|
|
|
},
|
|
|
|
span: 4..15,
|
|
|
|
},
|
2022-04-01 18:34:25 -05:00
|
|
|
body: vec![Spanned {
|
|
|
|
item: Stmt::Print(Spanned {
|
|
|
|
item: Expr::Literal(Value::Str("Buy Able products!".to_owned())),
|
|
|
|
span: 19..39,
|
|
|
|
}),
|
|
|
|
span: 19..46,
|
|
|
|
}],
|
2021-06-07 15:58:28 -05:00
|
|
|
},
|
|
|
|
span: 0..48,
|
|
|
|
}];
|
|
|
|
|
|
|
|
let ast = Parser::new(code).init().unwrap();
|
|
|
|
assert_eq!(ast, expected);
|
|
|
|
}
|
2021-06-07 16:06:13 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn tdark() {
|
2022-02-22 15:49:56 -06:00
|
|
|
let code = "T-Dark { var lang = /*lang*/ + lang; }";
|
2022-04-01 18:34:25 -05:00
|
|
|
let expected = &[Spanned {
|
|
|
|
item: Stmt::Var {
|
|
|
|
ident: Spanned {
|
|
|
|
item: "script".to_owned(),
|
2021-06-07 16:06:13 -05:00
|
|
|
span: 13..17,
|
|
|
|
},
|
2022-04-01 18:34:25 -05:00
|
|
|
init: Some(Spanned {
|
|
|
|
item: Expr::BinOp {
|
|
|
|
lhs: Box::new(Spanned {
|
|
|
|
item: Expr::Literal(Value::Str("script".to_owned())),
|
2021-06-07 16:06:13 -05:00
|
|
|
span: 20..26,
|
|
|
|
}),
|
2022-04-01 18:34:25 -05:00
|
|
|
rhs: Box::new(Spanned {
|
|
|
|
item: Expr::Variable("script".to_owned()),
|
2021-06-07 16:06:13 -05:00
|
|
|
span: 29..33,
|
|
|
|
}),
|
|
|
|
kind: BinOpKind::Add,
|
|
|
|
},
|
|
|
|
span: 20..33,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
span: 9..34,
|
|
|
|
}];
|
|
|
|
|
|
|
|
let ast = Parser::new(code).init().unwrap();
|
|
|
|
assert_eq!(ast, expected);
|
|
|
|
}
|
2021-07-29 15:39:28 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cart_construction() {
|
2022-02-22 15:49:56 -06:00
|
|
|
let code = "[/*able*/ <= 1, /*script*/ <= 3 - 1] print;";
|
2022-04-01 18:34:25 -05:00
|
|
|
let expected = &[Spanned {
|
|
|
|
item: Stmt::Print(Spanned {
|
|
|
|
item: Expr::Cart(vec![
|
2021-07-29 15:39:28 -05:00
|
|
|
(
|
2022-04-01 18:34:25 -05:00
|
|
|
Spanned {
|
|
|
|
item: Expr::Literal(Value::Str("able".to_owned())),
|
2021-07-29 15:39:28 -05:00
|
|
|
span: 1..7,
|
|
|
|
},
|
2022-04-01 18:34:25 -05:00
|
|
|
Spanned {
|
|
|
|
item: Expr::Literal(Value::Int(1)),
|
2021-07-29 15:39:28 -05:00
|
|
|
span: 11..12,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
(
|
2022-04-01 18:34:25 -05:00
|
|
|
Spanned {
|
|
|
|
item: Expr::Literal(Value::Str("script".to_owned())),
|
2021-07-29 15:39:28 -05:00
|
|
|
span: 14..22,
|
|
|
|
},
|
2022-04-01 18:34:25 -05:00
|
|
|
Spanned {
|
|
|
|
item: Expr::BinOp {
|
2021-07-29 15:39:28 -05:00
|
|
|
kind: BinOpKind::Subtract,
|
2022-04-01 18:34:25 -05:00
|
|
|
lhs: Box::new(Spanned {
|
|
|
|
item: Expr::Literal(Value::Int(3)),
|
2021-07-29 15:39:28 -05:00
|
|
|
span: 26..27,
|
|
|
|
}),
|
2022-04-01 18:34:25 -05:00
|
|
|
rhs: Box::new(Spanned {
|
|
|
|
item: Expr::Literal(Value::Int(1)),
|
2021-07-29 15:39:28 -05:00
|
|
|
span: 30..31,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
span: 26..31,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
]),
|
|
|
|
span: 0..32,
|
|
|
|
}),
|
|
|
|
span: 0..39,
|
|
|
|
}];
|
|
|
|
|
|
|
|
let ast = Parser::new(code).init().unwrap();
|
|
|
|
assert_eq!(ast, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cart_index() {
|
2022-02-22 15:49:56 -06:00
|
|
|
let code = "[/*able*/ <= /*ablecorp*/][/*ablecorp*/] print;";
|
2022-04-01 18:34:25 -05:00
|
|
|
let expected = &[Spanned {
|
|
|
|
item: Stmt::Print(Spanned {
|
|
|
|
item: Expr::Index {
|
|
|
|
expr: Box::new(Spanned {
|
|
|
|
item: Expr::Cart(vec![(
|
|
|
|
Spanned {
|
|
|
|
item: Expr::Literal(Value::Str("able".to_owned())),
|
2021-07-29 15:39:28 -05:00
|
|
|
span: 1..7,
|
|
|
|
},
|
2022-04-01 18:34:25 -05:00
|
|
|
Spanned {
|
|
|
|
item: Expr::Literal(Value::Str("ablecorp".to_owned())),
|
2021-07-29 15:39:28 -05:00
|
|
|
span: 11..21,
|
|
|
|
},
|
|
|
|
)]),
|
|
|
|
span: 0..22,
|
|
|
|
}),
|
2022-04-01 18:34:25 -05:00
|
|
|
index: Box::new(Spanned {
|
|
|
|
item: Expr::Literal(Value::Str("ablecorp".to_owned())),
|
2021-07-29 15:39:28 -05:00
|
|
|
span: 23..33,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
span: 0..34,
|
|
|
|
}),
|
|
|
|
span: 0..41,
|
|
|
|
}];
|
|
|
|
|
|
|
|
let ast = Parser::new(code).init().unwrap();
|
|
|
|
assert_eq!(ast, expected);
|
|
|
|
}
|
2021-06-07 15:58:28 -05:00
|
|
|
}
|