@harmoniclabs/pebble 0.1.10 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (182) hide show
  1. package/dist/IR/CompilationCtx.d.ts +40 -0
  2. package/dist/IR/CompilationCtx.js +54 -0
  3. package/dist/IR/IRHash.d.ts +23 -2
  4. package/dist/IR/IRHash.js +10 -60
  5. package/dist/IR/IRNodes/IRConst.js +35 -4
  6. package/dist/IR/IRNodes/IRHoisted.d.ts +0 -1
  7. package/dist/IR/IRNodes/IRHoisted.js +4 -6
  8. package/dist/IR/IRNodes/IRLetted.d.ts +0 -1
  9. package/dist/IR/IRNodes/IRLetted.js +4 -6
  10. package/dist/IR/IRNodes/IRNative/IRNativeTag.d.ts +22 -2
  11. package/dist/IR/IRNodes/IRNative/IRNativeTag.js +26 -2
  12. package/dist/IR/IRNodes/IRNative/index.d.ts +16 -1
  13. package/dist/IR/IRNodes/IRNative/index.js +31 -2
  14. package/dist/IR/IRNodes/utils/hashVarSym.d.ts +0 -1
  15. package/dist/IR/IRNodes/utils/hashVarSym.js +27 -33
  16. package/dist/IR/toUPLC/CompilerOptions.d.ts +34 -7
  17. package/dist/IR/toUPLC/CompilerOptions.js +19 -10
  18. package/dist/IR/toUPLC/compileIRToUPLC.js +39 -3
  19. package/dist/IR/toUPLC/subRoutines/inlineSingleUseLetBindingsAndReturnRoot.d.ts +23 -0
  20. package/dist/IR/toUPLC/subRoutines/inlineSingleUseLetBindingsAndReturnRoot.js +263 -0
  21. package/dist/IR/toUPLC/subRoutines/introduceCaseForDualHeadTailAndReturnRoot.d.ts +35 -0
  22. package/dist/IR/toUPLC/subRoutines/introduceCaseForDualHeadTailAndReturnRoot.js +169 -0
  23. package/dist/IR/toUPLC/subRoutines/replaceHoistedWithLetted.d.ts +0 -1
  24. package/dist/IR/toUPLC/subRoutines/replaceHoistedWithLetted.js +6 -6
  25. package/dist/IR/toUPLC/subRoutines/replaceNatives/nativeToIR.d.ts +2 -3
  26. package/dist/IR/toUPLC/subRoutines/replaceNatives/nativeToIR.js +106 -65
  27. package/dist/IR/toUPLC/subRoutines/rewriteHeadTailInCaseConsAndReturnRoot.d.ts +30 -0
  28. package/dist/IR/toUPLC/subRoutines/rewriteHeadTailInCaseConsAndReturnRoot.js +95 -0
  29. package/dist/IR/toUPLC/subRoutines/rewriteNativesAppliedToConstantsAndReturnRoot.js +36 -5
  30. package/dist/IR/toUPLC/subRoutines/rewriteToCaseOverConstAndReturnRoot.d.ts +35 -0
  31. package/dist/IR/toUPLC/subRoutines/rewriteToCaseOverConstAndReturnRoot.js +169 -0
  32. package/dist/IR/tree_utils/_ir_caseList.d.ts +15 -0
  33. package/dist/IR/tree_utils/_ir_caseList.js +19 -0
  34. package/dist/IR/tree_utils/bytesToHex.d.ts +8 -0
  35. package/dist/IR/tree_utils/bytesToHex.js +69 -0
  36. package/dist/ast/nodes/expr/functions/FuncExpr.d.ts +16 -2
  37. package/dist/ast/nodes/expr/functions/FuncExpr.js +17 -0
  38. package/dist/ast/nodes/expr/litteral/LitteralExpr.d.ts +2 -1
  39. package/dist/ast/nodes/expr/litteral/LitteralExpr.js +2 -0
  40. package/dist/ast/nodes/expr/litteral/TemplateStrExpr.d.ts +30 -0
  41. package/dist/ast/nodes/expr/litteral/TemplateStrExpr.js +35 -0
  42. package/dist/ast/nodes/statements/ExportStmt.d.ts +3 -3
  43. package/dist/ast/nodes/statements/PebbleStmt.d.ts +4 -3
  44. package/dist/ast/nodes/statements/PebbleStmt.js +6 -2
  45. package/dist/ast/nodes/statements/TestParam.d.ts +18 -0
  46. package/dist/ast/nodes/statements/TestParam.js +18 -0
  47. package/dist/ast/nodes/statements/TestStmt.d.ts +5 -3
  48. package/dist/ast/nodes/statements/TestStmt.js +3 -1
  49. package/dist/ast/nodes/statements/UsingStmt.d.ts +32 -2
  50. package/dist/ast/nodes/statements/UsingStmt.js +39 -3
  51. package/dist/ast/nodes/statements/declarations/NamespaceDecl.d.ts +21 -0
  52. package/dist/ast/nodes/statements/declarations/NamespaceDecl.js +31 -0
  53. package/dist/ast/nodes/statements/declarations/StructDecl.d.ts +16 -2
  54. package/dist/ast/nodes/statements/declarations/StructDecl.js +15 -1
  55. package/dist/compiler/AstCompiler/AstCompiler.d.ts +27 -0
  56. package/dist/compiler/AstCompiler/AstCompiler.js +244 -7
  57. package/dist/compiler/AstCompiler/internal/_deriveContractBody/_deriveContractBody.js +16 -5
  58. package/dist/compiler/AstCompiler/internal/exprs/_compileCallExpr.js +97 -6
  59. package/dist/compiler/AstCompiler/internal/exprs/_compileCaseExpr.js +31 -0
  60. package/dist/compiler/AstCompiler/internal/exprs/_compileFuncExpr.js +12 -5
  61. package/dist/compiler/AstCompiler/internal/exprs/_compileIsExpr.js +12 -0
  62. package/dist/compiler/AstCompiler/internal/exprs/_compileLitteralExpr.js +59 -0
  63. package/dist/compiler/AstCompiler/internal/exprs/_compilePropAccessExpr.d.ts +2 -3
  64. package/dist/compiler/AstCompiler/internal/exprs/_compilePropAccessExpr.js +64 -0
  65. package/dist/compiler/AstCompiler/internal/exprs/_compileUnaryPrefixExpr.js +13 -1
  66. package/dist/compiler/AstCompiler/internal/exprs/_compileVarAccessExpr.js +2 -0
  67. package/dist/compiler/AstCompiler/internal/exprs/binary/_compileAddExpr.js +18 -5
  68. package/dist/compiler/AstCompiler/internal/exprs/binary/_compileEqualExpr.js +3 -1
  69. package/dist/compiler/AstCompiler/internal/exprs/binary/_compileGreaterThanEqualExpr.js +2 -1
  70. package/dist/compiler/AstCompiler/internal/exprs/binary/_compileGreaterThanExpr.js +2 -1
  71. package/dist/compiler/AstCompiler/internal/exprs/binary/_compileLessThanEqualExpr.js +2 -1
  72. package/dist/compiler/AstCompiler/internal/exprs/binary/_compileLessThanExpr.js +2 -1
  73. package/dist/compiler/AstCompiler/internal/exprs/binary/_compileMultExpr.js +24 -6
  74. package/dist/compiler/AstCompiler/internal/exprs/binary/_compileNotEqualExpr.js +2 -1
  75. package/dist/compiler/AstCompiler/internal/exprs/binary/_compileSubExpr.js +16 -5
  76. package/dist/compiler/AstCompiler/internal/statements/_compileMatchStmt.js +33 -20
  77. package/dist/compiler/AstCompiler/internal/statements/_compileStatement.js +4 -1
  78. package/dist/compiler/AstCompiler/internal/statements/_compileTestStmt.d.ts +15 -1
  79. package/dist/compiler/AstCompiler/internal/statements/_compileTestStmt.js +70 -30
  80. package/dist/compiler/AstCompiler/internal/statements/_compileUsingAliasStmt.d.ts +11 -0
  81. package/dist/compiler/AstCompiler/internal/statements/_compileUsingAliasStmt.js +26 -0
  82. package/dist/compiler/AstCompiler/internal/statements/_compileUsingStmt.d.ts +9 -4
  83. package/dist/compiler/AstCompiler/internal/statements/_compileUsingStmt.js +51 -10
  84. package/dist/compiler/AstCompiler/internal/types/_compileDataEncodedConcreteType.js +21 -2
  85. package/dist/compiler/AstCompiler/internal/types/_compileSopEncodedConcreteType.js +17 -2
  86. package/dist/compiler/AstCompiler/scope/AstScope.d.ts +70 -1
  87. package/dist/compiler/AstCompiler/scope/AstScope.js +91 -0
  88. package/dist/compiler/AstCompiler/utils/getPropAccessReturnType.js +36 -1
  89. package/dist/compiler/AstCompiler/utils/monomorphizeGeneric.d.ts +36 -0
  90. package/dist/compiler/AstCompiler/utils/monomorphizeGeneric.js +123 -0
  91. package/dist/compiler/AstCompiler/utils/resolveNamespaceChain.d.ts +28 -0
  92. package/dist/compiler/AstCompiler/utils/resolveNamespaceChain.js +95 -0
  93. package/dist/compiler/AstCompiler/utils/resolveNamespacePath.d.ts +37 -0
  94. package/dist/compiler/AstCompiler/utils/resolveNamespacePath.js +93 -0
  95. package/dist/compiler/Compiler.d.ts +9 -1
  96. package/dist/compiler/Compiler.js +218 -30
  97. package/dist/compiler/TirCompiler/expressify/ExpressifyCtx.js +1 -1
  98. package/dist/compiler/TirCompiler/expressify/expressify.js +30 -2
  99. package/dist/compiler/TirCompiler/expressify/expressifyForStmt.d.ts +2 -1
  100. package/dist/compiler/TirCompiler/expressify/expressifyForStmt.js +45 -7
  101. package/dist/compiler/TirCompiler/expressify/expressifyVars.d.ts +0 -1
  102. package/dist/compiler/TirCompiler/expressify/expressifyVars.js +49 -15
  103. package/dist/compiler/test/TestResult.d.ts +38 -0
  104. package/dist/compiler/test/TestResult.js +6 -0
  105. package/dist/compiler/test/fuzz/PRNG.d.ts +26 -0
  106. package/dist/compiler/test/fuzz/PRNG.js +59 -0
  107. package/dist/compiler/tir/expressions/TirCaseExpr.d.ts +9 -0
  108. package/dist/compiler/tir/expressions/TirCaseExpr.js +144 -122
  109. package/dist/compiler/tir/expressions/TirElemAccessExpr.js +2 -2
  110. package/dist/compiler/tir/expressions/TirExpr.d.ts +2 -1
  111. package/dist/compiler/tir/expressions/TirExpr.js +2 -0
  112. package/dist/compiler/tir/expressions/TirFromDataExpr.js +102 -67
  113. package/dist/compiler/tir/expressions/TirIsExpr.js +14 -1
  114. package/dist/compiler/tir/expressions/TirNativeFunc.d.ts +18 -2
  115. package/dist/compiler/tir/expressions/TirNativeFunc.js +55 -118
  116. package/dist/compiler/tir/expressions/TirShowExpr.d.ts +52 -0
  117. package/dist/compiler/tir/expressions/TirShowExpr.js +199 -0
  118. package/dist/compiler/tir/expressions/TirToDataExpr.js +3 -0
  119. package/dist/compiler/tir/expressions/TirTraceExpr.js +11 -7
  120. package/dist/compiler/tir/expressions/TirTypeConversionExpr.js +10 -0
  121. package/dist/compiler/tir/expressions/TirVariableAccessExpr.d.ts +2 -3
  122. package/dist/compiler/tir/expressions/TirVariableAccessExpr.js +1 -4
  123. package/dist/compiler/tir/expressions/ToIRTermCtx.d.ts +20 -3
  124. package/dist/compiler/tir/expressions/ToIRTermCtx.js +48 -3
  125. package/dist/compiler/tir/expressions/binary/TirBinaryExpr.d.ts +2 -2
  126. package/dist/compiler/tir/expressions/binary/TirBinaryExpr.js +45 -8
  127. package/dist/compiler/tir/expressions/litteral/TirLitEnumMemberExpr.d.ts +19 -0
  128. package/dist/compiler/tir/expressions/litteral/TirLitEnumMemberExpr.js +24 -0
  129. package/dist/compiler/tir/expressions/litteral/TirLitteralExpr.d.ts +2 -1
  130. package/dist/compiler/tir/expressions/litteral/TirLitteralExpr.js +2 -0
  131. package/dist/compiler/tir/expressions/unary/TirUnaryMinus.js +4 -1
  132. package/dist/compiler/tir/program/TypedProgram.d.ts +101 -0
  133. package/dist/compiler/tir/program/TypedProgram.js +43 -0
  134. package/dist/compiler/tir/program/stdScope/populateBuiltinInterfaces.d.ts +17 -0
  135. package/dist/compiler/tir/program/stdScope/populateBuiltinInterfaces.js +70 -0
  136. package/dist/compiler/tir/program/stdScope/populateStdNamespace.d.ts +22 -0
  137. package/dist/compiler/tir/program/stdScope/populateStdNamespace.js +619 -0
  138. package/dist/compiler/tir/program/stdScope/prelude/preludeTypesSrc.js +35 -2
  139. package/dist/compiler/tir/program/stdScope/stdScope.d.ts +8 -0
  140. package/dist/compiler/tir/program/stdScope/stdScope.js +84 -41
  141. package/dist/compiler/tir/statements/TirStmt.js +0 -1
  142. package/dist/compiler/tir/statements/TirTestStmt.d.ts +46 -0
  143. package/dist/compiler/tir/statements/TirTestStmt.js +35 -0
  144. package/dist/compiler/tir/types/TirEnumType.d.ts +21 -0
  145. package/dist/compiler/tir/types/TirEnumType.js +36 -0
  146. package/dist/compiler/tir/types/TirNativeType/TirNativeType.d.ts +53 -2
  147. package/dist/compiler/tir/types/TirNativeType/TirNativeType.js +58 -1
  148. package/dist/compiler/tir/types/TirNativeType/native/array.d.ts +16 -0
  149. package/dist/compiler/tir/types/TirNativeType/native/array.js +38 -0
  150. package/dist/compiler/tir/types/TirNativeType/native/index.d.ts +2 -0
  151. package/dist/compiler/tir/types/TirNativeType/native/index.js +2 -0
  152. package/dist/compiler/tir/types/TirNativeType/native/value.d.ts +18 -0
  153. package/dist/compiler/tir/types/TirNativeType/native/value.js +17 -0
  154. package/dist/compiler/tir/types/TirStructType.js +6 -1
  155. package/dist/compiler/tir/types/TirType.d.ts +3 -2
  156. package/dist/compiler/tir/types/TirType.js +7 -2
  157. package/dist/compiler/tir/types/utils/canAssignTo.js +36 -1
  158. package/dist/compiler/tir/types/utils/canCastTo.js +14 -1
  159. package/dist/compiler/tir/types/utils/getDeconstructableType.d.ts +2 -1
  160. package/dist/compiler/tir/types/utils/getDeconstructableType.js +2 -0
  161. package/dist/compiler/tir/types/utils/inferTypeArgs.d.ts +19 -0
  162. package/dist/compiler/tir/types/utils/inferTypeArgs.js +83 -0
  163. package/dist/compiler/tir/types/utils/normalizeEnumToInt.d.ts +10 -0
  164. package/dist/compiler/tir/types/utils/normalizeEnumToInt.js +17 -0
  165. package/dist/compiler/tir/types/utils/substituteTypeParams.d.ts +9 -0
  166. package/dist/compiler/tir/types/utils/substituteTypeParams.js +67 -0
  167. package/dist/diagnostics/diagnosticMessages.generated.d.ts +10 -0
  168. package/dist/diagnostics/diagnosticMessages.generated.js +20 -0
  169. package/dist/index.d.ts +2 -0
  170. package/dist/index.js +2 -0
  171. package/dist/parser/Parser.d.ts +73 -3
  172. package/dist/parser/Parser.js +362 -46
  173. package/dist/tokenizer/Token.d.ts +106 -102
  174. package/dist/tokenizer/Token.js +111 -109
  175. package/dist/tokenizer/utils/tokenFromKeyword.js +11 -6
  176. package/dist/utils/semverSatisfies.d.ts +1 -0
  177. package/dist/utils/semverSatisfies.js +161 -0
  178. package/dist/version.generated.d.ts +1 -0
  179. package/dist/version.generated.js +2 -0
  180. package/package.json +5 -4
  181. package/dist/IR/tree_utils/_ir_lazyChooseList.d.ts +0 -3
  182. package/dist/IR/tree_utils/_ir_lazyChooseList.js +0 -7
@@ -1,3 +1,5 @@
1
+ import { TirValueT } from "../../types/TirNativeType/native/value.js";
2
+ import { getUnaliased } from "../../types/utils/getUnaliased.js";
1
3
  import { _ir_apps } from "../../../../IR/IRNodes/IRApp.js";
2
4
  import { IRNative } from "../../../../IR/IRNodes/IRNative/index.js";
3
5
  export class TirUnaryMinus {
@@ -23,6 +25,7 @@ export class TirUnaryMinus {
23
25
  }
24
26
  get isConstant() { return this.operand.isConstant; }
25
27
  toIR(ctx) {
26
- return _ir_apps(IRNative._negateInt, this.operand.toIR(ctx));
28
+ const operandTy = getUnaliased(this.operand.type);
29
+ return _ir_apps(operandTy instanceof TirValueT ? IRNative._negateValue : IRNative._negateInt, this.operand.toIR(ctx));
27
30
  }
28
31
  }
@@ -1,16 +1,85 @@
1
1
  import { DiagnosticEmitter } from "../../../diagnostics/DiagnosticEmitter.js";
2
2
  import { DiagnosticMessage } from "../../../diagnostics/DiagnosticMessage.js";
3
+ import { FuncExpr } from "../../../ast/nodes/expr/functions/FuncExpr.js";
3
4
  import { AstScope } from "../../AstCompiler/scope/AstScope.js";
4
5
  import { UidGenerator } from "../../internalVar.js";
5
6
  import { TirFuncExpr } from "../expressions/TirFuncExpr.js";
6
7
  import { TirInlineClosedIR } from "../expressions/TirInlineClosedIR.js";
7
8
  import { TirSimpleVarDecl } from "../statements/TirVarDecl/TirSimpleVarDecl.js";
9
+ import { TirTestStmt } from "../statements/TirTestStmt.js";
10
+ import { TirTypeParam } from "../types/TirTypeParam.js";
8
11
  import { TirType } from "../types/TirType.js";
9
12
  import { StdTypes } from "./stdScope/StdTypes.js";
10
13
  export interface IGenericType {
11
14
  arity: number;
12
15
  apply: (argsTirNames: string[]) => (TirType | undefined);
13
16
  }
17
+ /**
18
+ * A pending generic function template registered at AST→TIR collection time.
19
+ *
20
+ * Two flavors are supported:
21
+ * - "user" templates wrap a Pebble `function f<T>(...) { body }` AST node.
22
+ * `monomorphizeGeneric` instantiates one by re-compiling a clone of the
23
+ * AST in a scope where each type-param name aliases its concrete type.
24
+ * - "native" templates wrap a polymorphic UPLC builtin (e.g. `mkCons<T>`).
25
+ * Each instantiation produces a fresh `TirInlineClosedIR` directly; there
26
+ * is no AST involved.
27
+ *
28
+ * Both flavors share the same monomorphization memoization on
29
+ * `program.monomorphizationCache`.
30
+ */
31
+ /**
32
+ * An interface-constraint on a type parameter of a generic function.
33
+ * Captured at template-registration time so `monomorphizeGeneric` doesn't
34
+ * have to re-resolve the interface at each call site.
35
+ */
36
+ export interface TypeParamConstraint {
37
+ /** e.g. "ToData" */
38
+ interfaceName: string;
39
+ /** method-name -> AstFuncType signature (as registered in `scope.interfaces`) */
40
+ methods: Map<string, import("../../../ast/nodes/types/AstNativeTypeExpr.js").AstFuncType>;
41
+ }
42
+ export interface BaseGenericTemplate {
43
+ /** the AST function name (e.g. "id") or the canonical synthetic name (e.g. "mkCons") */
44
+ astFuncName: string;
45
+ /** the type-param symbols in declaration order */
46
+ typeParams: TirTypeParam[];
47
+ /** the canonical (pre-monomorphization) tir-name registered on the placeholder value */
48
+ canonicalTirName: string;
49
+ /** the placeholder TirFuncT (with TirTypeParams) used to type the generic identifier in scope */
50
+ placeholderFuncType: import("../types/TirNativeType/native/function.js").TirFuncT;
51
+ /**
52
+ * One slot per type param, aligned by index with `typeParams`.
53
+ * `undefined` for unconstrained params.
54
+ */
55
+ constraints: (TypeParamConstraint | undefined)[];
56
+ }
57
+ export interface UserGenericTemplate extends BaseGenericTemplate {
58
+ kind: "user";
59
+ /** the FuncExpr AST node — cloned and re-compiled per instantiation */
60
+ astFuncExpr: FuncExpr;
61
+ /** the lexical scope where this template was declared */
62
+ definingScope: AstScope;
63
+ }
64
+ export interface NativeGenericTemplate extends BaseGenericTemplate {
65
+ kind: "native";
66
+ /** produces a fresh closed-IR for a given type-arg vector */
67
+ instantiate: (typeArgs: TirType[]) => TirInlineClosedIR;
68
+ }
69
+ export type GenericTemplate = UserGenericTemplate | NativeGenericTemplate;
70
+ /**
71
+ * Factory for one method of an interface, evaluated against a concrete type
72
+ * at monomorphization time. Returns the raw `IRTerm` callable as the method
73
+ * dictionary — a closed IR closure `(self: ConcreteT, ...) -> ReturnT`.
74
+ *
75
+ * For the built-in `ToData` interface this is `_toDataUplcFunc(t)` from
76
+ * `TirToDataExpr.ts` — a hoisted `IRFunc` that converts its argument to data
77
+ * using the right per-type encoding (iData / bData / inline-struct-encoder / etc).
78
+ *
79
+ * Returning `undefined` signals "this type doesn't implement the interface",
80
+ * which the caller turns into a diagnostic.
81
+ */
82
+ export type BuiltinInterfaceImplFactory = (concreteType: TirType) => (import("../../../IR/IRTerm.js").IRTerm | undefined);
14
83
  /**
15
84
  * for now we only care about "executables"
16
85
  *
@@ -19,8 +88,40 @@ export interface IGenericType {
19
88
  export declare class TypedProgram extends DiagnosticEmitter {
20
89
  readonly constants: Map<string, TirSimpleVarDecl>;
21
90
  readonly functions: Map<string, TirFuncExpr | TirInlineClosedIR>;
91
+ /**
92
+ * Generic function templates awaiting monomorphization. Keyed by the
93
+ * canonical AST function name (e.g. "id" for `function id<T>(...)`).
94
+ */
95
+ readonly genericTemplates: Map<string, GenericTemplate>;
96
+ /**
97
+ * Monomorphization memo: `templateName + "$$" + concreteArgs.join("$$")`
98
+ * → the TIR function name of the previously-compiled instance.
99
+ */
100
+ readonly monomorphizationCache: Map<string, string>;
101
+ /**
102
+ * Stack of monomorphizations currently in-flight, used to detect
103
+ * polymorphic recursion (a generic instantiating itself with different
104
+ * type arguments mid-compile).
105
+ */
106
+ readonly monomorphizationInFlight: Set<string>;
107
+ /**
108
+ * Built-in interface implementations.
109
+ *
110
+ * builtinInterfaceImpls[interfaceName][methodName] = factory
111
+ *
112
+ * The factory, given a concrete TIR type, returns a `TirExpr` callable
113
+ * as the method's dictionary entry (typed `(self: T, ...) -> ReturnT`).
114
+ *
115
+ * Populated by `populateBuiltinInterfaces` for `ToData` and any other
116
+ * compiler-supplied interface that user types can implicitly satisfy.
117
+ * User-declared `type Foo implements I { ... }` impls live on the
118
+ * struct's `methodNamesPtr` instead.
119
+ */
120
+ readonly builtinInterfaceImpls: Map<string, Map<string, BuiltinInterfaceImplFactory>>;
22
121
  readonly types: Map<string, TirType>;
23
122
  private readonly genericTypes;
123
+ /** Top-level `test name() { ... }` declarations collected from the entry file. */
124
+ readonly tests: TirTestStmt[];
24
125
  /** main */
25
126
  contractTirFuncName: string;
26
127
  readonly stdTypes: StdTypes;
@@ -4,6 +4,8 @@ import { UidGenerator } from "../../internalVar.js";
4
4
  import { TirFuncExpr } from "../expressions/TirFuncExpr.js";
5
5
  import { isTirType } from "../types/TirType.js";
6
6
  import { populatePreludeScope, populateStdScope } from "./stdScope/stdScope.js";
7
+ import { populateStdNamespace } from "./stdScope/populateStdNamespace.js";
8
+ import { populateBuiltinInterfaces } from "./stdScope/populateBuiltinInterfaces.js";
7
9
  import { StdTypes } from "./stdScope/StdTypes.js";
8
10
  /**
9
11
  * for now we only care about "executables"
@@ -13,8 +15,40 @@ import { StdTypes } from "./stdScope/StdTypes.js";
13
15
  export class TypedProgram extends DiagnosticEmitter {
14
16
  constants;
15
17
  functions;
18
+ /**
19
+ * Generic function templates awaiting monomorphization. Keyed by the
20
+ * canonical AST function name (e.g. "id" for `function id<T>(...)`).
21
+ */
22
+ genericTemplates = new Map();
23
+ /**
24
+ * Monomorphization memo: `templateName + "$$" + concreteArgs.join("$$")`
25
+ * → the TIR function name of the previously-compiled instance.
26
+ */
27
+ monomorphizationCache = new Map();
28
+ /**
29
+ * Stack of monomorphizations currently in-flight, used to detect
30
+ * polymorphic recursion (a generic instantiating itself with different
31
+ * type arguments mid-compile).
32
+ */
33
+ monomorphizationInFlight = new Set();
34
+ /**
35
+ * Built-in interface implementations.
36
+ *
37
+ * builtinInterfaceImpls[interfaceName][methodName] = factory
38
+ *
39
+ * The factory, given a concrete TIR type, returns a `TirExpr` callable
40
+ * as the method's dictionary entry (typed `(self: T, ...) -> ReturnT`).
41
+ *
42
+ * Populated by `populateBuiltinInterfaces` for `ToData` and any other
43
+ * compiler-supplied interface that user types can implicitly satisfy.
44
+ * User-declared `type Foo implements I { ... }` impls live on the
45
+ * struct's `methodNamesPtr` instead.
46
+ */
47
+ builtinInterfaceImpls = new Map();
16
48
  types;
17
49
  genericTypes;
50
+ /** Top-level `test name() { ... }` declarations collected from the entry file. */
51
+ tests;
18
52
  /** main */
19
53
  contractTirFuncName = "";
20
54
  stdTypes;
@@ -32,12 +66,21 @@ export class TypedProgram extends DiagnosticEmitter {
32
66
  this.functions = new Map();
33
67
  this.types = new Map();
34
68
  this.genericTypes = new Map();
69
+ this.tests = [];
35
70
  this.filePrefix = new Map();
36
71
  this.stdScope = new AstScope(undefined, this, { isFunctionDeclScope: false, isMethodScope: false });
37
72
  populateStdScope(this);
38
73
  this.stdTypes = new StdTypes(this);
39
74
  this.preludeScope = new AstScope(this.stdScope, this, { isFunctionDeclScope: false, isMethodScope: false });
40
75
  populatePreludeScope(this);
76
+ // Register built-in interfaces (currently: `ToData`) and their
77
+ // compiler-supplied impl factories. Must precede `populateStdNamespace`
78
+ // so constrained native templates (`std.linearMap.prepend`) can
79
+ // reference `ToData` at registration time.
80
+ populateBuiltinInterfaces(this);
81
+ // The `std`, `std.crypto`, `std.crypto.bls12_381`, `std.builtins`
82
+ // namespaces and all their members live on the prelude scope.
83
+ populateStdNamespace(this);
41
84
  }
42
85
  registerType(tirType) {
43
86
  if (!isTirType(tirType))
@@ -0,0 +1,17 @@
1
+ import { TypedProgram } from "../TypedProgram.js";
2
+ /**
3
+ * Register compiler-built interfaces and their built-in implementation
4
+ * factories.
5
+ *
6
+ * - `ToData`: every type that supports a `data` encoding (which is most
7
+ * Pebble types) automatically satisfies it. The factory returns
8
+ * `_toDataUplcFunc(T)` — a closed IR closure `(T) -> data` produced by
9
+ * the same compile-time dispatch that `TirToDataExpr` uses.
10
+ *
11
+ * User types may also implement these interfaces explicitly via
12
+ * `type Foo implements ToData { toData(self): data { ... } }`. When a
13
+ * constraint is resolved at call time, the user-impl path is checked first
14
+ * (via the type's `methodNamesPtr`); only if no user impl exists does the
15
+ * compiler fall back to the built-in factory registered here.
16
+ */
17
+ export declare function populateBuiltinInterfaces(program: TypedProgram): void;
@@ -0,0 +1,70 @@
1
+ import { Identifier } from "../../../../ast/nodes/common/Identifier.js";
2
+ import { SourceRange } from "../../../../ast/Source/SourceRange.js";
3
+ import { AstFuncType } from "../../../../ast/nodes/types/AstNativeTypeExpr.js";
4
+ import { AstNamedTypeExpr } from "../../../../ast/nodes/types/AstNamedTypeExpr.js";
5
+ import { SimpleVarDecl } from "../../../../ast/nodes/statements/declarations/VarDecl/SimpleVarDecl.js";
6
+ import { CommonFlags } from "../../../../common.js";
7
+ // NOTE: `TirToDataExpr` transitively imports `common_hoisted`, which calls
8
+ // `IRConst.data(...)` at module-load time. Loading this at the top would
9
+ // cycle with `TypedProgram` during early compiler bootstrap and trip
10
+ // "Cannot read properties of undefined (reading 'data')" in tests that load
11
+ // the IR layer in isolation. Require it lazily inside the factory instead.
12
+ /**
13
+ * Register compiler-built interfaces and their built-in implementation
14
+ * factories.
15
+ *
16
+ * - `ToData`: every type that supports a `data` encoding (which is most
17
+ * Pebble types) automatically satisfies it. The factory returns
18
+ * `_toDataUplcFunc(T)` — a closed IR closure `(T) -> data` produced by
19
+ * the same compile-time dispatch that `TirToDataExpr` uses.
20
+ *
21
+ * User types may also implement these interfaces explicitly via
22
+ * `type Foo implements ToData { toData(self): data { ... } }`. When a
23
+ * constraint is resolved at call time, the user-impl path is checked first
24
+ * (via the type's `methodNamesPtr`); only if no user impl exists does the
25
+ * compiler fall back to the built-in factory registered here.
26
+ */
27
+ export function populateBuiltinInterfaces(program) {
28
+ const unkRange = SourceRange.unknown;
29
+ // ---- interface ToData { toData(self): data } ----
30
+ const selfParam = new SimpleVarDecl(new Identifier("self", unkRange),
31
+ // self has no annotated type; the implementer's type fills the slot
32
+ undefined, undefined, CommonFlags.None, unkRange);
33
+ const toDataSig = new AstFuncType([selfParam], new AstNamedTypeExpr(new Identifier("data", unkRange), [], unkRange), unkRange);
34
+ const toDataMethods = new Map([
35
+ ["toData", toDataSig],
36
+ ]);
37
+ // Register the interface in the prelude scope so users can write
38
+ // `<T implements ToData>`. The scope walker (`resolveInterface`) finds
39
+ // it via the parent chain.
40
+ program.preludeScope.interfaces.set("ToData", toDataMethods);
41
+ // Built-in impl factory: given a concrete TIR type, return a
42
+ // closed `IRTerm` of type `(T) -> data`. `_toDataUplcFunc` already
43
+ // dispatches compile-time per concrete type — return its hoisted
44
+ // closure directly. Loaded lazily to avoid the bootstrap cycle.
45
+ program.builtinInterfaceImpls.set("ToData", new Map([
46
+ ["toData", (concreteType) => {
47
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
48
+ const { _toDataUplcFunc } = require("../../expressions/TirToDataExpr.js");
49
+ return _toDataUplcFunc(concreteType);
50
+ }],
51
+ ]));
52
+ // ---- interface Show { show(self): bytes } ----
53
+ // Returns a UTF-8 textual representation suitable for human reading
54
+ // and for passing through `trace`. Built-in primitives auto-implement
55
+ // it (int -> decimal, bytes -> lowercase hex, bool -> "true"/"false",
56
+ // data -> serialiseData -> hex, List<T>/LinearMap<K,V> -> recursive,
57
+ // data-encoded structs -> serialiseData -> hex). User types may
58
+ // override with `type X implements Show { show(self): bytes { ... } }`.
59
+ const showSig = new AstFuncType([selfParam], new AstNamedTypeExpr(new Identifier("bytes", unkRange), [], unkRange), unkRange);
60
+ program.preludeScope.interfaces.set("Show", new Map([
61
+ ["show", showSig],
62
+ ]));
63
+ program.builtinInterfaceImpls.set("Show", new Map([
64
+ ["show", (concreteType) => {
65
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
66
+ const { _showUplcFunc } = require("../../expressions/TirShowExpr.js");
67
+ return _showUplcFunc(concreteType);
68
+ }],
69
+ ]));
70
+ }
@@ -0,0 +1,22 @@
1
+ import { TypedProgram } from "../TypedProgram.js";
2
+ /**
3
+ * Populate the top-level `std` namespace and its sub-namespaces:
4
+ *
5
+ * std
6
+ * ├── crypto
7
+ * │ ├── hashing & signatures (sha2_256, blake2b_256, verifyEd25519Signature, ...)
8
+ * │ └── bls12_381
9
+ * │ ├── types: G1, G2, MlResult
10
+ * │ └── g1Add, g1Neg, g2Add, ..., millerLoop, finalVerify
11
+ * └── builtins
12
+ * ├── types: RawConstr
13
+ * ├── arithmetic, byte-string, bitwise, data, string, trace, conversion ops
14
+ * └── (polymorphic intrinsics like mkCons<T> are registered as native
15
+ * generic templates and dispatched through the same monomorphizer
16
+ * as user generic functions — see `populateNativeGenericTemplates`)
17
+ *
18
+ * Identifiers like `std.crypto.sha2_256(b)` resolve through the existing
19
+ * namespace-chain walker; the function is registered on the program as a
20
+ * `TirInlineClosedIR` whose IR is the corresponding `IRNative` tag.
21
+ */
22
+ export declare function populateStdNamespace(program: TypedProgram): void;