2021-12-17 16:07:13 +08:00
|
|
|
//! Module defining script statements.
|
|
|
|
|
2022-01-28 10:11:40 +08:00
|
|
|
use super::{ASTNode, BinaryExpr, Expr, FnCallExpr, Ident, OptionFlags, AST_OPTION_FLAGS::*};
|
2021-12-30 12:14:54 +08:00
|
|
|
use crate::engine::KEYWORD_EVAL;
|
2021-12-17 16:07:13 +08:00
|
|
|
use crate::tokenizer::Token;
|
|
|
|
use crate::{calc_fn_hash, Position, StaticVec, INT};
|
|
|
|
#[cfg(feature = "no_std")]
|
|
|
|
use std::prelude::v1::*;
|
|
|
|
use std::{
|
|
|
|
collections::BTreeMap,
|
|
|
|
fmt,
|
|
|
|
hash::Hash,
|
|
|
|
mem,
|
|
|
|
ops::{Deref, DerefMut},
|
|
|
|
};
|
|
|
|
|
|
|
|
/// _(internals)_ An op-assignment operator.
|
|
|
|
/// Exported under the `internals` feature only.
|
|
|
|
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
|
|
|
|
pub struct OpAssignment<'a> {
|
|
|
|
/// Hash of the op-assignment call.
|
|
|
|
pub hash_op_assign: u64,
|
|
|
|
/// Hash of the underlying operator call (for fallback).
|
|
|
|
pub hash_op: u64,
|
|
|
|
/// Op-assignment operator.
|
2022-01-28 08:28:31 +08:00
|
|
|
pub op_assign: &'a str,
|
|
|
|
/// Underlying operator.
|
2021-12-17 16:07:13 +08:00
|
|
|
pub op: &'a str,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl OpAssignment<'_> {
|
|
|
|
/// Create a new [`OpAssignment`].
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
2021-12-17 16:55:24 +08:00
|
|
|
/// Panics if the name is not an op-assignment operator.
|
2021-12-17 16:07:13 +08:00
|
|
|
#[must_use]
|
2021-12-17 16:55:24 +08:00
|
|
|
#[inline(always)]
|
|
|
|
pub fn new(name: &str) -> Self {
|
|
|
|
Self::new_from_token(Token::lookup_from_syntax(name).expect("operator"))
|
|
|
|
}
|
|
|
|
/// Create a new [`OpAssignment`] from a [`Token`].
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics if the token is not an op-assignment operator.
|
|
|
|
#[must_use]
|
|
|
|
pub fn new_from_token(op: Token) -> Self {
|
2021-12-17 16:07:13 +08:00
|
|
|
let op_raw = op
|
2022-01-23 21:09:37 +08:00
|
|
|
.get_base_op_from_assignment()
|
2021-12-17 16:55:24 +08:00
|
|
|
.expect("op-assignment operator")
|
2021-12-17 16:07:13 +08:00
|
|
|
.literal_syntax();
|
|
|
|
Self {
|
2021-12-17 16:55:24 +08:00
|
|
|
hash_op_assign: calc_fn_hash(op.literal_syntax(), 2),
|
2021-12-17 16:07:13 +08:00
|
|
|
hash_op: calc_fn_hash(op_raw, 2),
|
2022-01-28 08:28:31 +08:00
|
|
|
op_assign: op.literal_syntax(),
|
|
|
|
op: op_raw,
|
2021-12-17 16:07:13 +08:00
|
|
|
}
|
|
|
|
}
|
2022-01-23 21:09:37 +08:00
|
|
|
/// Create a new [`OpAssignment`] from a base operator.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics if the name is not an operator that can be converted into an op-operator.
|
|
|
|
#[must_use]
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn new_from_base(name: &str) -> Self {
|
|
|
|
Self::new_from_base_token(Token::lookup_from_syntax(name).expect("operator"))
|
|
|
|
}
|
|
|
|
/// Convert a [`Token`] into a new [`OpAssignment`].
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics if the token is cannot be converted into an op-assignment operator.
|
|
|
|
#[inline(always)]
|
|
|
|
#[must_use]
|
|
|
|
pub fn new_from_base_token(op: Token) -> Self {
|
|
|
|
Self::new_from_token(op.convert_to_op_assignment().expect("operator"))
|
|
|
|
}
|
2021-12-17 16:07:13 +08:00
|
|
|
}
|
|
|
|
|
2022-01-28 10:11:40 +08:00
|
|
|
/// A statements block with an optional condition.
|
|
|
|
#[derive(Debug, Clone, Hash)]
|
|
|
|
pub struct ConditionalStmtBlock {
|
|
|
|
/// Optional condition.
|
|
|
|
pub condition: Option<Expr>,
|
|
|
|
/// Statements block.
|
|
|
|
pub statements: StmtBlock,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<B: Into<StmtBlock>> From<(Option<Expr>, B)> for ConditionalStmtBlock {
|
|
|
|
#[inline(always)]
|
|
|
|
fn from(value: (Option<Expr>, B)) -> Self {
|
|
|
|
Self {
|
|
|
|
condition: value.0,
|
|
|
|
statements: value.1.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ConditionalStmtBlock {
|
|
|
|
/// Does the condition exist?
|
|
|
|
#[inline(always)]
|
|
|
|
#[must_use]
|
|
|
|
pub const fn has_condition(&self) -> bool {
|
|
|
|
self.condition.is_some()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// _(internals)_ A type containing all cases for a `switch` statement.
|
|
|
|
/// Exported under the `internals` feature only.
|
|
|
|
#[derive(Debug, Clone, Hash)]
|
|
|
|
pub struct SwitchCases {
|
|
|
|
/// Dictionary mapping value hashes to [`ConditionalStmtBlock`]'s.
|
|
|
|
pub cases: BTreeMap<u64, Box<ConditionalStmtBlock>>,
|
|
|
|
/// Statements block for the default case (there can be no condition for the default case).
|
|
|
|
pub def_case: StmtBlock,
|
|
|
|
/// List of range cases.
|
|
|
|
pub ranges: StaticVec<(INT, INT, bool, ConditionalStmtBlock)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// _(internals)_ A `try-catch` block.
|
|
|
|
/// Exported under the `internals` feature only.
|
|
|
|
#[derive(Debug, Clone, Hash)]
|
|
|
|
pub struct TryCatchBlock {
|
|
|
|
/// `try` block.
|
|
|
|
pub try_block: StmtBlock,
|
|
|
|
/// `catch` variable, if any.
|
|
|
|
pub catch_var: Option<Ident>,
|
|
|
|
/// `catch` block.
|
|
|
|
pub catch_block: StmtBlock,
|
|
|
|
}
|
|
|
|
|
2021-12-17 16:07:13 +08:00
|
|
|
/// _(internals)_ A scoped block of statements.
|
|
|
|
/// Exported under the `internals` feature only.
|
|
|
|
#[derive(Clone, Hash, Default)]
|
|
|
|
pub struct StmtBlock(StaticVec<Stmt>, Position);
|
|
|
|
|
|
|
|
impl StmtBlock {
|
|
|
|
/// A [`StmtBlock`] that does not exist.
|
|
|
|
pub const NONE: Self = Self::empty(Position::NONE);
|
|
|
|
|
|
|
|
/// Create a new [`StmtBlock`].
|
|
|
|
#[must_use]
|
|
|
|
pub fn new(statements: impl IntoIterator<Item = Stmt>, pos: Position) -> Self {
|
|
|
|
let mut statements: StaticVec<_> = statements.into_iter().collect();
|
|
|
|
statements.shrink_to_fit();
|
|
|
|
Self(statements, pos)
|
|
|
|
}
|
|
|
|
/// Create an empty [`StmtBlock`].
|
|
|
|
#[inline(always)]
|
|
|
|
#[must_use]
|
|
|
|
pub const fn empty(pos: Position) -> Self {
|
|
|
|
Self(StaticVec::new_const(), pos)
|
|
|
|
}
|
|
|
|
/// Is this statements block empty?
|
|
|
|
#[inline(always)]
|
|
|
|
#[must_use]
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.0.is_empty()
|
|
|
|
}
|
|
|
|
/// Number of statements in this statements block.
|
|
|
|
#[inline(always)]
|
|
|
|
#[must_use]
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.0.len()
|
|
|
|
}
|
|
|
|
/// Get the statements of this statements block.
|
|
|
|
#[inline(always)]
|
|
|
|
#[must_use]
|
|
|
|
pub fn statements(&self) -> &[Stmt] {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
/// Extract the statements.
|
|
|
|
#[inline(always)]
|
|
|
|
#[must_use]
|
|
|
|
pub(crate) fn take_statements(&mut self) -> StaticVec<Stmt> {
|
|
|
|
mem::take(&mut self.0)
|
|
|
|
}
|
|
|
|
/// Get an iterator over the statements of this statements block.
|
|
|
|
#[inline(always)]
|
|
|
|
#[must_use]
|
|
|
|
pub fn iter(&self) -> impl Iterator<Item = &Stmt> {
|
|
|
|
self.0.iter()
|
|
|
|
}
|
|
|
|
/// Get the position (location of the beginning `{`) of this statements block.
|
|
|
|
#[inline(always)]
|
|
|
|
#[must_use]
|
|
|
|
pub const fn position(&self) -> Position {
|
|
|
|
self.1
|
|
|
|
}
|
|
|
|
/// Set the position (location of the beginning `{`) of this statements block.
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn set_position(&mut self, pos: Position) {
|
|
|
|
self.1 = pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for StmtBlock {
|
|
|
|
type Target = StaticVec<Stmt>;
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DerefMut for StmtBlock {
|
|
|
|
#[inline(always)]
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-28 10:11:40 +08:00
|
|
|
impl AsRef<[Stmt]> for StmtBlock {
|
|
|
|
#[inline(always)]
|
|
|
|
fn as_ref(&self) -> &[Stmt] {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsMut<[Stmt]> for StmtBlock {
|
|
|
|
#[inline(always)]
|
|
|
|
fn as_mut(&mut self) -> &mut [Stmt] {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-17 16:07:13 +08:00
|
|
|
impl fmt::Debug for StmtBlock {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
f.write_str("Block")?;
|
|
|
|
fmt::Debug::fmt(&self.0, f)?;
|
|
|
|
self.1.debug_print(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-22 10:48:19 +08:00
|
|
|
impl From<Stmt> for StmtBlock {
|
|
|
|
#[inline]
|
|
|
|
fn from(stmt: Stmt) -> Self {
|
|
|
|
match stmt {
|
|
|
|
Stmt::Block(mut block, pos) => Self(block.iter_mut().map(mem::take).collect(), pos),
|
|
|
|
Stmt::Noop(pos) => Self(StaticVec::new_const(), pos),
|
|
|
|
_ => {
|
|
|
|
let pos = stmt.position();
|
|
|
|
Self(vec![stmt].into(), pos)
|
|
|
|
}
|
|
|
|
}
|
2021-12-17 16:07:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoIterator for StmtBlock {
|
|
|
|
type Item = Stmt;
|
|
|
|
type IntoIter = smallvec::IntoIter<[Stmt; 3]>;
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
self.0.into_iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Extend<Stmt> for StmtBlock {
|
|
|
|
#[inline(always)]
|
|
|
|
fn extend<T: IntoIterator<Item = Stmt>>(&mut self, iter: T) {
|
|
|
|
self.0.extend(iter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// _(internals)_ A statement.
|
|
|
|
/// Exported under the `internals` feature only.
|
|
|
|
#[derive(Debug, Clone, Hash)]
|
|
|
|
pub enum Stmt {
|
|
|
|
/// No-op.
|
|
|
|
Noop(Position),
|
|
|
|
/// `if` expr `{` stmt `}` `else` `{` stmt `}`
|
|
|
|
If(Expr, Box<(StmtBlock, StmtBlock)>, Position),
|
2021-12-22 10:48:19 +08:00
|
|
|
/// `switch` expr `{` literal or range or _ `if` condition `=>` stmt `,` ... `}`
|
|
|
|
///
|
|
|
|
/// ### Data Structure
|
|
|
|
///
|
|
|
|
/// 0) Hash table for (condition, block)
|
|
|
|
/// 1) Default block
|
|
|
|
/// 2) List of ranges: (start, end, inclusive, condition, statement)
|
2022-01-28 10:11:40 +08:00
|
|
|
Switch(Expr, Box<SwitchCases>, Position),
|
2021-12-17 16:07:13 +08:00
|
|
|
/// `while` expr `{` stmt `}` | `loop` `{` stmt `}`
|
|
|
|
///
|
|
|
|
/// If the guard expression is [`UNIT`][Expr::Unit], then it is a `loop` statement.
|
|
|
|
While(Expr, Box<StmtBlock>, Position),
|
|
|
|
/// `do` `{` stmt `}` `while`|`until` expr
|
|
|
|
///
|
|
|
|
/// ### Option Flags
|
|
|
|
///
|
2022-01-28 08:28:17 +08:00
|
|
|
/// * [`AST_OPTION_NONE`] = `while`
|
|
|
|
/// * [`AST_OPTION_NEGATED`] = `until`
|
2021-12-17 16:07:13 +08:00
|
|
|
Do(Box<StmtBlock>, Expr, OptionFlags, Position),
|
|
|
|
/// `for` `(` id `,` counter `)` `in` expr `{` stmt `}`
|
|
|
|
For(Expr, Box<(Ident, Option<Ident>, StmtBlock)>, Position),
|
|
|
|
/// \[`export`\] `let`|`const` id `=` expr
|
|
|
|
///
|
|
|
|
/// ### Option Flags
|
|
|
|
///
|
2022-01-28 08:28:17 +08:00
|
|
|
/// * [`AST_OPTION_EXPORTED`] = `export`
|
|
|
|
/// * [`AST_OPTION_CONSTANT`] = `const`
|
2021-12-17 16:07:13 +08:00
|
|
|
Var(Expr, Box<Ident>, OptionFlags, Position),
|
|
|
|
/// expr op`=` expr
|
2022-01-28 10:11:40 +08:00
|
|
|
Assignment(Box<(Option<OpAssignment<'static>>, BinaryExpr)>, Position),
|
2021-12-17 16:07:13 +08:00
|
|
|
/// func `(` expr `,` ... `)`
|
|
|
|
///
|
|
|
|
/// Note - this is a duplicate of [`Expr::FnCall`] to cover the very common pattern of a single
|
|
|
|
/// function call forming one statement.
|
|
|
|
FnCall(Box<FnCallExpr>, Position),
|
|
|
|
/// `{` stmt`;` ... `}`
|
|
|
|
Block(Box<[Stmt]>, Position),
|
|
|
|
/// `try` `{` stmt; ... `}` `catch` `(` var `)` `{` stmt; ... `}`
|
2022-01-28 10:11:40 +08:00
|
|
|
TryCatch(Box<TryCatchBlock>, Position),
|
2021-12-17 16:07:13 +08:00
|
|
|
/// [expression][Expr]
|
|
|
|
Expr(Expr),
|
|
|
|
/// `continue`/`break`
|
|
|
|
///
|
|
|
|
/// ### Option Flags
|
|
|
|
///
|
2022-01-28 08:28:17 +08:00
|
|
|
/// * [`AST_OPTION_NONE`] = `continue`
|
|
|
|
/// * [`AST_OPTION_BREAK`] = `break`
|
2021-12-17 16:07:13 +08:00
|
|
|
BreakLoop(OptionFlags, Position),
|
|
|
|
/// `return`/`throw`
|
|
|
|
///
|
|
|
|
/// ### Option Flags
|
|
|
|
///
|
2022-01-28 08:28:17 +08:00
|
|
|
/// * [`AST_OPTION_NONE`] = `return`
|
|
|
|
/// * [`AST_OPTION_BREAK`] = `throw`
|
2021-12-17 16:07:13 +08:00
|
|
|
Return(OptionFlags, Option<Expr>, Position),
|
|
|
|
/// `import` expr `as` var
|
|
|
|
///
|
|
|
|
/// Not available under `no_module`.
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
|
|
|
Import(Expr, Option<Box<Ident>>, Position),
|
2022-01-28 10:11:40 +08:00
|
|
|
/// `export` var `as` var
|
2021-12-17 16:07:13 +08:00
|
|
|
///
|
|
|
|
/// Not available under `no_module`.
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
2022-01-28 10:11:40 +08:00
|
|
|
Export(Box<(Ident, Ident)>, Position),
|
2021-12-17 16:07:13 +08:00
|
|
|
/// Convert a variable to shared.
|
|
|
|
///
|
|
|
|
/// Not available under `no_closure`.
|
|
|
|
///
|
|
|
|
/// # Notes
|
|
|
|
///
|
|
|
|
/// This variant does not map to any language structure. It is currently only used only to
|
|
|
|
/// convert a normal variable into a shared variable when the variable is _captured_ by a closure.
|
|
|
|
#[cfg(not(feature = "no_closure"))]
|
|
|
|
Share(crate::Identifier),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Stmt {
|
|
|
|
#[inline(always)]
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::Noop(Position::NONE)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-22 10:48:19 +08:00
|
|
|
impl From<StmtBlock> for Stmt {
|
|
|
|
#[inline(always)]
|
|
|
|
fn from(block: StmtBlock) -> Self {
|
|
|
|
Self::Block(block.0.into_boxed_slice(), block.1)
|
2021-12-17 16:07:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Stmt {
|
|
|
|
/// Is this statement [`Noop`][Stmt::Noop]?
|
|
|
|
#[inline(always)]
|
|
|
|
#[must_use]
|
|
|
|
pub const fn is_noop(&self) -> bool {
|
|
|
|
matches!(self, Self::Noop(_))
|
|
|
|
}
|
|
|
|
/// Get the [position][Position] of this statement.
|
|
|
|
#[must_use]
|
|
|
|
pub const fn position(&self) -> Position {
|
|
|
|
match self {
|
|
|
|
Self::Noop(pos)
|
|
|
|
| Self::BreakLoop(_, pos)
|
|
|
|
| Self::Block(_, pos)
|
|
|
|
| Self::Assignment(_, pos)
|
|
|
|
| Self::FnCall(_, pos)
|
|
|
|
| Self::If(_, _, pos)
|
|
|
|
| Self::Switch(_, _, pos)
|
|
|
|
| Self::While(_, _, pos)
|
|
|
|
| Self::Do(_, _, _, pos)
|
|
|
|
| Self::For(_, _, pos)
|
|
|
|
| Self::Return(_, _, pos)
|
|
|
|
| Self::Var(_, _, _, pos)
|
|
|
|
| Self::TryCatch(_, pos) => *pos,
|
|
|
|
|
|
|
|
Self::Expr(x) => x.position(),
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
|
|
|
Self::Import(_, _, pos) => *pos,
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
|
|
|
Self::Export(_, pos) => *pos,
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_closure"))]
|
|
|
|
Self::Share(_) => Position::NONE,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Override the [position][Position] of this statement.
|
|
|
|
pub fn set_position(&mut self, new_pos: Position) -> &mut Self {
|
|
|
|
match self {
|
|
|
|
Self::Noop(pos)
|
|
|
|
| Self::BreakLoop(_, pos)
|
|
|
|
| Self::Block(_, pos)
|
|
|
|
| Self::Assignment(_, pos)
|
|
|
|
| Self::FnCall(_, pos)
|
|
|
|
| Self::If(_, _, pos)
|
|
|
|
| Self::Switch(_, _, pos)
|
|
|
|
| Self::While(_, _, pos)
|
|
|
|
| Self::Do(_, _, _, pos)
|
|
|
|
| Self::For(_, _, pos)
|
|
|
|
| Self::Return(_, _, pos)
|
|
|
|
| Self::Var(_, _, _, pos)
|
|
|
|
| Self::TryCatch(_, pos) => *pos = new_pos,
|
|
|
|
|
|
|
|
Self::Expr(x) => {
|
|
|
|
x.set_position(new_pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
|
|
|
Self::Import(_, _, pos) => *pos = new_pos,
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
|
|
|
Self::Export(_, pos) => *pos = new_pos,
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_closure"))]
|
|
|
|
Self::Share(_) => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
/// Does this statement return a value?
|
|
|
|
#[must_use]
|
|
|
|
pub const fn returns_value(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
Self::If(_, _, _)
|
|
|
|
| Self::Switch(_, _, _)
|
|
|
|
| Self::Block(_, _)
|
|
|
|
| Self::Expr(_)
|
|
|
|
| Self::FnCall(_, _) => true,
|
|
|
|
|
|
|
|
Self::Noop(_)
|
|
|
|
| Self::While(_, _, _)
|
|
|
|
| Self::Do(_, _, _, _)
|
|
|
|
| Self::For(_, _, _)
|
|
|
|
| Self::TryCatch(_, _) => false,
|
|
|
|
|
|
|
|
Self::Var(_, _, _, _)
|
|
|
|
| Self::Assignment(_, _)
|
|
|
|
| Self::BreakLoop(_, _)
|
|
|
|
| Self::Return(_, _, _) => false,
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
|
|
|
Self::Import(_, _, _) | Self::Export(_, _) => false,
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_closure"))]
|
|
|
|
Self::Share(_) => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Is this statement self-terminated (i.e. no need for a semicolon terminator)?
|
|
|
|
#[must_use]
|
|
|
|
pub const fn is_self_terminated(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
Self::If(_, _, _)
|
|
|
|
| Self::Switch(_, _, _)
|
|
|
|
| Self::While(_, _, _)
|
|
|
|
| Self::For(_, _, _)
|
|
|
|
| Self::Block(_, _)
|
|
|
|
| Self::TryCatch(_, _) => true,
|
|
|
|
|
|
|
|
// A No-op requires a semicolon in order to know it is an empty statement!
|
|
|
|
Self::Noop(_) => false,
|
|
|
|
|
|
|
|
Self::Expr(Expr::Custom(x, _)) if x.is_self_terminated() => true,
|
|
|
|
|
|
|
|
Self::Var(_, _, _, _)
|
|
|
|
| Self::Assignment(_, _)
|
|
|
|
| Self::Expr(_)
|
|
|
|
| Self::FnCall(_, _)
|
|
|
|
| Self::Do(_, _, _, _)
|
|
|
|
| Self::BreakLoop(_, _)
|
|
|
|
| Self::Return(_, _, _) => false,
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
|
|
|
Self::Import(_, _, _) | Self::Export(_, _) => false,
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_closure"))]
|
|
|
|
Self::Share(_) => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Is this statement _pure_?
|
|
|
|
///
|
|
|
|
/// A pure statement has no side effects.
|
|
|
|
#[must_use]
|
|
|
|
pub fn is_pure(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
Self::Noop(_) => true,
|
|
|
|
Self::Expr(expr) => expr.is_pure(),
|
|
|
|
Self::If(condition, x, _) => {
|
|
|
|
condition.is_pure()
|
2022-01-28 10:11:40 +08:00
|
|
|
&& x.0.iter().all(Stmt::is_pure)
|
|
|
|
&& x.1.iter().all(Stmt::is_pure)
|
2021-12-17 16:07:13 +08:00
|
|
|
}
|
|
|
|
Self::Switch(expr, x, _) => {
|
|
|
|
expr.is_pure()
|
2022-01-28 10:11:40 +08:00
|
|
|
&& x.cases.values().all(|block| {
|
|
|
|
block.condition.as_ref().map(Expr::is_pure).unwrap_or(true)
|
|
|
|
&& block.statements.iter().all(Stmt::is_pure)
|
2021-12-17 16:07:13 +08:00
|
|
|
})
|
2022-01-28 10:11:40 +08:00
|
|
|
&& x.ranges.iter().all(|(_, _, _, block)| {
|
|
|
|
block.condition.as_ref().map(Expr::is_pure).unwrap_or(true)
|
|
|
|
&& block.statements.iter().all(Stmt::is_pure)
|
2021-12-17 16:07:13 +08:00
|
|
|
})
|
2022-01-28 10:11:40 +08:00
|
|
|
&& x.def_case.iter().all(Stmt::is_pure)
|
2021-12-17 16:07:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Loops that exit can be pure because it can never be infinite.
|
|
|
|
Self::While(Expr::BoolConstant(false, _), _, _) => true,
|
|
|
|
Self::Do(body, Expr::BoolConstant(x, _), options, _)
|
2022-01-28 08:28:17 +08:00
|
|
|
if *x == options.contains(AST_OPTION_NEGATED) =>
|
2021-12-17 16:07:13 +08:00
|
|
|
{
|
|
|
|
body.iter().all(Stmt::is_pure)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loops are never pure since they can be infinite - and that's a side effect.
|
|
|
|
Self::While(_, _, _) | Self::Do(_, _, _, _) => false,
|
|
|
|
|
|
|
|
// For loops can be pure because if the iterable is pure, it is finite,
|
|
|
|
// so infinite loops can never occur.
|
2022-01-28 10:11:40 +08:00
|
|
|
Self::For(iterable, x, _) => iterable.is_pure() && x.2.iter().all(Stmt::is_pure),
|
2021-12-17 16:07:13 +08:00
|
|
|
|
|
|
|
Self::Var(_, _, _, _) | Self::Assignment(_, _) | Self::FnCall(_, _) => false,
|
|
|
|
Self::Block(block, _) => block.iter().all(|stmt| stmt.is_pure()),
|
|
|
|
Self::BreakLoop(_, _) | Self::Return(_, _, _) => false,
|
|
|
|
Self::TryCatch(x, _) => {
|
2022-01-28 10:11:40 +08:00
|
|
|
x.try_block.iter().all(Stmt::is_pure) && x.catch_block.iter().all(Stmt::is_pure)
|
2021-12-17 16:07:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
|
|
|
Self::Import(_, _, _) => false,
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
|
|
|
Self::Export(_, _) => false,
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_closure"))]
|
|
|
|
Self::Share(_) => false,
|
|
|
|
}
|
|
|
|
}
|
2021-12-30 12:14:54 +08:00
|
|
|
/// Does this statement's behavior depend on its containing block?
|
|
|
|
///
|
|
|
|
/// A statement that depends on its containing block behaves differently when promoted
|
|
|
|
/// to an upper block.
|
|
|
|
///
|
|
|
|
/// Currently only variable definitions (i.e. `let` and `const`), `import`/`export` statements,
|
|
|
|
/// and `eval` calls (which may in turn call define variables) fall under this category.
|
|
|
|
#[inline]
|
|
|
|
#[must_use]
|
|
|
|
pub fn is_block_dependent(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
Self::Var(_, _, _, _) => true,
|
|
|
|
|
2021-12-30 12:19:41 +08:00
|
|
|
Self::Expr(Expr::Stmt(s)) => s.iter().all(Stmt::is_block_dependent),
|
|
|
|
|
|
|
|
Self::FnCall(x, _) | Self::Expr(Expr::FnCall(x, _)) => {
|
2022-01-08 14:00:41 +08:00
|
|
|
!x.is_qualified() && x.name == KEYWORD_EVAL
|
2021-12-30 12:19:41 +08:00
|
|
|
}
|
2021-12-30 12:14:54 +08:00
|
|
|
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
|
|
|
Self::Import(_, _, _) | Self::Export(_, _) => true,
|
|
|
|
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2021-12-17 16:07:13 +08:00
|
|
|
/// Is this statement _pure_ within the containing block?
|
|
|
|
///
|
|
|
|
/// An internally pure statement only has side effects that disappear outside the block.
|
|
|
|
///
|
|
|
|
/// Currently only variable definitions (i.e. `let` and `const`) and `import`/`export`
|
2021-12-30 12:14:54 +08:00
|
|
|
/// statements are internally pure, other than pure expressions.
|
2021-12-17 16:07:13 +08:00
|
|
|
#[inline]
|
|
|
|
#[must_use]
|
|
|
|
pub fn is_internally_pure(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
Self::Var(expr, _, _, _) => expr.is_pure(),
|
|
|
|
|
2021-12-30 12:19:41 +08:00
|
|
|
Self::Expr(Expr::Stmt(s)) => s.iter().all(Stmt::is_internally_pure),
|
|
|
|
|
2021-12-17 16:07:13 +08:00
|
|
|
#[cfg(not(feature = "no_module"))]
|
|
|
|
Self::Import(expr, _, _) => expr.is_pure(),
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
|
|
|
Self::Export(_, _) => true,
|
|
|
|
|
|
|
|
_ => self.is_pure(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Does this statement break the current control flow through the containing block?
|
|
|
|
///
|
|
|
|
/// Currently this is only true for `return`, `throw`, `break` and `continue`.
|
|
|
|
///
|
|
|
|
/// All statements following this statement will essentially be dead code.
|
|
|
|
#[inline]
|
|
|
|
#[must_use]
|
|
|
|
pub const fn is_control_flow_break(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
Self::Return(_, _, _) | Self::BreakLoop(_, _) => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Recursively walk this statement.
|
|
|
|
/// Return `false` from the callback to terminate the walk.
|
|
|
|
pub fn walk<'a>(
|
|
|
|
&'a self,
|
|
|
|
path: &mut Vec<ASTNode<'a>>,
|
|
|
|
on_node: &mut impl FnMut(&[ASTNode]) -> bool,
|
|
|
|
) -> bool {
|
|
|
|
// Push the current node onto the path
|
|
|
|
path.push(self.into());
|
|
|
|
|
|
|
|
if !on_node(path) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
match self {
|
|
|
|
Self::Var(e, _, _, _) => {
|
|
|
|
if !e.walk(path, on_node) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Self::If(e, x, _) => {
|
|
|
|
if !e.walk(path, on_node) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-01-28 10:11:40 +08:00
|
|
|
for s in x.0.iter() {
|
2021-12-17 16:07:13 +08:00
|
|
|
if !s.walk(path, on_node) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2022-01-28 10:11:40 +08:00
|
|
|
for s in x.1.iter() {
|
2021-12-17 16:07:13 +08:00
|
|
|
if !s.walk(path, on_node) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Self::Switch(e, x, _) => {
|
|
|
|
if !e.walk(path, on_node) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-01-28 10:11:40 +08:00
|
|
|
for b in x.cases.values() {
|
|
|
|
if !b
|
|
|
|
.condition
|
|
|
|
.as_ref()
|
|
|
|
.map(|e| e.walk(path, on_node))
|
|
|
|
.unwrap_or(true)
|
|
|
|
{
|
2021-12-17 16:07:13 +08:00
|
|
|
return false;
|
|
|
|
}
|
2022-01-28 10:11:40 +08:00
|
|
|
for s in b.statements.iter() {
|
2021-12-17 16:07:13 +08:00
|
|
|
if !s.walk(path, on_node) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-28 10:11:40 +08:00
|
|
|
for (_, _, _, b) in &x.ranges {
|
|
|
|
if !b
|
|
|
|
.condition
|
|
|
|
.as_ref()
|
|
|
|
.map(|e| e.walk(path, on_node))
|
|
|
|
.unwrap_or(true)
|
|
|
|
{
|
2021-12-17 16:07:13 +08:00
|
|
|
return false;
|
|
|
|
}
|
2022-01-28 10:11:40 +08:00
|
|
|
for s in b.statements.iter() {
|
2021-12-17 16:07:13 +08:00
|
|
|
if !s.walk(path, on_node) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-28 10:11:40 +08:00
|
|
|
for s in x.def_case.iter() {
|
2021-12-17 16:07:13 +08:00
|
|
|
if !s.walk(path, on_node) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Self::While(e, s, _) | Self::Do(s, e, _, _) => {
|
|
|
|
if !e.walk(path, on_node) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for s in &s.0 {
|
|
|
|
if !s.walk(path, on_node) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Self::For(e, x, _) => {
|
|
|
|
if !e.walk(path, on_node) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-01-28 10:11:40 +08:00
|
|
|
for s in x.2.iter() {
|
2021-12-17 16:07:13 +08:00
|
|
|
if !s.walk(path, on_node) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Self::Assignment(x, _) => {
|
2022-01-28 10:11:40 +08:00
|
|
|
if !x.1.lhs.walk(path, on_node) {
|
2021-12-17 16:07:13 +08:00
|
|
|
return false;
|
|
|
|
}
|
2022-01-28 10:11:40 +08:00
|
|
|
if !x.1.rhs.walk(path, on_node) {
|
2021-12-17 16:07:13 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Self::FnCall(x, _) => {
|
|
|
|
for s in &x.args {
|
|
|
|
if !s.walk(path, on_node) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Self::Block(x, _) => {
|
|
|
|
for s in x.iter() {
|
|
|
|
if !s.walk(path, on_node) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Self::TryCatch(x, _) => {
|
2022-01-28 10:11:40 +08:00
|
|
|
for s in x.try_block.iter() {
|
2021-12-17 16:07:13 +08:00
|
|
|
if !s.walk(path, on_node) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2022-01-28 10:11:40 +08:00
|
|
|
for s in x.catch_block.iter() {
|
2021-12-17 16:07:13 +08:00
|
|
|
if !s.walk(path, on_node) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Self::Expr(e) | Self::Return(_, Some(e), _) => {
|
|
|
|
if !e.walk(path, on_node) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
|
|
|
Self::Import(e, _, _) => {
|
|
|
|
if !e.walk(path, on_node) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
2022-01-06 11:07:52 +08:00
|
|
|
path.pop().unwrap();
|
2021-12-17 16:07:13 +08:00
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|