-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathstore.ts
107 lines (102 loc) · 3.14 KB
/
store.ts
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import type * as Ast from "../ast/ast";
import { throwInternalCompilerError } from "../error/errors";
import type { CompilerContext } from "./context";
import { createContextStore } from "./context";
import type { Source } from "../imports/source";
import type { Parser } from "../grammar";
/**
* Represents the storage for all AST-related data within the compiler context.
* @public
* @property functions AST entries representing top-level functions.
* @property constants AST entries representing top-level constant definitions.
* @property types AST entries representing structures, contracts, and traits.
*/
export type AstStore = {
sources: Source[];
funcSources: { code: string; path: string }[];
functions: (
| Ast.FunctionDef
| Ast.NativeFunctionDecl
| Ast.AsmFunctionDef
)[];
constants: Ast.ConstantDef[];
types: Ast.TypeDecl[];
};
const store = createContextStore<AstStore>();
/**
* Retrieves the raw AST for the given context.
* @public
* @param ctx The compiler context from which the AST is retrieved.
* @throws Will throw an error if the AST is not found in the context.
* @returns The AST types associated with the context.
*/
export function getRawAST(ctx: CompilerContext): AstStore {
const r = store.get(ctx, "types");
if (!r) {
throwInternalCompilerError("No AST found in context");
}
return r;
}
/**
* Parses multiple Tact source files into AST modules.
* @public
*/
export function parseModules(sources: Source[], parser: Parser): Ast.Module[] {
return sources.map((source) => parser.parse(source));
}
/**
* Extends the compiler context by adding AST entries and source information from
* given sources and parsed programs.
* @public
* @param modules Previously parsed sources.
* @returns The updated compiler context.
*/
export function openContext(
ctx: CompilerContext,
sources: Source[],
funcSources: { code: string; path: string }[],
modules: Ast.Module[],
): CompilerContext {
const types: Ast.TypeDecl[] = [];
const functions: (
| Ast.NativeFunctionDecl
| Ast.FunctionDef
| Ast.AsmFunctionDef
)[] = [];
const constants: Ast.ConstantDef[] = [];
for (const module of modules) {
for (const item of module.items) {
switch (item.kind) {
case "struct_decl":
case "message_decl":
case "contract":
case "trait":
case "primitive_type_decl":
{
types.push(item);
}
break;
case "function_def":
case "asm_function_def":
case "native_function_decl":
{
functions.push(item);
}
break;
case "constant_def":
{
constants.push(item);
}
break;
}
}
}
ctx = store.set(ctx, "types", {
sources,
funcSources,
functions,
constants,
types,
});
return ctx;
}