36 using std::ostringstream;
37 using std::stringstream;
41 "",
"const ",
"urgent ",
"",
"broadcast ",
"",
"urgent broadcast ",
"",
45 void PrettyPrinter::indent()
47 for (uint32_t i = 0; i < level; i++)
59 select = guard = sync = update = probability = -1;
63 uint32_t position, uint32_t offset, uint32_t line,
const std::string& path)
70 throw std::runtime_error(msg);
75 throw std::runtime_error(msg);
86 type.push(type.top());
128 res +=
"int[" + l +
"," + u +
"]";
149 string size = st.back();
153 res +=
"scalar[" + size +
"]";
167 array.push(st.back());
173 array.push(type.top());
181 assert(fields.size()>=n);
182 for (std::vector<string>::const_iterator
183 i = fields.begin()+(fields.size()-n),
184 e = fields.end(); i != e; ++i)
186 ss <<
" " << (*i) <<
";\n";
189 fields.erase(fields.begin()+(fields.size()-n), fields.end());
196 ss << type.top() <<
" " << name;
198 while (!array.empty()) {
199 ss <<
"[" << array.top() <<
"]";
202 fields.push_back(ss.str());
208 *o.top() <<
"typedef " << type.top() <<
" " << name;
211 while (!array.empty())
213 *o.top() <<
'[' << array.top() <<
']';
217 *o.top() <<
';' << endl;
233 *o.top() << type.top() <<
' ' << id;
236 while (!array.empty())
238 *o.top() <<
'[' << array.top() <<
']';
244 *o.top() <<
" = " << i;
247 *o.top() <<
';' << endl;
252 string s = st.back();
256 s = st.back() +
", " + s;
259 st.push_back(
"{ " + s +
" }");
264 string s =
"scenario:";
271 const char *opString = NULL;
289 string s = st.back();
293 s = st.back() + opString + s;
296 st.push_back(
"{ " + s +
" }");
301 if (name && strlen(name))
303 st.back() = string(name) +
": " + st.back();
314 if (!param.empty()) param +=
", ";
318 param += type.top() +
" &" + name;
322 param += type.top() +
" " + name;
330 *o.top() << type.top() <<
" " << name <<
"(" << param <<
")" << endl;
332 *o.top() <<
"{" << endl;
342 *o.top() <<
"}" << endl;
349 *o.top() <<
"{" << endl;
358 *o.top() <<
"}" << endl;
364 *o.top() <<
';' << endl;
370 *o.top() <<
"for ( "<<
id <<
" : " << type.top() <<
" )" << endl;
384 o.push(
new ostringstream());
389 string expr3 = st.back(); st.pop_back();
390 string expr2 = st.back(); st.pop_back();
391 string expr1 = st.back(); st.pop_back();
392 ostringstream *s = (ostringstream*)o.top();
397 *o.top() <<
"for ( " << expr1 <<
"; " << expr2 <<
"; " 398 << expr3 <<
")" << endl
406 o.push(
new ostringstream());
411 string expr = st.back(); st.pop_back();
412 ostringstream *s = (ostringstream*)o.top();
418 *o.top() <<
"while (" << expr <<
")" << endl
436 o.push(
new ostringstream());
445 o.push(
new ostringstream());
450 auto e =
static_cast<ostringstream*
>(o.top()); o.pop();
451 auto t =
static_cast<ostringstream*
>(o.top()); o.pop();
452 auto c = st.back(); st.pop_back();
456 *o.top() <<
"if (" << c <<
")" << endl
462 *o.top() <<
"else" << endl;
463 *o.top() << e->str();
471 *o.top() <<
"break;" << endl;
477 *o.top() <<
"continue;" << endl;
483 *o.top() << st.back() <<
';' << endl;
492 *o.top() <<
"return " << st.back() <<
";" << endl;
497 *o.top() <<
"return;" << endl;
502 const string type,
const string mode)
504 *o.top() <<
"process " << (
id ? id :
"")
506 <<
'(' << param <<
")" << endl
520 *o.top() <<
"state\n";
540 *o.top() <<
" {" << st.back();
542 if (hasExpRate) *o.top() <<
" ; " << expRate;
547 *o.top() <<
" { ; " << expRate <<
"}";
553 if (!branchpoints.empty()) branchpoints +=
", ";
559 if (!urgent.empty()) urgent +=
", ";
565 if (!committed.empty()) committed +=
", ";
572 *o.top() <<
";" << endl;
574 if (!branchpoints.empty())
577 *o.top() <<
"branchpoint " << branchpoints <<
';' << endl;
578 branchpoints.clear();
581 if (!committed.empty())
584 *o.top() <<
"commit " << committed <<
';' << endl;
591 *o.top() <<
"urgent " << urgent <<
';' << endl;
596 *o.top() <<
"init " <<
id <<
';' << endl;
601 string t = type.top();
605 st.push_back(
string(
id) +
":" + t);
610 st.back() += string(
", ") +
id +
":" + t;
643 probability = st.size();
649 procEdgeBegin(from, to, control, NULL);
653 const bool control,
const char* actname)
661 *o.top() <<
"trans" << endl;
667 *o.top() <<
',' << endl;
671 *o.top() << source << (control ?
" -> " :
" -u-> ") << target <<
" {" << endl;
672 if (actname != NULL) {
675 *o.top() <<
"action " << actname <<
";" << endl;
687 *o.top() <<
"select " << st[select-1] <<
';' << endl;
693 *o.top() <<
"guard " << st[guard-1] <<
';' << endl;
699 *o.top() <<
"sync " << st[sync-1] <<
';' << endl;
705 *o.top() <<
"assign " << st[update-1] <<
';' << endl;
708 if (probability > -1)
711 *o.top() <<
"probability " << st[probability-1] <<
';' << endl;
716 if (probability > -1) st.pop_back();
717 if (update > -1) st.pop_back();
718 if (sync > -1) st.pop_back();
719 if (guard > -1) st.pop_back();
720 if (select > -1) st.pop_back();
722 probability = update = sync = guard = select = -1;
732 *o.top() <<
';' << endl;
737 *o.top() <<
'}' << endl << endl;
748 if (20 <= snprintf(s, 20,
"%d", n))
750 fprintf(stderr,
"Error: the integer number was truncated\n");
758 if (60 <= snprintf(s, 60,
"%.52g", d))
760 fprintf(stderr,
"Error: the floating point number was truncated\n");
767 st.push_back(
"true");
772 st.push_back(
"false");
797 string f = st.back();
799 st.back() +=
'[' + f +
']';
809 st.back() =
"++" + st.back();
819 st.back() =
"--" + st.back();
826 static const char* funNames[] = {
827 "abs",
"fabs",
"fmod",
"fma",
"fmax",
"fmin",
"fdim",
828 "exp",
"exp2",
"expm1",
"ln",
"log",
"log10",
"log2",
"log1p",
829 "pow",
"sqrt",
"cbrt",
"hypot",
830 "sin",
"cos",
"tan",
"asin",
"acos",
"atan",
"atan2",
831 "sinh",
"cosh",
"tanh",
"asinh",
"acosh",
"atanh",
832 "erf",
"erfc",
"tgamma",
"lgamma",
833 "ceil",
"floor",
"trunc",
"round",
"fint",
834 "ldexp",
"ilogb",
"logb",
"nextafter",
"copysign",
835 "fpclassify",
"isfinite",
"isinf",
"isnan",
"isnormal",
"signbit",
836 "isunordered",
"random",
"random_arcsine",
"random_beta",
837 "random_gamma",
"random_normal",
"random_poisson",
"random_weibull",
841 "Builtin function name list is wrong");
843 return funNames[kind-
ABS_F];
867 auto arg2 = st.back(); st.pop_back();
873 auto arg3 = st.back(); st.pop_back();
874 auto arg2 = st.back(); st.pop_back();
876 +
'(' + st.back()+
',' + arg2 +
',' + arg3 +
')';
881 string rhs = st.back(); st.pop_back();
882 string lhs = st.back(); st.pop_back();
884 st.push_back(
string());
888 st.back() =
'(' + lhs +
" = " + rhs +
')';
891 st.back() =
'(' + lhs +
" += " + rhs +
')';
894 st.back() =
'(' + lhs +
" -= " + rhs +
')';
897 st.back() =
'(' + lhs +
" *= " + rhs +
')';
900 st.back() =
'(' + lhs +
" /= " + rhs +
')';
903 st.back() =
'(' + lhs +
" %= " + rhs +
')';
906 st.back() =
'(' + lhs +
" |= " + rhs +
')';
909 st.back() =
'(' + lhs +
" &= " + rhs +
')';
912 st.back() =
'(' + lhs +
" ^= " + rhs +
')';
915 st.back() =
'(' + lhs +
" <<= " + rhs +
')';
918 st.back() =
'(' + lhs +
" >>= " + rhs +
')';
927 string exp = st.back(); st.pop_back();
929 st.push_back(
string());
933 st.back() =
'-' + exp;
936 st.back() =
'!' + exp;
939 st.back() =
'+' + exp;
942 st.back() = exp +
'\'';
945 st.back() =
"control_t*: " + exp;
948 st.back() =
"control: " + exp;
951 st.back() =
"E<> control: " + exp;
954 st.back() =
"implementation: " + exp;
957 st.back() =
"specification: " + exp;
966 string exp2 = st.back(); st.pop_back();
967 string exp1 = st.back(); st.pop_back();
969 st.push_back(
string());
973 st.back() = exp1 +
" control: " + exp2;
976 st.back() =
"control_t*(" + exp1 +
"): " + exp2;
979 st.back() =
'(' + exp1 +
" + " + exp2 +
')';
982 st.back() =
'(' + exp1 +
" - " + exp2 +
')';
985 st.back() =
'(' + exp1 +
" * " + exp2 +
')';
988 st.back() =
'(' + exp1 +
" / " + exp2 +
')';
991 st.back() =
'(' + exp1 +
" % " + exp2 +
')';
994 st.back() =
'(' + exp1 +
" : " + exp2 +
')';
997 st.back() =
'(' + exp1 +
" <? " + exp2 +
')';
1000 st.back() =
'(' + exp1 +
" >? " + exp2 +
')';
1003 st.back() =
'(' + exp1 +
" < " + exp2 +
')';
1006 st.back() =
"simulation: " + exp1 +
" <= " + exp2;
1009 st.back() =
"refinement: " + exp1 +
" <= " + exp2;
1012 st.back() =
'(' + exp1 +
" : " + exp2 +
')';
1015 st.back() =
'(' + exp1 +
" \\ " + exp2 +
')';
1018 st.back() =
'(' + exp1 +
" <= " + exp2 +
')';
1021 st.back() =
'(' + exp1 +
" == " + exp2 +
')';
1024 st.back() =
'(' + exp1 +
" != " + exp2 +
')';
1027 st.back() =
"simulation: " + exp1 +
" >= " + exp2;
1030 st.back() =
"refinement: " + exp1 +
" >= " + exp2;
1033 st.back() =
'(' + exp1 +
" >= " + exp2 +
')';
1036 st.back() =
'(' + exp1 +
" > " + exp2 +
')';
1039 st.back() =
'(' + exp1 +
" && " + exp2 +
')';
1042 st.back() =
'(' + exp1 +
" || " + exp2 +
')';
1045 st.back() =
'(' + exp1 +
" & " + exp2 +
')';
1048 st.back() =
'(' + exp1 +
" | " + exp2 +
')';
1051 st.back() =
'(' + exp1 +
" ^ " + exp2 +
')';
1054 st.back() =
'(' + exp1 +
" << " + exp2 +
')';
1057 st.back() =
'(' + exp1 +
" >> " + exp2 +
')';
1067 string exp3 = st.back(); st.pop_back();
1068 string exp2 = st.back(); st.pop_back();
1070 if (firstMissing) exp1 =
"1";
1071 else { exp1 = st.back(); st.pop_back(); }
1073 st.push_back(
string());
1077 st.back() =
"control_t*(" + exp1 +
"," + exp2 +
"): " + exp3;
1080 st.back() =
"control[" + exp1 +
"<=" + exp2 +
"]: " + exp3;
1089 string expr3 = st.back(); st.pop_back();
1090 string expr2 = st.back(); st.pop_back();
1091 string expr1 = st.back(); st.pop_back();
1093 st.push_back(
string());
1094 st.back() = expr1 +
" ? " + expr2 +
" : " + expr3;
1099 string expr2 = st.back(); st.pop_back();
1100 string expr1 = st.back(); st.pop_back();
1102 st.push_back(
string());
1103 st.back() = expr1 +
", " + expr2;
1108 st.back() = st.back() +
"." + field;
1113 st.push_back(
"deadlock");
1118 st.push_back(
string(
"forall (") + name +
":" + type.top() +
") ");
1124 string expr = st.back();
1131 st.push_back(
string(
"exists (") + name +
":" + type.top() +
") ");
1137 string expr = st.back();
1144 st.push_back(
string(
"sum (") + name +
":" + type.top() +
") ");
1150 string expr = st.back();
1157 *o.top() <<
"{" << endl;
1161 *o.top() << st.back() << endl;
1162 *o.top() <<
"}" << endl;
1167 *o.top() <<
"{" << endl;
1171 *o.top() << st.back() << endl;
1172 *o.top() <<
"}" << endl;
1181 const char* templ,
size_t arguments)
1190 *o.top() <<
id <<
" = " << templ <<
'(';
1193 *o.top() << s.top();
1200 *o.top() <<
");" << endl;
1207 *o.top() <<
"system " << id;
1212 *o.top() <<
", " << id;
1218 *o.top() <<
';' << endl;
1226 string pred2 = st.back(); st.pop_back();
1227 string pred1 = st.back(); st.pop_back();
1228 string bound = st.back(); st.pop_back();
1229 string bounded = st.back(); st.pop_back();
1243 string expr = st.back(); st.pop_back();
1245 ss <<
"(<>[" << low <<
"," << high <<
"] " << expr <<
")";
1246 st.push_back(ss.str());
1252 string expr = st.back(); st.pop_back();
1254 ss <<
"([][" << low <<
"," << high <<
"] " << expr <<
")";
1255 st.push_back(ss.str());
1260 int nbOfAcceptingRuns)
1263 string reachExpr = st.back();
1267 stack<string> exprs;
1268 for (
int i=0; i<nbExpr; i++)
1270 exprs.push(st.back()); st.pop_back();
1272 string nbRuns = st.back(); st.pop_back();
1273 string bound = st.back(); st.pop_back();
1274 string boundedExpr = st.back(); st.pop_back();
1275 if (boundedExpr==
"0")
1277 else if (boundedExpr==
"1")
1280 ss <<
"simulate [" << boundedExpr <<
"<=" << bound <<
"; "<< nbRuns <<
"] {";
1283 ss << exprs.top(); exprs.pop();
1284 while (!exprs.empty()) {
1285 ss <<
", " << exprs.top();
1292 if (nbOfAcceptingRuns>0)
1294 ss <<
" : " << nbOfAcceptingRuns;
1296 ss <<
" : " << reachExpr;
1298 st.push_back(ss.str());
1304 *o.top() <<
"\n/* Query begin: */" << endl;
1308 if (formula) *o.top() <<
"/* Formula: "<<formula <<
"*/" << endl;
1312 if (comment) *o.top() <<
"/* Comment: " << comment <<
"*/" << endl;
1316 *o.top() <<
"/* Query end. */" << endl;
void ifCondition() override
void exprDeadlock() override
void exprSumBegin(const char *name) override
void typeClock(PREFIX) override
Called whenever a clock type is parsed.
void beforeUpdate() override
void blockBegin() override
void typeName(PREFIX, const char *type) override
Called when a type name has been parsed.
void declParameter(const char *name, bool) override
void procStateCommit(const char *id) override
void whileBegin() override
bool isType(const char *) override
Must return true if and only if name is registered in the symbol table as a named type...
void exprPostIncrement() override
void exprPreIncrement() override
void typeArrayOfSize(size_t n) override
Called to create an array type.
static const char *const prefix_label[]
void typeDouble(PREFIX) override
Called whenever a double type is parsed.
void procBegin(const char *name, const bool isTA=true, const std::string type="", const std::string mode="") override
void exprFalse() override
void process(const char *id) override
void exprMitlDiamond(int, int) override
void procGuard() override
void instantiationEnd(const char *, size_t, const char *, size_t) override
void declFuncEnd() override
void returnStatement(bool hasValue) override
void typeStruct(PREFIX prefix, uint32_t n) override
Called when a struct-type has been parsed.
void iterationBegin(const char *name) override
void instantiationBegin(const char *, size_t, const char *) override
void exprNary(Constants::kind_t op, uint32_t num) override
void exprDot(const char *) override
void processListEnd() override
void exprExistsBegin(const char *name) override
void declVar(const char *id, bool init) override
Called to when a variable declaration has been parsed.
void exprAssignment(Constants::kind_t op) override
void exprInlineIf() override
void afterUpdate() override
void exprSimulate(int, bool, int) override
void typeBool(PREFIX) override
Called whenever a boolean type is parsed.
void exprId(const char *id) override
void addPosition(uint32_t position, uint32_t offset, uint32_t line, const std::string &path) override
Add mapping from an absolute position to a relative XML element.
void emptyStatement() override
void exprBuiltinFunction2(Constants::kind_t kind) override
void procSelect(const char *id) override
void exprForAllEnd(const char *name) override
void typeVoid() override
Called whenever a void type is parsed.
void procStateInit(const char *id) override
void exprNat(int32_t n) override
void iterationEnd(const char *name) override
void exprUnary(Constants::kind_t op) override
void typeBoundedInt(PREFIX) override
Called whenever an integer type with a range is parsed.
void typeChannel(PREFIX) override
Called whenever a channel type is parsed.
void procUpdate() override
void structField(const char *name) override
Called to declare a field of a structure.
void typeInt(PREFIX) override
Called whenever an integer type is parsed.
void queryBegin() override
Verification queries.
void breakStatement() override
void exprDouble(double d) override
void procEdgeBegin(const char *source, const char *target, const bool control)
void declFuncBegin(const char *name) override
Exception indicating a type error.
void exprArray() override
void ifEnd(bool) override
void exprExistsEnd(const char *name) override
void typePop() override
Pop type at the topof the type stack.
void exprCallBegin() override
void exprProbaQuantitative(Constants::kind_t) override
void handleError(const std::string &) override
void exprBuiltinFunction3(Constants::kind_t kind) override
void procSync(Constants::synchronisation_t type) override
void declTypeDef(const char *name) override
Used when a typedef declaration was parsed.
void exprCallEnd(uint32_t n) override
static const char * getBuiltinFunName(kind_t kind)
void exprPostDecrement() override
void declFieldInit(const char *name) override
void exprBuiltinFunction1(Constants::kind_t kind) override
void procState(const char *id, bool hasInvariant, bool hasExpRate) override
void procBranchpoint(const char *id) override
void typeDuplicate() override
Duplicate type at the top of the type stack.
void queryFormula(const char *formula, const char *location) override
void exprStatement() override
void exprTernary(Constants::kind_t op, bool firstMissing) override
void exprMitlBox(int, int) override
void exprScenario(const char *name) override
void procStateUrgent(const char *id) override
void typeScalar(PREFIX) override
Called whenever a scalar type is parsed.
void handleWarning(const std::string &) override
void exprBinary(Constants::kind_t op) override
void declInitialiserList(uint32_t num) override
void doWhileEnd() override
PrettyPrinter(std::ostream &stream)
void exprSumEnd(const char *name) override
void exprComma() override
void typeArrayOfType(size_t n) override
Called to create an array type.
void exprPreDecrement() override
void continueStatement() override
void queryComment(const char *comment) override
void exprForAllBegin(const char *name) override
void doWhileBegin() override
void procEdgeEnd(const char *source, const char *target) override