@@ -200,6 +200,62 @@ class NumericValue extends Value {
200
200
201
201
}
202
202
203
+ /** Numeric values (ints and floats).
204
+ * Includes those occurring in the source as a literal
205
+ * or in a builtin module as a value.
206
+ */
207
+ class NumericValue extends Value {
208
+
209
+ NumericValue ( ) {
210
+ this .asBuiltin ( ) .getClass ( ) = theIntType ( ) .asBuiltin ( ) or
211
+ this .asBuiltin ( ) .getClass ( ) = theLongType ( ) .asBuiltin ( ) or
212
+ this .asBuiltin ( ) .getClass ( ) = theFloatType ( ) .asBuiltin ( )
213
+ }
214
+
215
+ /** Gets the Boolean value that this object
216
+ * would evaluate to in a Boolean context,
217
+ * such as `bool(x)` or `if x: ...`
218
+ */
219
+ override boolean booleanValue ( ) {
220
+ this .intValue ( ) != 0 and result = true
221
+ or
222
+ this .intValue ( ) = 0 and result = false
223
+ or
224
+ this .floatValue ( ) != 0 and result = true
225
+ or
226
+ this .floatValue ( ) = 0 and result = false
227
+ }
228
+
229
+ /** Gets the value of this object if it is a constant integer and it fits in a QL int */
230
+ int intValue ( ) {
231
+ (
232
+ this .asBuiltin ( ) .getClass ( ) = theIntType ( ) .asBuiltin ( ) or
233
+ this .asBuiltin ( ) .getClass ( ) = theLongType ( ) .asBuiltin ( )
234
+ )
235
+ and
236
+ result = this .asBuiltin ( ) .getName ( ) .toInt ( )
237
+ }
238
+
239
+ /** Gets the value of this object if it is a constant float */
240
+ float floatValue ( ) {
241
+ this .asBuiltin ( ) .getClass ( ) = theFloatType ( ) .asBuiltin ( )
242
+ and
243
+ result = this .asBuiltin ( ) .getName ( ) .toFloat ( )
244
+ }
245
+
246
+ /** Gets the string representation of this object, equivalent to calling repr() in Python */
247
+ string repr ( ) {
248
+ exists ( string s |
249
+ s = this .asBuiltin ( ) .getName ( ) |
250
+ if this .asBuiltin ( ) .getClass ( ) = theLongType ( ) .asBuiltin ( ) then
251
+ result = s + "L"
252
+ else
253
+ result = s
254
+ )
255
+ }
256
+
257
+ }
258
+
203
259
/** Class representing modules in the Python program
204
260
* Each `ModuleValue` represents a module object in the Python program.
205
261
*/
0 commit comments