@harmoniclabs/pebble 0.1.10 → 0.2.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 (97) hide show
  1. package/dist/IR/IRNodes/IRConst.js +14 -3
  2. package/dist/IR/IRNodes/IRNative/index.js +5 -1
  3. package/dist/IR/toUPLC/CompilerOptions.d.ts +22 -7
  4. package/dist/IR/toUPLC/CompilerOptions.js +5 -1
  5. package/dist/IR/tree_utils/bytesToHex.d.ts +8 -0
  6. package/dist/IR/tree_utils/bytesToHex.js +69 -0
  7. package/dist/ast/nodes/expr/functions/FuncExpr.d.ts +16 -2
  8. package/dist/ast/nodes/expr/functions/FuncExpr.js +17 -0
  9. package/dist/ast/nodes/expr/litteral/LitteralExpr.d.ts +2 -1
  10. package/dist/ast/nodes/expr/litteral/LitteralExpr.js +2 -0
  11. package/dist/ast/nodes/expr/litteral/TemplateStrExpr.d.ts +30 -0
  12. package/dist/ast/nodes/expr/litteral/TemplateStrExpr.js +35 -0
  13. package/dist/ast/nodes/statements/ExportStmt.d.ts +3 -3
  14. package/dist/ast/nodes/statements/PebbleStmt.d.ts +4 -3
  15. package/dist/ast/nodes/statements/PebbleStmt.js +6 -2
  16. package/dist/ast/nodes/statements/TestParam.d.ts +18 -0
  17. package/dist/ast/nodes/statements/TestParam.js +18 -0
  18. package/dist/ast/nodes/statements/TestStmt.d.ts +5 -3
  19. package/dist/ast/nodes/statements/TestStmt.js +3 -1
  20. package/dist/ast/nodes/statements/UsingStmt.d.ts +32 -2
  21. package/dist/ast/nodes/statements/UsingStmt.js +39 -3
  22. package/dist/ast/nodes/statements/declarations/NamespaceDecl.d.ts +21 -0
  23. package/dist/ast/nodes/statements/declarations/NamespaceDecl.js +31 -0
  24. package/dist/compiler/AstCompiler/AstCompiler.d.ts +26 -0
  25. package/dist/compiler/AstCompiler/AstCompiler.js +203 -3
  26. package/dist/compiler/AstCompiler/internal/_deriveContractBody/_deriveContractBody.js +13 -2
  27. package/dist/compiler/AstCompiler/internal/exprs/_compileCallExpr.js +97 -6
  28. package/dist/compiler/AstCompiler/internal/exprs/_compileFuncExpr.js +12 -5
  29. package/dist/compiler/AstCompiler/internal/exprs/_compileLitteralExpr.js +59 -0
  30. package/dist/compiler/AstCompiler/internal/exprs/_compilePropAccessExpr.d.ts +2 -3
  31. package/dist/compiler/AstCompiler/internal/exprs/_compilePropAccessExpr.js +28 -0
  32. package/dist/compiler/AstCompiler/internal/exprs/_compileVarAccessExpr.js +2 -0
  33. package/dist/compiler/AstCompiler/internal/statements/_compileStatement.js +4 -1
  34. package/dist/compiler/AstCompiler/internal/statements/_compileTestStmt.d.ts +15 -1
  35. package/dist/compiler/AstCompiler/internal/statements/_compileTestStmt.js +70 -30
  36. package/dist/compiler/AstCompiler/internal/statements/_compileUsingAliasStmt.d.ts +11 -0
  37. package/dist/compiler/AstCompiler/internal/statements/_compileUsingAliasStmt.js +26 -0
  38. package/dist/compiler/AstCompiler/internal/statements/_compileUsingStmt.d.ts +9 -4
  39. package/dist/compiler/AstCompiler/internal/statements/_compileUsingStmt.js +51 -10
  40. package/dist/compiler/AstCompiler/internal/types/_compileDataEncodedConcreteType.js +21 -2
  41. package/dist/compiler/AstCompiler/internal/types/_compileSopEncodedConcreteType.js +17 -2
  42. package/dist/compiler/AstCompiler/scope/AstScope.d.ts +70 -1
  43. package/dist/compiler/AstCompiler/scope/AstScope.js +91 -0
  44. package/dist/compiler/AstCompiler/utils/getPropAccessReturnType.js +25 -1
  45. package/dist/compiler/AstCompiler/utils/monomorphizeGeneric.d.ts +36 -0
  46. package/dist/compiler/AstCompiler/utils/monomorphizeGeneric.js +123 -0
  47. package/dist/compiler/AstCompiler/utils/resolveNamespaceChain.d.ts +28 -0
  48. package/dist/compiler/AstCompiler/utils/resolveNamespaceChain.js +95 -0
  49. package/dist/compiler/AstCompiler/utils/resolveNamespacePath.d.ts +37 -0
  50. package/dist/compiler/AstCompiler/utils/resolveNamespacePath.js +93 -0
  51. package/dist/compiler/Compiler.d.ts +9 -1
  52. package/dist/compiler/Compiler.js +204 -9
  53. package/dist/compiler/TirCompiler/expressify/expressifyVars.js +26 -7
  54. package/dist/compiler/test/TestResult.d.ts +38 -0
  55. package/dist/compiler/test/TestResult.js +6 -0
  56. package/dist/compiler/test/fuzz/PRNG.d.ts +26 -0
  57. package/dist/compiler/test/fuzz/PRNG.js +59 -0
  58. package/dist/compiler/tir/expressions/TirExpr.d.ts +2 -1
  59. package/dist/compiler/tir/expressions/TirExpr.js +2 -0
  60. package/dist/compiler/tir/expressions/TirNativeFunc.d.ts +17 -0
  61. package/dist/compiler/tir/expressions/TirNativeFunc.js +53 -106
  62. package/dist/compiler/tir/expressions/TirShowExpr.d.ts +52 -0
  63. package/dist/compiler/tir/expressions/TirShowExpr.js +199 -0
  64. package/dist/compiler/tir/expressions/TirTraceExpr.js +11 -7
  65. package/dist/compiler/tir/program/TypedProgram.d.ts +101 -0
  66. package/dist/compiler/tir/program/TypedProgram.js +43 -0
  67. package/dist/compiler/tir/program/stdScope/populateBuiltinInterfaces.d.ts +17 -0
  68. package/dist/compiler/tir/program/stdScope/populateBuiltinInterfaces.js +70 -0
  69. package/dist/compiler/tir/program/stdScope/populateStdNamespace.d.ts +22 -0
  70. package/dist/compiler/tir/program/stdScope/populateStdNamespace.js +574 -0
  71. package/dist/compiler/tir/program/stdScope/stdScope.d.ts +1 -0
  72. package/dist/compiler/tir/program/stdScope/stdScope.js +1 -1
  73. package/dist/compiler/tir/statements/TirStmt.js +0 -1
  74. package/dist/compiler/tir/statements/TirTestStmt.d.ts +46 -0
  75. package/dist/compiler/tir/statements/TirTestStmt.js +35 -0
  76. package/dist/compiler/tir/types/TirNativeType/TirNativeType.d.ts +50 -1
  77. package/dist/compiler/tir/types/TirNativeType/TirNativeType.js +53 -1
  78. package/dist/compiler/tir/types/TirType.js +3 -1
  79. package/dist/compiler/tir/types/utils/canAssignTo.js +8 -1
  80. package/dist/compiler/tir/types/utils/inferTypeArgs.d.ts +19 -0
  81. package/dist/compiler/tir/types/utils/inferTypeArgs.js +79 -0
  82. package/dist/compiler/tir/types/utils/substituteTypeParams.d.ts +9 -0
  83. package/dist/compiler/tir/types/utils/substituteTypeParams.js +62 -0
  84. package/dist/diagnostics/diagnosticMessages.generated.d.ts +5 -0
  85. package/dist/diagnostics/diagnosticMessages.generated.js +10 -0
  86. package/dist/index.d.ts +2 -0
  87. package/dist/index.js +2 -0
  88. package/dist/parser/Parser.d.ts +73 -3
  89. package/dist/parser/Parser.js +333 -33
  90. package/dist/tokenizer/Token.d.ts +105 -102
  91. package/dist/tokenizer/Token.js +110 -109
  92. package/dist/tokenizer/utils/tokenFromKeyword.js +9 -6
  93. package/dist/utils/semverSatisfies.d.ts +1 -0
  94. package/dist/utils/semverSatisfies.js +161 -0
  95. package/dist/version.generated.d.ts +1 -0
  96. package/dist/version.generated.js +2 -0
  97. package/package.json +3 -2
@@ -2,7 +2,7 @@ import { SourceRange } from "../../../ast/Source/SourceRange.js";
2
2
  import { IRNativeTag } from "../../../IR/IRNodes/IRNative/IRNativeTag.js";
3
3
  import { IRNative } from "../../../IR/IRNodes/IRNative/index.js";
4
4
  import { bool_t, bytes_t, data_t, int_t, void_t } from "../program/stdScope/stdScope.js";
5
- import { TirUnConstrDataResultT, TirPairDataT, TirIntT, TirBytesT, TirDataT, TirBoolT, TirSopOptT } from "../types/TirNativeType/index.js";
5
+ import { TirUnConstrDataResultT, TirPairDataT, TirIntT, TirBytesT, TirDataT, TirBoolT, TirSopOptT, TirBlsG1T, TirBlsG2T, TirMlResultT } from "../types/TirNativeType/index.js";
6
6
  import { TirFuncT } from "../types/TirNativeType/native/function.js";
7
7
  import { TirLinearMapT } from "../types/TirNativeType/native/linearMap.js";
8
8
  import { TirLinearMapEntryT } from "../types/TirNativeType/native/linearMapEntry.js";
@@ -395,111 +395,58 @@ export class TirNativeFunc {
395
395
  bytes_t
396
396
  ], bool_t));
397
397
  }
398
- // BLS12-381 operations
399
- /* TODO: add bls supprot
400
- get bls12_381_G1_add(): TirNativeFunc {
401
- return new TirNativeFunc(
402
- IRNativeTag.bls12_381_G1_add,
403
- new TirFuncT([bytes_t, bytes_t], bytes_t)
404
- );
405
- }
406
- get bls12_381_G1_neg(): TirNativeFunc {
407
- return new TirNativeFunc(
408
- IRNativeTag.bls12_381_G1_neg,
409
- new TirFuncT([bytes_t], bytes_t)
410
- );
411
- }
412
- get bls12_381_G1_scalarMul(): TirNativeFunc {
413
- return new TirNativeFunc(
414
- IRNativeTag.bls12_381_G1_scalarMul,
415
- new TirFuncT([bytes_t, int_t], bytes_t)
416
- );
417
- }
418
- get bls12_381_G1_equal(): TirNativeFunc {
419
- return new TirNativeFunc(
420
- IRNativeTag.bls12_381_G1_equal,
421
- new TirFuncT([bytes_t, bytes_t], bool_t)
422
- );
423
- }
424
- get bls12_381_G1_hashToGroup(): TirNativeFunc {
425
- return new TirNativeFunc(
426
- IRNativeTag.bls12_381_G1_hashToGroup,
427
- new TirFuncT([bytes_t], bytes_t)
428
- );
429
- }
430
- get bls12_381_G1_compress(): TirNativeFunc {
431
- return new TirNativeFunc(
432
- IRNativeTag.bls12_381_G1_compress,
433
- new TirFuncT([bytes_t], bytes_t)
434
- );
435
- }
436
- get bls12_381_G1_uncompress(): TirNativeFunc {
437
- return new TirNativeFunc(
438
- IRNativeTag.bls12_381_G1_uncompress,
439
- new TirFuncT([bytes_t], bytes_t)
440
- );
441
- }
442
- get bls12_381_G2_add(): TirNativeFunc {
443
- return new TirNativeFunc(
444
- IRNativeTag.bls12_381_G2_add,
445
- new TirFuncT([bytes_t, bytes_t], bytes_t)
446
- );
447
- }
448
- get bls12_381_G2_neg(): TirNativeFunc {
449
- return new TirNativeFunc(
450
- IRNativeTag.bls12_381_G2_neg,
451
- new TirFuncT([bytes_t], bytes_t)
452
- );
453
- }
454
- get bls12_381_G2_scalarMul(): TirNativeFunc {
455
- return new TirNativeFunc(
456
- IRNativeTag.bls12_381_G2_scalarMul,
457
- new TirFuncT([bytes_t, int_t], bytes_t)
458
- );
459
- }
460
- get bls12_381_G2_equal(): TirNativeFunc {
461
- return new TirNativeFunc(
462
- IRNativeTag.bls12_381_G2_equal,
463
- new TirFuncT([bytes_t, bytes_t], bool_t)
464
- );
465
- }
466
- get bls12_381_G2_hashToGroup(): TirNativeFunc {
467
- return new TirNativeFunc(
468
- IRNativeTag.bls12_381_G2_hashToGroup,
469
- new TirFuncT([bytes_t], bytes_t)
470
- );
471
- }
472
- get bls12_381_G2_compress(): TirNativeFunc {
473
- return new TirNativeFunc(
474
- IRNativeTag.bls12_381_G2_compress,
475
- new TirFuncT([bytes_t], bytes_t)
476
- );
477
- }
478
- get bls12_381_G2_uncompress(): TirNativeFunc {
479
- return new TirNativeFunc(
480
- IRNativeTag.bls12_381_G2_uncompress,
481
- new TirFuncT([bytes_t], bytes_t)
482
- );
483
- }
484
- get bls12_381_millerLoop(): TirNativeFunc {
485
- return new TirNativeFunc(
486
- IRNativeTag.bls12_381_millerLoop,
487
- new TirFuncT([bytes_t, bytes_t], bytes_t)
488
- );
489
- }
490
- get bls12_381_mulMlResult(): TirNativeFunc {
491
- return new TirNativeFunc(
492
- IRNativeTag.bls12_381_mulMlResult,
493
- new TirFuncT([bytes_t, bytes_t], bytes_t)
494
- );
495
- }
496
- get bls12_381_finalVerify(): TirNativeFunc {
497
- return new TirNativeFunc(
498
- IRNativeTag.bls12_381_finalVerify,
499
- new TirFuncT([bytes_t, bytes_t], bool_t)
500
- );
501
- }
502
- //*/
398
+ // BLS12-381 operations -- now exposed under `std.crypto.bls12_381`
399
+ static get bls12_381_G1_add() {
400
+ return new TirNativeFunc(IRNativeTag.bls12_381_G1_add, new TirFuncT([new TirBlsG1T(), new TirBlsG1T()], new TirBlsG1T()));
401
+ }
402
+ static get bls12_381_G1_neg() {
403
+ return new TirNativeFunc(IRNativeTag.bls12_381_G1_neg, new TirFuncT([new TirBlsG1T()], new TirBlsG1T()));
404
+ }
405
+ static get bls12_381_G1_scalarMul() {
406
+ return new TirNativeFunc(IRNativeTag.bls12_381_G1_scalarMul, new TirFuncT([int_t, new TirBlsG1T()], new TirBlsG1T()));
407
+ }
408
+ static get bls12_381_G1_equal() {
409
+ return new TirNativeFunc(IRNativeTag.bls12_381_G1_equal, new TirFuncT([new TirBlsG1T(), new TirBlsG1T()], bool_t));
410
+ }
411
+ static get bls12_381_G1_hashToGroup() {
412
+ return new TirNativeFunc(IRNativeTag.bls12_381_G1_hashToGroup, new TirFuncT([bytes_t, bytes_t], new TirBlsG1T()));
413
+ }
414
+ static get bls12_381_G1_compress() {
415
+ return new TirNativeFunc(IRNativeTag.bls12_381_G1_compress, new TirFuncT([new TirBlsG1T()], bytes_t));
416
+ }
417
+ static get bls12_381_G1_uncompress() {
418
+ return new TirNativeFunc(IRNativeTag.bls12_381_G1_uncompress, new TirFuncT([bytes_t], new TirBlsG1T()));
419
+ }
420
+ static get bls12_381_G2_add() {
421
+ return new TirNativeFunc(IRNativeTag.bls12_381_G2_add, new TirFuncT([new TirBlsG2T(), new TirBlsG2T()], new TirBlsG2T()));
422
+ }
423
+ static get bls12_381_G2_neg() {
424
+ return new TirNativeFunc(IRNativeTag.bls12_381_G2_neg, new TirFuncT([new TirBlsG2T()], new TirBlsG2T()));
425
+ }
426
+ static get bls12_381_G2_scalarMul() {
427
+ return new TirNativeFunc(IRNativeTag.bls12_381_G2_scalarMul, new TirFuncT([int_t, new TirBlsG2T()], new TirBlsG2T()));
428
+ }
429
+ static get bls12_381_G2_equal() {
430
+ return new TirNativeFunc(IRNativeTag.bls12_381_G2_equal, new TirFuncT([new TirBlsG2T(), new TirBlsG2T()], bool_t));
431
+ }
432
+ static get bls12_381_G2_hashToGroup() {
433
+ return new TirNativeFunc(IRNativeTag.bls12_381_G2_hashToGroup, new TirFuncT([bytes_t, bytes_t], new TirBlsG2T()));
434
+ }
435
+ static get bls12_381_G2_compress() {
436
+ return new TirNativeFunc(IRNativeTag.bls12_381_G2_compress, new TirFuncT([new TirBlsG2T()], bytes_t));
437
+ }
438
+ static get bls12_381_G2_uncompress() {
439
+ return new TirNativeFunc(IRNativeTag.bls12_381_G2_uncompress, new TirFuncT([bytes_t], new TirBlsG2T()));
440
+ }
441
+ static get bls12_381_millerLoop() {
442
+ return new TirNativeFunc(IRNativeTag.bls12_381_millerLoop, new TirFuncT([new TirBlsG1T(), new TirBlsG2T()], new TirMlResultT()));
443
+ }
444
+ static get bls12_381_mulMlResult() {
445
+ return new TirNativeFunc(IRNativeTag.bls12_381_mulMlResult, new TirFuncT([new TirMlResultT(), new TirMlResultT()], new TirMlResultT()));
446
+ }
447
+ static get bls12_381_finalVerify() {
448
+ return new TirNativeFunc(IRNativeTag.bls12_381_finalVerify, new TirFuncT([new TirMlResultT(), new TirMlResultT()], bool_t));
449
+ }
503
450
  // Additional hashing functions
504
451
  static get keccak_256() {
505
452
  return new TirNativeFunc(IRNativeTag.keccak_256, new TirFuncT([
@@ -0,0 +1,52 @@
1
+ import { SourceRange } from "../../../ast/Source/SourceRange.js";
2
+ import { IRTerm } from "../../../IR/IRTerm.js";
3
+ import { TirType } from "../types/TirType.js";
4
+ import { ITirExpr } from "./ITirExpr.js";
5
+ import { TirExpr } from "./TirExpr.js";
6
+ import { ToIRTermCtx } from "./ToIRTermCtx.js";
7
+ /**
8
+ * `_showIR`-backed compile-time-dispatched show expression.
9
+ *
10
+ * Created when the user writes `expr.show()` on a built-in type whose Show
11
+ * impl the compiler provides (int, bytes, bool, data, void, string,
12
+ * List<T>, LinearMap<K,V>, data-encoded structs). For user-declared
13
+ * `type X implements Show { show(self): bytes { ... } }` impls the
14
+ * regular method-dispatch path in `expressifyVars` keeps applying — this
15
+ * expr is only emitted when no user impl is registered.
16
+ */
17
+ export declare class TirShowExpr implements ITirExpr {
18
+ readonly inner: TirExpr;
19
+ readonly range: SourceRange;
20
+ constructor(inner: TirExpr, range: SourceRange);
21
+ get type(): TirType;
22
+ get isConstant(): boolean;
23
+ toString(): string;
24
+ pretty(indent: number): string;
25
+ clone(): TirExpr;
26
+ deps(): string[];
27
+ toIR(ctx: ToIRTermCtx): IRTerm;
28
+ }
29
+ /**
30
+ * Inline an `IRTerm` of type `t` into its UTF-8 textual representation
31
+ * (also bytes). Returns the IR producing the show-encoded bytes.
32
+ *
33
+ * Mirrors `_inlineToData` — fully compile-time-dispatched per source type:
34
+ *
35
+ * int -> hoisted_intToUtf8Bytes (decimal, sign-aware)
36
+ * bytes -> hoisted_bytesToHex (lowercase hex)
37
+ * bool -> "true" / "false" via lazy ifThenElse
38
+ * data -> serialiseData -> bytesToHex
39
+ * void -> "()"
40
+ * string -> encodeUtf8 (treat as already-readable text)
41
+ * List<T> -> "[" + intercalate(", ", map(_showIR(T), elems)) + "]"
42
+ * LinearMap<K,V> -> "{" + intercalate(", ", map((k,v) -> showK(k) ++ ": " ++ showV(v), entries)) + "}"
43
+ *
44
+ * For struct-typed receivers (TirDataStructType / TirSoPStructType) this
45
+ * function does not produce IR — those should reach a user-declared
46
+ * `type X implements Show { show(self): bytes { ... } }` and dispatch
47
+ * through the regular method-call path. The caller is expected to detect
48
+ * that case before invoking `_showIR`.
49
+ */
50
+ export declare function _showIR(origin_t: TirType, exprIR: IRTerm): IRTerm;
51
+ /** A canonical 1-arg `(T) -> bytes` IR closure for use as a `Show` dictionary entry. */
52
+ export declare function _showUplcFunc(origin_t: TirType): IRTerm;
@@ -0,0 +1,199 @@
1
+ import { _ir_apps } from "../../../IR/IRNodes/IRApp.js";
2
+ import { IRConst } from "../../../IR/IRNodes/IRConst.js";
3
+ import { IRFunc } from "../../../IR/IRNodes/IRFunc.js";
4
+ import { IRHoisted } from "../../../IR/IRNodes/IRHoisted.js";
5
+ import { IRNative } from "../../../IR/IRNodes/IRNative/index.js";
6
+ import { IRRecursive } from "../../../IR/IRNodes/IRRecursive.js";
7
+ import { IRSelfCall } from "../../../IR/IRNodes/IRSelfCall.js";
8
+ import { IRVar } from "../../../IR/IRNodes/IRVar.js";
9
+ import { hoisted_bytesToHex } from "../../../IR/tree_utils/bytesToHex.js";
10
+ import { hoisted_intToUtf8Bytes } from "../../../IR/tree_utils/intToUtf8Bytes.js";
11
+ import { _ir_lazyIfThenElse } from "../../../IR/tree_utils/_ir_lazyIfThenElse.js";
12
+ import { fromUtf8 } from "@harmoniclabs/uint8array-utils";
13
+ import { bytes_t } from "../program/stdScope/stdScope.js";
14
+ import { TirDataStructType } from "../types/TirStructType.js";
15
+ import { TirBoolT } from "../types/TirNativeType/native/bool.js";
16
+ import { TirBytesT } from "../types/TirNativeType/native/bytes.js";
17
+ import { TirDataT } from "../types/TirNativeType/native/data.js";
18
+ import { TirIntT } from "../types/TirNativeType/native/int.js";
19
+ import { TirLinearMapT } from "../types/TirNativeType/native/linearMap.js";
20
+ import { TirListT } from "../types/TirNativeType/native/list.js";
21
+ import { TirStringT } from "../types/TirNativeType/native/string.js";
22
+ import { TirDataOptT } from "../types/TirNativeType/native/Optional/data.js";
23
+ import { TirVoidT } from "../types/TirNativeType/native/void.js";
24
+ import { getListTypeArg } from "../types/utils/getListTypeArg.js";
25
+ import { getUnaliased } from "../types/utils/getUnaliased.js";
26
+ /**
27
+ * `_showIR`-backed compile-time-dispatched show expression.
28
+ *
29
+ * Created when the user writes `expr.show()` on a built-in type whose Show
30
+ * impl the compiler provides (int, bytes, bool, data, void, string,
31
+ * List<T>, LinearMap<K,V>, data-encoded structs). For user-declared
32
+ * `type X implements Show { show(self): bytes { ... } }` impls the
33
+ * regular method-dispatch path in `expressifyVars` keeps applying — this
34
+ * expr is only emitted when no user impl is registered.
35
+ */
36
+ export class TirShowExpr {
37
+ inner;
38
+ range;
39
+ constructor(inner, range) {
40
+ this.inner = inner;
41
+ this.range = range;
42
+ }
43
+ get type() { return bytes_t; }
44
+ get isConstant() { return this.inner.isConstant; }
45
+ toString() { return `show( ${this.inner.toString()} )`; }
46
+ pretty(indent) {
47
+ return `show( ${this.inner.pretty(indent)} )`;
48
+ }
49
+ clone() {
50
+ return new TirShowExpr(this.inner.clone(), this.range.clone());
51
+ }
52
+ deps() { return this.inner.deps(); }
53
+ toIR(ctx) {
54
+ return _showIR(this.inner.type, this.inner.toIR(ctx));
55
+ }
56
+ }
57
+ /**
58
+ * Inline an `IRTerm` of type `t` into its UTF-8 textual representation
59
+ * (also bytes). Returns the IR producing the show-encoded bytes.
60
+ *
61
+ * Mirrors `_inlineToData` — fully compile-time-dispatched per source type:
62
+ *
63
+ * int -> hoisted_intToUtf8Bytes (decimal, sign-aware)
64
+ * bytes -> hoisted_bytesToHex (lowercase hex)
65
+ * bool -> "true" / "false" via lazy ifThenElse
66
+ * data -> serialiseData -> bytesToHex
67
+ * void -> "()"
68
+ * string -> encodeUtf8 (treat as already-readable text)
69
+ * List<T> -> "[" + intercalate(", ", map(_showIR(T), elems)) + "]"
70
+ * LinearMap<K,V> -> "{" + intercalate(", ", map((k,v) -> showK(k) ++ ": " ++ showV(v), entries)) + "}"
71
+ *
72
+ * For struct-typed receivers (TirDataStructType / TirSoPStructType) this
73
+ * function does not produce IR — those should reach a user-declared
74
+ * `type X implements Show { show(self): bytes { ... } }` and dispatch
75
+ * through the regular method-call path. The caller is expected to detect
76
+ * that case before invoking `_showIR`.
77
+ */
78
+ export function _showIR(origin_t, exprIR) {
79
+ const t = getUnaliased(origin_t);
80
+ if (t instanceof TirIntT)
81
+ return _ir_apps(hoisted_intToUtf8Bytes.clone(), exprIR);
82
+ if (t instanceof TirBytesT)
83
+ return _ir_apps(hoisted_bytesToHex.clone(), exprIR);
84
+ if (t instanceof TirBoolT)
85
+ return _ir_lazyIfThenElse(exprIR, _hoisted_litBytes("true").clone(), _hoisted_litBytes("false").clone());
86
+ if (t instanceof TirDataT)
87
+ return _ir_apps(hoisted_bytesToHex.clone(), _ir_apps(IRNative.serialiseData, exprIR));
88
+ if (t instanceof TirVoidT)
89
+ return _hoisted_litBytes("()").clone();
90
+ if (t instanceof TirStringT)
91
+ return _ir_apps(IRNative.encodeUtf8, exprIR);
92
+ if (t instanceof TirListT)
93
+ return _showListIR(t, exprIR);
94
+ if (t instanceof TirLinearMapT)
95
+ return _showLinearMapIR(t, exprIR);
96
+ // data-encoded structs and Optional<T> auto-show via serialiseData + hex.
97
+ // Users can override by declaring `type X implements Show { ... }` —
98
+ // that path is handled before _showIR is reached.
99
+ if (t instanceof TirDataStructType
100
+ || t instanceof TirDataOptT)
101
+ return _ir_apps(hoisted_bytesToHex.clone(), _ir_apps(IRNative.serialiseData, exprIR));
102
+ throw new Error(`_showIR: no built-in Show impl for type ${origin_t.toString()}; ` +
103
+ `the type must declare \`type X implements Show { show(self): bytes { ... } }\``);
104
+ }
105
+ // ---- helpers ----
106
+ const _hoistedLits = {};
107
+ function _hoisted_litBytes(s) {
108
+ let cached = _hoistedLits[s];
109
+ if (!cached) {
110
+ cached = new IRHoisted(IRConst.bytes(fromUtf8(s)));
111
+ cached.hash;
112
+ _hoistedLits[s] = cached;
113
+ }
114
+ return cached;
115
+ }
116
+ // recurses through `_showIR`. Caches per element-type to avoid repeated
117
+ // IR construction across multiple call sites for the same `List<T>`.
118
+ const _listShowCache = new Map();
119
+ function _showListIR(listT, exprIR) {
120
+ const elemT = getUnaliased(getListTypeArg(listT));
121
+ if (!elemT)
122
+ throw new Error("_showListIR: missing element type");
123
+ const key = elemT.toConcreteTirTypeName();
124
+ let hoisted = _listShowCache.get(key);
125
+ if (!hoisted) {
126
+ // \xs ->
127
+ // "[" ++
128
+ // ( recur xs True )
129
+ // ++ "]"
130
+ // recur = fix \self \xs \first ->
131
+ // if nullList xs then ""
132
+ // else
133
+ // let head = headList xs
134
+ // let tail = tailList xs
135
+ // ( if first then "" else ", " )
136
+ // ++ showElem(head)
137
+ // ++ self(tail, False)
138
+ const xsOuter = Symbol("show_list_xs_outer");
139
+ const recSelf = Symbol("show_list_self");
140
+ const xs = Symbol("show_list_xs");
141
+ const first = Symbol("show_list_first");
142
+ const recur = new IRRecursive(recSelf, new IRFunc([xs, first], _ir_lazyIfThenElse(_ir_apps(IRNative.nullList, new IRVar(xs)), _hoisted_litBytes("").clone(),
143
+ // separator + show(head) + recur(tail, false)
144
+ _ir_apps(IRNative.appendByteString,
145
+ // separator
146
+ _ir_lazyIfThenElse(new IRVar(first), _hoisted_litBytes("").clone(), _hoisted_litBytes(", ").clone()), _ir_apps(IRNative.appendByteString,
147
+ // show(head)
148
+ _showIR(elemT, _ir_apps(IRNative.headList, new IRVar(xs))),
149
+ // recur(tail, false)
150
+ _ir_apps(new IRSelfCall(recSelf), _ir_apps(IRNative.tailList, new IRVar(xs)), IRConst.bool(false)))))));
151
+ hoisted = new IRHoisted(new IRFunc([xsOuter], _ir_apps(IRNative.appendByteString, _hoisted_litBytes("[").clone(), _ir_apps(IRNative.appendByteString, _ir_apps(recur, new IRVar(xsOuter), IRConst.bool(true)), _hoisted_litBytes("]").clone()))));
152
+ hoisted.hash;
153
+ _listShowCache.set(key, hoisted);
154
+ }
155
+ return _ir_apps(hoisted.clone(), exprIR);
156
+ }
157
+ const _linearMapShowCache = new Map();
158
+ function _showLinearMapIR(mapT, exprIR) {
159
+ const keyT = getUnaliased(mapT.keyTypeArg);
160
+ const valT = getUnaliased(mapT.valTypeArg);
161
+ const cacheKey = keyT.toConcreteTirTypeName() + "→" + valT.toConcreteTirTypeName();
162
+ let hoisted = _linearMapShowCache.get(cacheKey);
163
+ if (!hoisted) {
164
+ // LinearMap is List<PairData> at the IR level. Iterate similarly to
165
+ // _showListIR, but on each entry call showK(fst(entry)) ++ ": " ++
166
+ // showV(snd(entry)). Both sides go through `unBData / unIData / etc.`
167
+ // implicitly because the Pebble runtime stores keys/values as data.
168
+ //
169
+ // For show purposes we treat the entry as an opaque pair-of-data and
170
+ // recover the value bytes via `_inlineFromData(...)`-like dispatch.
171
+ // To keep this turn focused, we invoke `_showIR` with `data` and
172
+ // accept that K/V are shown via their data encoding (compact). A
173
+ // future polish pass can recover the original K/V types and call
174
+ // `_showIR(K)` / `_showIR(V)` directly.
175
+ const xsOuter = Symbol("show_map_outer");
176
+ const recSelf = Symbol("show_map_self");
177
+ const xs = Symbol("show_map_xs");
178
+ const first = Symbol("show_map_first");
179
+ // key/value extraction at the IR level: an entry is a Pair<Data,Data>;
180
+ // fst gets the data-encoded key, snd the data-encoded value. We then
181
+ // just call _showIR(data, ...) on both, which is `serialiseData ++ hex`.
182
+ const showEntry = (entryIR) => _ir_apps(IRNative.appendByteString, _showIR(/* data */ /* fall back to data show */
183
+ /* Pebble stores K and V as data inside LinearMap */
184
+ /* see TirLinearMapT.toUplcConstType */
185
+ /* TirDataT */
186
+ /* hack: reach the singleton via a fresh instance */
187
+ new (require("../types/TirNativeType/native/data.js").TirDataT)(), _ir_apps(IRNative.fstPair, entryIR)), _ir_apps(IRNative.appendByteString, _hoisted_litBytes(": ").clone(), _showIR(new (require("../types/TirNativeType/native/data.js").TirDataT)(), _ir_apps(IRNative.sndPair, entryIR))));
188
+ const recur = new IRRecursive(recSelf, new IRFunc([xs, first], _ir_lazyIfThenElse(_ir_apps(IRNative.nullList, new IRVar(xs)), _hoisted_litBytes("").clone(), _ir_apps(IRNative.appendByteString, _ir_lazyIfThenElse(new IRVar(first), _hoisted_litBytes("").clone(), _hoisted_litBytes(", ").clone()), _ir_apps(IRNative.appendByteString, showEntry(_ir_apps(IRNative.headList, new IRVar(xs))), _ir_apps(new IRSelfCall(recSelf), _ir_apps(IRNative.tailList, new IRVar(xs)), IRConst.bool(false)))))));
189
+ hoisted = new IRHoisted(new IRFunc([xsOuter], _ir_apps(IRNative.appendByteString, _hoisted_litBytes("{").clone(), _ir_apps(IRNative.appendByteString, _ir_apps(recur, new IRVar(xsOuter), IRConst.bool(true)), _hoisted_litBytes("}").clone()))));
190
+ hoisted.hash;
191
+ _linearMapShowCache.set(cacheKey, hoisted);
192
+ }
193
+ return _ir_apps(hoisted.clone(), exprIR);
194
+ }
195
+ /** A canonical 1-arg `(T) -> bytes` IR closure for use as a `Show` dictionary entry. */
196
+ export function _showUplcFunc(origin_t) {
197
+ const xSym = Symbol("show_self");
198
+ return new IRHoisted(new IRFunc([xSym], _showIR(origin_t, new IRVar(xSym))));
199
+ }
@@ -2,10 +2,10 @@ import { _ir_apps } from "../../../IR/IRNodes/IRApp.js";
2
2
  import { IRDelayed } from "../../../IR/IRNodes/IRDelayed.js";
3
3
  import { IRForced } from "../../../IR/IRNodes/IRForced.js";
4
4
  import { IRNative } from "../../../IR/IRNodes/IRNative/index.js";
5
- import { hoisted_intToUtf8Bytes } from "../../../IR/tree_utils/intToUtf8Bytes.js";
6
5
  import { mergeSortedStrArrInplace } from "../../../utils/array/mergeSortedStrArrInplace.js";
7
- import { TirIntT } from "../types/TirNativeType/index.js";
6
+ import { TirBytesT } from "../types/TirNativeType/index.js";
8
7
  import { getUnaliased } from "../types/utils/getUnaliased.js";
8
+ import { _showIR } from "./TirShowExpr.js";
9
9
  export class TirTraceExpr {
10
10
  traceExpr;
11
11
  continuation;
@@ -40,13 +40,17 @@ export class TirTraceExpr {
40
40
  toIR(ctx) {
41
41
  let bytesIR;
42
42
  const exprType = getUnaliased(this.traceExpr.type);
43
- if (exprType instanceof TirIntT) {
44
- // int -> bytes via intToUtf8Bytes
45
- bytesIR = _ir_apps(hoisted_intToUtf8Bytes.clone(), this.traceExpr.toIR(ctx));
43
+ if (exprType instanceof TirBytesT) {
44
+ // bytes are assumed to already be valid UTF-8 — pass through
45
+ // unchanged, as documented for the Show interface.
46
+ bytesIR = this.traceExpr.toIR(ctx);
46
47
  }
47
48
  else {
48
- // assume bytes
49
- bytesIR = this.traceExpr.toIR(ctx);
49
+ // any other type implementing Show: dispatch through `_showIR`,
50
+ // which uses the per-type compile-time table (int → decimal,
51
+ // bool → "true"/"false", data → serialiseData+hex, struct →
52
+ // user impl or auto-derive, list/map → recursive, ...).
53
+ bytesIR = _showIR(exprType, this.traceExpr.toIR(ctx));
50
54
  }
51
55
  // Force(trace(msg, Delay(continuation)))
52
56
  // Delay prevents the continuation from being evaluated
@@ -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))