|
1 |
| -import { DefinedUsing } from "../types/defined/defined-using"; |
2 |
| -import { Using } from "../types/defined"; |
3 |
| -import { Context, Queries, Temps, Variables, Geometries, Textures, Materials } from "./types"; |
4 |
| - |
5 |
| -/** Can either be a MolangSet or a MolangSet[] */ |
6 |
| -export type MolangSetOptional = MolangSet | MolangFullSet; |
| 1 | +import { OffsetWord } from "bc-minecraft-bedrock-types/lib/types"; |
| 2 | +import { MolangSyntaxCache } from "./cache"; |
| 3 | +import { isMolang, isValidMolang } from "./functions"; |
| 4 | +import { ExpressionNode, FunctionCallNode, NodeType, ResourceReferenceNode, VariableNode, walk } from "./syntax"; |
7 | 5 |
|
8 | 6 | /** The interface for the molang set */
|
9 |
| -export interface MolangSet { |
10 |
| - /** The set of contexts variables used*/ |
11 |
| - contexts: Using<string>; |
12 |
| - /** The set of queries used */ |
13 |
| - queries: Using<string>; |
14 |
| - /** The set of variables defined and used*/ |
15 |
| - variables: DefinedUsing<string>; |
16 |
| - /** The set of temps variables defined and used*/ |
17 |
| - temps: DefinedUsing<string>; |
18 |
| -} |
| 7 | +export class MolangSet { |
| 8 | + public cache = new MolangSyntaxCache(); |
| 9 | + public assigned = new Set<ResourceReferenceNode | VariableNode>(); |
| 10 | + public functions = new Set<FunctionCallNode>(); |
| 11 | + public using = new Set<ResourceReferenceNode | VariableNode>(); |
19 | 12 |
|
20 |
| -/** The interface for the molang with the full set of data */ |
21 |
| -export interface MolangFullSet extends MolangSet { |
22 |
| - /** The set of geometries used */ |
23 |
| - geometries: DefinedUsing<string>; |
24 |
| - /** The set of materials used */ |
25 |
| - materials: DefinedUsing<string>; |
26 |
| - /** The set of textures used */ |
27 |
| - textures: DefinedUsing<string>; |
28 |
| -} |
| 13 | + constructor() {} |
29 | 14 |
|
30 |
| -/** The namespace for the molang set */ |
31 |
| -export namespace MolangSet { |
32 | 15 | /**
|
33 |
| - * Creates a new MolangSet |
34 |
| - * @returns A new MolangSet |
| 16 | + * adds the data from the molang code if it is valid molang |
| 17 | + * @param molang |
35 | 18 | */
|
36 |
| - export function create(): MolangSet { |
37 |
| - return { |
38 |
| - queries: Using.create<string>(), |
39 |
| - variables: DefinedUsing.create<string>(), |
40 |
| - temps: DefinedUsing.create<string>(), |
41 |
| - contexts: Using.create<string>(), |
42 |
| - }; |
| 19 | + addIf(molang: OffsetWord) { |
| 20 | + if (isValidMolang(molang.text)) this.add(molang); |
43 | 21 | }
|
44 | 22 |
|
45 | 23 | /**
|
46 |
| - * Harvests molang data from the given object |
47 |
| - * @param object The object to harvest from |
48 |
| - * @returns The molang data |
| 24 | + * |
| 25 | + * @param molang |
| 26 | + * @returns |
49 | 27 | */
|
50 |
| - export function harvest(object: any): MolangSet { |
51 |
| - const out = create(); |
52 |
| - |
53 |
| - Queries.getUsing(object, out.queries.using); |
54 |
| - Context.getUsing(object, out.contexts.using); |
55 |
| - |
56 |
| - Variables.getUsing(object, out.variables.using); |
57 |
| - Variables.getDefined(object, out.variables.defined); |
58 |
| - |
59 |
| - Temps.getUsing(object, out.temps.using); |
60 |
| - Temps.getDefined(object, out.temps.defined); |
61 |
| - |
62 |
| - return out; |
| 28 | + add(molang: OffsetWord) { |
| 29 | + const exp = this.cache.build(molang); |
| 30 | + if (exp === undefined) return; |
| 31 | + exp.forEach((e) => walk(e, this.walkFn.bind(this))); |
63 | 32 | }
|
64 |
| -} |
65 | 33 |
|
66 |
| -/** The namespace for the full molang set */ |
67 |
| -export namespace MolangFullSet { |
68 |
| - /** |
69 |
| - * Creates a new MolangFullSet |
70 |
| - * @returns A new MolangFullSet |
71 |
| - */ |
72 |
| - export function create(): MolangFullSet { |
73 |
| - return { |
74 |
| - queries: Using.create<string>(), |
75 |
| - variables: DefinedUsing.create<string>(), |
76 |
| - temps: DefinedUsing.create<string>(), |
77 |
| - contexts: Using.create<string>(), |
78 |
| - geometries: DefinedUsing.empty(), |
79 |
| - materials: DefinedUsing.empty(), |
80 |
| - textures: DefinedUsing.empty(), |
81 |
| - }; |
82 |
| - } |
83 |
| - |
84 |
| - /** |
85 |
| - * Add the necessary properties to the given data set to the full MolangFullSet |
86 |
| - * @param data The data to add to |
87 |
| - */ |
88 |
| - export function upgrade(data: MolangSet): MolangFullSet { |
89 |
| - const out = data as MolangFullSet; |
90 |
| - |
91 |
| - if (!DefinedUsing.is<string>(out.geometries)) out.geometries = DefinedUsing.empty(); |
92 |
| - if (!DefinedUsing.is<string>(out.materials)) out.materials = DefinedUsing.empty(); |
93 |
| - if (!DefinedUsing.is<string>(out.textures)) out.textures = DefinedUsing.empty(); |
94 |
| - |
95 |
| - return out; |
96 |
| - } |
97 |
| - |
98 |
| - /** |
99 |
| - * Harvests molang data from the given object |
100 |
| - * @param object The object to harvest from |
101 |
| - */ |
102 |
| - export function harvest(object: object | string): MolangFullSet { |
103 |
| - const out = create(); |
104 |
| - |
105 |
| - Queries.getUsing(object, out.queries.using); |
106 |
| - Context.getUsing(object, out.contexts.using); |
107 |
| - |
108 |
| - Variables.getUsing(object, out.variables.using); |
109 |
| - Variables.getDefined(object, out.variables.defined); |
110 |
| - |
111 |
| - Temps.getUsing(object, out.temps.using); |
112 |
| - Temps.getDefined(object, out.temps.defined); |
113 |
| - |
114 |
| - Textures.getUsing(object, out.textures.using); |
115 |
| - Geometries.getUsing(object, out.geometries.using); |
116 |
| - Materials.getUsing(object, out.materials.using); |
117 |
| - |
118 |
| - return out; |
| 34 | + private walkFn(node: ExpressionNode): void { |
| 35 | + switch (node.type) { |
| 36 | + case NodeType.Assignment: |
| 37 | + this.checkAssigned(node.left); |
| 38 | + break; |
| 39 | + case NodeType.FunctionCall: |
| 40 | + this.functions.add(node); |
| 41 | + break; |
| 42 | + case NodeType.ResourceReference: |
| 43 | + case NodeType.Variable: |
| 44 | + if (this.assigned.has(node)) break; |
| 45 | + this.using.add(node); |
| 46 | + break; |
| 47 | + } |
119 | 48 | }
|
120 | 49 |
|
121 |
| - /** |
122 |
| - * Merges the given MolangFullSet into the given MolangFullSet |
123 |
| - * @param value |
124 |
| - */ |
125 |
| - export function isEither(value: MolangSetOptional): value is MolangFullSet { |
126 |
| - const temp = value as MolangFullSet; |
127 |
| - |
128 |
| - if ( |
129 |
| - typeof temp.geometries === "object" && |
130 |
| - typeof temp.materials === "object" && |
131 |
| - typeof temp.textures === "object" |
132 |
| - ) { |
133 |
| - return true; |
| 50 | + private checkAssigned(node: ExpressionNode): void { |
| 51 | + switch (node.type) { |
| 52 | + case NodeType.ResourceReference: |
| 53 | + case NodeType.Variable: |
| 54 | + this.assigned.add(node); |
| 55 | + break; |
134 | 56 | }
|
135 |
| - |
136 |
| - return false; |
137 | 57 | }
|
138 | 58 |
|
139 |
| - export function fromScript(script: { variables?: Record<string, string> }, addTo: MolangSet): void { |
140 |
| - if (script.variables) { |
141 |
| - const keys = Object.getOwnPropertyNames(script.variables); |
| 59 | + harvest(object: Record<string, any> | string, originalText: string) { |
| 60 | + if (typeof object === "string") { |
| 61 | + if (isMolang(object)) { |
| 62 | + this.add(OffsetWord.create(object, originalText.indexOf(object))); |
| 63 | + return; |
| 64 | + } |
| 65 | + } |
142 | 66 |
|
143 |
| - for (let I = 0; I < keys.length; I++) { |
144 |
| - const key = keys[I]; |
145 |
| - if (typeof key !== "string") { |
146 |
| - continue; |
147 |
| - } |
148 |
| - const index = key.indexOf("."); |
149 |
| - if (index < 0) { |
150 |
| - continue; |
| 67 | + for (const [, value] of Object.entries(object)) { |
| 68 | + if (typeof value === "string") { |
| 69 | + if (isMolang(value)) { |
| 70 | + this.add(OffsetWord.create(value, originalText.indexOf(value))); |
151 | 71 | }
|
152 |
| - const id = key.substring(index + 1); |
153 |
| - if (id && id.length > 0) { |
154 |
| - addTo.variables.defined.push(id); |
| 72 | + } else if (typeof value === "object") { |
| 73 | + if (Array.isArray(value)) { |
| 74 | + value.forEach((v) => this.harvest(v, originalText)); |
| 75 | + } else { |
| 76 | + this.harvest(value, originalText); |
155 | 77 | }
|
156 | 78 | }
|
157 | 79 | }
|
|
0 commit comments