Ignore:
Timestamp:
Feb 15, 2012, 1:59:19 PM (15 years ago)
Author:
Víctor de Buen <vdebuen@…>
Branches:
1941-docker-image-to-test, devel, master
Children:
a566515
Parents:
2e40466
Message:

Fixes #1431
Implements the referential operator $

git-svn-id: ​https://www.tol-project.org/svn/tolp/trunk@4141 be4846ae-f051-483a-831f-b64e7d08527f

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tol/btol/bgrammar/spfuninst.cpp

    r2e40466 r699b7d8  
    737737}
    738738
     739//--------------------------------------------------------------------
     740/*! Evaluate Element expressions
     741 */
     742static BSyntaxObject* EvRef(BGrammar* gra, const List* tre, BBool left)
     743//--------------------------------------------------------------------
     744{
     745  static BText _name_ = BText("$");
     746  static BText _str_list_ =
     747  "  @Anything,@Code,@Date,@Polyn,@Ratio,@Real,@Complex,\n"
     748  "  @Matrix,@VMatrix,@Set,@NameBlock,@Text,@TimeSet,@Serie\n";
     749  static BText _error_any_eng = BText("Operator $ applies only over "
     750  "sets with one of these referential structures:\n") + _str_list_;
     751  static BText _error_any_esp = BText("El operador $ se aplica sólo sobre "
     752  "conjuntos con una de estas estructuras referenciales:\n") + _str_list_;
     753
     754  assert(gra != NIL);
     755  BSyntaxObject* result = NIL;
     756  BSyntaxObject* uSet  = NIL;
     757  BSyntaxObject * uIndex = NIL;
     758  BInt nb = BSpecialFunction::NumBranches(tre);
     759  bool deleteUIndex = false;
     760  bool needCleanTree = false;
     761/*Std(BText("EvElement(")+gra->Name()+","+
     762      BParser::Unparse(tre,"","")+","+(int)left+")\n\n"+
     763      BParser::treWrite((List*)tre,"  ",false));  */
     764  if(BSpecialFunction::TestNumArg(_name_, 1, nb, 1))
     765  {
     766  //Std(BText("\nEvElement tre=")+BParser::Unparse(tre));
     767    uSet = GraSet()->LeftEvaluateTree(Branch(tre,1));
     768    if (uSet)
     769    {
     770      BSet& set = Set(uSet);
     771      BStruct* str = set.Struct();
     772      if(!str)
     773      {
     774        Error(I2("Cannot apply operator $ over a non structured ",
     775                 "No se puede aplicar el operador $ sobre "
     776                 "un conjunto sin estructura ")+"\n"+
     777              I2("It was expected a set with referential structure ",
     778                 "Se esperaba un conjunto con estructura ")+
     779              "@"+gra->Name()+I2(" or "," o ")+"@Anything");
     780      }
     781      else if(gra==GraAnything())
     782      {
     783        if(str==RefAnything() ||
     784           str==RefCode() ||
     785           str==RefDate() ||
     786           str==RefPolyn() ||
     787           str==RefRatio() ||
     788           str==RefReal() ||
     789           str==RefComplex() ||
     790           str==RefMatrix() ||
     791           str==RefVMatrix() ||
     792           str==RefSet() ||
     793           str==RefNameBlock() ||
     794           str==RefText() ||
     795           str==RefTimeSet() ||
     796           str==RefSerie())
     797        {
     798          result = set[1];
     799        }
     800        else
     801        {
     802          Error(I2("Cannot apply operator $ over structure ",
     803                   "No se puede aplicar el operador $ sobre la estructura ")+
     804                str->Name()+"\n"+
     805                I2(_error_any_eng,_error_any_esp));
     806        }
     807      }
     808      else if(str==RefAnything())
     809      {
     810        result = set[1];
     811      }
     812      else if((gra==GraCode()      && str==RefCode()) ||
     813              (gra==GraDate()      && str==RefDate()) ||
     814              (gra==GraPolyn()     && str==RefPolyn()) ||
     815              (gra==GraRatio()     && str==RefRatio()) ||
     816              (gra==GraReal()      && str==RefReal()) ||
     817              (gra==GraComplex()   && str==RefComplex()) ||
     818              (gra==GraMatrix()    && str==RefMatrix()) ||
     819              (gra==GraVMatrix()   && str==RefVMatrix()) ||
     820              (gra==GraSet()       && str==RefSet()) ||
     821              (gra==GraNameBlock() && str==RefNameBlock()) ||
     822              (gra==GraText()      && str==RefText()) ||
     823              (gra==GraTimeSet()   && str==RefTimeSet()) ||
     824              (gra==GraSerie()     && str==RefSerie()))
     825      {
     826        result = set[1];
     827      }
     828      else
     829      {
     830        Error(I2("Cannot apply operator $ over structure ",
     831                 "No se puede aplicar el operador $ sobre la estructura ")+
     832                 str->Name()+"\n"+
     833              I2("It was expected a set with referential structure ",
     834                 "Se esperaba un conjunto con estructura ")+
     835              "@"+gra->Name()+I2(" or "," o ")+"@Anything");
     836      }
     837    }
     838  }
     839  result = BSpecialFunction::TestResult(_name_, result, tre, NIL, BFALSE);
     840  BText resGraName = (result)?result->Grammar()->Name():"";
     841  if (result && gra!=GraAnything() && result->Grammar() != gra)
     842  {
     843        BSyntaxObject *cast = gra->Casting(result);
     844          if(cast) result = cast;
     845          else
     846    {
     847            BText msg(_name_);
     848            Error(I2("Incompatible types in " ,"Tipos incompatibles en ") +
     849                  msg + ": " + result->Identify() + I2(" is a ", " es de tipo ") +
     850                  result->Grammar()->Name() +
     851                  I2(" but not a ", ", pero no de tipo ") + gra->Name() + ".");
     852            result = NIL;
     853          }
     854  }
     855  return(result);
     856}
    739857
    740858//--------------------------------------------------------------------
    … …  
    23182436  AddLeftInstance("Element",
    23192437  "(Set s, Real n)",
    2320   I2("Returns the umpteenth element of a set. Also it can be used "
     2438  I2("Returns the n-th element of a set. Also it can be used "
    23212439     "the brackets []\n\n"
    23222440     "Example:\n"
    … …  
    23382456  EvElement);
    23392457
     2458  AddLeftInstance("$",
     2459  "(Set s)",
     2460  I2("Returns the first element of a set used as an object referencer.\n"
     2461     "Example:\n",
     2462     "Devuelve el primer elemento de un conjunto usado como un "
     2463     "referenciador de objetos.\n"
     2464     "Ejemplo:\n")+
     2465     "Real a = 3;\n"
     2466     "Set s = @Real(a);\n"
     2467     "Real x = $s + 2;\n",
     2468  EvRef);
    23402469
    23412470  AddLeftInstance("#E#",
Note: See TracChangeset for help on using the changeset viewer.