Repository navigation
Expand file tree
/
Copy pathvariable.ts
More file actions
51 lines (43 loc) · 1.57 KB
/
Copy pathvariable.ts
File metadata and controls
51 lines (43 loc) · 1.57 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/** @module assemblyscript/reflection */ /** */
import Compiler from "../compiler";
import Type from "./type";
/** A reflected variable. */
export class Variable {
/** Compiler reference. */
compiler: Compiler;
/** Simple or global name, depending on context. */
name: string;
/** Reflected type. */
type: Type;
/** Whether mutable or not. */
mutable: boolean;
/** Local index, if applicable. */
localIndex: number;
/** Constant value, if applicable. */
constantValue: number | Long | null;
/** Constructs a new reflected variable. */
constructor(compiler: Compiler, name: string, type: Type, mutable: boolean = true, localIndex: number = -1, constantValue: number | Long | null = null) {
this.compiler = compiler;
this.name = name;
this.type = type;
this.mutable = mutable;
this.localIndex = localIndex;
this.constantValue = constantValue;
// register
if (localIndex < 0) {
if (compiler.globals[this.name])
throw Error("duplicate global: " + this.name);
compiler.globals[this.name] = this;
}
}
/** Tests if this variable is declared constant. */
get isConstant(): boolean { return !this.mutable; }
/** Tests if this is a global variable. */
get isGlobal(): boolean { return this.localIndex < 0; }
/** Tests if this is a local variable. */
get isLocal(): boolean { return this.localIndex >= 0; }
/** Tests if this variable's value is inlined. */
get isInlined(): boolean { return this.isConstant && this.constantValue !== null; }
toString(): string { return this.name; }
}
export default Variable;