this repo has no description
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2
3/*
4 * Main authors:
5 * Guido Tack <guido.tack@monash.edu>
6 */
7
8/* This Source Code Form is subject to the terms of the Mozilla Public
9 * License, v. 2.0. If a copy of the MPL was not distributed with this
10 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
11
12#include <minizinc/flatten_internal.hh>
13#include <minizinc/type.hh>
14
15namespace MiniZinc {
16
17std::string Type::toString(EnvI& env) const {
18 std::ostringstream oss;
19 if (_dim > 0) {
20 oss << "array[";
21 if (_enumId != 0U) {
22 const std::vector<unsigned int>& arrayEnumIds = env.getArrayEnum(_enumId);
23 for (unsigned int i = 0; i < arrayEnumIds.size() - 1; i++) {
24 if (i != 0) {
25 oss << ",";
26 }
27 unsigned int enumId = arrayEnumIds[i];
28 if (enumId == 0) {
29 oss << "int";
30 } else {
31 oss << *env.getEnum(enumId)->e()->id();
32 }
33 }
34 } else {
35 for (int i = 0; i < _dim; i++) {
36 oss << (i == 0 ? "" : ",") << "int";
37 }
38 }
39 oss << "] of ";
40 }
41 if (_dim < 0) {
42 oss << "array[$_] of ";
43 }
44 switch (static_cast<int>(_ti)) {
45 case TI_PAR:
46 break;
47 case TI_VAR:
48 oss << "var ";
49 break;
50 }
51 if (static_cast<OptType>(_ot) == OT_OPTIONAL) {
52 oss << "opt ";
53 }
54 if (static_cast<SetType>(_st) == ST_SET) {
55 oss << "set of ";
56 }
57 switch (static_cast<BaseType>(_bt)) {
58 case BT_INT: {
59 unsigned int enumId;
60 if (_enumId != 0U && _dim > 0) {
61 const std::vector<unsigned int>& arrayEnumIds = env.getArrayEnum(_enumId);
62 enumId = arrayEnumIds[arrayEnumIds.size() - 1];
63 } else {
64 enumId = _enumId;
65 }
66 if (enumId == 0) {
67 oss << "int";
68 } else {
69 oss << *env.getEnum(enumId)->e()->id();
70 }
71 } break;
72 case BT_BOOL:
73 oss << "bool";
74 break;
75 case BT_FLOAT:
76 oss << "float";
77 break;
78 case BT_STRING:
79 oss << "string";
80 break;
81 case BT_ANN:
82 oss << "ann";
83 break;
84 case BT_BOT:
85 oss << "bot";
86 break;
87 case BT_TOP:
88 oss << "top";
89 break;
90 case BT_UNKNOWN:
91 oss << "??? ";
92 break;
93 }
94 return oss.str();
95}
96
97std::string Type::nonEnumToString() const {
98 std::ostringstream oss;
99 if (_dim > 0) {
100 oss << "array[int";
101 for (int i = 1; i < _dim; i++) {
102 oss << ",int";
103 }
104 oss << "] of ";
105 }
106 if (_dim < 0) {
107 oss << "array[$_] of ";
108 }
109 switch (static_cast<TypeInst>(_ti)) {
110 case TI_PAR:
111 break;
112 case TI_VAR:
113 oss << "var ";
114 break;
115 }
116 if (static_cast<OptType>(_ot) == OT_OPTIONAL) {
117 oss << "opt ";
118 }
119 if (static_cast<SetType>(_st) == ST_SET) {
120 oss << "set of ";
121 }
122 switch (static_cast<BaseType>(_bt)) {
123 case BT_INT:
124 oss << "int";
125 break;
126 case BT_BOOL:
127 oss << "bool";
128 break;
129 case BT_FLOAT:
130 oss << "float";
131 break;
132 case BT_STRING:
133 oss << "string";
134 break;
135 case BT_ANN:
136 oss << "ann";
137 break;
138 case BT_BOT:
139 oss << "bot";
140 break;
141 case BT_TOP:
142 oss << "top";
143 break;
144 case BT_UNKNOWN:
145 oss << "??? ";
146 break;
147 }
148 return oss.str();
149}
150
151} // namespace MiniZinc