@harmoniclabs/pebble 0.2.0 → 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 (116) 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 +22 -2
  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 +27 -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 +12 -0
  17. package/dist/IR/toUPLC/CompilerOptions.js +14 -9
  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/ast/nodes/statements/declarations/StructDecl.d.ts +16 -2
  35. package/dist/ast/nodes/statements/declarations/StructDecl.js +15 -1
  36. package/dist/compiler/AstCompiler/AstCompiler.d.ts +1 -0
  37. package/dist/compiler/AstCompiler/AstCompiler.js +41 -4
  38. package/dist/compiler/AstCompiler/internal/_deriveContractBody/_deriveContractBody.js +3 -3
  39. package/dist/compiler/AstCompiler/internal/exprs/_compileCaseExpr.js +31 -0
  40. package/dist/compiler/AstCompiler/internal/exprs/_compileIsExpr.js +12 -0
  41. package/dist/compiler/AstCompiler/internal/exprs/_compilePropAccessExpr.js +36 -0
  42. package/dist/compiler/AstCompiler/internal/exprs/_compileUnaryPrefixExpr.js +13 -1
  43. package/dist/compiler/AstCompiler/internal/exprs/binary/_compileAddExpr.js +18 -5
  44. package/dist/compiler/AstCompiler/internal/exprs/binary/_compileEqualExpr.js +3 -1
  45. package/dist/compiler/AstCompiler/internal/exprs/binary/_compileGreaterThanEqualExpr.js +2 -1
  46. package/dist/compiler/AstCompiler/internal/exprs/binary/_compileGreaterThanExpr.js +2 -1
  47. package/dist/compiler/AstCompiler/internal/exprs/binary/_compileLessThanEqualExpr.js +2 -1
  48. package/dist/compiler/AstCompiler/internal/exprs/binary/_compileLessThanExpr.js +2 -1
  49. package/dist/compiler/AstCompiler/internal/exprs/binary/_compileMultExpr.js +24 -6
  50. package/dist/compiler/AstCompiler/internal/exprs/binary/_compileNotEqualExpr.js +2 -1
  51. package/dist/compiler/AstCompiler/internal/exprs/binary/_compileSubExpr.js +16 -5
  52. package/dist/compiler/AstCompiler/internal/statements/_compileMatchStmt.js +33 -20
  53. package/dist/compiler/AstCompiler/utils/getPropAccessReturnType.js +11 -0
  54. package/dist/compiler/Compiler.js +20 -27
  55. package/dist/compiler/TirCompiler/expressify/ExpressifyCtx.js +1 -1
  56. package/dist/compiler/TirCompiler/expressify/expressify.js +30 -2
  57. package/dist/compiler/TirCompiler/expressify/expressifyForStmt.d.ts +2 -1
  58. package/dist/compiler/TirCompiler/expressify/expressifyForStmt.js +45 -7
  59. package/dist/compiler/TirCompiler/expressify/expressifyVars.d.ts +0 -1
  60. package/dist/compiler/TirCompiler/expressify/expressifyVars.js +23 -8
  61. package/dist/compiler/tir/expressions/TirCaseExpr.d.ts +9 -0
  62. package/dist/compiler/tir/expressions/TirCaseExpr.js +144 -122
  63. package/dist/compiler/tir/expressions/TirElemAccessExpr.js +2 -2
  64. package/dist/compiler/tir/expressions/TirFromDataExpr.js +102 -67
  65. package/dist/compiler/tir/expressions/TirIsExpr.js +14 -1
  66. package/dist/compiler/tir/expressions/TirNativeFunc.d.ts +1 -2
  67. package/dist/compiler/tir/expressions/TirNativeFunc.js +2 -12
  68. package/dist/compiler/tir/expressions/TirToDataExpr.js +3 -0
  69. package/dist/compiler/tir/expressions/TirTypeConversionExpr.js +10 -0
  70. package/dist/compiler/tir/expressions/TirVariableAccessExpr.d.ts +2 -3
  71. package/dist/compiler/tir/expressions/TirVariableAccessExpr.js +1 -4
  72. package/dist/compiler/tir/expressions/ToIRTermCtx.d.ts +20 -3
  73. package/dist/compiler/tir/expressions/ToIRTermCtx.js +48 -3
  74. package/dist/compiler/tir/expressions/binary/TirBinaryExpr.d.ts +2 -2
  75. package/dist/compiler/tir/expressions/binary/TirBinaryExpr.js +45 -8
  76. package/dist/compiler/tir/expressions/litteral/TirLitEnumMemberExpr.d.ts +19 -0
  77. package/dist/compiler/tir/expressions/litteral/TirLitEnumMemberExpr.js +24 -0
  78. package/dist/compiler/tir/expressions/litteral/TirLitteralExpr.d.ts +2 -1
  79. package/dist/compiler/tir/expressions/litteral/TirLitteralExpr.js +2 -0
  80. package/dist/compiler/tir/expressions/unary/TirUnaryMinus.js +4 -1
  81. package/dist/compiler/tir/program/stdScope/populateStdNamespace.js +49 -4
  82. package/dist/compiler/tir/program/stdScope/prelude/preludeTypesSrc.js +35 -2
  83. package/dist/compiler/tir/program/stdScope/stdScope.d.ts +7 -0
  84. package/dist/compiler/tir/program/stdScope/stdScope.js +83 -40
  85. package/dist/compiler/tir/types/TirEnumType.d.ts +21 -0
  86. package/dist/compiler/tir/types/TirEnumType.js +36 -0
  87. package/dist/compiler/tir/types/TirNativeType/TirNativeType.d.ts +4 -2
  88. package/dist/compiler/tir/types/TirNativeType/TirNativeType.js +5 -0
  89. package/dist/compiler/tir/types/TirNativeType/native/array.d.ts +16 -0
  90. package/dist/compiler/tir/types/TirNativeType/native/array.js +38 -0
  91. package/dist/compiler/tir/types/TirNativeType/native/index.d.ts +2 -0
  92. package/dist/compiler/tir/types/TirNativeType/native/index.js +2 -0
  93. package/dist/compiler/tir/types/TirNativeType/native/value.d.ts +18 -0
  94. package/dist/compiler/tir/types/TirNativeType/native/value.js +17 -0
  95. package/dist/compiler/tir/types/TirStructType.js +6 -1
  96. package/dist/compiler/tir/types/TirType.d.ts +3 -2
  97. package/dist/compiler/tir/types/TirType.js +4 -1
  98. package/dist/compiler/tir/types/utils/canAssignTo.js +28 -0
  99. package/dist/compiler/tir/types/utils/canCastTo.js +14 -1
  100. package/dist/compiler/tir/types/utils/getDeconstructableType.d.ts +2 -1
  101. package/dist/compiler/tir/types/utils/getDeconstructableType.js +2 -0
  102. package/dist/compiler/tir/types/utils/inferTypeArgs.js +4 -0
  103. package/dist/compiler/tir/types/utils/normalizeEnumToInt.d.ts +10 -0
  104. package/dist/compiler/tir/types/utils/normalizeEnumToInt.js +17 -0
  105. package/dist/compiler/tir/types/utils/substituteTypeParams.js +5 -0
  106. package/dist/diagnostics/diagnosticMessages.generated.d.ts +5 -0
  107. package/dist/diagnostics/diagnosticMessages.generated.js +10 -0
  108. package/dist/parser/Parser.js +29 -13
  109. package/dist/tokenizer/Token.d.ts +8 -7
  110. package/dist/tokenizer/Token.js +8 -7
  111. package/dist/tokenizer/utils/tokenFromKeyword.js +2 -0
  112. package/dist/version.generated.d.ts +1 -1
  113. package/dist/version.generated.js +1 -1
  114. package/package.json +3 -3
  115. package/dist/IR/tree_utils/_ir_lazyChooseList.d.ts +0 -3
  116. package/dist/IR/tree_utils/_ir_lazyChooseList.js +0 -7
@@ -11,8 +11,15 @@ export declare const string_t: TirStringT;
11
11
  export declare const bytes_t: TirBytesT;
12
12
  export declare const bool_t: TirBoolT;
13
13
  export declare const data_t: TirDataT;
14
+ export declare const valueMapLovelacesName: string;
15
+ export declare const valueMapAmountOfName: string;
14
16
  export declare const valueLovelacesName: string;
15
17
  export declare const valueAmountOfName: string;
18
+ export declare const valueInsertCoinName: string;
19
+ export declare const valueUnionName: string;
20
+ export declare const valueContainsName: string;
21
+ export declare const valueScaleName: string;
22
+ export declare const valueToDataName: string;
16
23
  export declare const getCredentialHashFuncName: string;
17
24
  export declare function populateStdScope(program: TypedProgram): void;
18
25
  export declare function populatePreludeScope(program: TypedProgram): void;
@@ -6,6 +6,8 @@ import { TirDataT } from "../../types/TirNativeType/native/data.js";
6
6
  import { TirIntT } from "../../types/TirNativeType/native/int.js";
7
7
  import { TirLinearMapT } from "../../types/TirNativeType/native/linearMap.js";
8
8
  import { TirListT } from "../../types/TirNativeType/native/list.js";
9
+ import { TirArrayT } from "../../types/TirNativeType/native/array.js";
10
+ import { TirValueT } from "../../types/TirNativeType/native/value.js";
9
11
  import { TirDataOptT } from "../../types/TirNativeType/native/Optional/data.js";
10
12
  import { TirSopOptT } from "../../types/TirNativeType/native/Optional/sop.js";
11
13
  import { TirStringT } from "../../types/TirNativeType/native/string.js";
@@ -18,14 +20,23 @@ import { IRNative } from "../../../../IR/IRNodes/IRNative/index.js";
18
20
  import { IRFunc } from "../../../../IR/IRNodes/IRFunc.js";
19
21
  import { IRVar } from "../../../../IR/IRNodes/IRVar.js";
20
22
  import { _ir_apps } from "../../../../IR/IRNodes/IRApp.js";
23
+ import { IRConst } from "../../../../IR/IRNodes/IRConst.js";
21
24
  export const void_t = new TirVoidT();
22
25
  export const int_t = new TirIntT();
23
26
  export const string_t = new TirStringT();
24
27
  export const bytes_t = new TirBytesT();
25
28
  export const bool_t = new TirBoolT();
26
29
  export const data_t = new TirDataT();
27
- export const valueLovelacesName = PEBBLE_INTERNAL_IDENTIFIER_PREFIX + "sortedValueLovelaces";
28
- export const valueAmountOfName = PEBBLE_INTERNAL_IDENTIFIER_PREFIX + "amountOfValue";
30
+ export const valueMapLovelacesName = PEBBLE_INTERNAL_IDENTIFIER_PREFIX + "sortedValueLovelaces";
31
+ export const valueMapAmountOfName = PEBBLE_INTERNAL_IDENTIFIER_PREFIX + "amountOfValue";
32
+ // Native Value (ConstTyTag.value); methods route to the value builtins.
33
+ export const valueLovelacesName = PEBBLE_INTERNAL_IDENTIFIER_PREFIX + "valueLovelaces";
34
+ export const valueAmountOfName = PEBBLE_INTERNAL_IDENTIFIER_PREFIX + "valueAmountOf";
35
+ export const valueInsertCoinName = PEBBLE_INTERNAL_IDENTIFIER_PREFIX + "valueInsertCoin";
36
+ export const valueUnionName = PEBBLE_INTERNAL_IDENTIFIER_PREFIX + "valueUnion";
37
+ export const valueContainsName = PEBBLE_INTERNAL_IDENTIFIER_PREFIX + "valueContains";
38
+ export const valueScaleName = PEBBLE_INTERNAL_IDENTIFIER_PREFIX + "valueScale";
39
+ export const valueToDataName = PEBBLE_INTERNAL_IDENTIFIER_PREFIX + "valueToData";
29
40
  export const getCredentialHashFuncName = PEBBLE_INTERNAL_IDENTIFIER_PREFIX + "getCredentialHash";
30
41
  export function populateStdScope(program) {
31
42
  const stdScope = program.stdScope;
@@ -40,6 +51,24 @@ export function populateStdScope(program) {
40
51
  _defineStdUnambigous(bytes_t);
41
52
  _defineStdUnambigous(string_t);
42
53
  _defineStdUnambigous(data_t);
54
+ // Register native Value (ConstTyTag.value) in program.types so the prelude
55
+ // alias can wrap it. The user-facing `Value` name is defined in the
56
+ // prelude scope (with methods routed to the value builtins).
57
+ {
58
+ const value_native_t = new TirValueT();
59
+ program.types.set(value_native_t.toTirTypeKey(), value_native_t);
60
+ }
61
+ const array_name = TirArrayT.toTirTypeKey();
62
+ program.defineGenericType(array_name, 1, ([arg]) => new TirArrayT(arg));
63
+ stdScope.defineType(array_name, {
64
+ sopTirName: array_name,
65
+ dataTirName: array_name,
66
+ allTirNames: new Set([
67
+ array_name
68
+ ]),
69
+ methodsNames: new Map(),
70
+ isGeneric: true
71
+ });
43
72
  const opt_data_name = TirDataOptT.toTirTypeKey();
44
73
  program.defineGenericType(opt_data_name, 1, ([arg]) => new TirDataOptT(arg));
45
74
  const opt_sop_name = TirSopOptT.toTirTypeKey();
@@ -542,29 +571,29 @@ export function populatePreludeScope(program) {
542
571
  payment: credential_t,
543
572
  stake: opt_stakeCredential_t
544
573
  }, onlyData);
545
- // type Value = LinearMap<PolicyId, LinearMap<TokenName, int>>
574
+ // type ValueMap = LinearMap<PolicyId, LinearMap<TokenName, int>>
546
575
  const map_tokenName_int_t = program.getAppliedGeneric(TirLinearMapT.toTirTypeKey(), [tokenName_t, int_t]);
547
576
  if (!map_tokenName_int_t)
548
577
  throw new Error("expected map_tokenName_int_t");
549
578
  const map_policyId_map_tokenName_int_t = program.getAppliedGeneric(TirLinearMapT.toTirTypeKey(), [policyId_t, map_tokenName_int_t]);
550
579
  if (!map_policyId_map_tokenName_int_t)
551
580
  throw new Error("expected map_policyId_map_tokenName_int_t");
552
- const value_t = _defineUnambigousAlias("Value", map_policyId_map_tokenName_int_t, new Map([
581
+ const valueMap_t = _defineUnambigousAlias("ValueMap", map_policyId_map_tokenName_int_t, new Map([
553
582
  [
554
583
  "lovelaces",
555
- valueLovelacesName
584
+ valueMapLovelacesName
556
585
  ],
557
586
  [
558
587
  "amountOf",
559
- valueAmountOfName
588
+ valueMapAmountOfName
560
589
  ],
561
590
  ]));
562
- preludeScope.program.functions.set(valueLovelacesName, new TirInlineClosedIR(new TirFuncT([value_t], int_t), (ctx) => IRNative._sortedValueLovelaces, SourceRange.unknown));
563
- // Value.amountOf( policy: PolicyId, name: bytes ): int
591
+ preludeScope.program.functions.set(valueMapLovelacesName, new TirInlineClosedIR(new TirFuncT([valueMap_t], int_t), (ctx) => IRNative._sortedValueLovelaces, SourceRange.unknown));
592
+ // ValueMap.amountOf( policy: PolicyId, name: bytes ): int
564
593
  // The IR native _amountOfValue is curried as: (isPolicy)(value)(isTokenName) => int
565
594
  // where isPolicy and isTokenName are equality-check predicates.
566
595
  // This wrapper adapts (self, policy, tokenName) => _amountOfValue(p => equalsByteString(p, policy))(self)(tn => equalsByteString(tn, tokenName))
567
- preludeScope.program.functions.set(valueAmountOfName, new TirInlineClosedIR(new TirFuncT([value_t, policyId_t, bytes_t], int_t), (ctx) => {
596
+ preludeScope.program.functions.set(valueMapAmountOfName, new TirInlineClosedIR(new TirFuncT([valueMap_t, policyId_t, bytes_t], int_t), (ctx) => {
568
597
  const self = Symbol("amtOf_self");
569
598
  const policy = Symbol("amtOf_policy");
570
599
  const tokenName = Symbol("amtOf_tokenName");
@@ -578,37 +607,51 @@ export function populatePreludeScope(program) {
578
607
  // isTokenName predicate: \tn -> equalsByteString(tn, tokenName)
579
608
  _ir_apps(IRNative.equalsByteString, new IRVar(tokenName))));
580
609
  }, SourceRange.unknown));
581
- /* // TODO
582
- untagged struct FlatValueEntry {
583
- policy: PolicyId,
584
- name: bytes,
585
- amount: int
586
- }
587
-
588
- type FlatValue = List<FlatValueEntry>;
589
-
590
- type Value implements {
591
- amountOf( policy: PolicyId, name: bytes ): int
592
- {
593
- return native.valueAmountOf( this, policy, name );
594
- }
595
- lovelaces(): int
596
- {
597
- return this.amountOf( #, # );
598
- }
599
- flatten(): FlatValue
600
- {
601
- return native.flattenValue( this );
602
- }
603
- }
604
-
605
- type FlatValue implements {
606
- unflatten(): Value
607
- {
608
- return native.unflattenValue( this );
609
- }
610
- }
611
- */
610
+ // Native `Value` (ConstTyTag.value). Methods route to the value builtins.
611
+ const value_native_t = program.types.get(TirValueT.toTirTypeKey());
612
+ if (!value_native_t)
613
+ throw new Error("expected native Value type registered");
614
+ const value_t = _defineUnambigousAlias("Value", value_native_t, new Map([
615
+ ["lovelaces", valueLovelacesName],
616
+ ["amountOf", valueAmountOfName],
617
+ ["insert", valueInsertCoinName],
618
+ ["union", valueUnionName],
619
+ ["contains", valueContainsName],
620
+ ["scale", valueScaleName],
621
+ ["toData", valueToDataName],
622
+ ]));
623
+ // Value.lovelaces(): int -- lookupCoin "" "" self
624
+ preludeScope.program.functions.set(valueLovelacesName, new TirInlineClosedIR(new TirFuncT([value_t], int_t), (ctx) => {
625
+ const self = Symbol("value_lovelaces_self");
626
+ return new IRFunc([self], _ir_apps(IRNative.lookupCoin, IRConst.bytes(new Uint8Array(0)), IRConst.bytes(new Uint8Array(0)), new IRVar(self)));
627
+ }, SourceRange.unknown));
628
+ // Value.amountOf( policy, name ): int -- lookupCoin policy name self
629
+ preludeScope.program.functions.set(valueAmountOfName, new TirInlineClosedIR(new TirFuncT([value_t, policyId_t, bytes_t], int_t), (ctx) => {
630
+ const self = Symbol("value_amountOf_self");
631
+ const policy = Symbol("value_amountOf_policy");
632
+ const tokenName = Symbol("value_amountOf_tokenName");
633
+ return new IRFunc([self, policy, tokenName], _ir_apps(IRNative.lookupCoin, new IRVar(policy), new IRVar(tokenName), new IRVar(self)));
634
+ }, SourceRange.unknown));
635
+ // Value.insert( policy, name, amount ): Value -- insertCoin policy name amount self
636
+ preludeScope.program.functions.set(valueInsertCoinName, new TirInlineClosedIR(new TirFuncT([value_t, policyId_t, bytes_t, int_t], value_t), (ctx) => {
637
+ const self = Symbol("value_insert_self");
638
+ const policy = Symbol("value_insert_policy");
639
+ const tokenName = Symbol("value_insert_tokenName");
640
+ const amount = Symbol("value_insert_amount");
641
+ return new IRFunc([self, policy, tokenName, amount], _ir_apps(IRNative.insertCoin, new IRVar(policy), new IRVar(tokenName), new IRVar(amount), new IRVar(self)));
642
+ }, SourceRange.unknown));
643
+ // Value.union( other ): Value -- unionValue self other
644
+ preludeScope.program.functions.set(valueUnionName, new TirInlineClosedIR(new TirFuncT([value_t, value_t], value_t), (ctx) => IRNative.unionValue, SourceRange.unknown));
645
+ // Value.contains( other ): bool -- valueContains self other
646
+ preludeScope.program.functions.set(valueContainsName, new TirInlineClosedIR(new TirFuncT([value_t, value_t], bool_t), (ctx) => IRNative.valueContains, SourceRange.unknown));
647
+ // Value.scale( factor ): Value -- scaleValue factor self
648
+ preludeScope.program.functions.set(valueScaleName, new TirInlineClosedIR(new TirFuncT([value_t, int_t], value_t), (ctx) => {
649
+ const self = Symbol("value_scale_self");
650
+ const factor = Symbol("value_scale_factor");
651
+ return new IRFunc([self, factor], _ir_apps(IRNative.scaleValue, new IRVar(factor), new IRVar(self)));
652
+ }, SourceRange.unknown));
653
+ // Value.toData(): data -- valueData self
654
+ preludeScope.program.functions.set(valueToDataName, new TirInlineClosedIR(new TirFuncT([value_t], data_t), (ctx) => IRNative.valueData, SourceRange.unknown));
612
655
  // struct OutputDatum {
613
656
  // NoDatum {}
614
657
  // DatumHash { hash: Hash32 }
@@ -0,0 +1,21 @@
1
+ import { ConstType } from "@harmoniclabs/uplc";
2
+ import { AstFuncName, TirFuncName } from "../../AstCompiler/scope/AstScope.js";
3
+ import { ITirType, TirType } from "./TirType.js";
4
+ export declare class TirEnumType implements ITirType {
5
+ readonly name: string;
6
+ readonly fileUid: string;
7
+ readonly members: readonly string[];
8
+ readonly methodNamesPtr: Map<AstFuncName, TirFuncName>;
9
+ constructor(name: string, fileUid: string, members: readonly string[], methodNamesPtr: Map<AstFuncName, TirFuncName>);
10
+ indexOf(member: string): number;
11
+ isConcrete(): boolean;
12
+ hasDataEncoding(): boolean;
13
+ toString(): string;
14
+ toAstName(): string;
15
+ toTirTypeKey(): string;
16
+ toConcreteTirTypeName(): string;
17
+ toUplcConstType(): ConstType;
18
+ clone(): TirEnumType;
19
+ }
20
+ export declare function isTirEnumType(thing: any): thing is TirEnumType;
21
+ export declare function getEnumType(type: TirType | undefined): TirEnumType | undefined;
@@ -0,0 +1,36 @@
1
+ import { isObject } from "@harmoniclabs/obj-utils";
2
+ import { constT } from "@harmoniclabs/uplc";
3
+ import { TirAliasType } from "./TirAliasType.js";
4
+ export class TirEnumType {
5
+ name;
6
+ fileUid;
7
+ members;
8
+ methodNamesPtr;
9
+ constructor(name, fileUid, members, methodNamesPtr) {
10
+ this.name = name;
11
+ this.fileUid = fileUid;
12
+ this.members = members;
13
+ this.methodNamesPtr = methodNamesPtr;
14
+ }
15
+ indexOf(member) {
16
+ return this.members.indexOf(member);
17
+ }
18
+ isConcrete() { return true; }
19
+ hasDataEncoding() { return true; }
20
+ toString() { return this.name; }
21
+ toAstName() { return this.name; }
22
+ toTirTypeKey() { return "enum_" + this.name + "_" + this.fileUid; }
23
+ toConcreteTirTypeName() { return this.toTirTypeKey(); }
24
+ toUplcConstType() { return constT.int; }
25
+ clone() {
26
+ return new TirEnumType(this.name, this.fileUid, this.members.slice(), this.methodNamesPtr);
27
+ }
28
+ }
29
+ export function isTirEnumType(thing) {
30
+ return isObject(thing) && thing instanceof TirEnumType;
31
+ }
32
+ export function getEnumType(type) {
33
+ while (type instanceof TirAliasType)
34
+ type = type.aliased;
35
+ return type instanceof TirEnumType ? type : undefined;
36
+ }
@@ -12,9 +12,11 @@ import { TirDataOptT } from "./native/Optional/data.js";
12
12
  import { TirSopOptT } from "./native/Optional/sop.js";
13
13
  import { TirStringT } from "./native/string.js";
14
14
  import { TirVoidT } from "./native/void.js";
15
- export type TirNamedDestructableNativeType = TirDataT | TirDataOptT<TirType> | TirSopOptT<TirType> | TirListT<TirType> | TirLinearMapT<TirType, TirType>;
15
+ import { TirArrayT } from "./native/array.js";
16
+ import { TirValueT } from "./native/value.js";
17
+ export type TirNamedDestructableNativeType = TirDataT | TirDataOptT<TirType> | TirSopOptT<TirType> | TirListT<TirType> | TirArrayT<TirType> | TirLinearMapT<TirType, TirType>;
16
18
  export declare function isTirNamedDestructableNativeType(t: any): t is TirNamedDestructableNativeType;
17
- export type TirNativeType = TirVoidT | TirBoolT | TirIntT | TirBytesT | TirStringT | TirDataT | TirDataOptT<TirType> | TirSopOptT<TirType> | TirListT<TirType> | TirLinearMapT<TirType, TirType> | TirLinearMapEntryT<TirType, TirType> | TirFuncT | TirUnConstrDataResultT | TirPairDataT | TirBlsG1T | TirBlsG2T | TirMlResultT;
19
+ export type TirNativeType = TirVoidT | TirBoolT | TirIntT | TirBytesT | TirStringT | TirDataT | TirDataOptT<TirType> | TirSopOptT<TirType> | TirListT<TirType> | TirArrayT<TirType> | TirValueT | TirLinearMapT<TirType, TirType> | TirLinearMapEntryT<TirType, TirType> | TirFuncT | TirUnConstrDataResultT | TirPairDataT | TirBlsG1T | TirBlsG2T | TirMlResultT;
18
20
  export declare function isTirNativeType(t: any): t is TirNativeType;
19
21
  export declare class TirUnConstrDataResultT implements ITirType {
20
22
  constructor();
@@ -11,11 +11,14 @@ import { TirDataOptT } from "./native/Optional/data.js";
11
11
  import { TirSopOptT } from "./native/Optional/sop.js";
12
12
  import { TirStringT } from "./native/string.js";
13
13
  import { TirVoidT } from "./native/void.js";
14
+ import { TirArrayT } from "./native/array.js";
15
+ import { TirValueT } from "./native/value.js";
14
16
  export function isTirNamedDestructableNativeType(t) {
15
17
  return (t instanceof TirDataT
16
18
  || t instanceof TirDataOptT
17
19
  || t instanceof TirSopOptT
18
20
  || t instanceof TirListT
21
+ || t instanceof TirArrayT
19
22
  || t instanceof TirLinearMapT);
20
23
  }
21
24
  export function isTirNativeType(t) {
@@ -28,6 +31,8 @@ export function isTirNativeType(t) {
28
31
  || t instanceof TirDataOptT
29
32
  || t instanceof TirSopOptT
30
33
  || t instanceof TirListT
34
+ || t instanceof TirArrayT
35
+ || t instanceof TirValueT
31
36
  || t instanceof TirLinearMapT
32
37
  || t instanceof TirLinearMapEntryT
33
38
  || t instanceof TirFuncT // =>
@@ -0,0 +1,16 @@
1
+ import { ConstType } from "@harmoniclabs/uplc";
2
+ import { TirType, ITirType } from "../../TirType.js";
3
+ export declare class TirArrayT<T extends TirType = TirType> implements ITirType {
4
+ readonly typeArg: T;
5
+ constructor(typeArg: T);
6
+ hasDataEncoding(): boolean;
7
+ static toTirTypeKey(): string;
8
+ toTirTypeKey(): string;
9
+ toConcreteTirTypeName(): string;
10
+ toString(): string;
11
+ toAstName(): string;
12
+ private _isConcrete;
13
+ isConcrete(): boolean;
14
+ clone(): TirArrayT<T>;
15
+ toUplcConstType(): ConstType;
16
+ }
@@ -0,0 +1,38 @@
1
+ import { constT } from "@harmoniclabs/uplc";
2
+ import { getAppliedTirTypeName } from "../../../program/TypedProgram.js";
3
+ export class TirArrayT {
4
+ typeArg;
5
+ constructor(typeArg) {
6
+ this.typeArg = typeArg;
7
+ }
8
+ hasDataEncoding() { return this.typeArg.hasDataEncoding(); }
9
+ static toTirTypeKey() {
10
+ return "Array";
11
+ }
12
+ toTirTypeKey() {
13
+ return TirArrayT.toTirTypeKey();
14
+ }
15
+ toConcreteTirTypeName() {
16
+ return getAppliedTirTypeName(this.toTirTypeKey(), [this.typeArg.toConcreteTirTypeName()]);
17
+ }
18
+ toString() {
19
+ return `${this.toTirTypeKey()}<${this.typeArg.toString()}>`;
20
+ }
21
+ toAstName() {
22
+ return this.toTirTypeKey();
23
+ }
24
+ _isConcrete = undefined;
25
+ isConcrete() {
26
+ if (typeof this._isConcrete !== "boolean")
27
+ this._isConcrete = this.typeArg.isConcrete();
28
+ return this._isConcrete ?? false;
29
+ }
30
+ clone() {
31
+ const result = new TirArrayT(this.typeArg.clone());
32
+ result._isConcrete = this._isConcrete;
33
+ return result;
34
+ }
35
+ toUplcConstType() {
36
+ return constT.arrayOf(this.typeArg.toUplcConstType());
37
+ }
38
+ }
@@ -1,4 +1,5 @@
1
1
  export * from "./Optional/index.js";
2
+ export * from "./array.js";
2
3
  export * from "./bool.js";
3
4
  export * from "./bytes.js";
4
5
  export * from "./data.js";
@@ -8,4 +9,5 @@ export * from "./linearMap.js";
8
9
  export * from "./linearMapEntry.js";
9
10
  export * from "./list.js";
10
11
  export * from "./string.js";
12
+ export * from "./value.js";
11
13
  export * from "./void.js";
@@ -1,4 +1,5 @@
1
1
  export * from "./Optional/index.js";
2
+ export * from "./array.js";
2
3
  export * from "./bool.js";
3
4
  export * from "./bytes.js";
4
5
  export * from "./data.js";
@@ -8,4 +9,5 @@ export * from "./linearMap.js";
8
9
  export * from "./linearMapEntry.js";
9
10
  export * from "./list.js";
10
11
  export * from "./string.js";
12
+ export * from "./value.js";
11
13
  export * from "./void.js";
@@ -0,0 +1,18 @@
1
+ import { ConstType } from "@harmoniclabs/uplc";
2
+ import { ITirType } from "../../TirType.js";
3
+ export declare class TirValueT implements ITirType {
4
+ clone(): TirValueT;
5
+ isConcrete(): boolean;
6
+ toString(): string;
7
+ toAstName(): string;
8
+ static toTirTypeKey(): string;
9
+ toTirTypeKey(): string;
10
+ toConcreteTirTypeName(): string;
11
+ /**
12
+ * Native `Value` is bidirectionally reachable from `data` via
13
+ * `unValueData` / `valueData`, so it qualifies as data-encoded for
14
+ * the purposes of `data struct` field types.
15
+ */
16
+ hasDataEncoding(): boolean;
17
+ toUplcConstType(): ConstType;
18
+ }
@@ -0,0 +1,17 @@
1
+ import { constT } from "@harmoniclabs/uplc";
2
+ export class TirValueT {
3
+ clone() { return new TirValueT(); }
4
+ isConcrete() { return true; }
5
+ toString() { return "Value"; }
6
+ toAstName() { return "Value"; }
7
+ static toTirTypeKey() { return "Value"; }
8
+ toTirTypeKey() { return TirValueT.toTirTypeKey(); }
9
+ toConcreteTirTypeName() { return this.toTirTypeKey(); }
10
+ /**
11
+ * Native `Value` is bidirectionally reachable from `data` via
12
+ * `unValueData` / `valueData`, so it qualifies as data-encoded for
13
+ * the purposes of `data struct` field types.
14
+ */
15
+ hasDataEncoding() { return true; }
16
+ toUplcConstType() { return constT.value; }
17
+ }
@@ -27,7 +27,12 @@ export class TirDataStructType {
27
27
  this.constructors = constructors;
28
28
  this.methodNamesPtr = methodNamesPtr;
29
29
  this.untagged = untagged;
30
- this.untagged = false; // always false for now
30
+ // `untagged === true` requires a single constructor — its runtime
31
+ // form is `listData(fields)` instead of `constrData(idx, fields)`.
32
+ if (untagged && constructors.length !== 1) {
33
+ throw new Error("untagged data struct must have exactly one constructor; got "
34
+ + constructors.length);
35
+ }
31
36
  this.narrowedFromParentCtorIdxs = narrowedFromParentCtorIdxs;
32
37
  }
33
38
  hasDataEncoding() { return true; }
@@ -1,5 +1,6 @@
1
1
  import { TirAliasType } from "./TirAliasType.js";
2
2
  import { TirStructType } from "./TirStructType.js";
3
+ import { TirEnumType } from "./TirEnumType.js";
3
4
  import { TirNamedDestructableNativeType, TirNativeType } from "./TirNativeType/TirNativeType.js";
4
5
  import { TirTypeParam } from "./TirTypeParam.js";
5
6
  import { ConstType } from "@harmoniclabs/uplc";
@@ -28,7 +29,7 @@ export interface ITirType {
28
29
  }
29
30
  export type TirType = TirNativeType | TirCustomType | TirTypeParam;
30
31
  export declare function isTirType(thing: any): thing is TirType;
31
- export type TirCustomType = TirAliasType | TirStructType;
32
+ export type TirCustomType = TirAliasType | TirStructType | TirEnumType;
32
33
  export declare function isTirCustomType(thing: any): thing is TirCustomType;
33
- export type TirNamedDestructableType = TirNamedDestructableNativeType | TirStructType;
34
+ export type TirNamedDestructableType = TirNamedDestructableNativeType | TirStructType | TirEnumType;
34
35
  export declare function isTirNamedDestructableType(thing: any): thing is TirNamedDestructableType;
@@ -1,6 +1,7 @@
1
1
  import { isObject } from "@harmoniclabs/obj-utils";
2
2
  import { TirAliasType } from "./TirAliasType.js";
3
3
  import { isTirStructType } from "./TirStructType.js";
4
+ import { isTirEnumType } from "./TirEnumType.js";
4
5
  import { isTirNamedDestructableNativeType, isTirNativeType } from "./TirNativeType/TirNativeType.js";
5
6
  import { TirTypeParam } from "./TirTypeParam.js";
6
7
  export function isTirType(thing) {
@@ -10,9 +11,11 @@ export function isTirType(thing) {
10
11
  }
11
12
  export function isTirCustomType(thing) {
12
13
  return isObject(thing) && (thing instanceof TirAliasType
13
- || isTirStructType(thing));
14
+ || isTirStructType(thing)
15
+ || isTirEnumType(thing));
14
16
  }
15
17
  export function isTirNamedDestructableType(thing) {
16
18
  return isObject(thing) && (isTirStructType(thing)
19
+ || isTirEnumType(thing)
17
20
  || isTirNamedDestructableNativeType(thing));
18
21
  }
@@ -5,6 +5,8 @@ import { TirBytesT } from "../TirNativeType/native/bytes.js";
5
5
  import { TirDataT } from "../TirNativeType/native/data.js";
6
6
  import { TirFuncT } from "../TirNativeType/native/function.js";
7
7
  import { TirIntT } from "../TirNativeType/native/int.js";
8
+ import { TirArrayT } from "../TirNativeType/native/array.js";
9
+ import { TirValueT } from "../TirNativeType/native/value.js";
8
10
  import { TirLinearMapT } from "../TirNativeType/native/linearMap.js";
9
11
  import { TirLinearMapEntryT } from "../TirNativeType/native/linearMapEntry.js";
10
12
  import { TirListT } from "../TirNativeType/native/list.js";
@@ -13,6 +15,7 @@ import { TirSopOptT } from "../TirNativeType/native/Optional/sop.js";
13
15
  import { TirStringT } from "../TirNativeType/native/string.js";
14
16
  import { TirVoidT } from "../TirNativeType/native/void.js";
15
17
  import { isTirStructType, TirDataStructType, TirSoPStructType } from "../TirStructType.js";
18
+ import { TirEnumType } from "../TirEnumType.js";
16
19
  import { isTirNamedDestructableType } from "../TirType.js";
17
20
  import { TirTypeParam } from "../TirTypeParam.js";
18
21
  import { canCastToData } from "./canCastTo.js";
@@ -108,6 +111,10 @@ function uncheckedGetCanAssign(a, b, symbols) {
108
111
  if (b instanceof TirIntT) {
109
112
  if (a instanceof TirIntT)
110
113
  return CanAssign.Yes;
114
+ // enum runtime representation is a plain int, so an enum value can
115
+ // flow directly into any int operation.
116
+ if (a instanceof TirEnumType)
117
+ return CanAssign.Yes;
111
118
  if (a instanceof TirDataT)
112
119
  return CanAssign.RequiresExplicitCast;
113
120
  return CanAssign.No;
@@ -218,6 +225,19 @@ function uncheckedGetCanAssign(a, b, symbols) {
218
225
  }
219
226
  if (isTirStructType(a))
220
227
  return CanAssign.No;
228
+ if (b instanceof TirEnumType) {
229
+ if (a instanceof TirEnumType
230
+ && a.toTirTypeKey() === b.toTirTypeKey())
231
+ return CanAssign.Yes;
232
+ // `data` (e.g. datum/redeemer) can be decoded to an enum with an
233
+ // explicit cast; plain `int` cannot — there is no way to statically
234
+ // verify it is a valid tag.
235
+ if (a instanceof TirDataT)
236
+ return CanAssign.RequiresExplicitCast;
237
+ return CanAssign.No;
238
+ }
239
+ if (a instanceof TirEnumType)
240
+ return CanAssign.No;
221
241
  if (b instanceof TirFuncT) {
222
242
  if (!(a instanceof TirFuncT
223
243
  && a.argTypes.length === b.argTypes.length))
@@ -239,6 +259,14 @@ function uncheckedGetCanAssign(a, b, symbols) {
239
259
  return a instanceof TirBlsG2T ? CanAssign.Yes : CanAssign.No;
240
260
  if (b instanceof TirMlResultT)
241
261
  return a instanceof TirMlResultT ? CanAssign.Yes : CanAssign.No;
262
+ // Native Value / Array<T>
263
+ if (b instanceof TirValueT)
264
+ return a instanceof TirValueT ? CanAssign.Yes : CanAssign.No;
265
+ if (b instanceof TirArrayT) {
266
+ if (!(a instanceof TirArrayT))
267
+ return CanAssign.No;
268
+ return uncheckedGetCanAssign(a.typeArg, b.typeArg, symbols);
269
+ }
242
270
  const tsEnsureExhautstiveCheck = b;
243
271
  return CanAssign.No;
244
272
  }
@@ -12,6 +12,7 @@ import { TirSopOptT } from "../TirNativeType/native/Optional/sop.js";
12
12
  import { TirStringT } from "../TirNativeType/native/string.js";
13
13
  import { TirVoidT } from "../TirNativeType/native/void.js";
14
14
  import { isTirStructType, TirDataStructType } from "../TirStructType.js";
15
+ import { TirEnumType } from "../TirEnumType.js";
15
16
  import { CanAssign, getCanAssign } from "./canAssignTo.js";
16
17
  export function canCastTo(a, b) {
17
18
  if (!(a.isConcrete()
@@ -52,6 +53,8 @@ export function canCastTo(a, b) {
52
53
  if (b instanceof TirIntT) {
53
54
  if (a instanceof TirIntT)
54
55
  return true;
56
+ if (a instanceof TirEnumType)
57
+ return true; // enum is an int at runtime
55
58
  if (a instanceof TirDataT)
56
59
  return true;
57
60
  if (a instanceof TirBoolT)
@@ -113,6 +116,15 @@ export function canCastTo(a, b) {
113
116
  }
114
117
  return false;
115
118
  }
119
+ if (b instanceof TirEnumType) {
120
+ if (a instanceof TirEnumType)
121
+ return a.toTirTypeKey() === b.toTirTypeKey();
122
+ // only `data` (e.g. datum/redeemer decoding) can be cast to enum;
123
+ // plain `int` cannot — there is no way to verify it is a valid tag.
124
+ if (a instanceof TirDataT)
125
+ return true;
126
+ return false;
127
+ }
116
128
  if (b instanceof TirFuncT) {
117
129
  if (!(a instanceof TirFuncT))
118
130
  return false;
@@ -148,7 +160,8 @@ export function canCastToData(a) {
148
160
  || a instanceof TirStringT
149
161
  || a instanceof TirBoolT
150
162
  || a instanceof TirDataOptT
151
- || a instanceof TirDataStructType)
163
+ || a instanceof TirDataStructType
164
+ || a instanceof TirEnumType)
152
165
  return true;
153
166
  if (a instanceof TirLinearMapT) {
154
167
  const key = canCastToData(a.keyTypeArg);
@@ -1,3 +1,4 @@
1
+ import { TirEnumType } from "../TirEnumType.js";
1
2
  import { TirDataT } from "../TirNativeType/native/data.js";
2
3
  import { TirLinearMapT } from "../TirNativeType/native/linearMap.js";
3
4
  import { TirListT } from "../TirNativeType/native/list.js";
@@ -5,5 +6,5 @@ import { TirDataOptT } from "../TirNativeType/native/Optional/data.js";
5
6
  import { TirSopOptT } from "../TirNativeType/native/Optional/sop.js";
6
7
  import { TirStructType } from "../TirStructType.js";
7
8
  import { TirType } from "../TirType.js";
8
- export type DeconstructableTirType = TirStructType | TirSopOptT | TirDataOptT | TirListT | TirLinearMapT | TirDataT;
9
+ export type DeconstructableTirType = TirStructType | TirEnumType | TirSopOptT | TirDataOptT | TirListT | TirLinearMapT | TirDataT;
9
10
  export declare function getDeconstructableType(type: TirType): DeconstructableTirType | undefined;
@@ -1,4 +1,5 @@
1
1
  import { TirAliasType } from "../TirAliasType.js";
2
+ import { TirEnumType } from "../TirEnumType.js";
2
3
  import { TirDataT } from "../TirNativeType/native/data.js";
3
4
  import { TirLinearMapT } from "../TirNativeType/native/linearMap.js";
4
5
  import { TirListT } from "../TirNativeType/native/list.js";
@@ -9,6 +10,7 @@ export function getDeconstructableType(type) {
9
10
  while (type instanceof TirAliasType)
10
11
  type = type.aliased;
11
12
  if (isTirStructType(type)
13
+ || type instanceof TirEnumType
12
14
  || type instanceof TirSopOptT
13
15
  || type instanceof TirDataOptT
14
16
  || type instanceof TirListT
@@ -1,3 +1,4 @@
1
+ import { TirArrayT } from "../TirNativeType/native/array.js";
1
2
  import { TirFuncT } from "../TirNativeType/native/function.js";
2
3
  import { TirLinearMapT } from "../TirNativeType/native/linearMap.js";
3
4
  import { TirLinearMapEntryT } from "../TirNativeType/native/linearMapEntry.js";
@@ -34,6 +35,9 @@ export function inferTypeArgs(formal, actual, env) {
34
35
  if (formal instanceof TirListT && actual instanceof TirListT) {
35
36
  return inferTypeArgs(formal.typeArg, actual.typeArg, env);
36
37
  }
38
+ if (formal instanceof TirArrayT && actual instanceof TirArrayT) {
39
+ return inferTypeArgs(formal.typeArg, actual.typeArg, env);
40
+ }
37
41
  if (formal instanceof TirFuncT && actual instanceof TirFuncT) {
38
42
  if (formal.argTypes.length !== actual.argTypes.length)
39
43
  return false;
@@ -0,0 +1,10 @@
1
+ import { TirIntT } from "../TirNativeType/native/int.js";
2
+ import { TirType } from "../TirType.js";
3
+ /**
4
+ * If `t` is an enum type (or alias of one), returns the shared `int` type
5
+ * (since enums lower to plain ints at runtime). Otherwise returns `t`.
6
+ *
7
+ * Use this to relax type checks for binary operators that treat enums as
8
+ * integers (arithmetic, comparison, equality).
9
+ */
10
+ export declare function normalizeEnumToInt(t: TirType, int_t: TirIntT): TirType;