simc-ast-builder
Version:
Parser and AST generator for SimulationCraft files
116 lines • 4.13 kB
JavaScript
;
/**
* Factory for creating ExpressionNode instances
* Provides helper methods for creating nodes with appropriate defaults
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.NodeFactory = void 0;
const NodeBuilder_1 = require("./NodeBuilder");
const NodeRegistry_1 = require("./NodeRegistry");
/**
* Factory for creating ExpressionNode instances
*/
class NodeFactory {
/**
* Create a basic node
* @param type Type of the node
* @param options Additional options for the node
* @returns A new ExpressionNode
*/
static createBasic(type, options = {}) {
return new NodeBuilder_1.NodeBuilder()
.withType(type)
.withExpressionType(options.expressionType || "neutral")
.withValue(options["value"])
.withProperties(options)
.build();
}
/**
* Create a binary operation node
* @param operator Operator for the binary operation
* @param left Left operand
* @param right Right operand
* @param expressionType Type of the expression
* @returns A new binary operation ExpressionNode
*/
static createBinaryOp(operator, left, right, expressionType = "neutral") {
return new NodeBuilder_1.NodeBuilder()
.withType(operator)
.withExpressionType(expressionType)
.withProperty("left", left)
.withProperty("right", right)
.build();
}
/**
* Create a Hekili node
* @param type Type of the node
* @param options Additional options for the node
* @returns A new ExpressionNode
*/
static createHekili(type, options = {}) {
// Check if we have a template for this type in the registry
if (NodeRegistry_1.NodeRegistry.hasTemplate(type)) {
return NodeRegistry_1.NodeRegistry.getTemplate(type).apply(options);
}
// Default behavior if no template exists
return new NodeBuilder_1.NodeBuilder()
.withType(type)
.withExpressionType(options.expressionType || "neutral")
.withProperties(options)
.build();
}
/**
* Create a SimC node
* @param type Type of the node
* @param options Additional options for the node
* @returns A new ExpressionNode
*/
static createSimC(type, options = {}) {
// Check if we have a template for this type in the registry
if (NodeRegistry_1.NodeRegistry.hasTemplate(type)) {
return NodeRegistry_1.NodeRegistry.getTemplate(type).apply(options);
}
// Default behavior if no template exists
return new NodeBuilder_1.NodeBuilder()
.withType(type)
.withExpressionType(options.expressionType || "neutral")
.withActionName(options["actionName"])
.withField(options["field"])
.withProperties(options)
.build();
}
/**
* Create a unary operation node
* @param operator Operator for the unary operation
* @param operand Operand
* @param expressionType Type of the expression
* @returns A new unary operation ExpressionNode
*/
static createUnaryOp(operator, operand, expressionType = "neutral") {
return new NodeBuilder_1.NodeBuilder()
.withType(operator)
.withExpressionType(expressionType)
.withProperty("operand", operand)
.build();
}
/**
* Create a node from a builder
* @param builderFn Function that configures a NodeBuilder
* @returns A new ExpressionNode
*/
static fromBuilder(builderFn) {
const builder = new NodeBuilder_1.NodeBuilder();
return builderFn(builder).build();
}
/**
* Create a node from a template
* @param template Template to use
* @param values Values to override template defaults
* @returns A new ExpressionNode
*/
static fromTemplate(template, values = {}) {
return template.apply(values);
}
}
exports.NodeFactory = NodeFactory;
//# sourceMappingURL=NodeFactory.js.map