[6a3a178] | 1 | import {
|
---|
| 2 | AST_Array,
|
---|
| 3 | AST_Atom,
|
---|
| 4 | AST_Await,
|
---|
| 5 | AST_BigInt,
|
---|
| 6 | AST_Binary,
|
---|
| 7 | AST_Block,
|
---|
| 8 | AST_Call,
|
---|
| 9 | AST_Catch,
|
---|
| 10 | AST_Chain,
|
---|
| 11 | AST_Class,
|
---|
| 12 | AST_ClassProperty,
|
---|
| 13 | AST_ConciseMethod,
|
---|
| 14 | AST_Conditional,
|
---|
| 15 | AST_Debugger,
|
---|
| 16 | AST_Definitions,
|
---|
| 17 | AST_Destructuring,
|
---|
| 18 | AST_Directive,
|
---|
| 19 | AST_Do,
|
---|
| 20 | AST_Dot,
|
---|
| 21 | AST_DotHash,
|
---|
| 22 | AST_EmptyStatement,
|
---|
| 23 | AST_Expansion,
|
---|
| 24 | AST_Export,
|
---|
| 25 | AST_Finally,
|
---|
| 26 | AST_For,
|
---|
| 27 | AST_ForIn,
|
---|
| 28 | AST_ForOf,
|
---|
| 29 | AST_If,
|
---|
| 30 | AST_Import,
|
---|
| 31 | AST_ImportMeta,
|
---|
| 32 | AST_Jump,
|
---|
| 33 | AST_LabeledStatement,
|
---|
| 34 | AST_Lambda,
|
---|
| 35 | AST_LoopControl,
|
---|
| 36 | AST_NameMapping,
|
---|
| 37 | AST_NewTarget,
|
---|
| 38 | AST_Node,
|
---|
| 39 | AST_Number,
|
---|
| 40 | AST_Object,
|
---|
| 41 | AST_ObjectGetter,
|
---|
| 42 | AST_ObjectKeyVal,
|
---|
| 43 | AST_ObjectProperty,
|
---|
| 44 | AST_ObjectSetter,
|
---|
| 45 | AST_PrefixedTemplateString,
|
---|
| 46 | AST_PropAccess,
|
---|
| 47 | AST_RegExp,
|
---|
| 48 | AST_Sequence,
|
---|
| 49 | AST_SimpleStatement,
|
---|
| 50 | AST_String,
|
---|
| 51 | AST_Super,
|
---|
| 52 | AST_Switch,
|
---|
| 53 | AST_SwitchBranch,
|
---|
| 54 | AST_Symbol,
|
---|
| 55 | AST_TemplateSegment,
|
---|
| 56 | AST_TemplateString,
|
---|
| 57 | AST_This,
|
---|
| 58 | AST_Toplevel,
|
---|
| 59 | AST_Try,
|
---|
| 60 | AST_Unary,
|
---|
| 61 | AST_VarDef,
|
---|
| 62 | AST_While,
|
---|
| 63 | AST_With,
|
---|
| 64 | AST_Yield
|
---|
| 65 | } from "./ast.js";
|
---|
| 66 |
|
---|
| 67 | const shallow_cmp = (node1, node2) => {
|
---|
| 68 | return (
|
---|
| 69 | node1 === null && node2 === null
|
---|
| 70 | || node1.TYPE === node2.TYPE && node1.shallow_cmp(node2)
|
---|
| 71 | );
|
---|
| 72 | };
|
---|
| 73 |
|
---|
| 74 | export const equivalent_to = (tree1, tree2) => {
|
---|
| 75 | if (!shallow_cmp(tree1, tree2)) return false;
|
---|
| 76 | const walk_1_state = [tree1];
|
---|
| 77 | const walk_2_state = [tree2];
|
---|
| 78 |
|
---|
| 79 | const walk_1_push = walk_1_state.push.bind(walk_1_state);
|
---|
| 80 | const walk_2_push = walk_2_state.push.bind(walk_2_state);
|
---|
| 81 |
|
---|
| 82 | while (walk_1_state.length && walk_2_state.length) {
|
---|
| 83 | const node_1 = walk_1_state.pop();
|
---|
| 84 | const node_2 = walk_2_state.pop();
|
---|
| 85 |
|
---|
| 86 | if (!shallow_cmp(node_1, node_2)) return false;
|
---|
| 87 |
|
---|
| 88 | node_1._children_backwards(walk_1_push);
|
---|
| 89 | node_2._children_backwards(walk_2_push);
|
---|
| 90 |
|
---|
| 91 | if (walk_1_state.length !== walk_2_state.length) {
|
---|
| 92 | // Different number of children
|
---|
| 93 | return false;
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | return walk_1_state.length == 0 && walk_2_state.length == 0;
|
---|
| 98 | };
|
---|
| 99 |
|
---|
| 100 | // Creates a shallow compare function
|
---|
| 101 | const mkshallow = (props) => {
|
---|
| 102 | const comparisons = Object
|
---|
| 103 | .keys(props)
|
---|
| 104 | .map(key => {
|
---|
| 105 | if (props[key] === "eq") {
|
---|
| 106 | return `this.${key} === other.${key}`;
|
---|
| 107 | } else if (props[key] === "exist") {
|
---|
| 108 | return `(this.${key} == null ? other.${key} == null : this.${key} === other.${key})`;
|
---|
| 109 | } else {
|
---|
| 110 | throw new Error(`mkshallow: Unexpected instruction: ${props[key]}`);
|
---|
| 111 | }
|
---|
| 112 | })
|
---|
| 113 | .join(" && ");
|
---|
| 114 |
|
---|
| 115 | return new Function("other", "return " + comparisons);
|
---|
| 116 | };
|
---|
| 117 |
|
---|
| 118 | const pass_through = () => true;
|
---|
| 119 |
|
---|
| 120 | AST_Node.prototype.shallow_cmp = function () {
|
---|
| 121 | throw new Error("did not find a shallow_cmp function for " + this.constructor.name);
|
---|
| 122 | };
|
---|
| 123 |
|
---|
| 124 | AST_Debugger.prototype.shallow_cmp = pass_through;
|
---|
| 125 |
|
---|
| 126 | AST_Directive.prototype.shallow_cmp = mkshallow({ value: "eq" });
|
---|
| 127 |
|
---|
| 128 | AST_SimpleStatement.prototype.shallow_cmp = pass_through;
|
---|
| 129 |
|
---|
| 130 | AST_Block.prototype.shallow_cmp = pass_through;
|
---|
| 131 |
|
---|
| 132 | AST_EmptyStatement.prototype.shallow_cmp = pass_through;
|
---|
| 133 |
|
---|
| 134 | AST_LabeledStatement.prototype.shallow_cmp = mkshallow({ "label.name": "eq" });
|
---|
| 135 |
|
---|
| 136 | AST_Do.prototype.shallow_cmp = pass_through;
|
---|
| 137 |
|
---|
| 138 | AST_While.prototype.shallow_cmp = pass_through;
|
---|
| 139 |
|
---|
| 140 | AST_For.prototype.shallow_cmp = mkshallow({
|
---|
| 141 | init: "exist",
|
---|
| 142 | condition: "exist",
|
---|
| 143 | step: "exist"
|
---|
| 144 | });
|
---|
| 145 |
|
---|
| 146 | AST_ForIn.prototype.shallow_cmp = pass_through;
|
---|
| 147 |
|
---|
| 148 | AST_ForOf.prototype.shallow_cmp = pass_through;
|
---|
| 149 |
|
---|
| 150 | AST_With.prototype.shallow_cmp = pass_through;
|
---|
| 151 |
|
---|
| 152 | AST_Toplevel.prototype.shallow_cmp = pass_through;
|
---|
| 153 |
|
---|
| 154 | AST_Expansion.prototype.shallow_cmp = pass_through;
|
---|
| 155 |
|
---|
| 156 | AST_Lambda.prototype.shallow_cmp = mkshallow({
|
---|
| 157 | is_generator: "eq",
|
---|
| 158 | async: "eq"
|
---|
| 159 | });
|
---|
| 160 |
|
---|
| 161 | AST_Destructuring.prototype.shallow_cmp = mkshallow({
|
---|
| 162 | is_array: "eq"
|
---|
| 163 | });
|
---|
| 164 |
|
---|
| 165 | AST_PrefixedTemplateString.prototype.shallow_cmp = pass_through;
|
---|
| 166 |
|
---|
| 167 | AST_TemplateString.prototype.shallow_cmp = pass_through;
|
---|
| 168 |
|
---|
| 169 | AST_TemplateSegment.prototype.shallow_cmp = mkshallow({
|
---|
| 170 | "value": "eq"
|
---|
| 171 | });
|
---|
| 172 |
|
---|
| 173 | AST_Jump.prototype.shallow_cmp = pass_through;
|
---|
| 174 |
|
---|
| 175 | AST_LoopControl.prototype.shallow_cmp = pass_through;
|
---|
| 176 |
|
---|
| 177 | AST_Await.prototype.shallow_cmp = pass_through;
|
---|
| 178 |
|
---|
| 179 | AST_Yield.prototype.shallow_cmp = mkshallow({
|
---|
| 180 | is_star: "eq"
|
---|
| 181 | });
|
---|
| 182 |
|
---|
| 183 | AST_If.prototype.shallow_cmp = mkshallow({
|
---|
| 184 | alternative: "exist"
|
---|
| 185 | });
|
---|
| 186 |
|
---|
| 187 | AST_Switch.prototype.shallow_cmp = pass_through;
|
---|
| 188 |
|
---|
| 189 | AST_SwitchBranch.prototype.shallow_cmp = pass_through;
|
---|
| 190 |
|
---|
| 191 | AST_Try.prototype.shallow_cmp = mkshallow({
|
---|
| 192 | bcatch: "exist",
|
---|
| 193 | bfinally: "exist"
|
---|
| 194 | });
|
---|
| 195 |
|
---|
| 196 | AST_Catch.prototype.shallow_cmp = mkshallow({
|
---|
| 197 | argname: "exist"
|
---|
| 198 | });
|
---|
| 199 |
|
---|
| 200 | AST_Finally.prototype.shallow_cmp = pass_through;
|
---|
| 201 |
|
---|
| 202 | AST_Definitions.prototype.shallow_cmp = pass_through;
|
---|
| 203 |
|
---|
| 204 | AST_VarDef.prototype.shallow_cmp = mkshallow({
|
---|
| 205 | value: "exist"
|
---|
| 206 | });
|
---|
| 207 |
|
---|
| 208 | AST_NameMapping.prototype.shallow_cmp = pass_through;
|
---|
| 209 |
|
---|
| 210 | AST_Import.prototype.shallow_cmp = mkshallow({
|
---|
| 211 | imported_name: "exist",
|
---|
| 212 | imported_names: "exist"
|
---|
| 213 | });
|
---|
| 214 |
|
---|
| 215 | AST_ImportMeta.prototype.shallow_cmp = pass_through;
|
---|
| 216 |
|
---|
| 217 | AST_Export.prototype.shallow_cmp = mkshallow({
|
---|
| 218 | exported_definition: "exist",
|
---|
| 219 | exported_value: "exist",
|
---|
| 220 | exported_names: "exist",
|
---|
| 221 | module_name: "eq",
|
---|
| 222 | is_default: "eq",
|
---|
| 223 | });
|
---|
| 224 |
|
---|
| 225 | AST_Call.prototype.shallow_cmp = pass_through;
|
---|
| 226 |
|
---|
| 227 | AST_Sequence.prototype.shallow_cmp = pass_through;
|
---|
| 228 |
|
---|
| 229 | AST_PropAccess.prototype.shallow_cmp = pass_through;
|
---|
| 230 |
|
---|
| 231 | AST_Chain.prototype.shallow_cmp = pass_through;
|
---|
| 232 |
|
---|
| 233 | AST_Dot.prototype.shallow_cmp = mkshallow({
|
---|
| 234 | property: "eq"
|
---|
| 235 | });
|
---|
| 236 |
|
---|
| 237 | AST_DotHash.prototype.shallow_cmp = mkshallow({
|
---|
| 238 | property: "eq"
|
---|
| 239 | });
|
---|
| 240 |
|
---|
| 241 | AST_Unary.prototype.shallow_cmp = mkshallow({
|
---|
| 242 | operator: "eq"
|
---|
| 243 | });
|
---|
| 244 |
|
---|
| 245 | AST_Binary.prototype.shallow_cmp = mkshallow({
|
---|
| 246 | operator: "eq"
|
---|
| 247 | });
|
---|
| 248 |
|
---|
| 249 | AST_Conditional.prototype.shallow_cmp = pass_through;
|
---|
| 250 |
|
---|
| 251 | AST_Array.prototype.shallow_cmp = pass_through;
|
---|
| 252 |
|
---|
| 253 | AST_Object.prototype.shallow_cmp = pass_through;
|
---|
| 254 |
|
---|
| 255 | AST_ObjectProperty.prototype.shallow_cmp = pass_through;
|
---|
| 256 |
|
---|
| 257 | AST_ObjectKeyVal.prototype.shallow_cmp = mkshallow({
|
---|
| 258 | key: "eq"
|
---|
| 259 | });
|
---|
| 260 |
|
---|
| 261 | AST_ObjectSetter.prototype.shallow_cmp = mkshallow({
|
---|
| 262 | static: "eq"
|
---|
| 263 | });
|
---|
| 264 |
|
---|
| 265 | AST_ObjectGetter.prototype.shallow_cmp = mkshallow({
|
---|
| 266 | static: "eq"
|
---|
| 267 | });
|
---|
| 268 |
|
---|
| 269 | AST_ConciseMethod.prototype.shallow_cmp = mkshallow({
|
---|
| 270 | static: "eq",
|
---|
| 271 | is_generator: "eq",
|
---|
| 272 | async: "eq",
|
---|
| 273 | });
|
---|
| 274 |
|
---|
| 275 | AST_Class.prototype.shallow_cmp = mkshallow({
|
---|
| 276 | name: "exist",
|
---|
| 277 | extends: "exist",
|
---|
| 278 | });
|
---|
| 279 |
|
---|
| 280 | AST_ClassProperty.prototype.shallow_cmp = mkshallow({
|
---|
| 281 | static: "eq"
|
---|
| 282 | });
|
---|
| 283 |
|
---|
| 284 | AST_Symbol.prototype.shallow_cmp = mkshallow({
|
---|
| 285 | name: "eq"
|
---|
| 286 | });
|
---|
| 287 |
|
---|
| 288 | AST_NewTarget.prototype.shallow_cmp = pass_through;
|
---|
| 289 |
|
---|
| 290 | AST_This.prototype.shallow_cmp = pass_through;
|
---|
| 291 |
|
---|
| 292 | AST_Super.prototype.shallow_cmp = pass_through;
|
---|
| 293 |
|
---|
| 294 | AST_String.prototype.shallow_cmp = mkshallow({
|
---|
| 295 | value: "eq"
|
---|
| 296 | });
|
---|
| 297 |
|
---|
| 298 | AST_Number.prototype.shallow_cmp = mkshallow({
|
---|
| 299 | value: "eq"
|
---|
| 300 | });
|
---|
| 301 |
|
---|
| 302 | AST_BigInt.prototype.shallow_cmp = mkshallow({
|
---|
| 303 | value: "eq"
|
---|
| 304 | });
|
---|
| 305 |
|
---|
| 306 | AST_RegExp.prototype.shallow_cmp = function (other) {
|
---|
| 307 | return (
|
---|
| 308 | this.value.flags === other.value.flags
|
---|
| 309 | && this.value.source === other.value.source
|
---|
| 310 | );
|
---|
| 311 | };
|
---|
| 312 |
|
---|
| 313 | AST_Atom.prototype.shallow_cmp = pass_through;
|
---|