@harmoniclabs/pebble 0.1.0-dev4 → 0.1.0-dev6

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 (308) hide show
  1. package/dist/IR/IRHash.d.ts +3 -3
  2. package/dist/IR/IRHash.js +47 -52
  3. package/dist/IR/IRNodes/IRApp.d.ts +12 -8
  4. package/dist/IR/IRNodes/IRApp.js +34 -20
  5. package/dist/IR/IRNodes/IRCase.d.ts +11 -7
  6. package/dist/IR/IRNodes/IRCase.js +33 -32
  7. package/dist/IR/IRNodes/IRConst.d.ts +7 -5
  8. package/dist/IR/IRNodes/IRConst.js +23 -13
  9. package/dist/IR/IRNodes/IRConstr.d.ts +11 -7
  10. package/dist/IR/IRNodes/IRConstr.js +34 -37
  11. package/dist/IR/IRNodes/IRDelayed.d.ts +12 -9
  12. package/dist/IR/IRNodes/IRDelayed.js +22 -18
  13. package/dist/IR/IRNodes/IRError.d.ts +9 -6
  14. package/dist/IR/IRNodes/IRError.js +12 -5
  15. package/dist/IR/IRNodes/IRForced.d.ts +9 -6
  16. package/dist/IR/IRNodes/IRForced.js +22 -18
  17. package/dist/IR/IRNodes/IRFunc.d.ts +14 -10
  18. package/dist/IR/IRNodes/IRFunc.js +35 -38
  19. package/dist/IR/IRNodes/IRHoisted.d.ts +16 -9
  20. package/dist/IR/IRNodes/IRHoisted.js +36 -18
  21. package/dist/IR/IRNodes/IRLetted.d.ts +14 -34
  22. package/dist/IR/IRNodes/IRLetted.js +65 -297
  23. package/dist/IR/IRNodes/IRNative/IRNativeTag.d.ts +0 -2
  24. package/dist/IR/IRNodes/IRNative/IRNativeTag.js +0 -4
  25. package/dist/IR/IRNodes/IRNative/index.d.ts +9 -8
  26. package/dist/IR/IRNodes/IRNative/index.js +55 -18
  27. package/dist/IR/IRNodes/IRNative/isForcedNative.d.ts +2 -0
  28. package/dist/IR/IRNodes/IRNative/isForcedNative.js +1 -1
  29. package/dist/IR/IRNodes/IRRecursive.d.ts +18 -10
  30. package/dist/IR/IRNodes/IRRecursive.js +36 -44
  31. package/dist/IR/IRNodes/IRSelfCall.d.ts +10 -24
  32. package/dist/IR/IRNodes/IRSelfCall.js +25 -87
  33. package/dist/IR/IRNodes/IRVar.d.ts +11 -19
  34. package/dist/IR/IRNodes/IRVar.js +26 -54
  35. package/dist/IR/IRNodes/utils/dependsByDbns.d.ts +1 -1
  36. package/dist/IR/IRNodes/utils/dependsByDbns.js +5 -11
  37. package/dist/IR/IRNodes/utils/hashVarSym.d.ts +1 -0
  38. package/dist/IR/IRNodes/utils/hashVarSym.js +37 -0
  39. package/dist/IR/IRNodes/utils/makeArrayLikeProxy.d.ts +2 -1
  40. package/dist/IR/IRTerm.d.ts +7 -0
  41. package/dist/IR/toUPLC/_internal/_modifyChildFromTo.js +3 -0
  42. package/dist/IR/toUPLC/_internal/findAll.js +2 -73
  43. package/dist/IR/toUPLC/_internal/getDebruijnInTerm.js +7 -34
  44. package/dist/IR/toUPLC/_internal/iterTree.js +3 -17
  45. package/dist/IR/toUPLC/common_hoisted.d.ts +2 -0
  46. package/dist/IR/toUPLC/common_hoisted.js +4 -0
  47. package/dist/IR/toUPLC/compileIRToUPLC.d.ts +1 -1
  48. package/dist/IR/toUPLC/compileIRToUPLC.js +91 -14
  49. package/dist/IR/toUPLC/ctx/ToUplcCtx.d.ts +16 -0
  50. package/dist/IR/toUPLC/ctx/ToUplcCtx.js +75 -0
  51. package/dist/IR/toUPLC/subRoutines/_comptimeDropN.d.ts +8 -0
  52. package/dist/IR/toUPLC/subRoutines/_comptimeDropN.js +95 -0
  53. package/dist/IR/toUPLC/subRoutines/handleHoistedAndReturnRoot/index.js +7 -54
  54. package/dist/IR/toUPLC/subRoutines/handleLetted/groupByScope.d.ts +6 -13
  55. package/dist/IR/toUPLC/subRoutines/handleLetted/groupByScope.js +67 -71
  56. package/dist/IR/toUPLC/subRoutines/handleLetted/index.js +44 -170
  57. package/dist/IR/toUPLC/subRoutines/handleRecursiveTerms.js +7 -3
  58. package/dist/IR/toUPLC/subRoutines/hoistForcedNatives.js +3 -2
  59. package/dist/IR/toUPLC/subRoutines/inlineSingleUseAndReturnRoot/InlineSingleUseCtx.d.ts +22 -0
  60. package/dist/IR/toUPLC/subRoutines/inlineSingleUseAndReturnRoot/InlineSingleUseCtx.js +59 -0
  61. package/dist/IR/toUPLC/subRoutines/inlineSingleUseAndReturnRoot/inlineSingleUseAndReturnRoot.d.ts +0 -0
  62. package/dist/IR/toUPLC/subRoutines/inlineSingleUseAndReturnRoot/inlineSingleUseAndReturnRoot.js +217 -0
  63. package/dist/IR/toUPLC/subRoutines/markRecursiveHoistsAsForced.js +12 -49
  64. package/dist/IR/toUPLC/subRoutines/performUplcOptimizationsAndReturnRoot/expandFuncsAndReturnRoot.d.ts +1 -1
  65. package/dist/IR/toUPLC/subRoutines/performUplcOptimizationsAndReturnRoot/expandFuncsAndReturnRoot.js +11 -14
  66. package/dist/IR/toUPLC/subRoutines/performUplcOptimizationsAndReturnRoot/{index.d.ts → performUplcOptimizationsAndReturnRoot.d.ts} +0 -2
  67. package/dist/IR/toUPLC/subRoutines/performUplcOptimizationsAndReturnRoot/performUplcOptimizationsAndReturnRoot.js +151 -0
  68. package/dist/IR/toUPLC/subRoutines/removeUnusuedVarsAndReturnRoot/RemoveUnusedVarsCtx.d.ts +11 -0
  69. package/dist/IR/toUPLC/subRoutines/removeUnusuedVarsAndReturnRoot/RemoveUnusedVarsCtx.js +46 -0
  70. package/dist/IR/toUPLC/subRoutines/removeUnusuedVarsAndReturnRoot/removeUnusuedVarsAndReturnRoot.d.ts +2 -0
  71. package/dist/IR/toUPLC/subRoutines/removeUnusuedVarsAndReturnRoot/removeUnusuedVarsAndReturnRoot.js +80 -0
  72. package/dist/IR/toUPLC/subRoutines/replaceHoistedWithLetted.js +18 -11
  73. package/dist/IR/toUPLC/subRoutines/replaceNatives/nativeToIR.d.ts +43 -0
  74. package/dist/IR/toUPLC/subRoutines/replaceNatives/nativeToIR.js +255 -478
  75. package/dist/IR/toUPLC/subRoutines/rewriteNativesAppliedToConstantsAndReturnRoot.d.ts +2 -0
  76. package/dist/IR/toUPLC/subRoutines/rewriteNativesAppliedToConstantsAndReturnRoot.js +85 -0
  77. package/dist/IR/toUPLC/subRoutines/sanifyTree.js +1 -0
  78. package/dist/IR/toUPLC/utils/getApplicationTerms.d.ts +6 -0
  79. package/dist/IR/toUPLC/utils/getApplicationTerms.js +37 -0
  80. package/dist/IR/tree_utils/_ir_lazyChooseList.d.ts +2 -2
  81. package/dist/IR/tree_utils/_ir_lazyChooseList.js +4 -2
  82. package/dist/IR/tree_utils/_ir_lazyIfThenElse.d.ts +1 -1
  83. package/dist/IR/tree_utils/_ir_lazyIfThenElse.js +4 -2
  84. package/dist/IR/tree_utils/_ir_let.d.ts +2 -1
  85. package/dist/IR/tree_utils/_ir_let.js +6 -2
  86. package/dist/IR/utils/isClosedIRTerm.d.ts +1 -0
  87. package/dist/IR/utils/isClosedIRTerm.js +39 -25
  88. package/dist/IR/utils/positiveIntAsBytes.d.ts +1 -0
  89. package/dist/IR/utils/positiveIntAsBytes.js +4 -0
  90. package/dist/IR/utils/showIR.d.ts +10 -21
  91. package/dist/IR/utils/showIR.js +66 -64
  92. package/dist/ast/nodes/common/Identifier.d.ts +1 -0
  93. package/dist/ast/nodes/common/Identifier.js +3 -0
  94. package/dist/ast/nodes/expr/CaseExpr.d.ts +5 -5
  95. package/dist/ast/nodes/expr/ElemAccessExpr.d.ts +2 -2
  96. package/dist/ast/nodes/expr/ParentesizedExpr.d.ts +1 -1
  97. package/dist/ast/nodes/expr/PebbleExpr.d.ts +2 -1
  98. package/dist/ast/nodes/expr/PropAccessExpr.d.ts +3 -3
  99. package/dist/ast/nodes/expr/TernaryExpr.d.ts +3 -3
  100. package/dist/ast/nodes/expr/TypeConversionExpr.d.ts +2 -1
  101. package/dist/ast/nodes/expr/TypeConversionExpr.js +2 -0
  102. package/dist/ast/nodes/expr/binary/BinaryExpr.d.ts +42 -42
  103. package/dist/ast/nodes/expr/functions/CallExpr.d.ts +1 -1
  104. package/dist/ast/nodes/expr/functions/FuncExpr.d.ts +2 -1
  105. package/dist/ast/nodes/expr/functions/FuncExpr.js +6 -0
  106. package/dist/ast/nodes/expr/litteral/LitContextExpr.d.ts +6 -0
  107. package/dist/ast/nodes/expr/litteral/LitContextExpr.js +6 -0
  108. package/dist/ast/nodes/expr/litteral/LitFailExpr.d.ts +6 -0
  109. package/dist/ast/nodes/expr/litteral/LitFailExpr.js +6 -0
  110. package/dist/ast/nodes/expr/litteral/LitteralExpr.d.ts +3 -1
  111. package/dist/ast/nodes/expr/litteral/LitteralExpr.js +5 -1
  112. package/dist/ast/nodes/statements/AssertStmt.d.ts +2 -2
  113. package/dist/ast/nodes/statements/AssignmentStmt.d.ts +1 -1
  114. package/dist/ast/nodes/statements/BlockStmt.d.ts +1 -1
  115. package/dist/ast/nodes/statements/FailStmt.d.ts +1 -1
  116. package/dist/ast/nodes/statements/ForOfStmt.d.ts +4 -2
  117. package/dist/ast/nodes/statements/ForOfStmt.js +6 -0
  118. package/dist/ast/nodes/statements/ForStmt.d.ts +6 -4
  119. package/dist/ast/nodes/statements/ForStmt.js +6 -0
  120. package/dist/ast/nodes/statements/IfStmt.d.ts +8 -5
  121. package/dist/ast/nodes/statements/IfStmt.js +13 -0
  122. package/dist/ast/nodes/statements/MatchStmt.d.ts +13 -4
  123. package/dist/ast/nodes/statements/MatchStmt.js +22 -1
  124. package/dist/ast/nodes/statements/PebbleStmt.d.ts +2 -1
  125. package/dist/ast/nodes/statements/PebbleStmt.js +3 -1
  126. package/dist/ast/nodes/statements/ReturnStmt.d.ts +1 -1
  127. package/dist/ast/nodes/statements/WhileStmt.d.ts +4 -2
  128. package/dist/ast/nodes/statements/WhileStmt.js +6 -0
  129. package/dist/ast/nodes/statements/declarations/ContractDecl.d.ts +17 -0
  130. package/dist/ast/nodes/statements/declarations/ContractDecl.js +22 -0
  131. package/dist/ast/nodes/statements/declarations/VarDecl/ArrayLikeDeconstr.d.ts +1 -1
  132. package/dist/ast/nodes/statements/declarations/VarDecl/HasInit.d.ts +1 -1
  133. package/dist/ast/nodes/statements/declarations/VarDecl/NamedDeconstructVarDecl.d.ts +1 -1
  134. package/dist/ast/nodes/statements/declarations/VarDecl/SimpleVarDecl.d.ts +2 -1
  135. package/dist/ast/nodes/statements/declarations/VarDecl/SimpleVarDecl.js +6 -0
  136. package/dist/ast/nodes/statements/declarations/VarDecl/SingleDeconstructVarDecl.d.ts +1 -1
  137. package/dist/compiler/AstCompiler/AstCompiler.d.ts +6 -2
  138. package/dist/compiler/AstCompiler/AstCompiler.js +91 -24
  139. package/dist/compiler/AstCompiler/internal/_deriveContractBody/_deriveContractBody.d.ts +10 -0
  140. package/dist/compiler/AstCompiler/internal/_deriveContractBody/_deriveContractBody.js +777 -0
  141. package/dist/compiler/AstCompiler/internal/exprs/_compileBinaryExpr.js +1 -1
  142. package/dist/compiler/AstCompiler/internal/exprs/_compileExpr.js +1 -1
  143. package/dist/compiler/AstCompiler/internal/exprs/_compileLitteralExpr.js +8 -2
  144. package/dist/compiler/AstCompiler/internal/exprs/_compilePropAccessExpr.js +1 -1
  145. package/dist/compiler/AstCompiler/internal/exprs/_compileTypeConversionExpr.js +3 -1
  146. package/dist/compiler/AstCompiler/internal/exprs/_compileUnaryPrefixExpr.js +1 -1
  147. package/dist/compiler/AstCompiler/internal/statements/_compileAssignmentStmt.js +2 -2
  148. package/dist/compiler/AstCompiler/internal/statements/_compileMatchStmt.d.ts +2 -2
  149. package/dist/compiler/AstCompiler/internal/statements/_compileMatchStmt.js +48 -24
  150. package/dist/compiler/AstCompiler/internal/statements/_compileStatement.js +4 -1
  151. package/dist/compiler/AstCompiler/internal/statements/_compileVarStmt.js +2 -2
  152. package/dist/compiler/AstCompiler/internal/types/_compileDataEncodedConcreteType.js +1 -1
  153. package/dist/compiler/AstCompiler/internal/types/_compileSopEncodedConcreteType.js +4 -1
  154. package/dist/compiler/AstCompiler/scope/AstScope.js +4 -3
  155. package/dist/compiler/AstCompiler/utils/getPropAccessReturnType.js +6 -4
  156. package/dist/compiler/Compiler.js +3 -1
  157. package/dist/compiler/TirCompiler/compileTirProgram.js +3 -1
  158. package/dist/compiler/TirCompiler/expressify/ExpressifyCtx.d.ts +13 -3
  159. package/dist/compiler/TirCompiler/expressify/ExpressifyCtx.js +41 -37
  160. package/dist/compiler/TirCompiler/expressify/determineReassignedVariablesAndReturn.js +11 -2
  161. package/dist/compiler/TirCompiler/expressify/expressify.d.ts +2 -1
  162. package/dist/compiler/TirCompiler/expressify/expressify.js +82 -36
  163. package/dist/compiler/TirCompiler/expressify/expressifyForStmt.d.ts +2 -1
  164. package/dist/compiler/TirCompiler/expressify/expressifyForStmt.js +57 -8
  165. package/dist/compiler/TirCompiler/expressify/expressifyIfBranch.js +1 -1
  166. package/dist/compiler/TirCompiler/expressify/expressifyVarAssignmentStmt.js +1 -1
  167. package/dist/compiler/TirCompiler/expressify/expressifyVarDecl.js +1 -0
  168. package/dist/compiler/TirCompiler/expressify/expressifyVars.js +131 -9
  169. package/dist/compiler/TirCompiler/internal/_compileHoistedDeps.js +3 -1
  170. package/dist/compiler/io/IOutputStream.d.ts +1 -0
  171. package/dist/compiler/io/IOutputStream.js +18 -3
  172. package/dist/compiler/path/getAbsolutePath.d.ts +1 -0
  173. package/dist/compiler/path/getAbsolutePath.js +8 -5
  174. package/dist/compiler/tir/expressions/ITirExpr.d.ts +2 -0
  175. package/dist/compiler/tir/expressions/TirAssertAndContinueExpr.d.ts +4 -2
  176. package/dist/compiler/tir/expressions/TirAssertAndContinueExpr.js +16 -2
  177. package/dist/compiler/tir/expressions/TirCallExpr.d.ts +3 -1
  178. package/dist/compiler/tir/expressions/TirCallExpr.js +16 -1
  179. package/dist/compiler/tir/expressions/TirCaseExpr.d.ts +6 -1
  180. package/dist/compiler/tir/expressions/TirCaseExpr.js +103 -60
  181. package/dist/compiler/tir/expressions/TirElemAccessExpr.d.ts +3 -1
  182. package/dist/compiler/tir/expressions/TirElemAccessExpr.js +9 -1
  183. package/dist/compiler/tir/expressions/TirFailExpr.d.ts +3 -1
  184. package/dist/compiler/tir/expressions/TirFailExpr.js +9 -1
  185. package/dist/compiler/tir/expressions/TirFromDataExpr.d.ts +3 -2
  186. package/dist/compiler/tir/expressions/TirFromDataExpr.js +85 -56
  187. package/dist/compiler/tir/expressions/TirFuncExpr.d.ts +4 -10
  188. package/dist/compiler/tir/expressions/TirFuncExpr.js +30 -13
  189. package/dist/compiler/tir/expressions/TirHoistedExpr.d.ts +3 -1
  190. package/dist/compiler/tir/expressions/TirHoistedExpr.js +8 -0
  191. package/dist/compiler/tir/expressions/TirInlineClosedIR.d.ts +4 -1
  192. package/dist/compiler/tir/expressions/TirInlineClosedIR.js +8 -0
  193. package/dist/compiler/tir/expressions/TirLettedExpr.d.ts +5 -2
  194. package/dist/compiler/tir/expressions/TirLettedExpr.js +19 -4
  195. package/dist/compiler/tir/expressions/TirNativeFunc.d.ts +8 -5
  196. package/dist/compiler/tir/expressions/TirNativeFunc.js +50 -22
  197. package/dist/compiler/tir/expressions/TirParentesizedExpr.d.ts +3 -1
  198. package/dist/compiler/tir/expressions/TirParentesizedExpr.js +6 -0
  199. package/dist/compiler/tir/expressions/TirPropAccessExpr.d.ts +3 -1
  200. package/dist/compiler/tir/expressions/TirPropAccessExpr.js +9 -0
  201. package/dist/compiler/tir/expressions/TirTernaryExpr.d.ts +3 -1
  202. package/dist/compiler/tir/expressions/TirTernaryExpr.js +16 -5
  203. package/dist/compiler/tir/expressions/TirToDataExpr.d.ts +3 -1
  204. package/dist/compiler/tir/expressions/TirToDataExpr.js +56 -25
  205. package/dist/compiler/tir/expressions/TirTraceIfFalseExpr.d.ts +3 -1
  206. package/dist/compiler/tir/expressions/TirTraceIfFalseExpr.js +21 -4
  207. package/dist/compiler/tir/expressions/TirTypeConversionExpr.d.ts +3 -1
  208. package/dist/compiler/tir/expressions/TirTypeConversionExpr.js +9 -1
  209. package/dist/compiler/tir/expressions/TirVariableAccessExpr.d.ts +7 -3
  210. package/dist/compiler/tir/expressions/TirVariableAccessExpr.js +17 -7
  211. package/dist/compiler/tir/expressions/ToIRTermCtx.d.ts +16 -20
  212. package/dist/compiler/tir/expressions/ToIRTermCtx.js +67 -50
  213. package/dist/compiler/tir/expressions/binary/TirBinaryExpr.d.ts +40 -0
  214. package/dist/compiler/tir/expressions/binary/TirBinaryExpr.js +174 -3
  215. package/dist/compiler/tir/expressions/litteral/TirLitArrExpr.d.ts +4 -2
  216. package/dist/compiler/tir/expressions/litteral/TirLitArrExpr.js +8 -2
  217. package/dist/compiler/tir/expressions/litteral/TirLitFailExpr.d.ts +17 -0
  218. package/dist/compiler/tir/expressions/litteral/TirLitFailExpr.js +21 -0
  219. package/dist/compiler/tir/expressions/litteral/TirLitFalseExpr.d.ts +4 -1
  220. package/dist/compiler/tir/expressions/litteral/TirLitFalseExpr.js +4 -0
  221. package/dist/compiler/tir/expressions/litteral/TirLitHexBytesExpr.d.ts +4 -1
  222. package/dist/compiler/tir/expressions/litteral/TirLitHexBytesExpr.js +5 -0
  223. package/dist/compiler/tir/expressions/litteral/TirLitIntExpr.d.ts +4 -1
  224. package/dist/compiler/tir/expressions/litteral/TirLitIntExpr.js +4 -0
  225. package/dist/compiler/tir/expressions/litteral/TirLitNamedObjExpr.d.ts +3 -1
  226. package/dist/compiler/tir/expressions/litteral/TirLitNamedObjExpr.js +5 -0
  227. package/dist/compiler/tir/expressions/litteral/TirLitObjExpr.d.ts +3 -1
  228. package/dist/compiler/tir/expressions/litteral/TirLitObjExpr.js +5 -0
  229. package/dist/compiler/tir/expressions/litteral/TirLitStrExpr.d.ts +4 -1
  230. package/dist/compiler/tir/expressions/litteral/TirLitStrExpr.js +4 -0
  231. package/dist/compiler/tir/expressions/litteral/TirLitThisExpr.d.ts +4 -1
  232. package/dist/compiler/tir/expressions/litteral/TirLitThisExpr.js +7 -3
  233. package/dist/compiler/tir/expressions/litteral/TirLitTrueExpr.d.ts +4 -1
  234. package/dist/compiler/tir/expressions/litteral/TirLitTrueExpr.js +4 -0
  235. package/dist/compiler/tir/expressions/litteral/TirLitUndefExpr.d.ts +4 -1
  236. package/dist/compiler/tir/expressions/litteral/TirLitUndefExpr.js +4 -0
  237. package/dist/compiler/tir/expressions/litteral/TirLitVoidExpr.d.ts +4 -1
  238. package/dist/compiler/tir/expressions/litteral/TirLitVoidExpr.js +4 -0
  239. package/dist/compiler/tir/expressions/litteral/TirLitteralExpr.d.ts +2 -1
  240. package/dist/compiler/tir/expressions/litteral/TirLitteralExpr.js +3 -1
  241. package/dist/compiler/tir/expressions/unary/TirUnaryExclamation.d.ts +4 -2
  242. package/dist/compiler/tir/expressions/unary/TirUnaryExclamation.js +8 -3
  243. package/dist/compiler/tir/expressions/unary/TirUnaryMinus.d.ts +4 -2
  244. package/dist/compiler/tir/expressions/unary/TirUnaryMinus.js +8 -2
  245. package/dist/compiler/tir/expressions/unary/TirUnaryPlus.d.ts +3 -1
  246. package/dist/compiler/tir/expressions/unary/TirUnaryPlus.js +6 -0
  247. package/dist/compiler/tir/expressions/unary/TirUnaryTilde.d.ts +4 -2
  248. package/dist/compiler/tir/expressions/unary/TirUnaryTilde.js +8 -2
  249. package/dist/compiler/tir/program/TypedProgram.d.ts +1 -0
  250. package/dist/compiler/tir/program/TypedProgram.js +9 -0
  251. package/dist/compiler/tir/program/stdScope/stdScope.js +126 -126
  252. package/dist/compiler/tir/statements/TirAssertStmt.d.ts +2 -0
  253. package/dist/compiler/tir/statements/TirAssertStmt.js +10 -0
  254. package/dist/compiler/tir/statements/TirAssignmentStmt.d.ts +2 -0
  255. package/dist/compiler/tir/statements/TirAssignmentStmt.js +8 -0
  256. package/dist/compiler/tir/statements/TirBlockStmt.d.ts +2 -0
  257. package/dist/compiler/tir/statements/TirBlockStmt.js +16 -0
  258. package/dist/compiler/tir/statements/TirBreakStmt.d.ts +2 -0
  259. package/dist/compiler/tir/statements/TirBreakStmt.js +8 -0
  260. package/dist/compiler/tir/statements/TirContinueStmt.d.ts +2 -0
  261. package/dist/compiler/tir/statements/TirContinueStmt.js +8 -0
  262. package/dist/compiler/tir/statements/TirFailStmt.d.ts +2 -0
  263. package/dist/compiler/tir/statements/TirFailStmt.js +8 -0
  264. package/dist/compiler/tir/statements/TirForOfStmt.d.ts +2 -0
  265. package/dist/compiler/tir/statements/TirForOfStmt.js +14 -0
  266. package/dist/compiler/tir/statements/TirForStmt.d.ts +2 -0
  267. package/dist/compiler/tir/statements/TirForStmt.js +21 -0
  268. package/dist/compiler/tir/statements/TirIfStmt.d.ts +3 -0
  269. package/dist/compiler/tir/statements/TirIfStmt.js +19 -0
  270. package/dist/compiler/tir/statements/TirMatchStmt.d.ts +2 -0
  271. package/dist/compiler/tir/statements/TirMatchStmt.js +30 -0
  272. package/dist/compiler/tir/statements/TirReturnStmt.d.ts +2 -0
  273. package/dist/compiler/tir/statements/TirReturnStmt.js +8 -0
  274. package/dist/compiler/tir/statements/TirStmt.d.ts +2 -0
  275. package/dist/compiler/tir/statements/TirVarDecl/TirArrayLikeDeconstr.d.ts +2 -0
  276. package/dist/compiler/tir/statements/TirVarDecl/TirArrayLikeDeconstr.js +30 -0
  277. package/dist/compiler/tir/statements/TirVarDecl/TirNamedDeconstructVarDecl.d.ts +3 -1
  278. package/dist/compiler/tir/statements/TirVarDecl/TirNamedDeconstructVarDecl.js +28 -0
  279. package/dist/compiler/tir/statements/TirVarDecl/TirSimpleVarDecl.d.ts +3 -0
  280. package/dist/compiler/tir/statements/TirVarDecl/TirSimpleVarDecl.js +13 -0
  281. package/dist/compiler/tir/statements/TirVarDecl/TirSingleDeconstructVarDecl.d.ts +3 -1
  282. package/dist/compiler/tir/statements/TirVarDecl/TirSingleDeconstructVarDecl.js +28 -0
  283. package/dist/compiler/tir/statements/TirWhileStmt.d.ts +2 -0
  284. package/dist/compiler/tir/statements/TirWhileStmt.js +14 -0
  285. package/dist/compiler/tir/types/utils/canAssignTo.js +1 -1
  286. package/dist/diagnostics/DiagnosticEmitter.js +1 -1
  287. package/dist/diagnostics/diagnosticMessages.generated.d.ts +10 -1
  288. package/dist/diagnostics/diagnosticMessages.generated.js +20 -2
  289. package/dist/parser/Parser.d.ts +8 -3
  290. package/dist/parser/Parser.js +162 -30
  291. package/dist/tokenizer/Token.d.ts +81 -72
  292. package/dist/tokenizer/Token.js +82 -72
  293. package/dist/tokenizer/utils/tokenFromKeyword.js +32 -10
  294. package/dist/tokenizer/utils/tokenIsAlsoIdentifier.js +10 -0
  295. package/dist/utils/BitUtils/index.js +1 -1
  296. package/dist/utils/UPLCFlatUtils/index.js +1 -1
  297. package/package.json +2 -1
  298. package/dist/IR/IRNodes/utils/isClosedAtDbn.d.ts +0 -2
  299. package/dist/IR/IRNodes/utils/isClosedAtDbn.js +0 -22
  300. package/dist/IR/toUPLC/_internal/_irToUplc.d.ts +0 -9
  301. package/dist/IR/toUPLC/_internal/_irToUplc.js +0 -156
  302. package/dist/IR/toUPLC/subRoutines/handleLetted/incrementUnboundDbns.d.ts +0 -9
  303. package/dist/IR/toUPLC/subRoutines/handleLetted/incrementUnboundDbns.js +0 -73
  304. package/dist/IR/toUPLC/subRoutines/inlineSingleUseApplications.d.ts +0 -1
  305. package/dist/IR/toUPLC/subRoutines/inlineSingleUseApplications.js +0 -12
  306. package/dist/IR/toUPLC/subRoutines/performUplcOptimizationsAndReturnRoot/index.js +0 -227
  307. package/dist/IR/tree_utils/_ir_apps.d.ts +0 -3
  308. package/dist/IR/tree_utils/_ir_apps.js +0 -8
@@ -1,49 +1,43 @@
1
1
  import { BasePlutsError } from "../../utils/BasePlutsError.js";
2
2
  import { concatUint8Arr } from "../utils/concatUint8Arr.js";
3
- import { positiveIntAsBytes } from "../utils/positiveIntAsBytes.js";
4
3
  import { isIRParentTerm } from "../utils/isIRParentTerm.js";
5
4
  import { hashIrData, isIRHash } from "../IRHash.js";
6
5
  import { IRNodeKind } from "../IRNodeKind.js";
6
+ import { hashVarSym } from "./utils/hashVarSym.js";
7
+ import { UPLCVar } from "@harmoniclabs/uplc";
7
8
  export class IRVar {
9
+ _creationStack;
10
+ meta;
11
+ name;
12
+ constructor(name, _unsafeHash) {
13
+ if (!(typeof name === "symbol"
14
+ && typeof name.description === "string"
15
+ && name.description.length > 0))
16
+ throw new BasePlutsError("invalid name for IRVar");
17
+ this.name = name;
18
+ this._parent = undefined;
19
+ this.meta = {};
20
+ this._hash = isIRHash(_unsafeHash) ? _unsafeHash : undefined;
21
+ this._creationStack = (new Error()).stack ?? "unknown";
22
+ }
23
+ toUPLC(ctx) {
24
+ return new UPLCVar(ctx.getVarAccessDbn(this.name));
25
+ }
8
26
  _hash;
9
27
  get hash() {
10
- if (!isIRHash(this._hash)) {
11
- this._hash = getVarHashAtDbn(this.dbn);
12
- }
28
+ if (isIRHash(this._hash))
29
+ return this._hash;
30
+ this._hash = hashIrData(concatUint8Arr(IRVar.tag, hashVarSym(this.name)));
13
31
  return this._hash;
14
32
  }
15
- /**
16
- * called inside the dbn setter
17
- */
33
+ isHashPresent() { return isIRHash(this._hash); }
18
34
  markHashAsInvalid() {
19
35
  this._hash = undefined;
20
36
  this.parent?.markHashAsInvalid();
21
37
  }
22
- isHashPresent() { return true; }
23
- _dbn;
24
- /**
25
- * the IR DeBruijn index is not necessarly the same of the UPLC
26
- * ( more ofthen than not it won't be the same )
27
- *
28
- * this is because in the IR things like `IRLetted` and `IRHoisted`
29
- * are skipping some DeBruijin levels that are instead present
30
- * in the final UPLC
31
- **/
32
- get dbn() { return this._dbn; }
33
- set dbn(newDbn) {
34
- if (!(Number.isSafeInteger(newDbn) && newDbn >= 0)) {
35
- // console.log( e.stack );
36
- throw new BasePlutsError("invalid index for an `IRVar` instance; new DeBruijn was: " + newDbn);
37
- }
38
- if (newDbn === this._dbn)
39
- return; // everything ok
40
- this.markHashAsInvalid();
41
- this._dbn = newDbn;
42
- }
43
38
  static get kind() { return IRNodeKind.Var; }
44
39
  get kind() { return IRVar.kind; }
45
40
  static get tag() { return new Uint8Array([IRVar.kind]); }
46
- meta;
47
41
  _parent;
48
42
  get parent() { return this._parent; }
49
43
  set parent(newParent) {
@@ -57,37 +51,15 @@ export class IRVar {
57
51
  // change parent
58
52
  this._parent = newParent;
59
53
  }
60
- constructor(DeBruijn) {
61
- Object.defineProperty(this, "meta", {
62
- value: {},
63
- writable: false,
64
- enumerable: true,
65
- configurable: false
66
- });
67
- this._hash = undefined;
68
- DeBruijn = typeof DeBruijn === "number" ? DeBruijn : Number(DeBruijn);
69
- this._dbn = DeBruijn;
70
- // call setter to check for validity
71
- this.dbn = DeBruijn;
72
- this._parent = undefined;
73
- }
54
+ children() { return []; }
74
55
  clone() {
75
- return new IRVar(this.dbn);
56
+ return new IRVar(this.name, this._hash);
76
57
  }
77
58
  toJSON() { return this.toJson(); }
78
59
  toJson() {
79
60
  return {
80
61
  type: "IRVar",
81
- dbn: this.dbn
62
+ name: this.name
82
63
  };
83
64
  }
84
65
  }
85
- const _bdnVarHashCache = new Map();
86
- function getVarHashAtDbn(dbn) {
87
- const cached = _bdnVarHashCache.get(dbn)?.deref();
88
- if (isIRHash(cached))
89
- return cached;
90
- const hash = hashIrData(concatUint8Arr(IRVar.tag, positiveIntAsBytes(dbn)));
91
- _bdnVarHashCache.set(dbn, new WeakRef(hash));
92
- return hash;
93
- }
@@ -2,4 +2,4 @@ import { IRTerm } from "../../IRTerm.js";
2
2
  /**
3
3
  * returns true if the term depends on *ANY* of the deBrujin indices specified
4
4
  */
5
- export declare function dependsByDbns(term: IRTerm, depsDbns: readonly number[]): boolean;
5
+ export declare function dependsByVars(term: IRTerm, depsVars: readonly symbol[]): boolean;
@@ -4,20 +4,14 @@ import { IRVar } from "../IRVar.js";
4
4
  /**
5
5
  * returns true if the term depends on *ANY* of the deBrujin indices specified
6
6
  */
7
- export function dependsByDbns(term, depsDbns) {
8
- if (depsDbns.length === 0)
7
+ export function dependsByVars(term, depsVars) {
8
+ if (depsVars.length === 0)
9
9
  return false;
10
10
  let doesDepend = false;
11
11
  iterTree(term, (node, diff) => {
12
- if (node instanceof IRVar ||
13
- node instanceof IRSelfCall) {
14
- doesDepend = doesDepend || depsDbns.some(dbn => {
15
- const realDbn = dbn + diff;
16
- // realDbn === 0 implies no variables are in scope
17
- // so if node.dbn === 0 and realDbn === 1 then the
18
- // variable is pointing to that definition
19
- return node.dbn === realDbn;
20
- });
12
+ if (node instanceof IRVar
13
+ || node instanceof IRSelfCall) {
14
+ doesDepend = doesDepend || depsVars.some(sym => node.name === sym);
21
15
  }
22
16
  },
23
17
  // shouldSkipNode
@@ -0,0 +1 @@
1
+ export declare function hashVarSym(s: symbol): Uint8Array;
@@ -0,0 +1,37 @@
1
+ // HASH GENERATOR
2
+ import { fromHex } from "@harmoniclabs/uint8array-utils";
3
+ const MAX_SAFE_INTEGER = Number(globalThis.Number?.MAX_SAFE_INTEGER ?? ((2 ** 53) - 1));
4
+ const MIN_SAFE_INTEGER = Number(globalThis.Number?.MIN_SAFE_INTEGER ?? -MAX_SAFE_INTEGER);
5
+ /// @ts-ignore Type 'symbol' does not satisfy the constraint 'object'.
6
+ const _sym_to_hash = new WeakMap();
7
+ // we cannot store the symbol itself as it would prevent garbage collection
8
+ /// @ts-ignore Type 'symbol' does not satisfy the constraint 'object'.
9
+ const _hash_to_sym = new Map();
10
+ let _next_hash = MIN_SAFE_INTEGER;
11
+ const unusedHashes = [];
12
+ function _collectUnusedHashes() {
13
+ for (const [h, s] of _hash_to_sym) {
14
+ const newSymbol = s.deref();
15
+ if (!newSymbol
16
+ || !_sym_to_hash.has(newSymbol)) {
17
+ _hash_to_sym.delete(h);
18
+ unusedHashes.push(h);
19
+ }
20
+ }
21
+ }
22
+ export function hashVarSym(s) {
23
+ const limitReached = _next_hash >= MAX_SAFE_INTEGER;
24
+ if (_next_hash % 0xffff === 0
25
+ || limitReached)
26
+ _collectUnusedHashes();
27
+ if (limitReached
28
+ && unusedHashes.length <= 0)
29
+ throw new Error("ran out of IR hashes");
30
+ const result_hash = unusedHashes.shift() ?? _next_hash++;
31
+ _sym_to_hash.set(s, result_hash);
32
+ /// @ts-ignore Argument of type 'WeakRef<object>' is not assignable to parameter of type 'WeakRef<symbol>'
33
+ _hash_to_sym.set(result_hash, new WeakRef(s));
34
+ return fromHex((BigInt(result_hash) + BigInt(MAX_SAFE_INTEGER))
35
+ .toString(16)
36
+ .padStart(16, "0"));
37
+ }
@@ -1 +1,2 @@
1
- export declare function makeArrayLikeProxy<T>(arr: ArrayLike<T>, isValidElem: (elem: T) => boolean, initModifyElem: (elem: T) => T, modifyElem: (elem: T, oldElem: T) => T): ArrayLike<T>;
1
+ import { MutArrayLike } from "../../utils/MutArrayLike.js";
2
+ export declare function makeArrayLikeProxy<T>(arr: ArrayLike<T>, isValidElem: (elem: T) => boolean, initModifyElem: (elem: T) => T, modifyElem: (elem: T, oldElem: T) => T): MutArrayLike<T>;
@@ -12,4 +12,11 @@ import { IRConstr } from "./IRNodes/IRConstr.js";
12
12
  import { IRCase } from "./IRNodes/IRCase.js";
13
13
  import { IRRecursive } from "./IRNodes/IRRecursive.js";
14
14
  import { IRSelfCall } from "./IRNodes/IRSelfCall.js";
15
+ import { IHash } from "./interfaces/index.js";
16
+ import { UPLCTerm } from "@harmoniclabs/uplc";
17
+ import { ToUplcCtx } from "./toUPLC/ctx/ToUplcCtx.js";
15
18
  export type IRTerm = IRVar | IRFunc | IRApp | IRConst | IRNative | IRLetted | IRHoisted | IRError | IRForced | IRDelayed | IRConstr | IRCase | IRRecursive | IRSelfCall;
19
+ export interface IIRTerm extends IHash {
20
+ children(): IRTerm[];
21
+ toUPLC(ctx: ToUplcCtx): UPLCTerm;
22
+ }
@@ -138,4 +138,7 @@ export function _modifyChildFromTo(parent, currentChild, newChild) {
138
138
  parent.value = newChild;
139
139
  return;
140
140
  }
141
+ const tsEnsureExhaustiveCheck = parent;
142
+ throw new Error("unknown parent type in '_modifyChildFromTo': " +
143
+ parent.constructor?.name);
141
144
  }
@@ -1,11 +1,4 @@
1
- import { IRCase, IRConstr } from "../../IRNodes/index.js";
2
- import { IRApp } from "../../IRNodes/IRApp.js";
3
- import { IRDelayed } from "../../IRNodes/IRDelayed.js";
4
- import { IRForced } from "../../IRNodes/IRForced.js";
5
- import { IRFunc } from "../../IRNodes/IRFunc.js";
6
1
  import { IRHoisted } from "../../IRNodes/IRHoisted.js";
7
- import { IRLetted } from "../../IRNodes/IRLetted.js";
8
- import { IRRecursive } from "../../IRNodes/IRRecursive.js";
9
2
  export function findAll(term, predicate) {
10
3
  const stack = [term];
11
4
  const result = [];
@@ -13,37 +6,7 @@ export function findAll(term, predicate) {
13
6
  const t = stack.pop();
14
7
  if (predicate(t))
15
8
  result.push(t);
16
- if (t instanceof IRApp) {
17
- stack.push(t.fn, t.arg);
18
- continue;
19
- }
20
- if (t instanceof IRDelayed) {
21
- stack.push(t.delayed);
22
- continue;
23
- }
24
- if (t instanceof IRForced) {
25
- stack.push(t.forced);
26
- continue;
27
- }
28
- if (t instanceof IRFunc) {
29
- stack.push(t.body);
30
- continue;
31
- }
32
- if (t instanceof IRRecursive) {
33
- stack.push(t.body);
34
- continue;
35
- }
36
- if (t instanceof IRHoisted) {
37
- stack.push(t.hoisted);
38
- continue;
39
- }
40
- if (t instanceof IRLetted) {
41
- // same stuff as the hoisted terms
42
- // the only difference is that depth is then incremented
43
- // once the letted term reaches its final position
44
- stack.push(t.value);
45
- continue;
46
- }
9
+ stack.push(...t.children());
47
10
  }
48
11
  return result;
49
12
  }
@@ -54,46 +17,12 @@ export function findAllNoHoisted(term, predicate) {
54
17
  const t = stack.pop();
55
18
  if (predicate(t))
56
19
  result.push(t);
57
- if (t instanceof IRApp) {
58
- stack.push(t.fn, t.arg);
59
- continue;
60
- }
61
- if (t instanceof IRCase) {
62
- stack.push(t.constrTerm, ...t.continuations);
63
- continue;
64
- }
65
- if (t instanceof IRConstr) {
66
- stack.push(...t.fields);
67
- continue;
68
- }
69
- if (t instanceof IRDelayed) {
70
- stack.push(t.delayed);
71
- continue;
72
- }
73
- if (t instanceof IRForced) {
74
- stack.push(t.forced);
75
- continue;
76
- }
77
- if (t instanceof IRFunc) {
78
- stack.push(t.body);
79
- continue;
80
- }
81
- if (t instanceof IRRecursive) {
82
- stack.push(t.body);
83
- continue;
84
- }
85
20
  if (t instanceof IRHoisted) {
86
21
  // this is `findAllNoHoisted` remember?
87
22
  // stack.push( t.hoisted );
88
23
  continue;
89
24
  }
90
- if (t instanceof IRLetted) {
91
- // same stuff as the hoisted terms
92
- // the only difference is that depth is then incremented
93
- // once the letted term reaches its final position
94
- stack.push(t.value);
95
- continue;
96
- }
25
+ stack.push(...t.children());
97
26
  }
98
27
  return result;
99
28
  }
@@ -1,12 +1,7 @@
1
1
  import { equalIrHash } from "../../IRHash.js";
2
- import { IRCase, IRConstr } from "../../IRNodes/index.js";
3
- import { IRApp } from "../../IRNodes/IRApp.js";
4
- import { IRDelayed } from "../../IRNodes/IRDelayed.js";
5
- import { IRForced } from "../../IRNodes/IRForced.js";
2
+ import { IRHoisted } from "../../IRNodes/index.js";
6
3
  import { IRFunc } from "../../IRNodes/IRFunc.js";
7
- import { IRLetted } from "../../IRNodes/IRLetted.js";
8
4
  import { IRRecursive } from "../../IRNodes/IRRecursive.js";
9
- import { mapArrayLike } from "../../IRNodes/utils/mapArrayLike.js";
10
5
  export function getDebruijnInTerm(root, termToFind) {
11
6
  const termTofindHash = termToFind.hash;
12
7
  const stack = [{ term: root, dbn: 0 }];
@@ -14,38 +9,16 @@ export function getDebruijnInTerm(root, termToFind) {
14
9
  const { term, dbn } = stack.pop();
15
10
  if (term === termToFind || equalIrHash(term.hash, termTofindHash))
16
11
  return dbn;
17
- if (term instanceof IRApp) {
18
- stack.push({ term: term.fn, dbn }, { term: term.arg, dbn });
19
- continue;
20
- }
21
- if (term instanceof IRCase) {
22
- stack.push({ term: term.constrTerm, dbn }, ...mapArrayLike(term.continuations, (continuation) => ({ term: continuation, dbn })));
23
- continue;
24
- }
25
- if (term instanceof IRConstr) {
26
- stack.push(...mapArrayLike(term.fields, (field) => ({ term: field, dbn })));
27
- continue;
28
- }
29
- if (term instanceof IRDelayed) {
30
- stack.push({ term: term.delayed, dbn });
31
- continue;
32
- }
33
- if (term instanceof IRForced) {
34
- stack.push({ term: term.forced, dbn });
35
- continue;
36
- }
37
- if (term instanceof IRFunc) {
12
+ if (term instanceof IRFunc
13
+ || term instanceof IRRecursive) {
14
+ // new variable in scope
38
15
  stack.push({ term: term.body, dbn: dbn + term.arity });
39
16
  continue;
40
17
  }
41
- if (term instanceof IRRecursive) {
42
- stack.push({ term: term.body, dbn: dbn + term.arity });
18
+ if (term instanceof IRHoisted) {
43
19
  continue;
44
- }
45
- if (term instanceof IRLetted) {
46
- stack.push({ term: term.value, dbn });
47
- continue;
48
- }
20
+ } // skip hoisted since closed
21
+ stack.push(...term.children().map(t => ({ term: t, dbn })));
49
22
  }
50
23
  return -1;
51
24
  }
@@ -7,6 +7,7 @@ import { IRFunc } from "../../IRNodes/IRFunc.js";
7
7
  import { IRHoisted } from "../../IRNodes/IRHoisted.js";
8
8
  import { IRLetted } from "../../IRNodes/IRLetted.js";
9
9
  import { IRRecursive } from "../../IRNodes/IRRecursive.js";
10
+ import { sanifyTree } from "../subRoutines/sanifyTree.js";
10
11
  export function iterTree(_term, fn,
11
12
  // necessary for letted hash calculation (exclude hoisted)
12
13
  shouldSkipNode, shouldExit) {
@@ -32,31 +33,14 @@ shouldSkipNode, shouldExit) {
32
33
  continue;
33
34
  }
34
35
  if (t instanceof IRApp) {
35
- // sanifyTree as we go
36
- if (t.fn.parent !== t)
37
- t.fn = t.fn.clone();
38
- if (t.arg.parent !== t)
39
- t.arg = t.arg.clone();
40
36
  stack.push({ term: t.fn, dbn }, { term: t.arg, dbn, shouldPopIfParentIsModified: true });
41
37
  continue;
42
38
  }
43
39
  if (t instanceof IRCase) {
44
- // sanifyTree as we go
45
- if (t.constrTerm.parent !== t)
46
- t.constrTerm = t.constrTerm.clone();
47
- for (let i = 0; i < t.continuations.length; i++) {
48
- if (t.continuations[i].parent !== t)
49
- t.continuations[i] = t.continuations[i].clone();
50
- }
51
40
  stack.push({ term: t.constrTerm, dbn }, ...Array.from(t.continuations).map(cont => ({ term: cont, dbn, shouldPopIfParentIsModified: true })));
52
41
  continue;
53
42
  }
54
43
  if (t instanceof IRConstr) {
55
- // sanifyTree as we go
56
- for (let i = 0; i < t.fields.length; i++) {
57
- if (t.fields[i].parent !== t)
58
- t.fields[i] = t.fields[i].clone();
59
- }
60
44
  stack.push(...Array.from(t.fields).map((f, i) => ({ term: f, dbn, shouldPopIfParentIsModified: i !== 0 })));
61
45
  continue;
62
46
  }
@@ -89,5 +73,7 @@ shouldSkipNode, shouldExit) {
89
73
  stack.push({ term: t.value, dbn });
90
74
  continue;
91
75
  }
76
+ stack.push(...t.children().map(c => ({ term: c, dbn })));
92
77
  }
78
+ sanifyTree(_term);
93
79
  }
@@ -0,0 +1,2 @@
1
+ import { IRHoisted } from "../IRNodes/IRHoisted.js";
2
+ export declare const hoisted_constr1_empty: IRHoisted;
@@ -0,0 +1,4 @@
1
+ import { DataConstr } from "@harmoniclabs/plutus-data";
2
+ import { IRHoisted } from "../IRNodes/IRHoisted.js";
3
+ import { IRConst } from "../IRNodes/IRConst.js";
4
+ export const hoisted_constr1_empty = new IRHoisted(IRConst.data(new DataConstr(1, [])));
@@ -1,4 +1,4 @@
1
- import type { UPLCTerm } from "@harmoniclabs/uplc";
1
+ import { type UPLCTerm } from "@harmoniclabs/uplc";
2
2
  import type { IRTerm } from "../IRTerm.js";
3
3
  import { CompilerOptions } from "./CompilerOptions.js";
4
4
  export declare function compileIRToUPLC(term: IRTerm, paritalOptions?: Partial<CompilerOptions>): UPLCTerm;
@@ -1,7 +1,8 @@
1
+ import { isClosedTerm, prettyUPLC } from "@harmoniclabs/uplc";
1
2
  import { IRLetted } from "../IRNodes/IRLetted.js";
2
3
  import { IRHoisted } from "../IRNodes/IRHoisted.js";
3
4
  import { IRConst } from "../IRNodes/IRConst.js";
4
- import { _irToUplc } from "./_internal/_irToUplc.js";
5
+ import { _makeAllNegativeNativesHoisted } from "./_internal/_makeAllNegativeNativesHoisted.js";
5
6
  import { includesNode } from "./_internal/includesNode.js";
6
7
  import { handleLettedAndReturnRoot } from "./subRoutines/handleLetted/index.js";
7
8
  import { handleHoistedAndReturnRoot } from "./subRoutines/handleHoistedAndReturnRoot/index.js";
@@ -11,20 +12,26 @@ import { hoistForcedNatives } from "./subRoutines/hoistForcedNatives.js";
11
12
  import { handleRootRecursiveTerm } from "./subRoutines/handleRecursiveTerms.js";
12
13
  import { completeCompilerOptions, defaultOptions } from "./CompilerOptions.js";
13
14
  import { replaceHoistedWithLetted } from "./subRoutines/replaceHoistedWithLetted.js";
14
- import { IRCase, IRConstr, IRNative } from "../IRNodes/index.js";
15
+ import { IRCase, IRConstr, IRFunc, IRNative } from "../IRNodes/index.js";
15
16
  import { replaceForcedNativesWithHoisted } from "./subRoutines/replaceForcedNativesWithHoisted.js";
16
- import { performUplcOptimizationsAndReturnRoot } from "./subRoutines/performUplcOptimizationsAndReturnRoot/index.js";
17
+ import { performUplcOptimizationsAndReturnRoot } from "./subRoutines/performUplcOptimizationsAndReturnRoot/performUplcOptimizationsAndReturnRoot.js";
18
+ import { rewriteNativesAppliedToConstantsAndReturnRoot } from "./subRoutines/rewriteNativesAppliedToConstantsAndReturnRoot.js";
19
+ import { _debug_assertClosedIR } from "../utils/index.js";
20
+ import { ToUplcCtx } from "./ctx/ToUplcCtx.js";
21
+ import { removeUnusedVarsAndReturnRoot } from "./subRoutines/removeUnusuedVarsAndReturnRoot/removeUnusuedVarsAndReturnRoot.js";
22
+ import { IRRecursive } from "../IRNodes/IRRecursive.js";
17
23
  export function compileIRToUPLC(term, paritalOptions = defaultOptions) {
18
24
  // most of the time we are just compiling small
19
25
  // pre-execuded terms (hence constants)
20
26
  if (term instanceof IRConst)
21
- return _irToUplc(term).term;
27
+ return term.toUPLC();
22
28
  ///////////////////////////////////////////////////////////////////////////////
23
29
  // ------------------------------------------------------------------------- //
24
30
  // --------------------------------- init --------------------------------- //
25
31
  // ------------------------------------------------------------------------- //
26
32
  ///////////////////////////////////////////////////////////////////////////////
27
33
  const options = completeCompilerOptions(paritalOptions);
34
+ const debugAsserts = options.debugAsserts ?? false;
28
35
  // unwrap top level letted and hoisted;
29
36
  while (term instanceof IRLetted || term instanceof IRHoisted) {
30
37
  // replace with value
@@ -32,14 +39,20 @@ export function compileIRToUPLC(term, paritalOptions = defaultOptions) {
32
39
  // forget the parent; this is the new root
33
40
  term.parent = undefined;
34
41
  }
35
- // _makeAllNegativeNativesHoisted( term );
42
+ debugAsserts && _debug_assertions(term);
43
+ // term = preEvaluateDefinedTermsAndReturnRoot( term );
44
+ term = rewriteNativesAppliedToConstantsAndReturnRoot(term);
45
+ debugAsserts && _debug_assertions(term);
46
+ // removing unused variables BEFORE going into the rest of the compilation
47
+ // helps letted terms to find a better spot (and possibly be inlined instead of hoisted)
48
+ term = removeUnusedVarsAndReturnRoot(term);
49
+ debugAsserts && _debug_assertions(term);
50
+ _makeAllNegativeNativesHoisted(term);
36
51
  ///////////////////////////////////////////////////////////////////////////////
37
52
  // ------------------------------------------------------------------------- //
38
53
  // ----------------------------- optimizations ----------------------------- //
39
54
  // ------------------------------------------------------------------------- //
40
55
  ///////////////////////////////////////////////////////////////////////////////
41
- // --------------------------- optimize natives --------------------------- //
42
- // at constant -> head and tails
43
56
  // --------------------- optimize recursive functions --------------------- //
44
57
  // avoid passing whole structs
45
58
  // take letted terms outside
@@ -52,6 +65,7 @@ export function compileIRToUPLC(term, paritalOptions = defaultOptions) {
52
65
  // ------------------------------------------------------------------------- //
53
66
  ///////////////////////////////////////////////////////////////////////////////
54
67
  term = replaceNativesAndReturnRoot(term);
68
+ debugAsserts && _debug_assertions(term);
55
69
  // unwrap top level letted and hoisted;
56
70
  // some natives may be converted to hoisted;
57
71
  // this is really just an edge case
@@ -64,23 +78,32 @@ export function compileIRToUPLC(term, paritalOptions = defaultOptions) {
64
78
  if (term instanceof IRNative ||
65
79
  term instanceof IRConst // while we are at it
66
80
  )
67
- return _irToUplc(term).term;
68
- if (options.delayHoists)
69
- replaceForcedNativesWithHoisted(term);
81
+ return term.toUPLC();
82
+ replaceForcedNativesWithHoisted(term);
83
+ debugAsserts && _debug_assertions(term);
70
84
  if (options.delayHoists)
71
85
  replaceHoistedWithLetted(term);
72
86
  else
73
87
  replaceClosedLettedWithHoisted(term);
88
+ debugAsserts && _debug_assertions(term);
89
+ if (debugAsserts
90
+ && options.delayHoists
91
+ && includesNode(term, node => node instanceof IRHoisted)) {
92
+ throw new Error("debug assertion failed: hoisted nodes found while delayHoists is true");
93
+ }
74
94
  // handle letted before hoisted because the tree is smaller
75
95
  // and we also have less letted dependecies to handle
76
96
  term = handleLettedAndReturnRoot(term);
97
+ debugAsserts && _debug_assertions(term);
77
98
  term = handleHoistedAndReturnRoot(term);
99
+ debugAsserts && _debug_assertions(term);
78
100
  // replaced hoisted terms might include new letted terms
79
- while (includesNode(term, node => node instanceof IRLetted ||
80
- node instanceof IRHoisted)) {
101
+ while (includesNode(term, node => node instanceof IRLetted
102
+ || node instanceof IRHoisted)) {
81
103
  term = handleLettedAndReturnRoot(term);
82
104
  term = handleHoistedAndReturnRoot(term);
83
105
  }
106
+ debugAsserts && _debug_assertions(term);
84
107
  ///////////////////////////////////////////////////////////////////////////////
85
108
  // ------------------------------------------------------------------------- //
86
109
  // --------------------------- translate to UPLC --------------------------- //
@@ -94,12 +117,33 @@ export function compileIRToUPLC(term, paritalOptions = defaultOptions) {
94
117
  // if( options.delayHoists ) replaceHoistedWithLetted( term );
95
118
  // handle new hoisted terms
96
119
  term = handleHoistedAndReturnRoot(term);
120
+ debugAsserts && _debug_assertions(term);
97
121
  // strictly necessary to check the options
98
122
  // otherwise forced natives where already hoisted
99
123
  // will be re-hosited; causeing uselsess evaluations
100
124
  if (!options.delayHoists)
101
125
  term = hoistForcedNatives(term);
126
+ debugAsserts && _debug_assertions(term);
102
127
  // at this point we expect the IR to be translable 1:1 to UPLC
128
+ // The loop is needed because after inlining some params,
129
+ // new params in outer (or sibling) functions can become
130
+ // single‑use; a single bottom‑up pass doesn’t
131
+ // “see” those future states.
132
+ //
133
+ // ALWAYS AT LEAST 1 ITERATION
134
+ // const maxInlineIterations = Math.max( 3, 1 );
135
+ // for(
136
+ // let somethingWasInlined = true,
137
+ // inlineIterations = 0;
138
+ // somethingWasInlined
139
+ // && inlineIterations < maxInlineIterations;
140
+ // inlineIterations++
141
+ // ) {
142
+ // const inlineResult = inlineSingleUseAndReturnRoot( term );
143
+ // term = inlineResult.term;
144
+ // somethingWasInlined = inlineResult.somethingWasInlined;
145
+ // }
146
+ term = removeUnusedVarsAndReturnRoot(term);
103
147
  term = performUplcOptimizationsAndReturnRoot(term, options);
104
148
  if (options.addMarker &&
105
149
  options.targetUplcVersion.major >= 1 &&
@@ -111,8 +155,41 @@ export function compileIRToUPLC(term, paritalOptions = defaultOptions) {
111
155
  IRConst.int(42)
112
156
  ]);
113
157
  }
114
- const srcmap = {};
115
- const uplc = _irToUplc(term, srcmap).term;
158
+ // let irJson = prettyIR( term );
159
+ // console.log(
160
+ // "final IR before UPLC translation:\n",
161
+ // irJson.text,
162
+ // JSON.stringify( onlyHoistedAndLetted( irJson ) )
163
+ // );
164
+ debugAsserts && _debug_assertions(term);
165
+ // const srcmap = {};
166
+ const uplc = term.toUPLC(ToUplcCtx.root());
167
+ if (!isClosedTerm(uplc)) {
168
+ console.log(prettyUPLC(uplc));
169
+ throw new Error("compileIRToUPLC: final UPLC term is not closed:\n" +
170
+ "This is a compiler internal error; please open an issue on github so we can fix this.");
171
+ }
116
172
  // console.log( "srcmap", srcmap );
117
173
  return uplc;
118
174
  }
175
+ function _debug_assertions(term) {
176
+ _debug_assertClosedIR(term);
177
+ _debug_assertNoDoubleVars(term);
178
+ }
179
+ function _debug_assertNoDoubleVars(term) {
180
+ const seen = new Set();
181
+ const stack = [term];
182
+ let current = term;
183
+ while (current = stack.pop()) {
184
+ if (current instanceof IRFunc
185
+ || current instanceof IRRecursive) {
186
+ for (const p of current.params) {
187
+ if (seen.has(p)) {
188
+ throw new Error("debug assertion failed: double variable detected");
189
+ }
190
+ seen.add(p);
191
+ }
192
+ }
193
+ stack.push(...current.children());
194
+ }
195
+ }
@@ -0,0 +1,16 @@
1
+ export declare class ToUplcCtx {
2
+ readonly parent: ToUplcCtx | undefined;
3
+ readonly ctxMap: Map<symbol, ToUplcCtx>;
4
+ private readonly _variables;
5
+ private get _parentDbn();
6
+ get dbn(): number;
7
+ private _frozen;
8
+ constructor(parent: ToUplcCtx | undefined, variables: symbol[]);
9
+ static root(): ToUplcCtx;
10
+ newChild(variables: symbol[]): ToUplcCtx;
11
+ getVarDeclDbn(sym: symbol): number;
12
+ getVarAccessDbn(sym: symbol): number;
13
+ toJson(): any;
14
+ allVars(): symbol[];
15
+ expectedDbn(sym: symbol): number;
16
+ }