/**
 * IR Builder
 *
 * Fluent API for constructing IR nodes
 */
import { PrimitiveType, Ownership } from './types.js';
/**
 * Fluent builder for IR construction
 */
export class IRBuilder {
    blockCounter = 0;
    variableVersions = new Map();
    /**
     * Create a new basic block
     */
    block(instructions, terminator) {
        return {
            id: this.blockCounter++,
            instructions,
            terminator,
        };
    }
    /**
     * Create a function body (AST-level statements)
     */
    functionBody(statements) {
        return {
            statements,
        };
    }
    /**
     * Create a new SSA variable version
     */
    variable(name, type) {
        const version = this.variableVersions.get(name) ?? 0;
        this.variableVersions.set(name, version + 1);
        return {
            kind: 'variable',
            name,
            version,
            type,
        };
    }
    /**
     * Reset variable versions (for new function scope)
     */
    resetVersions() {
        this.variableVersions.clear();
    }
}
// ============================================================================
// Type Constructors
// ============================================================================
export const types = {
    primitive(type) {
        return { kind: 'primitive', type };
    },
    number() {
        return { kind: 'primitive', type: PrimitiveType.Number };
    },
    integer() {
        return { kind: 'primitive', type: PrimitiveType.Integer };
    },
    integer53() {
        return { kind: 'primitive', type: PrimitiveType.Integer53 };
    },
    string() {
        return { kind: 'primitive', type: PrimitiveType.String };
    },
    boolean() {
        return { kind: 'primitive', type: PrimitiveType.Boolean };
    },
    void() {
        return { kind: 'primitive', type: PrimitiveType.Void };
    },
    class(name, ownership, typeArgs) {
        return { kind: 'class', name, ownership, typeArgs };
    },
    array(element, ownership) {
        return { kind: 'array', element, ownership: ownership ?? Ownership.Value };
    },
    map(key, value, ownership) {
        return { kind: 'map', key, value, ownership: ownership ?? Ownership.Value };
    },
    promise(resultType) {
        return { kind: 'promise', resultType };
    },
    nullable(inner) {
        return { kind: 'nullable', inner };
    },
    union(types) {
        return { kind: 'union', types };
    },
    intersection(types) {
        return { kind: 'intersection', types };
    },
    typeAlias(name, aliasedType) {
        return { kind: 'typeAlias', name, aliasedType };
    },
    function(params, returnType) {
        return { kind: 'function', params, returnType };
    },
    struct(fields, ownership) {
        return { kind: 'struct', fields, ownership: ownership ?? Ownership.Value };
    },
};
// ============================================================================
// Expression Constructors
// ============================================================================
export const expr = {
    literal(value, type) {
        return { kind: 'literal', value, type };
    },
    binary(op, left, right, type) {
        return { kind: 'binary', op, left, right, type };
    },
    unary(op, operand, type) {
        return { kind: 'unary', op, operand, type };
    },
    conditional(condition, whenTrue, whenFalse, type) {
        return { kind: 'conditional', condition, whenTrue, whenFalse, type };
    },
    member(object, member, type) {
        return { kind: 'member', object, member, type };
    },
    index(object, index, type) {
        return { kind: 'index', object, index, type };
    },
    call(callee, args, type) {
        return { kind: 'callExpr', callee, args, type };
    },
    methodCall(object, method, args, type) {
        return { kind: 'methodCall', object, method, args, type };
    },
    new(className, args, type) {
        return { kind: 'new', className, args, type };
    },
    array(elements, type) {
        return { kind: 'array', elements, type };
    },
    object(properties, type) {
        return { kind: 'object', properties, type };
    },
    lambda(params, body, captures, type) {
        return { kind: 'lambda', params, body, captures, type };
    },
    move(source, type) {
        return { kind: 'move', source, type };
    },
    borrow(source, type) {
        return { kind: 'borrow', source, type };
    },
    variable(name, version, type) {
        return { kind: 'variable', name, version, type };
    },
    fieldAccess(object, field, type, optional) {
        return { kind: 'member', object, member: field, optional, type };
    },
};
/**
 * Expression builders (for AST-level IR)
 */
export const exprs = {
    literal(value, type, location) {
        return { kind: 'literal', value, type, location };
    },
    identifier(name, type, location) {
        return { kind: 'identifier', name, type, location };
    },
    binary(operator, left, right, type, location) {
        return { kind: 'binary', operator, left, right, type, location };
    },
    unary(operator, operand, type, location) {
        return { kind: 'unary', operator, operand, type, location };
    },
    call(callee, args, type, location) {
        return { kind: 'call', callee, arguments: args, type, location };
    },
    memberAccess(object, member, type, optional, location) {
        return { kind: 'memberAccess', object, member, optional, type, location };
    },
    indexAccess(object, index, type, location) {
        return { kind: 'indexAccess', object, index, type, location };
    },
    assignment(left, right, type, location) {
        return { kind: 'assignment', left, right, type, location };
    },
    arrayLiteral(elements, type, location) {
        return { kind: 'arrayLiteral', elements, type, location };
    },
    objectLiteral(properties, type, location) {
        return { kind: 'objectLiteral', properties, type, location };
    },
};
/**
 * Statement builders (for AST-level IR)
 */
export const stmts = {
    variableDeclaration(name, variableType, initializer, location) {
        return {
            kind: 'variableDeclaration',
            name,
            variableType,
            initializer,
            location,
        };
    },
    return(value, location) {
        return {
            kind: 'return',
            value,
            location,
        };
    },
    expressionStatement(expression, location) {
        return {
            kind: 'expressionStatement',
            expression,
            location,
        };
    },
    if(condition, thenBranch, elseBranch, location) {
        return {
            kind: 'if',
            condition,
            thenBranch,
            elseBranch,
            location,
        };
    },
    while(condition, body, location) {
        return {
            kind: 'while',
            condition,
            body,
            location,
        };
    },
    for(initializer, condition, increment, body, location) {
        return {
            kind: 'for',
            initializer,
            condition,
            increment,
            body,
            location,
        };
    },
    block(statements, location) {
        return {
            kind: 'block',
            statements,
            location,
        };
    },
    try(tryBlock, catchClause, finallyBlock, location) {
        return {
            kind: 'try',
            tryBlock,
            catchClause,
            finallyBlock,
            location,
        };
    },
    throw(expression, location) {
        return {
            kind: 'throw',
            expression,
            location,
        };
    },
    forOf(variable, variableType, iterable, body, location) {
        return {
            kind: 'for-of',
            variable,
            variableType,
            iterable,
            body,
            location,
        };
    },
    break(location) {
        return {
            kind: 'break',
            location,
        };
    },
    continue(location) {
        return {
            kind: 'continue',
            location,
        };
    },
};
//# sourceMappingURL=builder.js.map