1 // Copyright (c) 1997-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 #include <ExprIntrp_yaccintrf.hxx>
16 #include <ExprIntrp_yaccanal.hxx>
17 #include <Expr_GeneralExpression.hxx>
18 #include <Expr_NamedExpression.hxx>
19 #include <Expr_NamedUnknown.hxx>
20 #include <Expr_NamedConstant.hxx>
21 #include <Expr_NamedFunction.hxx>
22 #include <Expr_NumericValue.hxx>
23 #include <Expr_UnaryFunction.hxx>
24 #include <Expr_BinaryFunction.hxx>
25 #include <Expr_PolyFunction.hxx>
26 #include <Expr_Exponentiate.hxx>
27 #include <Expr_Absolute.hxx>
28 #include <Expr_ArcCosine.hxx>
29 #include <Expr_ArcSine.hxx>
30 #include <Expr_ArcTangent.hxx>
31 #include <Expr_ArgCosh.hxx>
32 #include <Expr_ArgSinh.hxx>
33 #include <Expr_ArgTanh.hxx>
34 #include <Expr_Cosh.hxx>
35 #include <Expr_Cosine.hxx>
36 #include <Expr_Exponential.hxx>
37 #include <Expr_LogOf10.hxx>
38 #include <Expr_LogOfe.hxx>
39 #include <Expr_Sign.hxx>
40 #include <Expr_Sine.hxx>
41 #include <Expr_Sinh.hxx>
42 #include <Expr_SquareRoot.hxx>
43 #include <Expr_Tangent.hxx>
44 #include <Expr_Tanh.hxx>
45 #include <Expr_Equal.hxx>
46 #include <Expr_SystemRelation.hxx>
47 #include <Expr_UnknownIterator.hxx>
48 #include <Expr_FunctionDerivative.hxx>
50 #include <Expr_SequenceOfGeneralExpression.hxx>
51 #include <Expr_Operators.hxx>
52 #include <ExprIntrp_SyntaxError.hxx>
53 #include <Expr_Array1OfNamedUnknown.hxx>
54 #include <Expr_Array1OfGeneralExpression.hxx>
56 static TCollection_AsciiString ExprIntrp_assname;
57 static TCollection_AsciiString ExprIntrp_funcdefname;
58 static Standard_Integer ExprIntrp_nbargs;
59 static Standard_Integer ExprIntrp_nbdiff;
61 extern "C" void ExprIntrp_StartFunction()
63 const TCollection_AsciiString& name = ExprIntrp_GetResult();
64 ExprIntrp_Recept.PushName(name);
68 extern "C" void ExprIntrp_StartDerivate()
70 const TCollection_AsciiString& name = ExprIntrp_GetResult();
71 ExprIntrp_Recept.PushName(name);
74 extern "C" void ExprIntrp_EndDerivate()
77 degree = ExprIntrp_GetDegree();
78 ExprIntrp_Recept.PushValue(degree);
81 extern "C" void ExprIntrp_Derivation()
83 ExprIntrp_Recept.PushValue(1);
84 const TCollection_AsciiString& thename = ExprIntrp_GetResult();
85 Handle(Expr_NamedExpression) namexp = ExprIntrp_Recept.GetNamed(thename);
86 if (namexp.IsNull()) {
87 namexp = new Expr_NamedUnknown(thename);
89 if (!namexp->IsKind(STANDARD_TYPE(Expr_NamedUnknown))) {
90 ExprIntrp_SyntaxError::Raise();
92 ExprIntrp_Recept.Push(namexp);
95 extern "C" void ExprIntrp_DerivationValue()
97 const TCollection_AsciiString& aStr = ExprIntrp_GetResult();
98 ExprIntrp_Recept.PopValue();
99 ExprIntrp_Recept.PushValue(aStr.IntegerValue());
102 extern "C" void ExprIntrp_EndDerivation()
104 Standard_Integer degree = ExprIntrp_Recept.PopValue();
105 Handle(Expr_NamedUnknown) var = Handle(Expr_NamedUnknown)::DownCast(ExprIntrp_Recept.Pop());
106 Handle(Expr_GeneralExpression) exp = ExprIntrp_Recept.Pop();
107 exp = exp->NDerivative(var,degree);
108 ExprIntrp_Recept.Push(exp);
111 extern "C" void ExprIntrp_StartDifferential()
113 ExprIntrp_StartDerivate();
114 ExprIntrp_nbdiff = 0;
117 extern "C" void ExprIntrp_DiffDegreeVar()
119 const TCollection_AsciiString& aStr = ExprIntrp_GetResult();
120 const char* s = aStr.ToCString();
121 if ( *s != 'X' && *s != 'x' ) {
122 ExprIntrp_SyntaxError::Raise();
125 Standard_Integer rank = atoi(s);
126 ExprIntrp_Recept.PushValue(rank);
130 extern "C" void ExprIntrp_DiffVar()
132 ExprIntrp_Recept.PushValue(1);
133 ExprIntrp_DiffDegreeVar();
136 extern "C" void ExprIntrp_DiffDegree()
138 const TCollection_AsciiString& aStr = ExprIntrp_GetResult();
139 Standard_Integer deg = aStr.IntegerValue();
140 ExprIntrp_Recept.PushValue(deg);
143 extern "C" void ExprIntrp_VerDiffDegree()
145 const TCollection_AsciiString& aStr = ExprIntrp_GetResult();
146 Standard_Integer deg = aStr.IntegerValue();
147 Standard_Integer thedeg = ExprIntrp_Recept.PopValue();
149 ExprIntrp_SyntaxError::Raise();
151 ExprIntrp_Recept.PushValue(deg);
154 extern "C" void ExprIntrp_EndDifferential()
156 TCollection_AsciiString name = ExprIntrp_Recept.PopName();
157 Handle(Expr_GeneralFunction) thefunc = ExprIntrp_Recept.GetFunction(name);
158 if (thefunc.IsNull()) {
159 ExprIntrp_SyntaxError::Raise();
161 Standard_Integer rank,degree;
162 Handle(Expr_NamedUnknown) thediff;
163 Standard_Integer nbvars = thefunc->NbOfVariables();
165 for (Standard_Integer i=1; i<= ExprIntrp_nbdiff; i++) {
166 rank = ExprIntrp_Recept.PopValue();
167 degree = ExprIntrp_Recept.PopValue();
168 if ((rank > nbvars) || (rank < 1)) {
169 ExprIntrp_SyntaxError::Raise();
171 thediff = thefunc->Variable(rank);
172 thefunc = new Expr_FunctionDerivative(thefunc,thediff,degree);
174 ExprIntrp_Recept.PushFunction(thefunc);
177 extern "C" void ExprIntrp_EndDiffFunction()
179 Handle(Expr_GeneralFunction) thefunc = ExprIntrp_Recept.PopFunction();
180 if (thefunc.IsNull()) {
181 ExprIntrp_SyntaxError::Raise();
183 Standard_Integer nbargs = thefunc->NbOfVariables();
185 Handle(Expr_GeneralExpression) op = ExprIntrp_Recept.Pop();
186 Handle(Expr_UnaryFunction) res =
187 new Expr_UnaryFunction(thefunc,op);
188 ExprIntrp_Recept.Push(res);
190 else if (nbargs == 2) {
191 Handle(Expr_GeneralExpression) arg2 = ExprIntrp_Recept.Pop();
192 Handle(Expr_GeneralExpression) arg1 = ExprIntrp_Recept.Pop();
194 ExprIntrp_SyntaxError::Raise();
196 Handle(Expr_BinaryFunction) res =
197 new Expr_BinaryFunction(thefunc,arg1,arg2);
198 ExprIntrp_Recept.Push(res);
201 Expr_Array1OfGeneralExpression tabarg(1,nbargs);
202 Handle(Expr_GeneralExpression) arg;
203 for (Standard_Integer i = 1; i<= nbargs; i++) {
204 arg = ExprIntrp_Recept.Pop();
206 ExprIntrp_SyntaxError::Raise();
208 tabarg(nbargs-i+1) = arg;
210 Handle(Expr_PolyFunction) res =
211 new Expr_PolyFunction(thefunc,tabarg);
212 ExprIntrp_Recept.Push(res);
216 static Handle(Expr_GeneralExpression) ExprIntrp_StandardFunction(const TCollection_AsciiString& name, const Handle(Expr_GeneralExpression)& op)
218 // return standard functions equivalent corresponding to <name>
219 // with given operand <op> if exists. Returns null value if not.
220 // <name> is not case sensitive
222 Handle(Expr_GeneralExpression) res;
223 if ((name == "abs") || (name == "Abs")) {
224 res = new Expr_Absolute(op);
226 else if ((name == "acos") || (name == "ACos")) {
227 res = new Expr_ArcCosine(op);
229 else if ((name == "asin") || (name == "ASin")) {
230 res = new Expr_ArcSine(op);
232 else if ((name == "atan") || (name == "ATan")) {
233 res = new Expr_ArcTangent(op);
235 else if ((name == "acosh") || (name == "ACosh")) {
236 res = new Expr_ArgCosh(op);
238 else if ((name == "asinh") || (name == "ASinh")) {
239 res = new Expr_ArgSinh(op);
241 else if ((name == "atanh") || (name == "ATanh")) {
242 res = new Expr_ArgTanh(op);
244 else if ((name == "cosh") || (name == "Cosh")) {
245 res = new Expr_Cosh(op);
247 else if ((name == "cos") || (name == "Cos")) {
248 res = new Expr_Cosine(op);
250 else if ((name == "exp") || (name == "Exp")) {
251 res = new Expr_Exponential(op);
253 else if (name == "log") {
254 res = new Expr_LogOf10(op);
256 else if ((name == "Log") || (name == "Ln")) {
257 res = new Expr_LogOfe(op);
259 else if ((name == "sign") || (name == "Sign")) {
260 res = new Expr_Sign(op);
262 else if ((name == "sin") || (name == "Sin")) {
263 res = new Expr_Sine(op);
265 else if ((name == "sinh") || (name == "Sinh")) {
266 res = new Expr_Sinh(op);
268 else if ((name == "sqrt") || (name == "Sqrt")) {
269 res = new Expr_SquareRoot(op);
271 else if ((name == "tan") || (name == "Tan")) {
272 res = new Expr_Tangent(op);
274 else if ((name == "tanh") || (name == "Tanh")) {
275 res = new Expr_Tanh(op);
281 extern "C" void ExprIntrp_EndDerFunction()
283 TCollection_AsciiString name = ExprIntrp_Recept.PopName();
284 Handle(Expr_GeneralExpression) op = ExprIntrp_Recept.Pop();
285 Handle(Expr_GeneralExpression) resstand = ExprIntrp_StandardFunction(name,op);
287 if (!resstand.IsNull()) {
288 Handle(Expr_NamedUnknown) var;
289 Expr_UnknownIterator rit(resstand);
292 ExprIntrp_SyntaxError::Raise();
296 if (var->IsAssigned()) {
303 ExprIntrp_SyntaxError::Raise();
306 Handle(Expr_GeneralExpression) res = resstand->NDerivative(var,ExprIntrp_Recept.PopValue());
307 ExprIntrp_Recept.Push(res);
311 Handle(Expr_NamedFunction) thefunc = ExprIntrp_Recept.GetFunction(name);
312 if (thefunc.IsNull()) {
313 ExprIntrp_SyntaxError::Raise();
315 Standard_Integer nbargs = thefunc->NbOfVariables();
317 ExprIntrp_SyntaxError::Raise();
319 Handle(Expr_NamedUnknown) var = thefunc->Variable(1);
320 Handle(Expr_FunctionDerivative) thefuncder =
321 new Expr_FunctionDerivative(thefunc,var,ExprIntrp_Recept.PopValue());
322 Handle(Expr_UnaryFunction) res =
323 new Expr_UnaryFunction(thefuncder,op);
324 ExprIntrp_Recept.Push(res);
328 extern "C" void ExprIntrp_EndFunction()
330 TCollection_AsciiString name = ExprIntrp_Recept.PopName();
331 Handle(Expr_GeneralExpression) op = ExprIntrp_Recept.Pop();
333 Handle(Expr_GeneralExpression) resstand = ExprIntrp_StandardFunction(name,op);
334 if (!resstand.IsNull()) {
335 ExprIntrp_Recept.Push(resstand->ShallowSimplified());
338 Handle(Expr_NamedFunction) thefunc = ExprIntrp_Recept.GetFunction(name);
339 if (thefunc.IsNull()) {
340 ExprIntrp_SyntaxError::Raise();
342 Standard_Integer nbargs = thefunc->NbOfVariables();
344 Handle(Expr_UnaryFunction) res =
345 new Expr_UnaryFunction(thefunc,op);
346 ExprIntrp_Recept.Push(res);
348 else if (nbargs == 2) {
349 Handle(Expr_GeneralExpression) arg1 = ExprIntrp_Recept.Pop();
351 ExprIntrp_SyntaxError::Raise();
353 Handle(Expr_BinaryFunction) res =
354 new Expr_BinaryFunction(thefunc,arg1,op);
355 ExprIntrp_Recept.Push(res);
358 Expr_Array1OfGeneralExpression tabarg(1,nbargs);
359 Handle(Expr_GeneralExpression) arg;
361 for (Standard_Integer i = 1; i< nbargs; i++) {
362 arg = ExprIntrp_Recept.Pop();
364 ExprIntrp_SyntaxError::Raise();
366 tabarg(nbargs-i) = arg;
368 Handle(Expr_PolyFunction) res =
369 new Expr_PolyFunction(thefunc,tabarg);
370 ExprIntrp_Recept.Push(res);
375 extern "C" void ExprIntrp_NextFuncArg()
380 extern "C" void ExprIntrp_EndFuncArg()
385 extern "C" void ExprIntrp_SumOperator()
387 Handle(Expr_GeneralExpression) op2 = ExprIntrp_Recept.Pop();
388 Handle(Expr_GeneralExpression) op1 = ExprIntrp_Recept.Pop();
389 Handle(Expr_Sum) sres = op1 + op2;
390 Handle(Expr_GeneralExpression) res = sres->ShallowSimplified();
391 ExprIntrp_Recept.Push(res);
394 extern "C" void ExprIntrp_MinusOperator()
396 Handle(Expr_GeneralExpression) op2 = ExprIntrp_Recept.Pop();
397 Handle(Expr_GeneralExpression) op1 = ExprIntrp_Recept.Pop();
398 Handle(Expr_Difference) res = op1 - op2;
399 ExprIntrp_Recept.Push(res->ShallowSimplified());
402 extern "C" void ExprIntrp_DivideOperator()
404 Handle(Expr_GeneralExpression) op2 = ExprIntrp_Recept.Pop();
405 Handle(Expr_GeneralExpression) op1 = ExprIntrp_Recept.Pop();
406 Handle(Expr_Division) res = op1 / op2;
407 ExprIntrp_Recept.Push(res->ShallowSimplified());
410 extern "C" void ExprIntrp_ExpOperator()
412 Handle(Expr_GeneralExpression) op2 = ExprIntrp_Recept.Pop();
413 Handle(Expr_GeneralExpression) op1 = ExprIntrp_Recept.Pop();
414 Handle(Expr_Exponentiate) res = new Expr_Exponentiate(op1,op2);
415 ExprIntrp_Recept.Push(res->ShallowSimplified());
418 extern "C" void ExprIntrp_ProductOperator()
420 Handle(Expr_GeneralExpression) op2 = ExprIntrp_Recept.Pop();
421 Handle(Expr_GeneralExpression) op1 = ExprIntrp_Recept.Pop();
422 Handle(Expr_Product) res = op1 * op2;
423 ExprIntrp_Recept.Push(res->ShallowSimplified());
426 extern "C" void ExprIntrp_UnaryMinusOperator()
428 Handle(Expr_GeneralExpression) op = ExprIntrp_Recept.Pop();
429 Handle(Expr_UnaryMinus) res = new Expr_UnaryMinus(op);
430 ExprIntrp_Recept.Push(res->ShallowSimplified());
433 extern "C" void ExprIntrp_UnaryPlusOperator()
435 Handle(Expr_GeneralExpression) op = ExprIntrp_Recept.Pop();
436 ExprIntrp_Recept.Push(op);
439 extern "C" void ExprIntrp_VariableIdentifier()
441 const TCollection_AsciiString& thename = ExprIntrp_GetResult();
442 Handle(Expr_NamedExpression) nameexp = ExprIntrp_Recept.GetNamed(thename);
443 if (nameexp.IsNull()) {
444 nameexp = new Expr_NamedUnknown(thename);
445 ExprIntrp_Recept.Use(nameexp);
447 ExprIntrp_Recept.Push(nameexp);
450 extern "C" void ExprIntrp_NumValue()
452 const TCollection_AsciiString& aStr = ExprIntrp_GetResult();
453 Standard_Real value = aStr.RealValue();
454 Handle(Expr_NumericValue) nval = new Expr_NumericValue(value);
455 ExprIntrp_Recept.Push(nval);
458 extern "C" void ExprIntrp_AssignVariable()
460 ExprIntrp_assname = ExprIntrp_GetResult();
463 extern "C" void ExprIntrp_Deassign()
465 const TCollection_AsciiString& thename = ExprIntrp_GetResult();
466 Handle(Expr_NamedExpression) nameexp = ExprIntrp_Recept.GetNamed(thename);
467 if (nameexp.IsNull()) {
468 ExprIntrp_SyntaxError::Raise();
470 if (!nameexp->IsKind(STANDARD_TYPE(Expr_NamedUnknown))) {
471 ExprIntrp_SyntaxError::Raise();
473 Handle(Expr_NamedUnknown) var = Handle(Expr_NamedUnknown)::DownCast(nameexp);
477 extern "C" void ExprIntrp_DefineFunction()
479 ExprIntrp_funcdefname = ExprIntrp_Recept.PopName();
480 ExprIntrp_Recept.PushValue(ExprIntrp_nbargs);
483 extern "C" void ExprIntrp_close()
485 ExprIntrp_stop_string();
488 extern "C" void ExprIntrperror(char* msg)
491 ExprIntrp_SyntaxError::Raise(msg);
495 extern "C" void ExprIntrp_EndOfEqual()
497 Handle(Expr_GeneralExpression) memb2 = ExprIntrp_Recept.Pop();
498 Handle(Expr_GeneralExpression) memb1 = ExprIntrp_Recept.Pop();
499 Handle(Expr_Equal) res = new Expr_Equal(memb1,memb2);
500 ExprIntrp_Recept.PushRelation(res);
503 extern "C" void ExprIntrp_EndOfRelation()
505 Handle(Expr_SystemRelation) sys;
506 Handle(Expr_GeneralRelation) currel;
507 Handle(Expr_GeneralRelation) oldrel;
508 while (!ExprIntrp_Recept.IsRelStackEmpty()) {
509 currel = ExprIntrp_Recept.PopRelation();
513 else if (!oldrel.IsNull()) {
514 sys = new Expr_SystemRelation(oldrel);
522 ExprIntrp_Recept.PushRelation(currel);
525 ExprIntrp_Recept.PushRelation(sys);
529 extern "C" void ExprIntrp_EndOfAssign()
531 Handle(Expr_NamedExpression) namexp = ExprIntrp_Recept.GetNamed(ExprIntrp_assname);
532 Handle(Expr_NamedUnknown) namu;
533 if (namexp.IsNull()) {
534 namu = new Expr_NamedUnknown(ExprIntrp_assname);
535 ExprIntrp_Recept.Use(namu);
538 if (!namexp->IsKind(STANDARD_TYPE(Expr_NamedUnknown))) {
539 ExprIntrp_SyntaxError::Raise();
541 namu = Handle(Expr_NamedUnknown)::DownCast(namexp);
543 namu->Assign(ExprIntrp_Recept.Pop());
546 extern "C" void ExprIntrp_EndOfFuncDef()
548 Handle(Expr_GeneralExpression) theexp = ExprIntrp_Recept.Pop();
549 Standard_Integer nbargs = ExprIntrp_Recept.PopValue();
550 Expr_Array1OfNamedUnknown vars(1,nbargs);
551 Expr_Array1OfNamedUnknown internvars(1,nbargs);
553 for (i=nbargs; i > 0; i--) {
554 vars(i) = Handle(Expr_NamedUnknown)::DownCast(ExprIntrp_Recept.Pop());
555 internvars(i) = Handle(Expr_NamedUnknown)::DownCast(vars(i)->Copy());
557 theexp = Expr::CopyShare(theexp); // ATTENTION, PROTECTION BUG STACK
558 for (i=1; i<= nbargs; i++) {
559 if (theexp->Contains(vars(i))) {
560 theexp->Replace(vars(i),internvars(i));
563 if (theexp == vars(i)) {
564 theexp = internvars(i);
568 Handle(Expr_NamedFunction) thefunc =
569 new Expr_NamedFunction(ExprIntrp_funcdefname,
572 ExprIntrp_Recept.Use(thefunc);
575 extern "C" void ExprIntrp_ConstantIdentifier()
577 const TCollection_AsciiString& thename = ExprIntrp_GetResult();
578 ExprIntrp_Recept.PushName(thename);
581 extern "C" void ExprIntrp_ConstantDefinition()
583 TCollection_AsciiString name = ExprIntrp_Recept.PopName();
584 const TCollection_AsciiString& aStr = ExprIntrp_GetResult();
585 Standard_Real val = aStr.RealValue();
586 Handle(Expr_NamedConstant) theconst = new Expr_NamedConstant(name,val);
587 ExprIntrp_Recept.Use(theconst);
588 ExprIntrp_Recept.Push(theconst);
592 extern "C" void ExprIntrp_Sumator()
594 Handle(Expr_NumericValue) number = Handle(Expr_NumericValue)::DownCast(ExprIntrp_Recept.Pop());
595 Standard_Integer nb = (Standard_Integer) number->GetValue();
596 Handle(Expr_GeneralExpression) inc = ExprIntrp_Recept.Pop();
597 Handle(Expr_GeneralExpression) first = ExprIntrp_Recept.Pop();
598 Handle(Expr_NamedUnknown) var = Handle(Expr_NamedUnknown)::DownCast(ExprIntrp_Recept.Pop());
599 Handle(Expr_GeneralExpression) theexp = ExprIntrp_Recept.Pop();
600 Standard_Boolean thesame = (theexp == var);
601 Handle(Expr_GeneralExpression) cur = Expr::CopyShare(first);
602 Handle(Expr_GeneralExpression) res;
603 Handle(Expr_GeneralExpression) member;
604 Expr_SequenceOfGeneralExpression seq;
605 for (Standard_Integer i=1; i<= nb; i++) {
610 member = Expr::CopyShare(theexp);
611 member->Replace(var,cur);
614 cur = (cur + inc)->ShallowSimplified();
616 res = new Expr_Sum(seq);
617 ExprIntrp_Recept.Push(res->ShallowSimplified());
620 extern "C" void ExprIntrp_Productor()
622 Handle(Expr_NumericValue) number = Handle(Expr_NumericValue)::DownCast(ExprIntrp_Recept.Pop());
623 Standard_Integer nb = (Standard_Integer) number->GetValue();
624 Handle(Expr_GeneralExpression) inc = ExprIntrp_Recept.Pop();
625 Handle(Expr_GeneralExpression) first = ExprIntrp_Recept.Pop();
626 Handle(Expr_NamedUnknown) var = Handle(Expr_NamedUnknown)::DownCast(ExprIntrp_Recept.Pop());
627 Handle(Expr_GeneralExpression) theexp = ExprIntrp_Recept.Pop();
628 Standard_Boolean thesame = (theexp == var);
629 Handle(Expr_GeneralExpression) cur = Expr::CopyShare(first);
630 Handle(Expr_GeneralExpression) res;
631 Handle(Expr_GeneralExpression) member;
632 Expr_SequenceOfGeneralExpression seq;
633 for (Standard_Integer i=1; i<= nb; i++) {
638 member = Expr::CopyShare(theexp);
639 member->Replace(var,cur);
642 cur = (cur + inc)->ShallowSimplified();
644 res = new Expr_Product(seq);
645 ExprIntrp_Recept.Push(res->ShallowSimplified());