• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024-2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import { BigIntType, BooleanType, NullType, NumberType, StringType, Type, UndefinedType } from './Type';
17import { Value } from './Value';
18import { NULL_KEYWORD, UNDEFINED_KEYWORD } from '../common/TSConst';
19
20/**
21 * @category core/base
22 */
23export class Constant implements Value {
24    private readonly value: string;
25    private readonly type: Type;
26
27    constructor(value: string, type: Type) {
28        this.value = value;
29        this.type = type;
30    }
31
32    /**
33     * Returns the constant's value as a **string**.
34     * @returns The constant's value.
35     */
36    public getValue(): string {
37        return this.value;
38    }
39
40    public getUses(): Value[] {
41        return [];
42    }
43
44    /**
45     * Returns the type of this constant.
46     * @returns The type of this constant.
47     */
48    public getType(): Type {
49        return this.type;
50    }
51
52    /**
53     * Get a string of constant value in Constant.
54     * @returns The string of constant value.
55     */
56    public toString(): string {
57        let str = '';
58        if (this.type instanceof StringType) {
59            str = "'" + this.value + "'";
60        } else {
61            str = this.value;
62        }
63        return str;
64    }
65}
66
67export class BooleanConstant extends Constant {
68    private static readonly FALSE = new BooleanConstant(false);
69    private static readonly TRUE = new BooleanConstant(true);
70
71    constructor(value: boolean) {
72        super(value.toString(), BooleanType.getInstance());
73    }
74
75    public static getInstance(value: boolean): NullConstant {
76        return value ? this.TRUE : this.FALSE;
77    }
78}
79
80export class NumberConstant extends Constant {
81    constructor(value: number) {
82        super(value.toString(), NumberType.getInstance());
83    }
84}
85
86export class BigIntConstant extends Constant {
87    constructor(value: bigint) {
88        super(value.toString(), BigIntType.getInstance());
89    }
90}
91
92export class StringConstant extends Constant {
93    constructor(value: string) {
94        super(value.toString(), StringType.getInstance());
95    }
96}
97
98export class NullConstant extends Constant {
99    private static readonly INSTANCE = new NullConstant();
100
101    constructor() {
102        super(NULL_KEYWORD, NullType.getInstance());
103    }
104
105    public static getInstance(): NullConstant {
106        return this.INSTANCE;
107    }
108}
109
110export class UndefinedConstant extends Constant {
111    private static readonly INSTANCE = new UndefinedConstant();
112
113    constructor() {
114        super(UNDEFINED_KEYWORD, UndefinedType.getInstance());
115    }
116
117    public static getInstance(): UndefinedConstant {
118        return this.INSTANCE;
119    }
120}
121