@yeyelailou/co 0.0.3 → 0.0.5

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 (112) hide show
  1. package/README.md +261 -28
  2. package/bin//345/205/245/345/217/243.js +2 -9
  3. package/install.sh +0 -0
  4. package/package.json +5 -6
  5. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//346/234/272/345/231/250/347/240/201/347/224/237/346/210/220/345/231/250.js +2520 -229
  6. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//350/257/215/346/263/225/345/210/206/346/236/220/345/231/250.js +1 -1
  7. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//350/257/255/346/263/225/345/210/206/346/236/220/345/231/250.js +106 -3
  8. package//345/267/245/345/205/267//350/207/252/344/270/276/347/274/226/350/257/221/345/231/250//346/234/272/345/231/250/347/240/201/347/224/237/346/210/220/345/231/250.co +21 -1
  9. package//345/267/245/345/205/267//350/207/252/344/270/276/347/274/226/350/257/221/345/231/250//350/207/252/344/270/276/347/274/226/350/257/221.sh +74 -0
  10. package//345/267/245/345/205/267//350/207/252/344/270/276/347/274/226/350/257/221/345/231/250//350/207/252/344/270/276/351/252/214/350/257/201//347/274/226/350/257/221/351/251/261/345/212/250.co +3 -1
  11. package//345/267/245/345/205/267//350/207/252/344/270/276/347/274/226/350/257/221/345/231/250//350/207/252/344/270/276/351/252/214/350/257/201//347/274/226/350/257/221/351/252/214/350/257/201.sh +24 -8
  12. package/bin/install.js +0 -276
  13. package//345/267/245/345/205/267/lsp/server.js +0 -419
  14. package//345/267/245/345/205/267/lsp//346/265/213/350/257/225.sh +0 -81
  15. package//345/267/245/345/205/267/vscode-cocn/extension.js +0 -203
  16. package//345/267/245/345/205/267/vscode-cocn/package.json +0 -58
  17. package//345/267/245/345/205/267/vscode-cocn/syntaxes/coCN.tmLanguage.json +0 -130
  18. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/273/243/347/240/201/347/224/237/346/210/220/345/231/250.js +0 -2284
  19. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/analysis.js +0 -116
  20. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/benchmark_demo.js +0 -75
  21. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/dominance.js +0 -158
  22. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/gvn.js +0 -45
  23. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/index.js +0 -121
  24. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/ir.js +0 -237
  25. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/passes/constProp.js +0 -126
  26. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/passes/copyProp.js +0 -57
  27. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/passes/cse.js +0 -62
  28. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/passes/dce.js +0 -68
  29. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/passes/devirt.js +0 -38
  30. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/passes/escape.js +0 -67
  31. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/passes/inline.js +0 -142
  32. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/passes/loopOpt.js +0 -215
  33. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/peephole.js +0 -42
  34. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/regalloc.js +0 -212
  35. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/scheduler.js +0 -86
  36. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/spill.js +0 -78
  37. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/ssa.js +0 -235
  38. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/traceSched.js +0 -47
  39. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/unroll.js +0 -46
  40. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250//346/265/213/350/257/225/harness.js +0 -33
  41. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250//346/265/213/350/257/225/run_all.js +0 -11
  42. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250//346/265/213/350/257/225/test_constfold.js +0 -58
  43. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250//346/265/213/350/257/225/test_dce.js +0 -49
  44. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250//346/265/213/350/257/225/test_e2e.js +0 -57
  45. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250//346/265/213/350/257/225/test_gvn_spill.js +0 -45
  46. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250//346/265/213/350/257/225/test_loop.js +0 -46
  47. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250//346/265/213/350/257/225/test_regalloc.js +0 -51
  48. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250//346/265/213/350/257/225/test_ssa.js +0 -65
  49. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//345/205/245/345/217/243.c +0 -9
  50. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//345/205/245/345/217/243.js +0 -38
  51. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237/AST/345/267/245/345/205/267.js +0 -126
  52. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237/index.js +0 -103
  53. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//345/206/205/350/201/224.js +0 -194
  54. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//345/207/275/346/225/260/347/211/271/345/214/226.js +0 -168
  55. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//346/243/200/346/237/245/346/266/210/351/231/244.js +0 -166
  56. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//346/265/213/350/257/225//345/206/205/350/201/224/346/265/213/350/257/225.js +0 -53
  57. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//346/265/213/350/257/225//345/207/275/346/225/260/347/211/271/345/214/226/346/265/213/350/257/225.js +0 -67
  58. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//346/265/213/350/257/225//347/253/257/345/210/260/347/253/257/346/265/213/350/257/225.js +0 -104
  59. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//346/265/213/350/257/225//347/261/273/345/236/213/346/216/250/346/226/255/346/265/213/350/257/225.js +0 -87
  60. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//346/265/213/350/257/225//350/277/220/350/241/214/346/265/213/350/257/225.js +0 -43
  61. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//346/265/213/350/257/225//351/200/203/351/200/270/345/210/206/346/236/220/346/265/213/350/257/225.js +0 -58
  62. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//347/211/271/345/214/226/347/274/223/345/255/230.js +0 -83
  63. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//347/216/257/345/242/203.js +0 -126
  64. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//347/261/273/345/236/213.js +0 -294
  65. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//347/261/273/345/236/213/346/216/250/346/226/255.js +0 -461
  66. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//350/260/203/347/224/250/345/233/276.js +0 -119
  67. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//351/200/203/351/200/270/345/210/206/346/236/220.js +0 -184
  68. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/274/226/350/257/221.sh +0 -45
  69. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//350/277/220/350/241/214/346/227/266/alloc.c +0 -206
  70. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//350/277/220/350/241/214/346/227/266/build.sh +0 -70
  71. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//350/277/220/350/241/214/346/227/266/dict.c +0 -143
  72. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//350/277/220/350/241/214/346/227/266/list.c +0 -148
  73. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//350/277/220/350/241/214/346/227/266/math.c +0 -129
  74. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//350/277/220/350/241/214/346/227/266/print.c +0 -135
  75. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//350/277/220/350/241/214/346/227/266/rt.h +0 -177
  76. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//350/277/220/350/241/214/346/227/266/string.c +0 -288
  77. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//350/277/220/350/241/214/346/227/266/syscall.asm +0 -153
  78. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//350/277/220/350/241/214/346/227/266/test.c +0 -228
  79. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//350/277/220/350/241/214/346/227/266//344/270/264/346/227/266.c +0 -379
  80. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//350/277/220/350/241/214/346/227/266.c +0 -441
  81. package//345/267/245/345/205/267//350/207/252/344/270/276/347/274/226/350/257/221/345/231/250//346/267/261/345/272/246/346/257/224/350/276/203.js +0 -51
  82. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/Empty.java +0 -1
  83. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.c +0 -53
  84. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.co +0 -42
  85. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.go +0 -43
  86. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.java +0 -29
  87. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.js +0 -24
  88. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.py +0 -36
  89. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.rs +0 -35
  90. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/empty.co +0 -0
  91. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/empty_c.c +0 -1
  92. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/empty_go.go +0 -2
  93. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/empty_rs.rs +0 -1
  94. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/257/271C/350/257/255/350/250/200_/346/213/274/346/216/245.c +0 -32
  95. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/257/271C/350/257/255/350/250/200_/346/213/274/346/216/245.co +0 -10
  96. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/257/271C/350/257/255/350/250/200_/346/216/222/345/272/217.c +0 -23
  97. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/257/271C/350/257/255/350/250/200_/346/216/222/345/272/217.co +0 -31
  98. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/257/271C/350/257/255/350/250/200_/346/226/220/346/263/242/351/202/243/345/245/221.c +0 -17
  99. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/257/271C/350/257/255/350/250/200_/346/226/220/346/263/242/351/202/243/345/245/221.co +0 -10
  100. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/257/271C/350/257/255/350/250/200_/346/226/220/346/263/242/351/202/243/345/245/22120.co +0 -10
  101. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/257/271C/350/257/255/350/250/200_/347/264/257/345/212/240.c +0 -14
  102. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/257/271C/350/257/255/350/250/200_/347/264/257/345/212/240.co +0 -10
  103. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/257/271C/350/257/255/350/250/200/345/257/271/346/257/224/347/273/223/346/236/234.txt +0 -164
  104. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/276/256/345/237/272/345/207/206/fib.c +0 -9
  105. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/276/256/345/237/272/345/207/206/fib.co +0 -2
  106. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/276/256/345/237/272/345/207/206/sum.c +0 -10
  107. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/276/256/345/237/272/345/207/206/sum.co +0 -3
  108. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/276/256/345/237/272/345/207/206//350/256/241/346/265/213.sh +0 -32
  109. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/276/256/345/237/272/345/207/206//350/256/241/346/265/213/347/273/223/346/236/234.txt +0 -7
  110. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//350/241/245/346/265/213.sh +0 -45
  111. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//350/256/241/346/265/213.sh +0 -163
  112. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//350/256/241/346/265/213/347/273/223/346/236/234.txt +0 -29
@@ -1,461 +0,0 @@
1
- // ============================================================
2
- // 类型推断.js —— 数据流类型推断引擎
3
- //
4
- // 输入:coCN AST(JSON)。
5
- // 输出:
6
- // 1) 每个表达式/变量节点就地标注 _类型(静态类型);
7
- // 2) 每个函数的【签名】(参数类型 + 返回类型);
8
- // 3) 全局类型表,供函数特化 / 内联 / 代码生成消费。
9
- //
10
- // 推断策略(自底向上 + 定点迭代):
11
- // - 字面量 -> 直接类型
12
- // - 变量声明 -> 从初始化表达式推断
13
- // - 赋值 -> 与既有类型 LUB;漂移则升级动态
14
- // - 二元/一元 -> 按运算符规则推导
15
- // - 函数调用 -> 用实参类型累积形参类型,返回值取函数已知返回类型
16
- // - 控制流汇合(如果分支 / 循环头)-> 逐变量 LUB 合并
17
- // - 函数间互相递归 -> 对所有函数做定点迭代直到签名收敛
18
- // ============================================================
19
-
20
- 'use strict';
21
-
22
- const T = require('./类型');
23
- const 环境 = require('./环境');
24
- const AU = require('./AST工具');
25
-
26
- class 类型推断 {
27
- /**
28
- * @param {object} ast 程序节点 {类型:'程序', 语句:[...]}
29
- */
30
- constructor(ast) {
31
- this.ast = ast;
32
- this.函数表 = AU.收集顶层函数(ast); // 名字 -> 函数节点
33
- /** 函数签名:名字 -> tFunc */
34
- this.签名 = {};
35
- /** 函数返回类型:名字 -> Type */
36
- this.返回类型 = {};
37
- /** 调用点记录:[{调用节点, 函数名, 实参类型:[Type]}] */
38
- this.调用点 = [];
39
- /** 全局(顶层)变量类型 */
40
- this.全局 = {};
41
- this.最大迭代 = 30;
42
- }
43
-
44
- // ---------- 入口:跑定点迭代直到收敛 ----------
45
- 分析() {
46
- // 1) 先把结构体/类字段登记进类型系统
47
- this.登记类型定义();
48
- // 2) 迭代分析所有函数 + 顶层体,直到签名与全局类型都不再变化
49
- let n = 0;
50
- while (n < this.最大迭代) {
51
- this._变了 = false;
52
- this.调用点 = []; // 每轮重记调用点(按节点引用去重)
53
- for (const [名字, fn] of Object.entries(this.函数表)) {
54
- this.分析一个函数(名字, fn);
55
- }
56
- this.分析顶层(); // 顶层调用也参与定点反馈
57
- n++;
58
- if (!this._变了) break;
59
- }
60
- this.迭代次数 = n;
61
- return this.结果();
62
- }
63
-
64
- 结果() {
65
- return {
66
- 签名: this.签名,
67
- 返回类型: this.返回类型,
68
- 调用点: this.调用点,
69
- 全局: this.全局,
70
- 迭代次数: this.迭代次数
71
- };
72
- }
73
-
74
- // ---------- 登记结构体/类/枚举 ----------
75
- 登记类型定义() {
76
- this.结构体 = {}; // 名字 -> {字段:[str]}
77
- this.类 = {}; // 名字 -> {父类, 字段:[声明], 方法:[fn]}
78
- AU.遍历(this.ast, (node) => {
79
- if (node.类型 === '结构体定义') {
80
- this.结构体[node.名字] = { 字段: node.字段 || [] };
81
- } else if (node.类型 === '类定义') {
82
- this.类[node.名字] = { 父类: node.父类, 字段: node.字段, 方法: node.方法 };
83
- }
84
- });
85
- }
86
-
87
- // ---------- 顶层程序体 ----------
88
- 分析顶层() {
89
- const env = new 环境();
90
- // 顶层函数本身在环境中可见(作为函数类型)
91
- for (const [名字, fn] of Object.entries(this.函数表)) {
92
- env.定义(名字, T.tFunc(名字, [], this.返回类型[名字] || T.bot()));
93
- }
94
- if (this.ast && Array.isArray(this.ast.语句)) {
95
- for (const st of this.ast.语句) this.分析语句(st, env);
96
- }
97
- // 导出全局变量
98
- for (const [k, v] of env.打平()) this.全局[k] = v;
99
- }
100
-
101
- // ---------- 分析单个函数体,更新其签名 ----------
102
- 分析一个函数(名字, fn) {
103
- const env = new 环境();
104
- const 形参 = fn.参数 || [];
105
- // 形参类型:取该函数在所有调用点累积的 LUB;若有标注则用标注
106
- const 形参类型 = 形参.map((p, i) => {
107
- const 标注 = fn.参数类型 && fn.参数类型[i];
108
- if (标注) return this.标注到类型(标注);
109
- // 累积自调用点:签名[名字].params[i]
110
- const sig = this.签名[名字];
111
- if (sig && sig.params[i]) return sig.params[i];
112
- return T.bot();
113
- });
114
- 形参.forEach((p, i) => env.定义(p, 形参类型[i]));
115
-
116
- const 返回 = this.分析程序节点(fn.体, env);
117
-
118
- // 返回类型累积 LUB(函数可能有多条 return)
119
- const 旧返回 = this.返回类型[名字] || T.bot();
120
- const 新返回 = T.lub(旧返回, 返回);
121
- if (!T.equal(旧返回, 新返回)) {
122
- this.返回类型[名字] = 新返回;
123
- this._变了 = true;
124
- }
125
- // 签名
126
- const 旧签名 = this.签名[名字];
127
- const 新签名 = T.tFunc(名字, 形参类型, 新返回);
128
- if (!旧签名 || !T.equal(旧签名, 新签名)) {
129
- this.签名[名字] = 新签名;
130
- this._变了 = true;
131
- }
132
- }
133
-
134
- // 把 coCN 类型标注关键字转成类型
135
- 标注到类型(标注) {
136
- switch (标注) {
137
- case '整数': return T.tInt();
138
- case '浮点': return T.tFloat();
139
- case '字符串': return T.tStr();
140
- case '布尔': return T.tBool();
141
- case '空': case '空值': return T.tNil();
142
- default: return T.tDyn();
143
- }
144
- }
145
-
146
- // ---------- 分析一个「程序」节点(语句列表包裹) ----------
147
- 分析程序节点(程序节点, env) {
148
- if (!程序节点) return T.bot();
149
- const 语句列表 = 程序节点.语句 || 程序节点; // 容忍直接传数组
150
- return this.分析语句列表(语句列表, env);
151
- }
152
-
153
- // ---------- 分析语句列表,返回最后一条表达式语句的类型 ----------
154
- 分析语句列表(列表, env) {
155
- let 末类型 = T.bot();
156
- if (!Array.isArray(列表)) return 末类型;
157
- for (const st of 列表) {
158
- if (!st || typeof st !== 'object') continue;
159
- const t = this.分析语句(st, env);
160
- if (t) 末类型 = t;
161
- }
162
- return 末类型;
163
- }
164
-
165
- // ---------- 语句分发 ----------
166
- 分析语句(st, env) {
167
- switch (st.类型) {
168
- case '变量声明': {
169
- let t = T.bot();
170
- if (st.初始化) t = this.分析表达式(st.初始化, env);
171
- if (st.类型标注) t = T.lub(t, this.标注到类型(st.类型标注));
172
- env.定义(st.名字, t);
173
- AU.标注类型(st, t);
174
- return null;
175
- }
176
- case '赋值': {
177
- const v = this.分析表达式(st.值, env);
178
- this.对目标赋值(st.目标, v, env);
179
- AU.标注类型(st, v);
180
- return v;
181
- }
182
- case '如果': return this.分析如果(st, env);
183
- case '当': return this.分析当(st, env);
184
- case '对于': return this.分析对于(st, env);
185
- case '返回': {
186
- const t = st.值 ? this.分析表达式(st.值, env) : T.tNil();
187
- AU.标注类型(st, t);
188
- // 记录到当前函数返回类型
189
- return t;
190
- }
191
- case '跳出': case '继续': return null;
192
- case '抛出': return this.分析表达式(st.值, env);
193
- case '尝试': {
194
- this.分析语句列表(st.体, env);
195
- if (st.捕获变量) env.定义(st.捕获变量, T.tDyn());
196
- this.分析语句列表(st.捕获体, env);
197
- this.分析语句列表(st.最终体, env);
198
- return null;
199
- }
200
- case '匹配': {
201
- const on = this.分析表达式(st.表达式, env);
202
- for (const br of (st.分支 || [])) this.分析语句列表(br.体, env);
203
- return on;
204
- }
205
- case '函数定义': case '函数字面量': {
206
- // 嵌套函数:登记进函数表(延迟分析,下一轮迭代覆盖)
207
- if (st.名字 && !this.函数表[st.名字]) this.函数表[st.名字] = st;
208
- env.定义(st.名字, T.tFunc(st.名字, [], T.bot()));
209
- return null;
210
- }
211
- case '结构体定义': case '类定义': case '枚举': case '导入': return null;
212
- default: {
213
- // 表达式语句
214
- if (st.被调者 || st.左 !== undefined || st.类型 === '标识符' ||
215
- st.类型 === '字面量' || st.类型 === '函数调用' || st.类型 === '下标') {
216
- return this.分析表达式(st, env);
217
- }
218
- return null;
219
- }
220
- }
221
- }
222
-
223
- // 赋值目标可能是 标识符 / 下标
224
- 对目标赋值(目标, v, env) {
225
- if (!目标) return;
226
- if (目标.类型 === '标识符') {
227
- env.赋值(目标.名字, v);
228
- AU.标注类型(目标, v);
229
- } else if (目标.类型 === '下标') {
230
- this.分析表达式(目标.容器, env);
231
- this.分析表达式(目标.索引, env);
232
- }
233
- }
234
-
235
- // ---------- 如果:分支汇合 LUB ----------
236
- 分析如果(st, env) {
237
- this.分析表达式(st.条件, env);
238
- const before = env.打平();
239
-
240
- // 收集所有分支结束后的视图
241
- const 分支视图 = [];
242
- const thenEnv = this.派生环境(env, before);
243
- this.分析语句列表(st.则体, thenEnv);
244
- 分支视图.push(thenEnv.打平());
245
-
246
- for (const br of (st.否则链 || [])) {
247
- if (br.条件) this.分析表达式(br.条件, env);
248
- const brEnv = this.派生环境(env, before);
249
- this.分析语句列表(br.体, brEnv);
250
- 分支视图.push(brEnv.打平());
251
- }
252
- // 没有最终 else 分支时,控制流也可能「跳过整个如果」-> 并入 before
253
- 分支视图.push(before);
254
-
255
- // 逐变量 LUB 合并回 env 顶层
256
- const 所有变量 = new Set();
257
- for (const v of 分支视图) for (const k of v.keys()) 所有变量.add(k);
258
- for (const k of 所有变量) {
259
- let t = T.bot();
260
- for (const v of 分支视图) t = T.lub(t, v.get(k) || T.bot());
261
- env.赋值(k, t);
262
- }
263
- return null;
264
- }
265
-
266
- // 从一个打平视图派生出独立环境(不共享父层可变表)
267
- 派生环境(env, 视图) {
268
- const e = new 环境(env.父);
269
- for (const [k, t] of 视图) e.表.set(k, t);
270
- return e;
271
- }
272
-
273
- // ---------- 当循环:迭代到收敛 ----------
274
- 分析当(st, env) {
275
- for (let i = 0; i < 10; i++) {
276
- const before = env.打平();
277
- this.分析表达式(st.条件, env);
278
- this.分析语句列表(st.体, env);
279
- const after = env.打平();
280
- if (视图相等(before, after)) break;
281
- }
282
- return null;
283
- }
284
-
285
- // ---------- 对于循环 ----------
286
- 分析对于(st, env) {
287
- if (st.是否遍历 === 1) {
288
- // for-each:x 于 list -> x 为 list.elem
289
- const 容器类型 = this.分析表达式(st.遍历源, env);
290
- let 元素 = T.bot();
291
- if (容器类型.k === 'list') 元素 = 容器类型.elem;
292
- else if (容器类型.k === 'str') 元素 = T.tStr();
293
- else 元素 = T.tDyn();
294
- env.定义(st.变量, 元素);
295
- AU.标注类型({ 类型: '标识符', 名字: st.变量 }, 元素);
296
- for (let i = 0; i < 8; i++) {
297
- const before = env.打平();
298
- this.分析语句列表(st.体, env);
299
- const after = env.打平();
300
- if (视图相等(before, after)) break;
301
- }
302
- } else {
303
- // 范围:对于 x 从 a 到 b -> x 为 lub(a,b)
304
- const a = this.分析表达式(st.起点, env);
305
- const b = this.分析表达式(st.终点, env);
306
- env.定义(st.变量, T.lub(a, b));
307
- if (st.步长) this.分析表达式(st.步长, env);
308
- for (let i = 0; i < 8; i++) {
309
- const before = env.打平();
310
- this.分析语句列表(st.体, env);
311
- const after = env.打平();
312
- if (视图相等(before, after)) break;
313
- }
314
- }
315
- return null;
316
- }
317
-
318
- // ---------- 表达式分发 ----------
319
- 分析表达式(node, env) {
320
- if (!node || typeof node !== 'object') return T.bot();
321
- let t;
322
- switch (node.类型) {
323
- case '字面量':
324
- t = T.fromLiteral(node.字面类型);
325
- break;
326
- case '标识符':
327
- t = env.查找(node.名字);
328
- break;
329
- case '二元': {
330
- const l = this.分析表达式(node.左, env);
331
- const r = this.分析表达式(node.右, env);
332
- t = T.binaryResult(node.运算符, l, r);
333
- break;
334
- }
335
- case '一元': {
336
- const o = this.分析表达式(node.操作数, env);
337
- t = (node.运算符 === '非') ? T.tBool() : (o.k === 'float' ? o : T.lub(o, T.tInt()));
338
- break;
339
- }
340
- case '函数调用':
341
- t = this.分析调用(node, env);
342
- break;
343
- case '下标': {
344
- const c = this.分析表达式(node.容器, env);
345
- this.分析表达式(node.索引, env);
346
- t = this.下标结果(c, node.索引);
347
- break;
348
- }
349
- case '列表字面量': {
350
- if (node.是否推导式 === 1) {
351
- // 列表推导式:元素类型 = 推导体类型
352
- const src = this.分析表达式(node.推导源, env);
353
- env.定义(node.迭代变量, src.k === 'list' ? src.elem : T.tDyn());
354
- if (node.过滤器) this.分析表达式(node.过滤器, env);
355
- const body = this.分析表达式(node.推导体, env);
356
- t = T.tList(body, false);
357
- } else {
358
- const elems = (node.元素 || []).map(e => this.分析表达式(e, env));
359
- const 合并 = elems.reduce((a, b) => T.lub(a, b), T.bot());
360
- t = T.tList(合并, elems.length > 0);
361
- }
362
- break;
363
- }
364
- case '字典字面量': {
365
- const ks = (node.键 || []).map(k => this.分析表达式(k, env));
366
- const vs = (node.值 || []).map(v => this.分析表达式(v, env));
367
- const k = ks.reduce((a, b) => T.lub(a, b), T.tStr());
368
- const v = vs.reduce((a, b) => T.lub(a, b), T.bot());
369
- t = T.tDict(k, v, (node.键 || []).length > 0);
370
- break;
371
- }
372
- case '元组字面量': {
373
- (node.元素 || []).map(e => this.分析表达式(e, env));
374
- t = T.tList(T.tDyn(), false, 'boxed[]');
375
- break;
376
- }
377
- case '范围': {
378
- this.分析表达式(node.起点, env);
379
- this.分析表达式(node.终点, env);
380
- t = T.tList(T.tInt(), true, 'int[]');
381
- break;
382
- }
383
- case '函数字面量': {
384
- t = T.tFunc(node.名字 || '', node.参数类型 || [], T.bot());
385
- break;
386
- }
387
- case '对象': {
388
- t = T.tObject(node.类名 || '?', {});
389
- break;
390
- }
391
- default:
392
- t = T.tDyn();
393
- }
394
- AU.标注类型(node, t);
395
- return t;
396
- }
397
-
398
- // 下标结果:list[i] -> elem;dict[k] -> val;obj.f -> 字段类型
399
- 下标结果(容器类型, 索引节点) {
400
- if (容器类型.k === 'list') return 容器类型.elem;
401
- if (容器类型.k === 'str') return T.tStr();
402
- if (容器类型.k === 'dict') return 容器类型.val;
403
- if (容器类型.k === 'struct' && 索引节点 && 索引节点.类型 === '字面量') {
404
- const f = 容器类型.fields[索引节点.值];
405
- return f || T.tDyn();
406
- }
407
- if (容器类型.k === 'object' && 索引节点 && 索引节点.类型 === '字面量') {
408
- const f = 容器类型.fields[索引节点.值];
409
- return f || T.tDyn();
410
- }
411
- return T.tDyn();
412
- }
413
-
414
- // ---------- 函数调用:累积实参类型,返回返回类型 ----------
415
- 分析调用(node, env) {
416
- // 解析被调者名字
417
- let 函数名 = null;
418
- if (node.被调者.类型 === '标识符') 函数名 = node.被调者.名字;
419
- else if (node.被调者.类型 === '下标' && node.被调者.容器.类型 === '标识符') {
420
- // 方法调用 obj.方法(...) —— 暂按普通函数处理,方法特化阶段再细化
421
- 函数名 = null;
422
- }
423
-
424
- // 实参类型
425
- const 实参 = (node.参数 || []).map(a => this.分析表达式(a, env));
426
-
427
- if (函数名 && this.函数表[函数名]) {
428
- // 累积到形参类型(LUB)—— 这是反馈回定点迭代的关键
429
- const sig = this.签名[函数名] || T.tFunc(函数名, [], T.bot());
430
- const 旧参 = sig.params.slice();
431
- const 新参 = sig.params.map((p, i) => (实参[i] ? T.lub(p, 实参[i]) : p));
432
- // 形参数目可能滞后(首次见到该调用)
433
- while (新参.length < 实参.length) 新参.push(实参[新参.length] || T.bot());
434
- let 参变了 = false;
435
- for (let i = 0; i < 新参.length; i++) {
436
- if (!T.equal(新参[i], 旧参[i] || T.bot())) { 参变了 = true; break; }
437
- }
438
- if (参变了) {
439
- this.签名[函数名] = T.tFunc(函数名, 新参, sig.ret);
440
- this._变了 = true;
441
- }
442
- this.调用点.push({ 调用节点: node, 函数名, 实参类型: 实参 });
443
- // 返回值取已知返回类型
444
- const ret = this.返回类型[函数名] || T.bot();
445
- return ret;
446
- }
447
- // 未知函数 / 内建函数 -> 动态
448
- return T.tDyn();
449
- }
450
- }
451
-
452
- // ---------- 两个打平视图是否相等 ----------
453
- function 视图相等(a, b) {
454
- if (a.size !== b.size) return false;
455
- for (const [k, t] of a) {
456
- if (!T.equal(t, b.get(k) || T.bot())) return false;
457
- }
458
- return true;
459
- }
460
-
461
- module.exports = 类型推断;
@@ -1,119 +0,0 @@
1
- // ============================================================
2
- // 调用图.js —— 函数调用关系图构建
3
- //
4
- // 记录:
5
- // 调用者 -> [被调者...] (边)
6
- // 被调者 -> [调用者...] (反向边,用于内联决策:被调用次数)
7
- // 递归检测:某函数是否直接/间接调用自己
8
- //
9
- // 内联决策需要这张图回答:
10
- // - 这个函数被几个调用点使用?(少则值得内联)
11
- // - 这个函数递归吗?(递归则不内联)
12
- // - 内联一个函数后,是否暴露新的内联机会?(需要迭代)
13
- // ============================================================
14
-
15
- 'use strict';
16
-
17
- const AU = require('./AST工具');
18
-
19
- class 调用图 {
20
- constructor(程序节点) {
21
- this.程序 = 程序节点;
22
- this.函数表 = AU.收集顶层函数(程序节点);
23
- /** 邻接表:调用者 -> Set(被调者名) */
24
- this.边 = new Map();
25
- /** 反向边:被调者 -> Set(调用者名) */
26
- this.反边 = new Map();
27
- /** 每个函数的调用点列表:被调者 -> [调用节点] */
28
- this.调用点 = new Map();
29
- }
30
-
31
- 构建() {
32
- for (const [名, fn] of Object.entries(this.函数表)) {
33
- this.边.set(名, new Set());
34
- // 遍历函数体里的所有调用
35
- AU.遍历(fn.体, (node) => {
36
- if (node.类型 === '函数调用' && node.被调者 && node.被调者.类型 === '标识符') {
37
- const 被调 = node.被调者.名字;
38
- this.边.get(名).add(被调);
39
- if (!this.反边.has(被调)) this.反边.set(被调, new Set());
40
- this.反边.get(被调).add(名);
41
- if (!this.调用点.has(被调)) this.调用点.set(被调, []);
42
- this.调用点.get(被调).push(node);
43
- }
44
- });
45
- }
46
- // 顶层程序体当作合成函数 "<main>",扫描其中的调用。
47
- // 注意:只扫顶层语句及其表达式,不下降进嵌套函数体(那些已在上面登记)。
48
- this.边.set('<main>', new Set());
49
- if (this.程序 && Array.isArray(this.程序.语句)) {
50
- for (const st of this.程序.语句) {
51
- if (!st || typeof st !== 'object') continue;
52
- if (st.类型 === '函数定义' || st.类型 === '类定义') continue; // 跳过函数体
53
- this.收集调用(st, '<main>');
54
- }
55
- }
56
- return this;
57
- }
58
-
59
- // 在一个节点内收集所有函数调用(不下降进函数定义体)
60
- 收集调用(node, 调用者) {
61
- if (!node || typeof node !== 'object') return;
62
- if (Array.isArray(node)) {
63
- for (const x of node) this.收集调用(x, 调用者);
64
- return;
65
- }
66
- if (typeof node.类型 !== 'string') return;
67
- if (node.类型 === '函数定义' || node.类型 === '函数字面量' || node.类型 === '类定义') return;
68
- if (node.类型 === '函数调用' && node.被调者 && node.被调者.类型 === '标识符') {
69
- const 被调 = node.被调者.名字;
70
- this.边.get(调用者).add(被调);
71
- if (!this.调用点.has(被调)) this.调用点.set(被调, []);
72
- this.调用点.get(被调).push(node);
73
- }
74
- for (const k of Object.keys(node)) {
75
- if (k === '类型' || k === '名字') continue;
76
- this.收集调用(node[k], 调用者);
77
- }
78
- }
79
-
80
- /** 某函数被多少个调用点调用 */
81
- 被调次数(函数名) {
82
- return this.调用点.has(函数名) ? this.调用点.get(函数名).length : 0;
83
- }
84
-
85
- /** 某函数是否递归(直接调用自己,或通过调用图闭包回到自己) */
86
- 是否递归(函数名) {
87
- const 访问 = new Set();
88
- const 栈 = [函数名];
89
- while (栈.length) {
90
- const cur = 栈.pop();
91
- const 被调集 = this.边.get(cur);
92
- if (!被调集) continue;
93
- for (const 被调 of 被调集) {
94
- if (被调 === 函数名) return true; // 直接递归
95
- if (!访问.has(被调)) { 访问.add(被调); 栈.push(被调); }
96
- }
97
- }
98
- return false;
99
- }
100
-
101
- /** 某函数是否含循环(用于内联决策:循环体小则内联收益大) */
102
- 含循环(函数名) {
103
- const fn = this.函数表[函数名];
104
- if (!fn) return false;
105
- let 有 = false;
106
- AU.遍历(fn.体, (node) => {
107
- if (node.类型 === '当' || node.类型 === '对于') 有 = true;
108
- });
109
- return 有;
110
- }
111
-
112
- /** 函数体规模(节点数) */
113
- 规模(函数名) {
114
- const fn = this.函数表[函数名];
115
- return fn ? AU.函数规模(fn) : 0;
116
- }
117
- }
118
-
119
- module.exports = 调用图;