/** * Intermediate Representation (IR) for Elo * * The IR is a typed representation of Elo expressions where: * - Literals carry their type explicitly * - Operators are replaced by typed function calls * - Temporal keywords are replaced by function calls * * This allows target compilers to generate optimal code based on types. */ import { EloType } from "./types"; /** * IR expression types */ export type IRExpr = IRIntLiteral | IRFloatLiteral | IRBoolLiteral | IRNullLiteral | IRStringLiteral | IRDateLiteral | IRDateTimeLiteral | IRDurationLiteral | IRObjectLiteral | IRArrayLiteral | IRVariable | IRCall | IRApply | IRLet | IRMemberAccess | IRIf | IRLambda | IRAlternative | IRDataPath | IRTypeDef | IRGuard; /** * Integer literal */ export interface IRIntLiteral { type: "int_literal"; value: number; } /** * Float literal */ export interface IRFloatLiteral { type: "float_literal"; value: number; } /** * Boolean literal */ export interface IRBoolLiteral { type: "bool_literal"; value: boolean; } /** * Null literal */ export interface IRNullLiteral { type: "null_literal"; } /** * String literal */ export interface IRStringLiteral { type: "string_literal"; value: string; } /** * Date literal (ISO8601 date) */ export interface IRDateLiteral { type: "date_literal"; value: string; } /** * DateTime literal (ISO8601 datetime) */ export interface IRDateTimeLiteral { type: "datetime_literal"; value: string; } /** * Duration literal (ISO8601 duration) */ export interface IRDurationLiteral { type: "duration_literal"; value: string; } /** * Object literal property */ export interface IRObjectProperty { key: string; value: IRExpr; } /** * Object literal: {key: value, ...} */ export interface IRObjectLiteral { type: "object_literal"; properties: IRObjectProperty[]; } /** * Array literal: [expr, expr, ...] */ export interface IRArrayLiteral { type: "array_literal"; elements: IRExpr[]; } /** * Variable reference with inferred type */ export interface IRVariable { type: "variable"; name: string; inferredType: EloType; } /** * Function call (includes operators rewritten as functions) * * The fn field contains a simple function name (e.g., 'add', 'sub', 'mul') * rather than a type-mangled name. The argTypes array provides the types * of each argument, allowing compilers to dispatch to the correct implementation. */ export interface IRCall { type: "call"; fn: string; args: IRExpr[]; argTypes: EloType[]; resultType: EloType; } /** * Lambda application (calling a lambda stored in a variable) * * Unlike IRCall which dispatches to stdlib functions, IRApply calls * a lambda expression that's been bound to a variable. */ export interface IRApply { type: "apply"; fn: IRExpr; args: IRExpr[]; argTypes: EloType[]; resultType: EloType; } /** * Let binding */ export interface IRLetBinding { name: string; value: IRExpr; } /** * Let expression */ export interface IRLet { type: "let"; bindings: IRLetBinding[]; body: IRExpr; } /** * Member access (dot notation) */ export interface IRMemberAccess { type: "member_access"; object: IRExpr; property: string; } /** * If expression: if condition then consequent else alternative */ export interface IRIf { type: "if"; condition: IRExpr; then: IRExpr; else: IRExpr; } /** * Lambda parameter with inferred type */ export interface IRLambdaParam { name: string; inferredType: EloType; } /** * Lambda expression: fn( params ~> body ) */ export interface IRLambda { type: "lambda"; params: IRLambdaParam[]; body: IRExpr; resultType: EloType; } /** * Alternative expression: a | b | c * Evaluates alternatives left-to-right, returns first non-null value. */ export interface IRAlternative { type: "alternative"; alternatives: IRExpr[]; resultType: EloType; } /** * DataPath literal: .x.y.z or .items.0.name * A path for navigating data structures. * Segments can be property names (strings) or array indices (numbers). */ export interface IRDataPath { type: "datapath"; segments: (string | number)[]; } /** * IR type expression (used in type definitions) */ export type IRTypeExpr = IRTypeRef | IRTypeSchema | IRSubtypeConstraint | IRArrayType | IRUnionType; /** * Reference to a base type */ export interface IRTypeRef { kind: "type_ref"; name: string; } /** * Object type schema * extras controls handling of extra attributes: * - undefined/'closed': extra attributes are not allowed (default) * - 'ignored': extra attributes are allowed but not included in output * - IRTypeExpr: extra attributes are allowed and must match this type */ export interface IRTypeSchema { kind: "type_schema"; properties: IRTypeSchemaProperty[]; extras?: "closed" | "ignored" | IRTypeExpr; } /** * A single constraint with optional label (IR version) */ export interface IRConstraint { label?: string; condition: IRExpr; } /** * Subtype constraint: Int(i | i > 0) or Int(i | positive: i > 0, even: i % 2 == 0) */ export interface IRSubtypeConstraint { kind: "subtype_constraint"; baseType: IRTypeExpr; variable: string; constraints: IRConstraint[]; } /** * Array type: [Int] */ export interface IRArrayType { kind: "array_type"; elementType: IRTypeExpr; } /** * Union type: Int|String * Tries each type in order, returns first successful parse */ export interface IRUnionType { kind: "union_type"; types: IRTypeExpr[]; } /** * Property in a type schema */ export interface IRTypeSchemaProperty { key: string; typeExpr: IRTypeExpr; optional?: boolean; } /** * Type definition: let Person = { name: String, age: Int } in body */ export interface IRTypeDef { type: "typedef"; name: string; typeExpr: IRTypeExpr; body: IRExpr; } /** * Guard expression: guard [label:] condition in body * Throws at runtime if condition is false, unless guards are stripped. * guardType distinguishes preconditions (guard) from postconditions (check). */ export interface IRGuard { type: "guard"; constraints: IRConstraint[]; body: IRExpr; guardType: "guard" | "check"; } /** * Factory functions for creating IR nodes */ export declare function irInt(value: number): IRIntLiteral; export declare function irFloat(value: number): IRFloatLiteral; export declare function irBool(value: boolean): IRBoolLiteral; export declare function irNull(): IRNullLiteral; export declare function irString(value: string): IRStringLiteral; export declare function irDate(value: string): IRDateLiteral; export declare function irDateTime(value: string): IRDateTimeLiteral; export declare function irDuration(value: string): IRDurationLiteral; export declare function irObject(properties: IRObjectProperty[]): IRObjectLiteral; export declare function irArray(elements: IRExpr[]): IRArrayLiteral; export declare function irVariable(name: string, inferredType?: EloType): IRVariable; export declare function irCall(fn: string, args: IRExpr[], argTypes: EloType[], resultType?: EloType): IRCall; export declare function irApply(fn: IRExpr, args: IRExpr[], argTypes: EloType[], resultType?: EloType): IRApply; export declare function irLet(bindings: IRLetBinding[], body: IRExpr): IRLet; export declare function irMemberAccess(object: IRExpr, property: string): IRMemberAccess; export declare function irIf(condition: IRExpr, thenBranch: IRExpr, elseBranch: IRExpr): IRIf; export declare function irLambda(params: IRLambdaParam[], body: IRExpr, resultType: EloType): IRLambda; export declare function irAlternative(alternatives: IRExpr[], resultType: EloType): IRAlternative; export declare function irDataPath(segments: (string | number)[]): IRDataPath; export declare function irTypeRef(name: string): IRTypeRef; export declare function irTypeSchema(properties: IRTypeSchemaProperty[], extras?: "closed" | "ignored" | IRTypeExpr): IRTypeSchema; export declare function irSubtypeConstraint(baseType: IRTypeExpr, variable: string, constraints: IRConstraint[]): IRSubtypeConstraint; export declare function irArrayType(elementType: IRTypeExpr): IRArrayType; export declare function irUnionType(types: IRTypeExpr[]): IRUnionType; export declare function irTypeDef(name: string, typeExpr: IRTypeExpr, body: IRExpr): IRTypeDef; export declare function irGuard(constraints: IRConstraint[], body: IRExpr, guardType?: "guard" | "check"): IRGuard; /** * Infer the type of an IR expression */ export declare function inferType(ir: IRExpr): EloType; /** * Check if an IR expression uses the input variable `_` as a free variable. * This is used to determine if the compiled output needs to be wrapped * as a function taking `_` as a parameter. */ export declare function usesInput(ir: IRExpr, boundVars?: Set): boolean; //# sourceMappingURL=ir.d.ts.map