next up previous contents index
Next: Index Up: FAQ Previous: How can I create

How can I change the dimensions in a declaration ?

The following script applies the transformation A(m:n) -> A(m+1:n+1) to the declaration of the selected variable.

SCRIPT changeArrayDim ()
         
  var := $csel 

  // -1- Get the symbole of the variable
  // the method depends on the selection

  IF (var.VARIANT == "array_decl") THEN
    // FIRST CASE: this is a selection X(...) in declaration. 
    // Using CHILD you can access most of the information in the AST

    // Remark : To see the AST children of the selection, use 
    //  PRINT $csel.AST 
    // This will give you the variant name and the number of children

    symbol := var.CHILD(1).SYMBOL
  ELSE
    IF ($csel.VARIANT == "name") THEN
     // SECOND CASE: the selection is a symbol in an expression
     symbol := var.SYMBOL
    ELSE
      PRINT "ERREUR : SELECT a variable name or a declaration (like A(m:n,p:q)) "
      EXIT   
    ENDIF
  ENDIF 


  // -2- verify the number of dimensions
  IF ( symbol.DIMENSION < 1 ) THEN 
    PRINT  "ERREUR : SELECT a 1D or +  matrix"
    EXIT
  ENDIF

  // -3- Get the bounds of the dimension
  dim1 := symbol.DIMENSION(1) // Warning : this is the original ; not a copy

  IF (dim1.VARIANT == "dim_colon") THEN                 
     // This is the case ; dim1 looks like m:n     
     //  The AST structure of "dim_colon" is ("dim_colon" lower_bound upper_bound) 
     lb := dim1.CHILD(1) // The lower bound
     ub := dim1.CHILD(2) // The upper bound
  ELSE 
     // the default case ; dim1 is an expression    
     lb := CREATEEXP("1") 
     ub := dim1
  ENDIF
  newlb := CREATEEXP("#lb+1") // The new lower bound
  newub := CREATEEXP("#ub+1") // The new upper bound

  // -4- Create the dim_colon AST with its two childs
  newdim := CREATE("dim_colon",newlb,newub)

  // -5- Set the new dimension (replace the old AST by the new one) 
  symbol.DIMENSION(1) <- newdim

ENDSCRIPT



Yann Mevel
1999-04-30