From 6a4f13dc9d1a32615ff4a50cd1e30aaa6b5707eb Mon Sep 17 00:00:00 2001 From: Bob Eberlein Date: Tue, 10 Jul 2018 11:16:56 -0400 Subject: [PATCH] fix unary +/- equation output - add in PULSE_TRAIN and RANDOM_NORMAL, DELAY1 --- src/Function/Function.cpp | 41 ++++++++++++++++++++++++++++++++++++++ src/Function/Function.h | 22 +++++++++++++++++++- src/Vensim/VYacc.tab.cpp | 4 ++-- src/Vensim/VYacc.y | 4 ++-- src/Vensim/VensimParse.cpp | 3 +++ 5 files changed, 69 insertions(+), 5 deletions(-) diff --git a/src/Function/Function.cpp b/src/Function/Function.cpp index 8f7d2d8..f628da4 100644 --- a/src/Function/Function.cpp +++ b/src/Function/Function.cpp @@ -100,6 +100,47 @@ void FunctionSampleIfTrue::OutputComputable(ContextInfo *info, ExpressionList *a Function::OutputComputable(info, arg); } +void FunctionPulseTrain::OutputComputable(ContextInfo *info, ExpressionList *arg) +{ + if (arg->Length() == 4) + { + *info << "( IF TIME >= ("; + const_cast((*arg)[0])->OutputComputable(info); // OutputComputable should really be const + *info << ") AND TIME <= ("; + const_cast((*arg)[3])->OutputComputable(info); // OutputComputable should really be const + *info << ") AND (TIME - ("; + const_cast((*arg)[0])->OutputComputable(info); // OutputComputable should really be const + *info << ")) MOD ("; + const_cast((*arg)[2])->OutputComputable(info); // OutputComputable should really be const + *info << ") < ("; + const_cast((*arg)[1])->OutputComputable(info); // OutputComputable should really be const + *info << ") THEN 1 ELSE 0 )"; + return; + } + Function::OutputComputable(info, arg); +} + +void FunctionRandomNormal::OutputComputable(ContextInfo *info, ExpressionList *arg) +{ + if (arg->Length() == 5) + { + *info << "NORMAL("; + const_cast((*arg)[2])->OutputComputable(info); // OutputComputable should really be const + *info << ", "; + const_cast((*arg)[3])->OutputComputable(info); // OutputComputable should really be const + *info << ", "; + const_cast((*arg)[4])->OutputComputable(info); // OutputComputable should really be const + *info << ", "; + const_cast((*arg)[0])->OutputComputable(info); // OutputComputable should really be const + *info << ", "; + const_cast((*arg)[1])->OutputComputable(info); // OutputComputable should really be const + *info << ")"; + return; + } + Function::OutputComputable(info, arg); +} + + void FunctionIfThenElse::OutputComputable(ContextInfo *info, ExpressionList *arg) { if (arg->Length() == 3) diff --git a/src/Function/Function.h b/src/Function/Function.h index e539949..b7809c5 100644 --- a/src/Function/Function.h +++ b/src/Function/Function.h @@ -136,12 +136,14 @@ FSubclass(FunctionVectorElmMap, "VECTOR ELM MAP", 2, "VECTOR ELM MAP"); FSubclass(FunctionVectorSortOrder, "VECTOR SORT ORDER", 2, "VECTOR SORT ORDER"); FSubclass(FunctionGame, "GAME", 1, ""); // don't need this FSubclass(FunctionRandom01, "RANDOM 0 1", 0, "UNIFORM(0,1)"); +FSubclass(FunctionRandomUniform, "RANDOM UNIFORM", 3, "UNIFORM"); // actually memory but no init - or init - does not matter for translation FSubclass(FunctionSmooth, "SMOOTH", 2, "SMTH1") FSubclass(FunctionSmoothI, "SMOOTHI", 3, "SMTH1") FSubclass(FunctionSmooth3, "SMOOTH3", 2, "SMTH3") FSubclass(FunctionTrend,"TREND", 3, "TREND") +FSubclass(FunctionDelay1, "DELAY1", 2, "DELAY1") FSubclass(FunctionDelay3, "DELAY3", 2, "DELAY3") FSubclass(FunctionDelay, "DELAY FIXED", 3, "DELAY") FSubclass(FunctionNPV,"NPV",4,"NPV") @@ -154,7 +156,6 @@ FSubclass(FunctionModulo,"MODULO",2,"MODULO") FSubclass(FunctionGetDataAtTime, "GET DATA AT TIME", 2, "GET_DATA_AT_TIME") FSubclass(FunctionGetDataLastTime, "GET DATA LAST TIME", 1, "GET_DATA_LAST_TIME") FSubclass(FunctionLookupArea, "LOOKUP AREA", 3, "LOOKUP_AREA") -FSubclass(FunctionRandomNormal, "RANDOM NORMAL", 5, "RANDOM_NORMAL") FSubclassStart(FunctionDelayN, "DELAY N", 4, "DELAYN") public: @@ -203,6 +204,25 @@ class FunctionSampleIfTrue : public Function private: }; +class FunctionPulseTrain : public Function +{ +public: + FunctionPulseTrain(SymbolNameSpace *sns) : Function(sns, "PULSE TRAIN", 4) {} + ~FunctionPulseTrain(void) {} + std::string ComputableName(void) { return "PulseTrain"; } + virtual void OutputComputable(ContextInfo *info, ExpressionList *arg); +private: +}; +class FunctionRandomNormal : public Function +{ +public: + FunctionRandomNormal(SymbolNameSpace *sns) : Function(sns, "RANDOM NORMAL", 5) {} + ~FunctionRandomNormal(void) {} + std::string ComputableName(void) { return "RandomNormal"; } + virtual void OutputComputable(ContextInfo *info, ExpressionList *arg); +private: +}; + class FunctionIfThenElse : public Function { diff --git a/src/Vensim/VYacc.tab.cpp b/src/Vensim/VYacc.tab.cpp index 5bff093..696d67c 100644 --- a/src/Vensim/VYacc.tab.cpp +++ b/src/Vensim/VYacc.tab.cpp @@ -2045,13 +2045,13 @@ yyparse () case 81: /* Line 1792 of yacc.c */ #line 220 "C:\\sdev\\xmutil\\src\\Vensim\\VYacc.y" - { (yyval.exn) = vpyy_operator_expression('-',(yyvsp[(2) - (2)].exn),NULL) ; } + { (yyval.exn) = vpyy_operator_expression('-',NULL, (yyvsp[(2) - (2)].exn)) ; } break; case 82: /* Line 1792 of yacc.c */ #line 221 "C:\\sdev\\xmutil\\src\\Vensim\\VYacc.y" - { (yyval.exn) = vpyy_operator_expression('+',(yyvsp[(2) - (2)].exn),NULL) ; } + { (yyval.exn) = vpyy_operator_expression('+',NULL, (yyvsp[(2) - (2)].exn)) ; } break; case 83: diff --git a/src/Vensim/VYacc.y b/src/Vensim/VYacc.y index da54595..cd86d00 100644 --- a/src/Vensim/VYacc.y +++ b/src/Vensim/VYacc.y @@ -217,8 +217,8 @@ exp: | exp VPTT_and exp { $$ = vpyy_operator_expression(VPTT_and,$1,$3) ; } | VPTT_not exp { $$ = vpyy_operator_expression(VPTT_not,$2,NULL) ; } | exp '=' exp { $$ = vpyy_operator_expression('=',$1,$3) ; } - | '-' exp { $$ = vpyy_operator_expression('-',$2,NULL) ; } /* unary plus - might be used by numbers */ - | '+' exp { $$ = vpyy_operator_expression('+',$2,NULL) ; } /* unary plus - might be used by numbers */ + | '-' exp { $$ = vpyy_operator_expression('-',NULL, $2) ; } /* unary plus - might be used by numbers */ + | '+' exp { $$ = vpyy_operator_expression('+',NULL, $2) ; } /* unary plus - might be used by numbers */ | exp '^' exp { $$ = vpyy_operator_expression('^',$1,$3) ; } ; diff --git a/src/Vensim/VensimParse.cpp b/src/Vensim/VensimParse.cpp index 646ad8b..b0650f0 100644 --- a/src/Vensim/VensimParse.cpp +++ b/src/Vensim/VensimParse.cpp @@ -47,6 +47,7 @@ void VensimParse::ReadyFunctions() new FunctionInitial(pSymbolNameSpace); new FunctionSampleIfTrue(pSymbolNameSpace); new FunctionPulse(pSymbolNameSpace); + new FunctionPulseTrain(pSymbolNameSpace); new FunctionIfThenElse(pSymbolNameSpace); new FunctionLog(pSymbolNameSpace); new FunctionZidz(pSymbolNameSpace); @@ -60,6 +61,7 @@ void VensimParse::ReadyFunctions() new FunctionSmoothI(pSymbolNameSpace); new FunctionSmooth3(pSymbolNameSpace); new FunctionTrend(pSymbolNameSpace); + new FunctionDelay1(pSymbolNameSpace); new FunctionDelay3(pSymbolNameSpace); new FunctionDelay(pSymbolNameSpace); new FunctionDelayN(pSymbolNameSpace); @@ -78,6 +80,7 @@ void VensimParse::ReadyFunctions() new FunctionVectorSortOrder(pSymbolNameSpace); new FunctionGame(pSymbolNameSpace); new FunctionRandom01(pSymbolNameSpace); + new FunctionRandomUniform(pSymbolNameSpace); new FunctionAbs(pSymbolNameSpace); new FunctionExp(pSymbolNameSpace); new FunctionSqrt(pSymbolNameSpace);