All files / src/ast/literal long-literal.ts

100% Statements 6/6
100% Branches 0/0
100% Functions 4/4
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34                      34x 34x           34x       1x       31x       2x      
import type { ExpressionState } from '../../expression-state.js';
import { TypedValue } from '../../typed-value.js';
import { Literal } from '../spel-node.js';
import { NodeType } from '../../language/node-type.js';
import { longLiteralKind, type NumericKind } from '../../type/numeric.js';
 
export class LongLiteral extends Literal {
  private readonly value: number | bigint;
  private readonly kind: NumericKind;
 
  constructor(startPos: number, endPos: number, value: number | bigint) {
    super(NodeType.LONG_LITERAL, startPos, endPos, String(value) + 'L');
    this.value = value;
    // A long literal is a `long` whatever its magnitude, unlike an unsuffixed
    // literal whose kind follows its size — otherwise `1L` is typed `int` and
    // `2147483647L + 1L` wraps at 32 bits. Beyond the range a JavaScript number
    // holds exactly the value is kept as a bigint, so `9007199254740993L` no
    // longer rounds to its neighbour.
    this.kind = longLiteralKind(this.value);
  }
 
  public getParsedValue(): number | bigint {
    return this.value;
  }
 
  public getValueInternal(_state: ExpressionState): TypedValue {
    return new TypedValue(this.value, null, this.kind);
  }
 
  public toStringAST(): string {
    return String(this.value) + 'L';
  }
}