11//! Module defining script expressions.
22
3- use super :: { ASTFlags , ASTNode , Ident , Namespace , Stmt , StmtBlock } ;
3+ use super :: { ASTFlags , ASTNode , Ident , Stmt , StmtBlock } ;
44use crate :: engine:: KEYWORD_FN_PTR ;
55use crate :: tokenizer:: Token ;
66use crate :: types:: dynamic:: Union ;
@@ -184,7 +184,8 @@ impl FnCallHashes {
184184#[ derive( Clone , Hash ) ]
185185pub struct FnCallExpr {
186186 /// Namespace of the function, if any.
187- pub namespace : Namespace ,
187+ #[ cfg( not( feature = "no_module" ) ) ]
188+ pub namespace : super :: Namespace ,
188189 /// Function name.
189190 pub name : ImmutableString ,
190191 /// Pre-calculated hashes.
@@ -202,13 +203,14 @@ impl fmt::Debug for FnCallExpr {
202203 #[ inline( never) ]
203204 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
204205 let mut ff = f. debug_struct ( "FnCallExpr" ) ;
206+ #[ cfg( not( feature = "no_module" ) ) ]
205207 if !self . namespace . is_empty ( ) {
206208 ff. field ( "namespace" , & self . namespace ) ;
207209 }
208210 ff. field ( "hash" , & self . hashes )
209211 . field ( "name" , & self . name )
210212 . field ( "args" , & self . args ) ;
211- if self . op_token . is_some ( ) {
213+ if self . is_operator_call ( ) {
212214 ff. field ( "op_token" , & self . op_token ) ;
213215 }
214216 if self . capture_parent_scope {
@@ -221,12 +223,19 @@ impl fmt::Debug for FnCallExpr {
221223impl FnCallExpr {
222224 /// Does this function call contain a qualified namespace?
223225 ///
224- /// Always `false` under `no_module`.
226+ /// Not available under [`no_module`]
227+ #[ cfg( not( feature = "no_module" ) ) ]
225228 #[ inline( always) ]
226229 #[ must_use]
227230 pub fn is_qualified ( & self ) -> bool {
228231 !self . namespace . is_empty ( )
229232 }
233+ /// Is this function call an operator expression?
234+ #[ inline( always) ]
235+ #[ must_use]
236+ pub fn is_operator_call ( & self ) -> bool {
237+ self . op_token . is_some ( )
238+ }
230239 /// Convert this into an [`Expr::FnCall`].
231240 #[ inline( always) ]
232241 #[ must_use]
@@ -276,13 +285,15 @@ pub enum Expr {
276285 ) ,
277286 /// ()
278287 Unit ( Position ) ,
279- /// Variable access - (optional long index, namespace , namespace hash, variable name ), optional short index, position
288+ /// Variable access - (optional long index, variable name , namespace, namespace hash ), optional short index, position
280289 ///
281290 /// The short index is [`u8`] which is used when the index is <= 255, which should be
282291 /// the vast majority of cases (unless there are more than 255 variables defined!).
283292 /// This is to avoid reading a pointer redirection during each variable access.
284293 Variable (
285- Box < ( Option < NonZeroUsize > , Namespace , u64 , ImmutableString ) > ,
294+ #[ cfg( not( feature = "no_module" ) ) ]
295+ Box < ( Option < NonZeroUsize > , ImmutableString , super :: Namespace , u64 ) > ,
296+ #[ cfg( feature = "no_module" ) ] Box < ( Option < NonZeroUsize > , ImmutableString ) > ,
286297 Option < NonZeroU8 > ,
287298 Position ,
288299 ) ,
@@ -371,16 +382,16 @@ impl fmt::Debug for Expr {
371382 f. write_str ( "Variable(" ) ?;
372383
373384 #[ cfg( not( feature = "no_module" ) ) ]
374- if !x. 1 . is_empty ( ) {
385+ if !x. 2 . is_empty ( ) {
375386 write ! ( f, "{}{}" , x. 1 , crate :: engine:: NAMESPACE_SEPARATOR ) ?;
376- let pos = x. 1 . position ( ) ;
387+ let pos = x. 2 . position ( ) ;
377388 if !pos. is_none ( ) {
378389 display_pos = pos;
379390 }
380391 }
381- f. write_str ( & x. 3 ) ?;
392+ f. write_str ( & x. 1 ) ?;
382393 #[ cfg( not( feature = "no_module" ) ) ]
383- if let Some ( n) = x. 1 . index {
394+ if let Some ( n) = x. 2 . index {
384395 write ! ( f, " #{n}" ) ?;
385396 }
386397 if let Some ( n) = i. map_or_else ( || x. 0 , |n| NonZeroUsize :: new ( n. get ( ) as usize ) ) {
@@ -495,18 +506,20 @@ impl Expr {
495506 s. into ( )
496507 }
497508
498- // Fn
499- Self :: FnCall ( ref x, ..)
500- if !x. is_qualified ( ) && x. args . len ( ) == 1 && x. name == KEYWORD_FN_PTR =>
501- {
509+ // Qualified function call
510+ #[ cfg( not( feature = "no_module" ) ) ]
511+ Self :: FnCall ( x, ..) if x. is_qualified ( ) => return None ,
512+
513+ // Function call
514+ Self :: FnCall ( x, ..) if x. args . len ( ) == 1 && x. name == KEYWORD_FN_PTR => {
502515 match x. args [ 0 ] {
503516 Self :: StringConstant ( ref s, ..) => FnPtr :: new ( s. clone ( ) ) . ok ( ) ?. into ( ) ,
504517 _ => return None ,
505518 }
506519 }
507520
508- // Binary operators
509- Self :: FnCall ( x, ..) if !x . is_qualified ( ) && x. args . len ( ) == 2 => {
521+ // Binary operator call
522+ Self :: FnCall ( x, ..) if x. args . len ( ) == 2 => {
510523 pub const OP_EXCLUSIVE_RANGE : & str = Token :: ExclusiveRange . literal_syntax ( ) ;
511524 pub const OP_INCLUSIVE_RANGE : & str = Token :: InclusiveRange . literal_syntax ( ) ;
512525
@@ -559,7 +572,8 @@ impl Expr {
559572
560573 Union :: FnPtr ( f, ..) if !f. is_curried ( ) => Self :: FnCall (
561574 FnCallExpr {
562- namespace : Namespace :: NONE ,
575+ #[ cfg( not( feature = "no_module" ) ) ]
576+ namespace : super :: Namespace :: NONE ,
563577 name : KEYWORD_FN_PTR . into ( ) ,
564578 hashes : FnCallHashes :: from_hash ( calc_fn_hash ( None , f. fn_name ( ) , 1 ) ) ,
565579 args : once ( Self :: StringConstant ( f. fn_name ( ) . into ( ) , pos) ) . collect ( ) ,
@@ -581,8 +595,8 @@ impl Expr {
581595 pub ( crate ) fn get_variable_name ( & self , _non_qualified : bool ) -> Option < & str > {
582596 match self {
583597 #[ cfg( not( feature = "no_module" ) ) ]
584- Self :: Variable ( x, ..) if _non_qualified && !x. 1 . is_empty ( ) => None ,
585- Self :: Variable ( x, ..) => Some ( & x. 3 ) ,
598+ Self :: Variable ( x, ..) if _non_qualified && !x. 2 . is_empty ( ) => None ,
599+ Self :: Variable ( x, ..) => Some ( & x. 1 ) ,
586600 _ => None ,
587601 }
588602 }
@@ -661,10 +675,10 @@ impl Expr {
661675 match self {
662676 #[ cfg( not( feature = "no_module" ) ) ]
663677 Self :: Variable ( x, ..) => {
664- if x. 1 . is_empty ( ) {
678+ if x. 2 . is_empty ( ) {
665679 self . position ( )
666680 } else {
667- x. 1 . position ( )
681+ x. 2 . position ( )
668682 }
669683 }
670684
0 commit comments