@akala/core
Version:
145 lines • 4.69 kB
JavaScript
import ErrorWithStatus from "../../../errorWithStatus.js";
import { ExpressionVisitor } from "./expression-visitor.js";
/**
* Compares two expressions.
* @param {any} expr1 - The first expression.
* @param {any} expr2 - The second expression.
* @returns {boolean} True if the expressions are equal, false otherwise.
*/
export class ExpressionComparer extends ExpressionVisitor {
source;
constructor(source) {
super();
this.source = source;
}
static equals(exp1, exp2) {
if (exp1.type != exp2.type) {
debugger;
return false;
}
try {
new ExpressionComparer(exp1).visit(exp2);
}
catch (e) {
if (e.statusCode == 400)
return false;
throw e;
}
return true;
}
visitMember(arg0) {
if (this.source.type != arg0.type)
throw new ErrorWithStatus(400);
const exp1 = this.source;
if (arg0.source) {
this.source = arg0.source;
this.visit(arg0.source);
}
else if (this.source.source)
throw new ErrorWithStatus(400);
this.source = exp1.member;
this.visit(arg0.member);
return arg0;
}
visitApplySymbol(arg0) {
const exp = this.source;
if (exp.type != arg0.type)
throw new ErrorWithStatus(400);
this.source = exp.source;
this.visit(arg0.source);
this.source = exp.argument;
this.visit(arg0.argument);
return arg0;
}
visitBinary(expression) {
const exp = this.source;
if (exp.type != expression.type)
throw new ErrorWithStatus(400);
this.source = exp.left;
this.visit(expression.left);
this.source = exp.right;
this.visit(expression.right);
return expression;
}
visitCall(arg0) {
const exp = this.source;
if (exp.type != arg0.type)
throw new ErrorWithStatus(400);
this.source = exp.source;
this.visit(arg0.source);
if (arg0.arguments.length != exp.arguments.length)
throw new ErrorWithStatus(400);
this.visitArray(arg0.arguments, (_, i) => this.source = exp.arguments[i]);
return arg0;
}
visitConstant(arg0) {
if (this.source.type != arg0.type)
throw new ErrorWithStatus(400);
if (arg0.value != this.source.value)
throw new ErrorWithStatus(400);
return arg0;
}
visitFormat(expression) {
const exp = this.source;
if (exp.type != expression.type)
throw new ErrorWithStatus(400);
if (exp.formatter != expression.formatter)
throw new ErrorWithStatus(400);
this.source = exp.lhs;
this.visit(expression.lhs);
this.source = exp.settings;
if (expression.settings)
this.visit(expression.settings);
else if (this.source)
throw new ErrorWithStatus(400);
return expression;
}
visitLambda(arg0) {
const exp = this.source;
if (exp.type != arg0.type)
throw new ErrorWithStatus(400);
if (arg0.parameters.length != exp.parameters.length)
throw new ErrorWithStatus(400);
this.visitArray(arg0.parameters, (e, i) => this.source = exp.parameters[i]);
this.source = exp.body;
this.visit(arg0.body);
return arg0;
}
visitNew(arg0) {
const exp = this.source;
if (exp.type != arg0.type)
throw new ErrorWithStatus(400);
this.visitArray(arg0.init, (e, i) => this.source = exp.init[i]);
return arg0;
}
visitParameter(arg0) {
if (this.source.type != arg0.type)
throw new ErrorWithStatus(400);
if (arg0.name != this.source.name)
throw new ErrorWithStatus(400);
return arg0;
}
visitTernary(arg0) {
const exp = this.source;
if (exp.type != arg0.type)
throw new ErrorWithStatus(400);
this.source = exp.first;
this.visit(arg0.first);
this.source = exp.second;
this.visit(arg0.second);
this.source = exp.third;
this.visit(arg0.third);
return super.visitTernary(arg0);
}
visitUnary(arg0) {
const exp = this.source;
if (exp.type != arg0.type)
throw new ErrorWithStatus(400);
if (exp.operator != arg0.operator)
throw new ErrorWithStatus(400);
this.source = exp.operand;
this.visit(arg0.operand);
return arg0;
}
}
//# sourceMappingURL=expression-comparer.js.map