Code cleanup and refactor.

This commit is contained in:
Stephen Chung
2022-11-28 23:24:22 +08:00
parent 29a397b216
commit 0c85f0c796
26 changed files with 762 additions and 704 deletions

View File

@@ -1,9 +1,9 @@
//! Module defining script expressions.
use super::{ASTFlags, ASTNode, Ident, Stmt, StmtBlock};
use super::{ASTFlags, ASTNode, Ident, Namespace, Stmt, StmtBlock};
use crate::engine::{KEYWORD_FN_PTR, OP_EXCLUSIVE_RANGE, OP_INCLUSIVE_RANGE};
use crate::func::hashing::ALT_ZERO_HASH;
use crate::tokenizer::Token;
use crate::tokenizer::{Token, NO_TOKEN};
use crate::types::dynamic::Union;
use crate::{
calc_fn_hash, Dynamic, FnPtr, Identifier, ImmutableString, Position, SmartString, StaticVec,
@@ -197,8 +197,7 @@ impl FnCallHashes {
#[derive(Clone, Hash)]
pub struct FnCallExpr {
/// Namespace of the function, if any.
#[cfg(not(feature = "no_module"))]
pub namespace: super::Namespace,
pub namespace: Namespace,
/// Function name.
pub name: ImmutableString,
/// Pre-calculated hashes.
@@ -208,7 +207,7 @@ pub struct FnCallExpr {
/// Does this function call capture the parent scope?
pub capture_parent_scope: bool,
/// Is this function call a native operator?
/// Otherwise set to [`Token::NonToken`].
/// Otherwise set to [`Token::NONE`].
pub op_token: Token,
}
@@ -217,14 +216,13 @@ impl fmt::Debug for FnCallExpr {
#[inline(never)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut ff = f.debug_struct("FnCallExpr");
#[cfg(not(feature = "no_module"))]
if !self.namespace.is_empty() {
ff.field("namespace", &self.namespace);
}
ff.field("hash", &self.hashes)
.field("name", &self.name)
.field("args", &self.args);
if self.op_token != Token::NonToken {
if self.op_token != NO_TOKEN {
ff.field("op_token", &self.op_token);
}
if self.capture_parent_scope {
@@ -241,10 +239,7 @@ impl FnCallExpr {
#[inline(always)]
#[must_use]
pub fn is_qualified(&self) -> bool {
#[cfg(not(feature = "no_module"))]
return !self.namespace.is_empty();
#[cfg(feature = "no_module")]
return false;
!self.namespace.is_empty()
}
/// Convert this into an [`Expr::FnCall`].
#[inline(always)]
@@ -304,9 +299,7 @@ pub enum Expr {
/// majority of cases (unless there are more than 255 variables defined!).
/// This is to avoid reading a pointer redirection during each variable access.
Variable(
#[cfg(not(feature = "no_module"))]
Box<(Option<NonZeroUsize>, super::Namespace, u64, ImmutableString)>,
#[cfg(feature = "no_module")] Box<(Option<NonZeroUsize>, [(); 0], u64, ImmutableString)>,
Box<(Option<NonZeroUsize>, Namespace, u64, ImmutableString)>,
Option<NonZeroU8>,
Position,
),
@@ -584,13 +577,12 @@ impl Expr {
Union::FnPtr(f, ..) if !f.is_curried() => Self::FnCall(
FnCallExpr {
#[cfg(not(feature = "no_module"))]
namespace: super::Namespace::NONE,
namespace: Namespace::NONE,
name: KEYWORD_FN_PTR.into(),
hashes: calc_fn_hash(None, f.fn_name(), 1).into(),
args: once(Self::StringConstant(f.fn_name().into(), pos)).collect(),
capture_parent_scope: false,
op_token: Token::NonToken,
op_token: NO_TOKEN,
}
.into(),
pos,

View File

@@ -5,6 +5,7 @@ pub mod expr;
pub mod flags;
pub mod ident;
pub mod namespace;
pub mod namespace_none;
pub mod script_fn;
pub mod stmt;
@@ -16,6 +17,8 @@ pub use flags::{ASTFlags, FnAccess};
pub use ident::Ident;
#[cfg(not(feature = "no_module"))]
pub use namespace::Namespace;
#[cfg(feature = "no_module")]
pub use namespace_none::Namespace;
#[cfg(not(feature = "no_module"))]
#[cfg(not(feature = "no_function"))]
pub use script_fn::EncapsulatedEnviron;

22
src/ast/namespace_none.rs Normal file
View File

@@ -0,0 +1,22 @@
//! Namespace reference type.
#![cfg(feature = "no_module")]
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
/// _(internals)_ A chain of [module][crate::Module] names to namespace-qualify a variable or function call.
/// Exported under the `internals` feature only.
///
/// Not available under `no_module`.
#[derive(Debug, Clone, Eq, PartialEq, Default, Hash)]
pub struct Namespace;
impl Namespace {
/// Constant for no namespace.
pub const NONE: Self = Self;
#[inline(always)]
pub const fn is_empty(&self) -> bool {
true
}
}

View File

@@ -16,7 +16,6 @@ use std::{fmt, hash::Hash};
///
/// Not available under `no_module` or `no_function`.
#[cfg(not(feature = "no_module"))]
#[cfg(not(feature = "no_function"))]
#[derive(Debug, Clone)]
pub struct EncapsulatedEnviron {
/// Functions defined within the same [`AST`][crate::AST].
@@ -35,7 +34,6 @@ pub struct ScriptFnDef {
pub body: StmtBlock,
/// Encapsulated AST environment, if any.
#[cfg(not(feature = "no_module"))]
#[cfg(not(feature = "no_function"))]
pub environ: Option<crate::Shared<EncapsulatedEnviron>>,
/// Function name.
pub name: ImmutableString,

View File

@@ -2,7 +2,8 @@
use super::{ASTFlags, ASTNode, BinaryExpr, Expr, FnCallExpr, Ident};
use crate::engine::KEYWORD_EVAL;
use crate::tokenizer::{Span, Token};
use crate::tokenizer::Token;
use crate::types::Span;
use crate::{calc_fn_hash, Position, StaticVec, INT};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;