@harmoniclabs/pebble 0.1.0-dev5 → 0.1.0-dev7

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 (321) 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 +3 -3
  24. package/dist/IR/IRNodes/IRNative/IRNativeTag.js +4 -4
  25. package/dist/IR/IRNodes/IRNative/index.d.ts +10 -8
  26. package/dist/IR/IRNodes/IRNative/index.js +56 -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/CompilerOptions.d.ts +5 -0
  42. package/dist/IR/toUPLC/CompilerOptions.js +10 -1
  43. package/dist/IR/toUPLC/_internal/_modifyChildFromTo.js +3 -0
  44. package/dist/IR/toUPLC/_internal/findAll.js +2 -73
  45. package/dist/IR/toUPLC/_internal/getDebruijnInTerm.js +7 -34
  46. package/dist/IR/toUPLC/_internal/iterTree.js +3 -17
  47. package/dist/IR/toUPLC/common_hoisted.d.ts +2 -0
  48. package/dist/IR/toUPLC/common_hoisted.js +4 -0
  49. package/dist/IR/toUPLC/compileIRToUPLC.d.ts +1 -1
  50. package/dist/IR/toUPLC/compileIRToUPLC.js +91 -14
  51. package/dist/IR/toUPLC/ctx/ToUplcCtx.d.ts +16 -0
  52. package/dist/IR/toUPLC/ctx/ToUplcCtx.js +67 -0
  53. package/dist/IR/toUPLC/subRoutines/_comptimeDropN.d.ts +8 -0
  54. package/dist/IR/toUPLC/subRoutines/_comptimeDropN.js +95 -0
  55. package/dist/IR/toUPLC/subRoutines/handleHoistedAndReturnRoot/index.js +7 -54
  56. package/dist/IR/toUPLC/subRoutines/handleLetted/groupByScope.d.ts +6 -13
  57. package/dist/IR/toUPLC/subRoutines/handleLetted/groupByScope.js +65 -71
  58. package/dist/IR/toUPLC/subRoutines/handleLetted/index.js +44 -170
  59. package/dist/IR/toUPLC/subRoutines/handleRecursiveTerms.js +7 -3
  60. package/dist/IR/toUPLC/subRoutines/hoistForcedNatives.js +3 -2
  61. package/dist/IR/toUPLC/subRoutines/inlineSingleUseAndReturnRoot/InlineSingleUseCtx.d.ts +22 -0
  62. package/dist/IR/toUPLC/subRoutines/inlineSingleUseAndReturnRoot/InlineSingleUseCtx.js +59 -0
  63. package/dist/IR/toUPLC/subRoutines/inlineSingleUseAndReturnRoot/inlineSingleUseAndReturnRoot.d.ts +0 -0
  64. package/dist/IR/toUPLC/subRoutines/inlineSingleUseAndReturnRoot/inlineSingleUseAndReturnRoot.js +217 -0
  65. package/dist/IR/toUPLC/subRoutines/markRecursiveHoistsAsForced.js +12 -49
  66. package/dist/IR/toUPLC/subRoutines/performUplcOptimizationsAndReturnRoot/expandFuncsAndReturnRoot.d.ts +1 -1
  67. package/dist/IR/toUPLC/subRoutines/performUplcOptimizationsAndReturnRoot/expandFuncsAndReturnRoot.js +11 -14
  68. package/dist/IR/toUPLC/subRoutines/performUplcOptimizationsAndReturnRoot/{index.d.ts → performUplcOptimizationsAndReturnRoot.d.ts} +0 -2
  69. package/dist/IR/toUPLC/subRoutines/performUplcOptimizationsAndReturnRoot/performUplcOptimizationsAndReturnRoot.js +151 -0
  70. package/dist/IR/toUPLC/subRoutines/removeUnusuedVarsAndReturnRoot/RemoveUnusedVarsCtx.d.ts +11 -0
  71. package/dist/IR/toUPLC/subRoutines/removeUnusuedVarsAndReturnRoot/RemoveUnusedVarsCtx.js +46 -0
  72. package/dist/IR/toUPLC/subRoutines/removeUnusuedVarsAndReturnRoot/removeUnusuedVarsAndReturnRoot.d.ts +2 -0
  73. package/dist/IR/toUPLC/subRoutines/removeUnusuedVarsAndReturnRoot/removeUnusuedVarsAndReturnRoot.js +80 -0
  74. package/dist/IR/toUPLC/subRoutines/replaceHoistedWithLetted.js +18 -11
  75. package/dist/IR/toUPLC/subRoutines/replaceNatives/nativeToIR.d.ts +45 -0
  76. package/dist/IR/toUPLC/subRoutines/replaceNatives/nativeToIR.js +277 -478
  77. package/dist/IR/toUPLC/subRoutines/rewriteNativesAppliedToConstantsAndReturnRoot.d.ts +2 -0
  78. package/dist/IR/toUPLC/subRoutines/rewriteNativesAppliedToConstantsAndReturnRoot.js +85 -0
  79. package/dist/IR/toUPLC/subRoutines/sanifyTree.js +1 -0
  80. package/dist/IR/toUPLC/utils/getApplicationTerms.d.ts +6 -0
  81. package/dist/IR/toUPLC/utils/getApplicationTerms.js +37 -0
  82. package/dist/IR/tree_utils/_ir_lazyChooseList.d.ts +2 -2
  83. package/dist/IR/tree_utils/_ir_lazyChooseList.js +4 -2
  84. package/dist/IR/tree_utils/_ir_lazyIfThenElse.d.ts +1 -1
  85. package/dist/IR/tree_utils/_ir_lazyIfThenElse.js +4 -2
  86. package/dist/IR/tree_utils/_ir_let.d.ts +2 -1
  87. package/dist/IR/tree_utils/_ir_let.js +6 -2
  88. package/dist/IR/utils/isClosedIRTerm.d.ts +1 -0
  89. package/dist/IR/utils/isClosedIRTerm.js +40 -26
  90. package/dist/IR/utils/positiveIntAsBytes.d.ts +1 -0
  91. package/dist/IR/utils/positiveIntAsBytes.js +4 -4
  92. package/dist/IR/utils/showIR.d.ts +10 -21
  93. package/dist/IR/utils/showIR.js +66 -64
  94. package/dist/ast/nodes/common/Identifier.d.ts +1 -0
  95. package/dist/ast/nodes/common/Identifier.js +3 -0
  96. package/dist/ast/nodes/expr/CaseExpr.d.ts +5 -5
  97. package/dist/ast/nodes/expr/ElemAccessExpr.d.ts +2 -2
  98. package/dist/ast/nodes/expr/ParentesizedExpr.d.ts +1 -1
  99. package/dist/ast/nodes/expr/PebbleExpr.d.ts +2 -1
  100. package/dist/ast/nodes/expr/PropAccessExpr.d.ts +3 -3
  101. package/dist/ast/nodes/expr/TernaryExpr.d.ts +3 -3
  102. package/dist/ast/nodes/expr/TypeConversionExpr.d.ts +2 -1
  103. package/dist/ast/nodes/expr/TypeConversionExpr.js +2 -0
  104. package/dist/ast/nodes/expr/binary/BinaryExpr.d.ts +42 -42
  105. package/dist/ast/nodes/expr/functions/CallExpr.d.ts +1 -1
  106. package/dist/ast/nodes/expr/functions/FuncExpr.d.ts +2 -1
  107. package/dist/ast/nodes/expr/functions/FuncExpr.js +6 -0
  108. package/dist/ast/nodes/expr/litteral/LitContextExpr.d.ts +6 -0
  109. package/dist/ast/nodes/expr/litteral/LitContextExpr.js +6 -0
  110. package/dist/ast/nodes/expr/litteral/LitFailExpr.d.ts +6 -0
  111. package/dist/ast/nodes/expr/litteral/LitFailExpr.js +6 -0
  112. package/dist/ast/nodes/expr/litteral/LitteralExpr.d.ts +3 -1
  113. package/dist/ast/nodes/expr/litteral/LitteralExpr.js +5 -1
  114. package/dist/ast/nodes/statements/AssertStmt.d.ts +2 -2
  115. package/dist/ast/nodes/statements/AssignmentStmt.d.ts +1 -1
  116. package/dist/ast/nodes/statements/BlockStmt.d.ts +1 -1
  117. package/dist/ast/nodes/statements/FailStmt.d.ts +1 -1
  118. package/dist/ast/nodes/statements/ForOfStmt.d.ts +4 -2
  119. package/dist/ast/nodes/statements/ForOfStmt.js +6 -0
  120. package/dist/ast/nodes/statements/ForStmt.d.ts +6 -4
  121. package/dist/ast/nodes/statements/ForStmt.js +6 -0
  122. package/dist/ast/nodes/statements/IfStmt.d.ts +8 -5
  123. package/dist/ast/nodes/statements/IfStmt.js +13 -0
  124. package/dist/ast/nodes/statements/MatchStmt.d.ts +13 -4
  125. package/dist/ast/nodes/statements/MatchStmt.js +22 -1
  126. package/dist/ast/nodes/statements/PebbleStmt.d.ts +2 -1
  127. package/dist/ast/nodes/statements/PebbleStmt.js +3 -1
  128. package/dist/ast/nodes/statements/ReturnStmt.d.ts +1 -1
  129. package/dist/ast/nodes/statements/WhileStmt.d.ts +4 -2
  130. package/dist/ast/nodes/statements/WhileStmt.js +6 -0
  131. package/dist/ast/nodes/statements/declarations/ContractDecl.d.ts +17 -0
  132. package/dist/ast/nodes/statements/declarations/ContractDecl.js +22 -0
  133. package/dist/ast/nodes/statements/declarations/VarDecl/ArrayLikeDeconstr.d.ts +1 -1
  134. package/dist/ast/nodes/statements/declarations/VarDecl/HasInit.d.ts +1 -1
  135. package/dist/ast/nodes/statements/declarations/VarDecl/NamedDeconstructVarDecl.d.ts +1 -1
  136. package/dist/ast/nodes/statements/declarations/VarDecl/SimpleVarDecl.d.ts +2 -1
  137. package/dist/ast/nodes/statements/declarations/VarDecl/SimpleVarDecl.js +6 -0
  138. package/dist/ast/nodes/statements/declarations/VarDecl/SingleDeconstructVarDecl.d.ts +1 -1
  139. package/dist/compiler/AstCompiler/AstCompiler.d.ts +7 -2
  140. package/dist/compiler/AstCompiler/AstCompiler.js +129 -25
  141. package/dist/compiler/AstCompiler/internal/_deriveContractBody/_deriveContractBody.d.ts +10 -0
  142. package/dist/compiler/AstCompiler/internal/_deriveContractBody/_deriveContractBody.js +777 -0
  143. package/dist/compiler/AstCompiler/internal/exprs/_compileBinaryExpr.js +1 -1
  144. package/dist/compiler/AstCompiler/internal/exprs/_compileExpr.js +5 -1
  145. package/dist/compiler/AstCompiler/internal/exprs/_compileFuncExpr.js +7 -4
  146. package/dist/compiler/AstCompiler/internal/exprs/_compileLitteralExpr.js +8 -2
  147. package/dist/compiler/AstCompiler/internal/exprs/_compilePropAccessExpr.js +1 -1
  148. package/dist/compiler/AstCompiler/internal/exprs/_compileTypeConversionExpr.js +2 -1
  149. package/dist/compiler/AstCompiler/internal/exprs/_compileUnaryPrefixExpr.js +1 -1
  150. package/dist/compiler/AstCompiler/internal/exprs/_compileVarAccessExpr.js +3 -1
  151. package/dist/compiler/AstCompiler/internal/statements/_compileAssignmentStmt.js +8 -8
  152. package/dist/compiler/AstCompiler/internal/statements/_compileMatchStmt.d.ts +2 -2
  153. package/dist/compiler/AstCompiler/internal/statements/_compileMatchStmt.js +48 -24
  154. package/dist/compiler/AstCompiler/internal/statements/_compileStatement.js +4 -1
  155. package/dist/compiler/AstCompiler/internal/statements/_compileVarStmt.d.ts +2 -1
  156. package/dist/compiler/AstCompiler/internal/statements/_compileVarStmt.js +4 -3
  157. package/dist/compiler/AstCompiler/internal/types/_compileDataEncodedConcreteType.js +1 -1
  158. package/dist/compiler/AstCompiler/internal/types/_compileSopEncodedConcreteType.js +4 -1
  159. package/dist/compiler/AstCompiler/scope/AstScope.d.ts +1 -0
  160. package/dist/compiler/AstCompiler/scope/AstScope.js +9 -3
  161. package/dist/compiler/AstCompiler/utils/getPropAccessReturnType.js +6 -4
  162. package/dist/compiler/Compiler.js +7 -1
  163. package/dist/compiler/TirCompiler/compileTirProgram.js +4 -1
  164. package/dist/compiler/TirCompiler/expressify/ExpressifyCtx.d.ts +17 -5
  165. package/dist/compiler/TirCompiler/expressify/ExpressifyCtx.js +79 -49
  166. package/dist/compiler/TirCompiler/expressify/determineReassignedVariablesAndReturn.js +13 -2
  167. package/dist/compiler/TirCompiler/expressify/expressify.d.ts +2 -1
  168. package/dist/compiler/TirCompiler/expressify/expressify.js +102 -41
  169. package/dist/compiler/TirCompiler/expressify/expressifyForStmt.d.ts +2 -1
  170. package/dist/compiler/TirCompiler/expressify/expressifyForStmt.js +77 -12
  171. package/dist/compiler/TirCompiler/expressify/expressifyIfBranch.js +1 -1
  172. package/dist/compiler/TirCompiler/expressify/expressifyVarAssignmentStmt.d.ts +1 -2
  173. package/dist/compiler/TirCompiler/expressify/expressifyVarAssignmentStmt.js +16 -9
  174. package/dist/compiler/TirCompiler/expressify/expressifyVarDecl.js +6 -8
  175. package/dist/compiler/TirCompiler/expressify/expressifyVars.js +146 -13
  176. package/dist/compiler/TirCompiler/expressify/flattenSopNamedDeconstructInplace_addTopDestructToCtx_getNestedDeconstruct.js +1 -0
  177. package/dist/compiler/internalVar.d.ts +1 -1
  178. package/dist/compiler/internalVar.js +31 -24
  179. package/dist/compiler/io/CompilerIoApi.d.ts +1 -1
  180. package/dist/compiler/io/IOutputStream.d.ts +1 -0
  181. package/dist/compiler/io/IOutputStream.js +18 -3
  182. package/dist/compiler/path/getAbsolutePath.js +4 -6
  183. package/dist/compiler/tir/expressions/ITirExpr.d.ts +2 -0
  184. package/dist/compiler/tir/expressions/TirAssertAndContinueExpr.d.ts +4 -2
  185. package/dist/compiler/tir/expressions/TirAssertAndContinueExpr.js +16 -2
  186. package/dist/compiler/tir/expressions/TirCallExpr.d.ts +3 -1
  187. package/dist/compiler/tir/expressions/TirCallExpr.js +16 -1
  188. package/dist/compiler/tir/expressions/TirCaseExpr.d.ts +6 -1
  189. package/dist/compiler/tir/expressions/TirCaseExpr.js +105 -60
  190. package/dist/compiler/tir/expressions/TirElemAccessExpr.d.ts +3 -1
  191. package/dist/compiler/tir/expressions/TirElemAccessExpr.js +9 -1
  192. package/dist/compiler/tir/expressions/TirFailExpr.d.ts +3 -1
  193. package/dist/compiler/tir/expressions/TirFailExpr.js +9 -1
  194. package/dist/compiler/tir/expressions/TirFromDataExpr.d.ts +3 -2
  195. package/dist/compiler/tir/expressions/TirFromDataExpr.js +85 -56
  196. package/dist/compiler/tir/expressions/TirFuncExpr.d.ts +4 -10
  197. package/dist/compiler/tir/expressions/TirFuncExpr.js +30 -13
  198. package/dist/compiler/tir/expressions/TirHoistedExpr.d.ts +3 -1
  199. package/dist/compiler/tir/expressions/TirHoistedExpr.js +8 -0
  200. package/dist/compiler/tir/expressions/TirInlineClosedIR.d.ts +4 -1
  201. package/dist/compiler/tir/expressions/TirInlineClosedIR.js +8 -0
  202. package/dist/compiler/tir/expressions/TirLettedExpr.d.ts +6 -3
  203. package/dist/compiler/tir/expressions/TirLettedExpr.js +20 -7
  204. package/dist/compiler/tir/expressions/TirNativeFunc.d.ts +9 -5
  205. package/dist/compiler/tir/expressions/TirNativeFunc.js +58 -22
  206. package/dist/compiler/tir/expressions/TirParentesizedExpr.d.ts +3 -1
  207. package/dist/compiler/tir/expressions/TirParentesizedExpr.js +6 -0
  208. package/dist/compiler/tir/expressions/TirPropAccessExpr.d.ts +3 -1
  209. package/dist/compiler/tir/expressions/TirPropAccessExpr.js +9 -1
  210. package/dist/compiler/tir/expressions/TirTernaryExpr.d.ts +3 -1
  211. package/dist/compiler/tir/expressions/TirTernaryExpr.js +16 -5
  212. package/dist/compiler/tir/expressions/TirToDataExpr.d.ts +3 -1
  213. package/dist/compiler/tir/expressions/TirToDataExpr.js +56 -25
  214. package/dist/compiler/tir/expressions/TirTraceIfFalseExpr.d.ts +3 -1
  215. package/dist/compiler/tir/expressions/TirTraceIfFalseExpr.js +21 -4
  216. package/dist/compiler/tir/expressions/TirTypeConversionExpr.d.ts +3 -1
  217. package/dist/compiler/tir/expressions/TirTypeConversionExpr.js +9 -1
  218. package/dist/compiler/tir/expressions/TirVariableAccessExpr.d.ts +6 -3
  219. package/dist/compiler/tir/expressions/TirVariableAccessExpr.js +22 -8
  220. package/dist/compiler/tir/expressions/ToIRTermCtx.d.ts +19 -20
  221. package/dist/compiler/tir/expressions/ToIRTermCtx.js +74 -50
  222. package/dist/compiler/tir/expressions/binary/TirBinaryExpr.d.ts +41 -0
  223. package/dist/compiler/tir/expressions/binary/TirBinaryExpr.js +180 -4
  224. package/dist/compiler/tir/expressions/litteral/TirLitArrExpr.d.ts +4 -2
  225. package/dist/compiler/tir/expressions/litteral/TirLitArrExpr.js +8 -2
  226. package/dist/compiler/tir/expressions/litteral/TirLitFailExpr.d.ts +17 -0
  227. package/dist/compiler/tir/expressions/litteral/TirLitFailExpr.js +21 -0
  228. package/dist/compiler/tir/expressions/litteral/TirLitFalseExpr.d.ts +4 -1
  229. package/dist/compiler/tir/expressions/litteral/TirLitFalseExpr.js +4 -0
  230. package/dist/compiler/tir/expressions/litteral/TirLitHexBytesExpr.d.ts +4 -1
  231. package/dist/compiler/tir/expressions/litteral/TirLitHexBytesExpr.js +5 -0
  232. package/dist/compiler/tir/expressions/litteral/TirLitIntExpr.d.ts +4 -1
  233. package/dist/compiler/tir/expressions/litteral/TirLitIntExpr.js +4 -0
  234. package/dist/compiler/tir/expressions/litteral/TirLitNamedObjExpr.d.ts +3 -1
  235. package/dist/compiler/tir/expressions/litteral/TirLitNamedObjExpr.js +5 -0
  236. package/dist/compiler/tir/expressions/litteral/TirLitObjExpr.d.ts +3 -1
  237. package/dist/compiler/tir/expressions/litteral/TirLitObjExpr.js +5 -0
  238. package/dist/compiler/tir/expressions/litteral/TirLitStrExpr.d.ts +4 -1
  239. package/dist/compiler/tir/expressions/litteral/TirLitStrExpr.js +4 -0
  240. package/dist/compiler/tir/expressions/litteral/TirLitThisExpr.d.ts +4 -1
  241. package/dist/compiler/tir/expressions/litteral/TirLitThisExpr.js +7 -3
  242. package/dist/compiler/tir/expressions/litteral/TirLitTrueExpr.d.ts +4 -1
  243. package/dist/compiler/tir/expressions/litteral/TirLitTrueExpr.js +4 -0
  244. package/dist/compiler/tir/expressions/litteral/TirLitUndefExpr.d.ts +4 -1
  245. package/dist/compiler/tir/expressions/litteral/TirLitUndefExpr.js +4 -0
  246. package/dist/compiler/tir/expressions/litteral/TirLitVoidExpr.d.ts +4 -1
  247. package/dist/compiler/tir/expressions/litteral/TirLitVoidExpr.js +4 -0
  248. package/dist/compiler/tir/expressions/litteral/TirLitteralExpr.d.ts +2 -1
  249. package/dist/compiler/tir/expressions/litteral/TirLitteralExpr.js +3 -1
  250. package/dist/compiler/tir/expressions/unary/TirUnaryExclamation.d.ts +4 -2
  251. package/dist/compiler/tir/expressions/unary/TirUnaryExclamation.js +8 -3
  252. package/dist/compiler/tir/expressions/unary/TirUnaryMinus.d.ts +4 -2
  253. package/dist/compiler/tir/expressions/unary/TirUnaryMinus.js +8 -2
  254. package/dist/compiler/tir/expressions/unary/TirUnaryPlus.d.ts +3 -1
  255. package/dist/compiler/tir/expressions/unary/TirUnaryPlus.js +6 -0
  256. package/dist/compiler/tir/expressions/unary/TirUnaryTilde.d.ts +4 -2
  257. package/dist/compiler/tir/expressions/unary/TirUnaryTilde.js +8 -2
  258. package/dist/compiler/tir/program/TypedProgram.d.ts +3 -2
  259. package/dist/compiler/tir/program/TypedProgram.js +9 -0
  260. package/dist/compiler/tir/program/stdScope/stdScope.js +135 -149
  261. package/dist/compiler/tir/statements/TirAssertStmt.d.ts +2 -0
  262. package/dist/compiler/tir/statements/TirAssertStmt.js +10 -0
  263. package/dist/compiler/tir/statements/TirAssignmentStmt.d.ts +2 -0
  264. package/dist/compiler/tir/statements/TirAssignmentStmt.js +8 -0
  265. package/dist/compiler/tir/statements/TirBlockStmt.d.ts +2 -0
  266. package/dist/compiler/tir/statements/TirBlockStmt.js +16 -0
  267. package/dist/compiler/tir/statements/TirBreakStmt.d.ts +2 -0
  268. package/dist/compiler/tir/statements/TirBreakStmt.js +8 -0
  269. package/dist/compiler/tir/statements/TirContinueStmt.d.ts +2 -0
  270. package/dist/compiler/tir/statements/TirContinueStmt.js +8 -0
  271. package/dist/compiler/tir/statements/TirFailStmt.d.ts +2 -0
  272. package/dist/compiler/tir/statements/TirFailStmt.js +8 -0
  273. package/dist/compiler/tir/statements/TirForOfStmt.d.ts +2 -0
  274. package/dist/compiler/tir/statements/TirForOfStmt.js +14 -0
  275. package/dist/compiler/tir/statements/TirForStmt.d.ts +2 -0
  276. package/dist/compiler/tir/statements/TirForStmt.js +21 -0
  277. package/dist/compiler/tir/statements/TirIfStmt.d.ts +2 -0
  278. package/dist/compiler/tir/statements/TirIfStmt.js +16 -0
  279. package/dist/compiler/tir/statements/TirMatchStmt.d.ts +2 -0
  280. package/dist/compiler/tir/statements/TirMatchStmt.js +30 -0
  281. package/dist/compiler/tir/statements/TirReturnStmt.d.ts +2 -0
  282. package/dist/compiler/tir/statements/TirReturnStmt.js +8 -0
  283. package/dist/compiler/tir/statements/TirStmt.d.ts +2 -0
  284. package/dist/compiler/tir/statements/TirVarDecl/TirArrayLikeDeconstr.d.ts +2 -0
  285. package/dist/compiler/tir/statements/TirVarDecl/TirArrayLikeDeconstr.js +30 -0
  286. package/dist/compiler/tir/statements/TirVarDecl/TirNamedDeconstructVarDecl.d.ts +3 -1
  287. package/dist/compiler/tir/statements/TirVarDecl/TirNamedDeconstructVarDecl.js +28 -0
  288. package/dist/compiler/tir/statements/TirVarDecl/TirSimpleVarDecl.d.ts +3 -0
  289. package/dist/compiler/tir/statements/TirVarDecl/TirSimpleVarDecl.js +13 -0
  290. package/dist/compiler/tir/statements/TirVarDecl/TirSingleDeconstructVarDecl.d.ts +3 -1
  291. package/dist/compiler/tir/statements/TirVarDecl/TirSingleDeconstructVarDecl.js +28 -0
  292. package/dist/compiler/tir/statements/TirWhileStmt.d.ts +2 -0
  293. package/dist/compiler/tir/statements/TirWhileStmt.js +14 -0
  294. package/dist/compiler/tir/types/utils/canAssignTo.js +1 -1
  295. package/dist/diagnostics/DiagnosticEmitter.js +1 -1
  296. package/dist/diagnostics/diagnosticMessages.generated.d.ts +10 -1
  297. package/dist/diagnostics/diagnosticMessages.generated.js +20 -2
  298. package/dist/parser/Parser.d.ts +8 -3
  299. package/dist/parser/Parser.js +180 -32
  300. package/dist/parser/Precedence.js +1 -0
  301. package/dist/tokenizer/Token.d.ts +81 -72
  302. package/dist/tokenizer/Token.js +82 -72
  303. package/dist/tokenizer/utils/tokenFromKeyword.js +32 -10
  304. package/dist/tokenizer/utils/tokenIsAlsoIdentifier.js +10 -0
  305. package/dist/utils/BitUtils/index.js +1 -1
  306. package/dist/utils/UPLCFlatUtils/index.js +1 -1
  307. package/dist/utils/array/keepSortedArrInplace.js +1 -0
  308. package/package.json +3 -1
  309. package/dist/IR/IRNodes/utils/isClosedAtDbn.d.ts +0 -2
  310. package/dist/IR/IRNodes/utils/isClosedAtDbn.js +0 -22
  311. package/dist/IR/toUPLC/_internal/_irToUplc.d.ts +0 -9
  312. package/dist/IR/toUPLC/_internal/_irToUplc.js +0 -156
  313. package/dist/IR/toUPLC/subRoutines/handleLetted/incrementUnboundDbns.d.ts +0 -9
  314. package/dist/IR/toUPLC/subRoutines/handleLetted/incrementUnboundDbns.js +0 -73
  315. package/dist/IR/toUPLC/subRoutines/inlineSingleUseApplications.d.ts +0 -1
  316. package/dist/IR/toUPLC/subRoutines/inlineSingleUseApplications.js +0 -12
  317. package/dist/IR/toUPLC/subRoutines/performUplcOptimizationsAndReturnRoot/index.js +0 -227
  318. package/dist/IR/tree_utils/_ir_apps.d.ts +0 -3
  319. package/dist/IR/tree_utils/_ir_apps.js +0 -8
  320. package/dist/compiler/TirCompiler/internal/_compileHoistedDeps.d.ts +0 -5
  321. package/dist/compiler/TirCompiler/internal/_compileHoistedDeps.js +0 -37
@@ -5,6 +5,7 @@ import { TirLitIntExpr } from "../../tir/expressions/litteral/TirLitIntExpr.js";
5
5
  import { TirCallExpr } from "../../tir/expressions/TirCallExpr.js";
6
6
  import { TirElemAccessExpr } from "../../tir/expressions/TirElemAccessExpr.js";
7
7
  import { TirFromDataExpr } from "../../tir/expressions/TirFromDataExpr.js";
8
+ import { TirHoistedExpr } from "../../tir/expressions/TirHoistedExpr.js";
8
9
  import { TirLettedExpr } from "../../tir/expressions/TirLettedExpr.js";
9
10
  import { TirNativeFunc } from "../../tir/expressions/TirNativeFunc.js";
10
11
  import { data_t, int_t } from "../../tir/program/stdScope/stdScope.js";
@@ -26,12 +27,13 @@ export function isExpressifyFuncParam(thing) {
26
27
  export class ExpressifyCtx {
27
28
  parent;
28
29
  returnType;
30
+ program;
29
31
  variables;
30
32
  funcParams;
31
33
  lettedConstants;
32
34
  properties;
33
35
  hoisted;
34
- constructor(parent, returnType, hoisted,
36
+ constructor(parent, returnType, program, hoisted,
35
37
  /** var name -> latest constant name */
36
38
  variables = new Map(),
37
39
  /** constant name -> func param name (to build var access) */
@@ -42,39 +44,64 @@ export class ExpressifyCtx {
42
44
  properties = new Map()) {
43
45
  this.parent = parent;
44
46
  this.returnType = returnType;
47
+ this.program = program;
45
48
  this.variables = variables;
46
49
  this.funcParams = funcParams;
47
50
  this.lettedConstants = lettedConstants;
48
51
  this.properties = properties;
49
- this.hoisted = hoisted ?? this.parent?.hoisted ?? new Map();
52
+ this.hoisted = (hoisted
53
+ ?? this.parent?.hoisted
54
+ ?? new Map([...program.constants.entries()].map(([name, decl]) => {
55
+ if (!decl.initExpr)
56
+ throw new Error(`expected init expr in hoisted constant '${name}'`);
57
+ const expr = decl.initExpr instanceof TirHoistedExpr ? decl.initExpr : new TirHoistedExpr(name, decl.initExpr);
58
+ return [name, expr];
59
+ })));
60
+ }
61
+ allVariablesNoLetted() {
62
+ return (this.parent?.allVariablesNoLetted() ?? []).concat(...this.variables.keys());
63
+ }
64
+ allVariables() {
65
+ const thisVars = new Set([
66
+ ...this.variables.keys(),
67
+ ...this.lettedConstants.keys(),
68
+ ...this.hoisted.keys(),
69
+ ]);
70
+ return (this.parent?.allVariables() ?? []).concat(...thisVars);
50
71
  }
51
72
  newChild() {
52
- return new ExpressifyCtx(this, this.returnType);
73
+ return new ExpressifyCtx(this, this.returnType, this.program);
53
74
  }
54
75
  setNewVariableName(oldName, newName) {
55
76
  let latestNameSSA = this.variables.get(oldName);
56
77
  if (!latestNameSSA)
57
78
  this.variables.set(oldName, { latestName: newName });
58
- else
79
+ else {
80
+ const prevName = latestNameSSA.latestName;
81
+ if (prevName === newName)
82
+ return; // no change
59
83
  latestNameSSA.latestName = newName;
84
+ this.setNewVariableName(prevName, newName);
85
+ this.setNewVariableName(newName, newName);
86
+ }
60
87
  }
61
88
  setFuncParam(name, type) {
62
- this.variables.set(name, { latestName: name });
89
+ this.setNewVariableName(name, name);
63
90
  this.funcParams.set(name, { name, type });
64
91
  }
65
92
  /**
66
93
  * `undefined` means the variable is not in the local context.
67
94
  */
68
95
  getLocalVariable(name) {
69
- // declared as constant shortcut
70
- const constResult = this.lettedConstants.get(name);
71
- if (constResult)
72
- return constResult;
73
96
  // declared as variable or param
74
97
  const latestConstName = this.variables.get(name)?.latestName;
75
- // no such variable
76
- if (!latestConstName)
77
- return undefined;
98
+ // no such variable (mutable at least)
99
+ if (!latestConstName) {
100
+ // try to get var declared as constant
101
+ // return undefined if not present
102
+ return this.lettedConstants.get(name);
103
+ }
104
+ ;
78
105
  return (this.lettedConstants.get(latestConstName)
79
106
  ?? this.funcParams.get(latestConstName));
80
107
  }
@@ -92,11 +119,15 @@ export class ExpressifyCtx {
92
119
  const result = (this._getNonHoistedVariable(name)
93
120
  ?? this.hoisted.get(name));
94
121
  if (!result) {
95
- console.log(this);
122
+ console.log(this.allVariables());
96
123
  throw new Error(`variable '${name}' not found in the context`);
97
124
  }
98
125
  return result;
99
126
  }
127
+ getVariableSSA(name) {
128
+ return (this.variables.get(name)
129
+ ?? this.parent?.getVariableSSA(name));
130
+ }
100
131
  introduceFuncParams(params) {
101
132
  for (const param of params) {
102
133
  param.isConst = true;
@@ -104,9 +135,13 @@ export class ExpressifyCtx {
104
135
  }
105
136
  }
106
137
  introduceLettedConstant(name, lettedExpr, declRange) {
138
+ // if( this.lettedConstants.has( name ) ) throw new Error(`constant '${name}' already introduced in the context`);
139
+ const existing = this.lettedConstants.get(name);
140
+ if (existing)
141
+ return existing.clone();
107
142
  const result = new TirLettedExpr(name, lettedExpr, declRange);
108
143
  this.lettedConstants.set(name, result);
109
- return result.unsafeClone();
144
+ return result.clone();
110
145
  }
111
146
  introduceSopConstrFieldsAsProperties(sopName, destructuredPattern) {
112
147
  const fieldsMap = new Map();
@@ -130,7 +165,7 @@ export class ExpressifyCtx {
130
165
  if (constr.fields.length === 0)
131
166
  return; // no fields to extract
132
167
  const lettedRawFieldsName = getUniqueInternalName(`${varName}_fields`);
133
- const lettedFields = this.introduceLettedConstant(lettedRawFieldsName, new TirCallExpr(TirNativeFunc.unConstrDataResultFields, [new TirCallExpr(TirNativeFunc.unConstrData, [structExpr], new TirUnConstrDataResultT(), structExpr.range)], new TirListT(data_t), structExpr.range), structExpr.range);
168
+ const lettedFields = this.introduceLettedConstant(lettedRawFieldsName, new TirLettedExpr(lettedRawFieldsName, new TirCallExpr(TirNativeFunc.unConstrDataResultFields, [new TirCallExpr(TirNativeFunc.unConstrData, [structExpr], new TirUnConstrDataResultT(), structExpr.range)], new TirListT(data_t), structExpr.range), structExpr.range), structExpr.range);
134
169
  const fieldsMap = new Map();
135
170
  for (let i = 0; i < constr.fields.length; i++) {
136
171
  const field = constr.fields[i];
@@ -150,25 +185,26 @@ export class ExpressifyCtx {
150
185
  }
151
186
  this.properties.set(varName, fieldsMap);
152
187
  }
153
- introduceDeconstrDataLettedFields(stmt) {
188
+ introduceDeconstrDataLettedFields(stmt, lettedInitExpr) {
154
189
  const structType = stmt.type;
155
190
  if (!(stmt.initExpr))
156
191
  throw new Error("expected init expr in deconstruct data statement");
157
192
  if (!(structType instanceof TirDataStructType))
158
193
  throw new Error("expected data struct type in deconstruct data statement");
159
- const varName = getUniqueInternalName(structType.toString().toLowerCase());
194
+ const varName = lettedInitExpr.varName ?? getUniqueInternalName(structType.toString().toLowerCase());
160
195
  const constrName = stmt.constrName;
161
196
  const constrIdx = structType.constructors.findIndex(c => c.name === constrName);
162
197
  if (constrIdx < 0)
163
198
  throw new Error(`constructor ${constrName} not found in data struct ${structType.name}`);
164
199
  const constr = structType.constructors[constrIdx];
165
200
  if (constr.fields.length === 0)
166
- return []; // no fields to extract, no assertions
167
- const structExpr = stmt.initExpr;
201
+ return { implicitAssertions: [], nestedDeconstructs: [] }; // no fields to extract, no assertions
202
+ const structExpr = lettedInitExpr ?? stmt.initExpr;
168
203
  const lettedUnconstrName = getUniqueInternalName(`${varName}_unconstrPair`);
169
- const lettedUnconstr = this.introduceLettedConstant(lettedUnconstrName, new TirCallExpr(TirNativeFunc.unBData, [stmt.initExpr], new TirUnConstrDataResultT(), stmt.range), stmt.range);
204
+ const lettedUnconstr = this.introduceLettedConstant(lettedUnconstrName, new TirCallExpr(TirNativeFunc.unConstrData, [structExpr], new TirUnConstrDataResultT(), stmt.range), stmt.range);
170
205
  const assertions = [];
171
- if (structType.constructors.length > 0)
206
+ const nestedDeconstructs = [];
207
+ if (structType.constructors.length > 1)
172
208
  assertions.push(new TirAssertStmt(new TirEqualExpr(new TirCallExpr(TirNativeFunc.unConstrDataResultIndex, [lettedUnconstr], int_t, stmt.range), new TirLitIntExpr(BigInt(constrIdx), stmt.range), stmt.range), undefined, // no trace message
173
209
  stmt.range));
174
210
  const lettedRawFieldsName = getUniqueInternalName(`${varName}_fields`);
@@ -179,7 +215,7 @@ export class ExpressifyCtx {
179
215
  for (let i = 0; i < constr.fields.length; i++) {
180
216
  const field = constr.fields[i];
181
217
  const fieldName = field.name;
182
- const fieldType = field.type;
218
+ let fieldType = field.type;
183
219
  const isFieldToIntroduce = fieldsToIntroduce.includes(fieldName);
184
220
  if (!hasRest && !isFieldToIntroduce)
185
221
  continue; // skip unused field
@@ -196,34 +232,20 @@ export class ExpressifyCtx {
196
232
  if (!fieldVarDecl)
197
233
  throw new Error(`field '${fieldName}' not found in deconstruct data statement`);
198
234
  if (fieldVarDecl instanceof TirSimpleVarDecl) {
199
- this.variables.set(fieldVarDecl.name, { latestName: lettedField.varName });
200
- }
201
- // data structs cannot have sop-encoded fields (thanks god)
202
- // recursively introduce letted fields for nested single-constructor data structs
203
- if (!(fieldType instanceof TirDataStructType))
204
- continue;
205
- // if the field is simply introduced but still a single constr struct
206
- // we need to introduce its fields as well
207
- if (fieldVarDecl instanceof TirSimpleVarDecl
208
- && fieldType.constructors.length === 1) {
209
- this.introduceSingleConstrDataLettedFields(fieldVarName, lettedField, fieldType);
210
- }
211
- else if (fieldVarDecl instanceof TirNamedDeconstructVarDecl
212
- || fieldVarDecl instanceof TirSingleDeconstructVarDecl) {
213
- // if the field is destructured, we need to introduce its fields as well
214
- const namedVarDecl = toNamedDeconstructVarDecl(fieldVarDecl);
215
- namedVarDecl.initExpr = lettedField;
216
- assertions.push(...this.introduceDeconstrDataLettedFields(namedVarDecl));
235
+ this.setNewVariableName(fieldVarDecl.name, lettedField.varName);
217
236
  }
218
- else if (fieldVarDecl instanceof TirArrayLikeDeconstr) {
237
+ else {
238
+ // fieldVarDecl.type ??= fieldType;
219
239
  fieldVarDecl.initExpr = lettedField;
220
- assertions.push(...this.introduceArrayDeconstr(fieldVarDecl, true // is destructured field
221
- ));
240
+ nestedDeconstructs.push(fieldVarDecl);
222
241
  }
223
242
  }
224
243
  if (hasRest)
225
244
  this.properties.set(stmt.rest, fieldsMap);
226
- return assertions;
245
+ return {
246
+ implicitAssertions: assertions,
247
+ nestedDeconstructs
248
+ };
227
249
  }
228
250
  introduceArrayDeconstr(stmt, isDestructuredField = false) {
229
251
  if (!(stmt.initExpr))
@@ -240,6 +262,7 @@ export class ExpressifyCtx {
240
262
  lettedArrayExpr = this.introduceLettedConstant(lettedArrayName, stmt.initExpr, stmt.range);
241
263
  }
242
264
  const assertions = [];
265
+ const nestedDeconstructs = [];
243
266
  const nElems = stmt.elements.length;
244
267
  for (let i = 0; i < nElems; i++) {
245
268
  const elemVarDecl = stmt.elements[i];
@@ -249,7 +272,7 @@ export class ExpressifyCtx {
249
272
  lettedExpr = new TirFromDataExpr(lettedExpr, elemVarDecl.type, elemVarDecl.range);
250
273
  const lettedElem = this.introduceLettedConstant(lettedElemName, lettedExpr, elemVarDecl.range);
251
274
  if (elemVarDecl instanceof TirSimpleVarDecl) {
252
- this.variables.set(elemVarDecl.name, { latestName: lettedElem.varName });
275
+ this.setNewVariableName(elemVarDecl.name, lettedElem.varName);
253
276
  if (lettedElem.type instanceof TirDataStructType
254
277
  && lettedElem.type.constructors.length === 1) {
255
278
  // if the element is a single-constructor data struct,
@@ -261,12 +284,16 @@ export class ExpressifyCtx {
261
284
  || elemVarDecl instanceof TirSingleDeconstructVarDecl) {
262
285
  const namedVarDecl = toNamedDeconstructVarDecl(elemVarDecl);
263
286
  namedVarDecl.initExpr = lettedElem;
264
- assertions.push(...this.introduceDeconstrDataLettedFields(namedVarDecl));
287
+ const introduceFieldsResult = this.introduceDeconstrDataLettedFields(namedVarDecl, lettedElem);
288
+ assertions.push(...introduceFieldsResult.implicitAssertions);
289
+ nestedDeconstructs.push(...introduceFieldsResult.nestedDeconstructs);
265
290
  }
266
291
  else if (elemVarDecl instanceof TirArrayLikeDeconstr) {
267
292
  elemVarDecl.initExpr = lettedElem;
268
- assertions.push(...this.introduceArrayDeconstr(elemVarDecl, true // is destructured field
269
- ));
293
+ const introduceFieldsResult = this.introduceArrayDeconstr(elemVarDecl, true // is destructured field
294
+ );
295
+ assertions.push(...introduceFieldsResult.implicitAssertions);
296
+ nestedDeconstructs.push(...introduceFieldsResult.nestedDeconstructs);
270
297
  }
271
298
  }
272
299
  if (stmt.rest) {
@@ -277,6 +304,9 @@ export class ExpressifyCtx {
277
304
  lettedArrayExpr
278
305
  ], new TirListT(elemsType), stmt.range), stmt.range);
279
306
  }
280
- return assertions;
307
+ return {
308
+ implicitAssertions: assertions,
309
+ nestedDeconstructs
310
+ };
281
311
  }
282
312
  }
@@ -34,6 +34,7 @@ export function determineReassignedVariablesAndReturn(stmt) {
34
34
  returns = true;
35
35
  continue;
36
36
  }
37
+ // TODO: can optimize list iterations
37
38
  if (stmt instanceof TirAssignmentStmt) {
38
39
  reassignedSet.add(stmt.varIdentifier.varName);
39
40
  continue;
@@ -147,6 +148,8 @@ export function determineReassignedVariablesAndFlowInfos(stmt) {
147
148
  continue;
148
149
  }
149
150
  const tsEnsureExsaustiveCheck = stmt;
151
+ console.error("unexpected", stmt);
152
+ throw new Error("unexpected statement type");
150
153
  }
151
154
  let reassigned = [...reassignedSet].sort();
152
155
  reassigned = keepSortedStrArrInplace(reassigned, originalStmtDeps);
@@ -235,8 +238,16 @@ export function getBodyStateType({ sop, initState }, stmt) {
235
238
  for (const { name, type, range, initExpr, isConst } of stmt.init) {
236
239
  if (!initExpr)
237
240
  throw new Error("loop init variable requires initialization expression");
238
- // TODO: optimize for `isConst`
239
- const uniqueFieldName = getUniqueInternalName(name);
241
+ // using `getUniqueInternalName` here messes up variable access later
242
+ // due to how for statements are expressified (see piece of code from expressifyForStmt)
243
+ // ```
244
+ // // define loop function parameters
245
+ // for( const { name, type } of bodyStateType.constructors[0].fields ) {
246
+ // loopCompilationCtx.setFuncParam( name, type );
247
+ // }
248
+ // ```
249
+ // the introduced vars are taken from the bodyStateType that we derive here
250
+ const uniqueFieldName = name; // getUniqueInternalName( name );
240
251
  bodyStateConstr.fields.push(new TirStructField(uniqueFieldName, type));
241
252
  initState.fieldNames.push(new Identifier(uniqueFieldName, range));
242
253
  initState.values.push(initExpr);
@@ -6,7 +6,8 @@ import { TirContinueStmt } from "../../tir/statements/TirContinueStmt.js";
6
6
  import { TirReturnStmt } from "../../tir/statements/TirReturnStmt.js";
7
7
  import { TirStmt } from "../../tir/statements/TirStmt.js";
8
8
  import { ExpressifyCtx } from "./ExpressifyCtx.js";
9
- export declare function expressify(func: TirFuncExpr, loopReplacements: LoopReplacements | undefined, parentCtx?: ExpressifyCtx | undefined): void;
9
+ import { TypedProgram } from "../../tir/program/TypedProgram.js";
10
+ export declare function expressify(func: TirFuncExpr, loopReplacements: LoopReplacements | undefined, program: TypedProgram, parentCtx?: ExpressifyCtx | undefined): void;
10
11
  export interface LoopReplacements {
11
12
  readonly replaceReturnValue: (ctx: ExpressifyCtx, stmt: TirReturnStmt) => TirExpr;
12
13
  readonly compileBreak: (ctx: ExpressifyCtx, stmt: TirBreakStmt) => TirExpr;
@@ -33,14 +33,14 @@ import { expressifyTerminatingIfStmt } from "./expressifyTerminatingIfStmt.js";
33
33
  import { determineReassignedVariablesAndFlowInfos, determineReassignedVariablesAndReturn, getBodyStateType, getBranchStmtReturnType } from "./determineReassignedVariablesAndReturn.js";
34
34
  import { TirTernaryExpr } from "../../tir/expressions/TirTernaryExpr.js";
35
35
  import { expressifyIfBranch } from "./expressifyIfBranch.js";
36
- import { expressifyForStmt, whileToFor } from "./expressifyForStmt.js";
36
+ import { expressifyForStmt, loopToForStmt } from "./expressifyForStmt.js";
37
37
  import { getListTypeArg } from "../../tir/types/utils/getListTypeArg.js";
38
38
  import { TirElemAccessExpr } from "../../tir/expressions/TirElemAccessExpr.js";
39
39
  import { TirLitIntExpr } from "../../tir/expressions/litteral/TirLitIntExpr.js";
40
40
  import { TirCallExpr } from "../../tir/expressions/TirCallExpr.js";
41
41
  import { TirNativeFunc } from "../../tir/expressions/TirNativeFunc.js";
42
- export function expressify(func, loopReplacements, parentCtx = undefined) {
43
- const ctx = new ExpressifyCtx(parentCtx, func.returnType);
42
+ export function expressify(func, loopReplacements, program, parentCtx = undefined) {
43
+ const ctx = new ExpressifyCtx(parentCtx, func.returnType, program);
44
44
  ctx.introduceFuncParams(func.params);
45
45
  func.body.stmts = [
46
46
  new TirReturnStmt(expressifyFuncBody(ctx, func.body.stmts, loopReplacements), func.body.range)
@@ -52,6 +52,9 @@ loopReplacements, assertions = []) {
52
52
  bodyStmts = bodyStmts.slice();
53
53
  let stmt;
54
54
  while (stmt = bodyStmts.shift()) {
55
+ // const isEdgeCase = stmt instanceof TirForStmt || stmt instanceof TirForOfStmt || stmt instanceof TirWhileStmt;
56
+ // if( isEdgeCase ) console.log([stmt, ...bodyStmts].map(s => s.toString()));
57
+ // if( isEdgeCase ) console.log( stmt );
55
58
  if (stmt instanceof TirBreakStmt) {
56
59
  if (typeof loopReplacements?.compileBreak !== "function")
57
60
  throw new Error("break statement in function body.");
@@ -71,8 +74,9 @@ loopReplacements, assertions = []) {
71
74
  stmt.value = modifiedExpr;
72
75
  return TirAssertAndContinueExpr.fromStmtsAndContinuation(assertions, modifiedExpr);
73
76
  }
74
- else
77
+ else {
75
78
  return TirAssertAndContinueExpr.fromStmtsAndContinuation(assertions, new TirLitVoidExpr(stmt.range));
79
+ }
76
80
  }
77
81
  // if( isTirVarDecl( stmt ) ) expressifyVarDecl( ctx, stmt );
78
82
  else if (stmt instanceof TirSimpleVarDecl) {
@@ -80,7 +84,10 @@ loopReplacements, assertions = []) {
80
84
  throw new Error("simple var decl without init expr");
81
85
  const initExpr = expressifyVars(ctx, stmt.initExpr);
82
86
  stmt.initExpr = initExpr;
83
- const lettedExpr = ctx.introduceLettedConstant(stmt.name, initExpr, stmt.range);
87
+ const lettedExpr = initExpr instanceof TirLettedExpr ? initExpr : ctx.introduceLettedConstant(stmt.name, initExpr, stmt.range);
88
+ if (!stmt.isConst) {
89
+ ctx.setNewVariableName(stmt.name, lettedExpr.varName);
90
+ }
84
91
  if (!isSingleConstrStruct(stmt.type))
85
92
  continue;
86
93
  if ((initExpr instanceof TirVariableAccessExpr || initExpr instanceof TirLettedExpr)
@@ -128,9 +135,12 @@ loopReplacements, assertions = []) {
128
135
  .concat(bodyStmts), loopReplacements), stmt.range)], new TirWildcardCaseMatcher(new TirFailExpr(undefined, ctx.returnType, stmt.range), stmt.range), ctx.returnType, stmt.range));
129
136
  }
130
137
  else if (stmt.type instanceof TirDataStructType) {
131
- assertions.push(...ctx.introduceDeconstrDataLettedFields(toNamedDeconstructVarDecl(stmt)));
138
+ const { implicitAssertions, nestedDeconstructs } = ctx.introduceDeconstrDataLettedFields(stmt, lettedExpr);
139
+ assertions.push(...implicitAssertions);
140
+ bodyStmts.unshift(...nestedDeconstructs);
132
141
  continue;
133
142
  }
143
+ throw new Error("deconstruction of non-struct type" + stmt.type.toString());
134
144
  }
135
145
  else if (stmt instanceof TirArrayLikeDeconstr) {
136
146
  if (!stmt.initExpr)
@@ -175,8 +185,21 @@ loopReplacements, assertions = []) {
175
185
  bodyStmts.push(...nextDeclarations);
176
186
  continue;
177
187
  }
178
- else if (stmt instanceof TirAssignmentStmt)
179
- bodyStmts.unshift(expressifyVarAssignmentStmt(ctx, stmt));
188
+ else if (stmt instanceof TirAssignmentStmt) {
189
+ void expressifyVarAssignmentStmt(ctx, stmt);
190
+ // !!! IMPORTANT BUG !!!
191
+ // .unshift the resulting `TirSimpleVarDecl` into bodyStmts
192
+ // will cause the assigned expression to be re-expressified
193
+ // meaning that variables that depend on themselves (e.g. `x = x + 1`) will be processed twice
194
+ //
195
+ // x = (x + 1) --> const x_i = (x + 1) --> const x_i = (x_i/*x + 1*/ */ + 1) (??? BUG ???)
196
+ //
197
+ // so ultimately `x = x + 1` would be compiled as `x = x + 1 + 1`
198
+ //
199
+ // const constDecl = expressifyVarAssignmentStmt( ctx, stmt );
200
+ // bodyStmts.unshift( constDecl );
201
+ continue;
202
+ }
180
203
  else if (stmt instanceof TirBlockStmt) {
181
204
  // inline the block
182
205
  bodyStmts = stmt.stmts.concat(bodyStmts);
@@ -188,8 +211,9 @@ loopReplacements, assertions = []) {
188
211
  stmt.failMsgExpr = modifiedExpr;
189
212
  return TirAssertAndContinueExpr.fromStmtsAndContinuation(assertions, new TirFailExpr(modifiedExpr, ctx.returnType, stmt.range));
190
213
  }
191
- else
214
+ else {
192
215
  return TirAssertAndContinueExpr.fromStmtsAndContinuation(assertions, new TirFailExpr(undefined, ctx.returnType, stmt.range));
216
+ }
193
217
  }
194
218
  else if (stmt instanceof TirAssertStmt) {
195
219
  const condition = expressifyVars(ctx, stmt.condition);
@@ -239,7 +263,7 @@ loopReplacements, assertions = []) {
239
263
  const stmtExpr = new TirTernaryExpr(condition, expressifyIfBranch(ctx.newChild(), stmt.thenBranch, reassignsAndReturns.reassigned, sop, loopReplacements), stmt.elseBranch ? expressifyIfBranch(ctx.newChild(), stmt.elseBranch, reassignsAndReturns.reassigned, sop, loopReplacements) : initState, // no else branch means the variables stay unchanged
240
264
  sop, stmt.range);
241
265
  // expressify as ternary that returns the SoP type
242
- return TirAssertAndContinueExpr.fromStmtsAndContinuation(assertions, getFinalStmtCaseExpr(stmtExpr, sop, ctx, stmt.range, reassignsAndReturns, bodyStmts, loopReplacements));
266
+ return TirAssertAndContinueExpr.fromStmtsAndContinuation(assertions, wrapNonTerminatingFinalStmtAsCaseExpr(stmtExpr, sop, ctx, stmt.range, reassignsAndReturns, bodyStmts, loopReplacements));
243
267
  }
244
268
  else if (stmt instanceof TirMatchStmt) {
245
269
  /**
@@ -272,9 +296,8 @@ loopReplacements, assertions = []) {
272
296
  const reassignsAndReturns = determineReassignedVariablesAndReturn(stmt);
273
297
  if (isDirectReturn) {
274
298
  // build a SoP type to return
275
- const { sop, initState } = getBranchStmtReturnType(reassignsAndReturns, ctx, stmt.range);
276
- // expressify as ternary that returns the SoP type
277
- return TirAssertAndContinueExpr.fromStmtsAndContinuation(assertions, getFinalStmtCaseExpr(new TirCaseExpr(expressifyVars(ctx, stmt.matchExpr), stmt.cases.map(_case => {
299
+ // const { sop, initState } = getBranchStmtReturnType( reassignsAndReturns, ctx, stmt.range );
300
+ const finalExpression = new TirCaseExpr(expressifyVars(ctx, stmt.matchExpr), stmt.cases.map(_case => {
278
301
  if (_case.pattern instanceof TirArrayLikeDeconstr)
279
302
  throw new Error("array-like deconstruction in match statement is not supported");
280
303
  _case.pattern = toNamedDeconstructVarDecl(_case.pattern);
@@ -282,43 +305,79 @@ loopReplacements, assertions = []) {
282
305
  flattenSopNamedDeconstructInplace_addTopDestructToCtx_getNestedDeconstruct(_case.pattern, caseCtx);
283
306
  const caseBody = expressifyFuncBody(caseCtx, _case.body instanceof TirBlockStmt
284
307
  ? _case.body.stmts
285
- : [_case.body], loopReplacements, assertions);
308
+ : [_case.body], loopReplacements, []);
286
309
  return new TirCaseMatcher(_case.pattern, caseBody, _case.range);
287
310
  }), stmt.wildcardCase ? new TirWildcardCaseMatcher(expressifyFuncBody(ctx.newChild(), stmt.wildcardCase.body instanceof TirBlockStmt
288
311
  ? stmt.wildcardCase.body.stmts
289
- : [stmt.wildcardCase.body], loopReplacements, []), stmt.wildcardCase.range) : undefined, ctx.returnType, stmt.range), sop, ctx, stmt.range, reassignsAndReturns, bodyStmts, loopReplacements));
290
- }
291
- }
292
- else if (stmt instanceof TirForOfStmt) {
293
- // determine affected variables
294
- // determine if we may have an early return
295
- // determine if we can break or continue
296
- const reassignedAndFlow = determineReassignedVariablesAndFlowInfos(stmt);
297
- const { reassigned, returns, canBreak, canContinue } = reassignedAndFlow;
298
- const { sop, initState } = getBranchStmtReturnType(reassignedAndFlow, ctx, stmt.range);
299
- if (!returns
300
- && !canBreak
301
- // && !canContinue
302
- ) {
303
- // **only for...of** can be optimized as a simple `.reduce`
304
- // producing the new state in this case
305
- // (`.reduce` has no way to break or early return (efficiently))
306
- // continue is ok, because we only need to pass the state up to that point
312
+ : [stmt.wildcardCase.body], loopReplacements, []), stmt.wildcardCase.range) : undefined, ctx.returnType, stmt.range);
313
+ // expressify as ternary that returns the SoP type
314
+ return TirAssertAndContinueExpr.fromStmtsAndContinuation(assertions,
315
+ // isDirectReturn === true, so we don't need to wrap
316
+ finalExpression
317
+ /*
318
+ true ? finalExpression : wrapNonTerminatingFinalStmtAsCaseExpr(
319
+ finalExpression,
320
+ sop,
321
+ ctx,
322
+ stmt.range,
323
+ reassignsAndReturns,
324
+ bodyStmts,
325
+ loopReplacements
326
+ )
327
+ //*/
328
+ );
307
329
  }
330
+ throw new Error("match statement with multiple non-terminating cases is not implemented yet (sorry)");
308
331
  }
332
+ // else if( stmt instanceof TirForOfStmt ) {
333
+ //
334
+ // const definitelyTerminates = stmt.definitelyTerminates();
335
+ //
336
+ // // determine affected variables
337
+ // // determine if we may have an early return
338
+ // // determine if we can break or continue
339
+ // const reassignedAndFlow = determineReassignedVariablesAndFlowInfos( stmt );
340
+ // const {
341
+ // reassigned,
342
+ // returns,
343
+ // canBreak,
344
+ // canContinue
345
+ // } = reassignedAndFlow;
346
+ // const { sop, initState } = getBranchStmtReturnType( reassignedAndFlow, ctx, stmt.range );
347
+ //
348
+ // if(
349
+ // !returns
350
+ // && !canBreak
351
+ // // && !canContinue
352
+ // ) {
353
+ // // **only for...of** can be optimized as a simple `.reduce`
354
+ // // producing the new state in this case
355
+ // // (`.reduce` has no way to break or early return (efficiently))
356
+ // // continue is ok, because we only need to pass the state up to that point
357
+ // }
358
+ //
359
+ // }
309
360
  else if (stmt instanceof TirForStmt
310
- || stmt instanceof TirWhileStmt) {
361
+ || stmt instanceof TirWhileStmt
362
+ || stmt instanceof TirForOfStmt) {
363
+ const definitelyTerminates = stmt.definitelyTerminates();
311
364
  const reassignedAndFlow = determineReassignedVariablesAndFlowInfos(stmt);
312
365
  const returnTypeAndInvalidInit = getBranchStmtReturnType(reassignedAndFlow, ctx, stmt.range);
313
- const forStmt = whileToFor(stmt);
366
+ const forStmt = loopToForStmt(stmt);
314
367
  const { bodyStateType, initState } = getBodyStateType(returnTypeAndInvalidInit, forStmt);
315
- const loopExpr = expressifyForStmt(ctx.newChild(), forStmt, returnTypeAndInvalidInit.sop, bodyStateType, initState);
368
+ const loopExprCtx = ctx.newChild();
369
+ const loopExpr = expressifyForStmt(loopExprCtx, forStmt, returnTypeAndInvalidInit.sop, bodyStateType, initState);
370
+ const result = TirAssertAndContinueExpr.fromStmtsAndContinuation(assertions, definitelyTerminates ? loopExpr : wrapNonTerminatingFinalStmtAsCaseExpr(loopExpr, returnTypeAndInvalidInit.sop, ctx, stmt.range, reassignedAndFlow, bodyStmts, loopReplacements));
371
+ return result;
316
372
  }
317
373
  else {
318
- const tsEnsureExsautstiveCheck = stmt;
319
- console.error(stmt);
320
- throw new Error("unreachable::expressify::stmt");
374
+ // const tsEnsureExhautstiveCheck: never = stmt;
375
+ // console.error( stmt );
376
+ // throw new Error("unreachable::expressify::stmt");
321
377
  }
378
+ const tsEnsureExhautstiveCheck = stmt;
379
+ console.error(stmt);
380
+ throw new Error("unreachable::expressify::stmt");
322
381
  }
323
382
  return TirAssertAndContinueExpr.fromStmtsAndContinuation(assertions, new TirLitVoidExpr(SourceRange.mock));
324
383
  }
@@ -355,7 +414,8 @@ function getNestedDestructsInSingleSopDestructPattern(pattern) {
355
414
  }
356
415
  return result;
357
416
  }
358
- function getFinalStmtCaseExpr(finalStmtExpr, sop, ctx, stmtRange, reassignsAndReturns, nextBodyStmts, loopReplacements) {
417
+ function wrapNonTerminatingFinalStmtAsCaseExpr(finalStmtExpr, sop, ctx, stmtRange, reassignsAndReturns, nextBodyStmts, loopReplacements) {
418
+ nextBodyStmts = nextBodyStmts.slice();
359
419
  const continuations = [];
360
420
  const contBranchCtx = ctx.newChild();
361
421
  const contConstr = sop.constructors[0];
@@ -370,9 +430,10 @@ function getFinalStmtCaseExpr(finalStmtExpr, sop, ctx, stmtRange, reassignsAndRe
370
430
  false, // not a constant
371
431
  stmtRange);
372
432
  const nestedDeconstructs = flattenSopNamedDeconstructInplace_addTopDestructToCtx_getNestedDeconstruct(contPattern, contBranchCtx);
373
- continuations.push(new TirCaseMatcher(contPattern, expressifyFuncBody(contBranchCtx, nestedDeconstructs
433
+ const mainContinuaiton = expressifyFuncBody(contBranchCtx, nestedDeconstructs
374
434
  .concat(nextBodyStmts), loopReplacements, [] // assertions are added before if statement exectution
375
- ), stmtRange));
435
+ );
436
+ continuations.push(new TirCaseMatcher(contPattern, mainContinuaiton, stmtRange));
376
437
  if (reassignsAndReturns.returns) {
377
438
  const earlyRetConstr = sop.constructors[1];
378
439
  const earlyRetField = earlyRetConstr.fields[0];
@@ -1,8 +1,9 @@
1
1
  import { TirLitNamedObjExpr } from "../../tir/expressions/litteral/TirLitNamedObjExpr.js";
2
2
  import { TirCallExpr } from "../../tir/expressions/TirCallExpr.js";
3
+ import { TirForOfStmt } from "../../tir/statements/TirForOfStmt.js";
3
4
  import { TirForStmt } from "../../tir/statements/TirForStmt.js";
4
5
  import { TirWhileStmt } from "../../tir/statements/TirWhileStmt.js";
5
6
  import { TirSoPStructType } from "../../tir/types/TirStructType.js";
6
7
  import { ExpressifyCtx } from "./ExpressifyCtx.js";
7
- export declare function whileToFor(stmt: TirWhileStmt | TirForStmt): TirForStmt;
8
+ export declare function loopToForStmt(stmt: TirWhileStmt | TirForOfStmt | TirForStmt): TirForStmt;
8
9
  export declare function expressifyForStmt(ctx: ExpressifyCtx, stmt: TirForStmt, returnType: TirSoPStructType, bodyStateType: TirSoPStructType, initState: TirLitNamedObjExpr): TirCallExpr;