@yeyelailou/co 0.0.2 → 0.0.4
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.
- package/README.md +73 -16
- package/bin//345/205/245/345/217/243.js +0 -8
- package/package.json +7 -6
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//345/220/257/345/212/250/345/231/250.c +3 -3
- 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
- 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 +73 -0
- 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
- 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
- package/bin/install.js +0 -276
- package//345/267/245/345/205/267/lsp/server.js +0 -419
- package//345/267/245/345/205/267/lsp//346/265/213/350/257/225.sh +0 -81
- package//345/267/245/345/205/267/vscode-cocn/extension.js +0 -203
- package//345/267/245/345/205/267/vscode-cocn/package.json +0 -58
- package//345/267/245/345/205/267/vscode-cocn/syntaxes/coCN.tmLanguage.json +0 -130
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/Empty.java +0 -1
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.c +0 -53
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.co +0 -42
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.go +0 -43
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.java +0 -29
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.js +0 -24
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.py +0 -36
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.rs +0 -35
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/empty.co +0 -0
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/empty_c.c +0 -1
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/empty_go.go +0 -2
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/empty_rs.rs +0 -1
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
package/bin/install.js
DELETED
|
@@ -1,276 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* coCN npm 包 postinstall 脚本(自解释器版)
|
|
4
|
-
*
|
|
5
|
-
* 自解释器(词法分析器.co / 语法分析器.co / 求值器.co / 解释器.co /
|
|
6
|
-
* 标准库.co)已随 npm 包发布,但它本身需要一个【引导 cocn 二进制】来启动。
|
|
7
|
-
* 本脚本按平台/架构下载预编译引导二进制到 bin/cocn(Windows: bin/cocn.exe)。
|
|
8
|
-
*
|
|
9
|
-
* 注意:npm 包不再包含 co.c,因此不再提供源码编译回退;
|
|
10
|
-
* 下载失败时给出明确错误,提示用户手动安装。
|
|
11
|
-
*/
|
|
12
|
-
'use strict';
|
|
13
|
-
|
|
14
|
-
const fs = require('fs');
|
|
15
|
-
const path = require('path');
|
|
16
|
-
const https = require('https');
|
|
17
|
-
const http = require('http');
|
|
18
|
-
const { execSync } = require('child_process');
|
|
19
|
-
|
|
20
|
-
const VERSION = '0.1.20';
|
|
21
|
-
// 引导二进制版本:使用 v0.1.0(最后一个 C 构建版本,含完整内置函数)。
|
|
22
|
-
// 引导二进制仅用于启动 coCN 自解释器,其版本无需与包版本一致。
|
|
23
|
-
const BOOTSTRAP_VERSION = '0.1.0';
|
|
24
|
-
const BIN_DIR = path.join(__dirname, 'bin');
|
|
25
|
-
const BIN_NAME = process.platform === 'win32' ? '引导器.exe' : '引导器';
|
|
26
|
-
const BIN_PATH = path.join(BIN_DIR, BIN_NAME);
|
|
27
|
-
|
|
28
|
-
// 预编译引导二进制下载源(Gitee 优先,GitHub 备选)
|
|
29
|
-
const DOWNLOAD_BASES = [
|
|
30
|
-
`https://gitee.com/COSMOnb666/coCN/releases/download/v${BOOTSTRAP_VERSION}`,
|
|
31
|
-
`https://github.com/3477856804/coCN/releases/download/v${BOOTSTRAP_VERSION}`
|
|
32
|
-
];
|
|
33
|
-
|
|
34
|
-
function log(msg) {
|
|
35
|
-
console.log(`\x1b[34m[coCN]\x1b[0m ${msg}`);
|
|
36
|
-
}
|
|
37
|
-
function warn(msg) {
|
|
38
|
-
console.warn(`\x1b[33m[coCN!]\x1b[0m ${msg}`);
|
|
39
|
-
}
|
|
40
|
-
function error(msg) {
|
|
41
|
-
console.error(`\x1b[31m[coCN!!]\x1b[0m ${msg}`);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function getPlatform() {
|
|
45
|
-
switch (process.platform) {
|
|
46
|
-
case 'darwin': return 'darwin';
|
|
47
|
-
case 'linux': return 'linux';
|
|
48
|
-
case 'win32': return 'windows';
|
|
49
|
-
default: return null;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function getArch() {
|
|
54
|
-
switch (process.arch) {
|
|
55
|
-
case 'x64': return 'x86_64';
|
|
56
|
-
case 'arm64': return 'aarch64';
|
|
57
|
-
default: return null;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function download(url, dest, redirectCount = 0) {
|
|
62
|
-
return new Promise((resolve, reject) => {
|
|
63
|
-
if (redirectCount > 5) {
|
|
64
|
-
reject(new Error('重定向次数过多'));
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
const client = url.startsWith('https') ? https : http;
|
|
68
|
-
const request = client.get(url, { timeout: 30000, headers: { 'User-Agent': 'coCN-npm-installer' } }, (response) => {
|
|
69
|
-
// 处理重定向
|
|
70
|
-
if (response.statusCode === 301 || response.statusCode === 302 || response.statusCode === 307 || response.statusCode === 308) {
|
|
71
|
-
const location = response.headers.location;
|
|
72
|
-
if (!location) {
|
|
73
|
-
reject(new Error(`重定向缺少 Location 头(HTTP ${response.statusCode})`));
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
const nextUrl = location.startsWith('http') ? location : new URL(location, url).href;
|
|
77
|
-
response.resume(); // 消耗响应体
|
|
78
|
-
download(nextUrl, dest, redirectCount + 1).then(resolve).catch(reject);
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
if (response.statusCode !== 200) {
|
|
82
|
-
response.resume();
|
|
83
|
-
reject(new Error(`HTTP ${response.statusCode}`));
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
const file = fs.createWriteStream(dest);
|
|
87
|
-
response.pipe(file);
|
|
88
|
-
file.on('finish', () => {
|
|
89
|
-
file.close(() => resolve(dest));
|
|
90
|
-
});
|
|
91
|
-
file.on('error', (err) => {
|
|
92
|
-
fs.unlink(dest, () => {});
|
|
93
|
-
reject(err);
|
|
94
|
-
});
|
|
95
|
-
});
|
|
96
|
-
request.on('error', (err) => {
|
|
97
|
-
reject(err);
|
|
98
|
-
});
|
|
99
|
-
request.on('timeout', () => {
|
|
100
|
-
request.destroy();
|
|
101
|
-
reject(new Error('下载超时'));
|
|
102
|
-
});
|
|
103
|
-
});
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
async function tryDownloadBinary() {
|
|
107
|
-
const platform = getPlatform();
|
|
108
|
-
const arch = getArch();
|
|
109
|
-
if (!platform || !arch) {
|
|
110
|
-
error(`不支持的平台/架构: ${process.platform}/${process.arch}`);
|
|
111
|
-
error('请前往 https://gitee.com/COSMOnb666/coCN/releases 手动下载对应二进制,放到 bin/cocn 后重试。');
|
|
112
|
-
return false;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
const asset = `coCN-${platform}-${arch}${process.platform === 'win32' ? '.exe' : ''}`;
|
|
116
|
-
|
|
117
|
-
for (const base of DOWNLOAD_BASES) {
|
|
118
|
-
const url = `${base}/${asset}`;
|
|
119
|
-
log(`尝试下载引导二进制: ${url}`);
|
|
120
|
-
try {
|
|
121
|
-
if (!fs.existsSync(BIN_DIR)) fs.mkdirSync(BIN_DIR, { recursive: true });
|
|
122
|
-
await download(url, BIN_PATH);
|
|
123
|
-
if (process.platform !== 'win32') {
|
|
124
|
-
fs.chmodSync(BIN_PATH, 0o755);
|
|
125
|
-
}
|
|
126
|
-
// 验证二进制可运行
|
|
127
|
-
try {
|
|
128
|
-
const out = execSync(`"${BIN_PATH}" --版本`, { encoding: 'utf8', timeout: 10000 });
|
|
129
|
-
if (out.includes('coCN')) {
|
|
130
|
-
log(`引导二进制下载成功: ${asset}`);
|
|
131
|
-
return true;
|
|
132
|
-
}
|
|
133
|
-
} catch (e) {
|
|
134
|
-
warn(`二进制验证失败: ${e.message}`);
|
|
135
|
-
}
|
|
136
|
-
fs.unlinkSync(BIN_PATH);
|
|
137
|
-
} catch (e) {
|
|
138
|
-
warn(`下载失败: ${e.message}`);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
return false;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/** 清理旧版本留下的编译缓存。
|
|
145
|
-
*
|
|
146
|
-
* 为什么需要:缓存指纹现在掺了包版本(-DCOCN_ABI="cocn-direct-<版本>"),
|
|
147
|
-
* 升级后每个程序都会重编一遍,这是对的;但旧产物只是「不可达」,
|
|
148
|
-
* 仍躺在 ~/.cocn/cache/ 里——里面可能有 0.1.14 那批「算错」的 ELF,
|
|
149
|
-
* 占着磁盘还容易被误当有效产物。所以发版时按版本戳清一次。
|
|
150
|
-
*
|
|
151
|
-
* 安全边界(这是删用户主目录下的东西,必须收敛):
|
|
152
|
-
* - 只动 ~/.cocn/cache 这一层,不递归;
|
|
153
|
-
* - 只删【普通文件】(lstat,不是软链接、不是目录);
|
|
154
|
-
* - 文件名必须是 16 位十六进制,或 <16位hex>.tmp<数字>(原子写的临时名);
|
|
155
|
-
* - 版本戳文件本身不参与匹配(前缀不是 16 位 hex)。
|
|
156
|
-
* 任何一步出错都只 warn,绝不中断安装。 */
|
|
157
|
-
function 清理旧版缓存() {
|
|
158
|
-
const home = process.env.HOME || require('os').homedir();
|
|
159
|
-
if (!home) return;
|
|
160
|
-
const cache = path.join(home, '.cocn', 'cache');
|
|
161
|
-
const stamp = path.join(home, '.cocn', 'abi-version');
|
|
162
|
-
let 当前 = null;
|
|
163
|
-
try { 当前 = fs.readFileSync(stamp, 'utf8').trim(); } catch (e) { /* 首装 */ }
|
|
164
|
-
if (当前 === VERSION) return; // 同版本重复安装,不动任何文件
|
|
165
|
-
|
|
166
|
-
const 名字 = /^[0-9a-f]{16}(\.tmp\d+)?$/;
|
|
167
|
-
let 删 = 0, 跳 = 0;
|
|
168
|
-
try {
|
|
169
|
-
for (const f of fs.readdirSync(cache)) {
|
|
170
|
-
if (!名字.test(f)) { 跳++; continue; }
|
|
171
|
-
const p = path.join(cache, f);
|
|
172
|
-
try {
|
|
173
|
-
const st = fs.lstatSync(p);
|
|
174
|
-
if (!st.isFile()) { 跳++; continue; } // 目录/软链接一律不碰
|
|
175
|
-
fs.unlinkSync(p); 删++;
|
|
176
|
-
} catch (e) { 跳++; }
|
|
177
|
-
}
|
|
178
|
-
} catch (e) {
|
|
179
|
-
return; // 目录不存在 = 没缓存,正常
|
|
180
|
-
}
|
|
181
|
-
try {
|
|
182
|
-
fs.mkdirSync(path.dirname(stamp), { recursive: true });
|
|
183
|
-
fs.writeFileSync(stamp, VERSION + '\n');
|
|
184
|
-
} catch (e) { /* 写戳失败不影响正确性 */ }
|
|
185
|
-
if (删 || 跳) log(`已清理旧版编译缓存:删除 ${删} 个产物,保留 ${跳} 项非产物文件`);
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
async function main() {
|
|
189
|
-
log(`coCN v${VERSION} 安装开始(${process.platform}/${process.arch})`);
|
|
190
|
-
|
|
191
|
-
// 如果是 npm publish 或 dry-run,跳过安装
|
|
192
|
-
if (process.env.npm_config_dry_run) {
|
|
193
|
-
log('dry-run 模式,跳过引导二进制安装');
|
|
194
|
-
return;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
清理旧版缓存();
|
|
198
|
-
|
|
199
|
-
// 优先使用包内自带的启动器(Linux x64 已随包打包,开箱即用)。
|
|
200
|
-
// 注意:原生启动器不认识 --版本 参数(会按文件名报错并退出),
|
|
201
|
-
// 旧版在此做 --版本 验证导致包内启动器被误删后替换为老引导器,
|
|
202
|
-
// cocn 命令因此全部退回自解释器模式(秒级)。此处改为直接信任包内文件。
|
|
203
|
-
if (fs.existsSync(BIN_PATH)) {
|
|
204
|
-
log(`包内启动器就绪: ${BIN_PATH}`);
|
|
205
|
-
log('运行方式: cocn 程序.co(原生编译缓存通道,毫秒级启动)');
|
|
206
|
-
return;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
// 包内无可用二进制(其他平台),尝试下载
|
|
210
|
-
const success = await tryDownloadBinary();
|
|
211
|
-
|
|
212
|
-
if (success) {
|
|
213
|
-
log(`引导二进制就绪: ${BIN_PATH}`);
|
|
214
|
-
} else {
|
|
215
|
-
error('引导二进制不可用。npm 包内置的 coCN 自解释器需要一个引导 cocn 二进制才能启动。');
|
|
216
|
-
error('请手动处理:');
|
|
217
|
-
error(' 1. 前往 https://gitee.com/COSMOnb666/coCN/releases 下载对应平台的引导二进制');
|
|
218
|
-
error(` 2. 重命名为 ${BIN_NAME} 并放到 npm 包的 bin/ 目录`);
|
|
219
|
-
error(' 3. 或直接重新安装: npm install -g @yeyelailou/co');
|
|
220
|
-
process.exit(1);
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
// 构建原生组件:运行时目标文件 + 原生启动器(有 gcc 才有 AOT 加速)
|
|
224
|
-
buildNative();
|
|
225
|
-
log('运行方式: cocn 程序.co');
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
/** 编译 运行时.c / 入口.c / 启动器.c,使 cocn 具备原生编译能力 */
|
|
229
|
-
function buildNative() {
|
|
230
|
-
if (process.platform === 'win32') {
|
|
231
|
-
warn('Windows 暂不构建原生启动器,回退自解释器模式');
|
|
232
|
-
return;
|
|
233
|
-
}
|
|
234
|
-
const NATIVE_DIR = path.join(__dirname, '工具', '原生编译器');
|
|
235
|
-
const objs = [
|
|
236
|
-
[path.join(NATIVE_DIR, '运行时.c'), path.join(BIN_DIR, 'rt.o')],
|
|
237
|
-
[path.join(NATIVE_DIR, '入口.c'), path.join(BIN_DIR, 'entry.o')]
|
|
238
|
-
];
|
|
239
|
-
try {
|
|
240
|
-
execSync('command -v gcc', { encoding: 'utf8', timeout: 10000 });
|
|
241
|
-
} catch (e) {
|
|
242
|
-
warn('未检测到 gcc,跳过原生组件构建(coCN 仍可用,走自解释器)');
|
|
243
|
-
return;
|
|
244
|
-
}
|
|
245
|
-
try {
|
|
246
|
-
if (!fs.existsSync(BIN_DIR)) fs.mkdirSync(BIN_DIR, { recursive: true });
|
|
247
|
-
for (const [src, out] of objs) {
|
|
248
|
-
if (!fs.existsSync(src)) continue;
|
|
249
|
-
execSync(`gcc -O2 -fno-pie -c "${src}" -o "${out}"`, { encoding: 'utf8', timeout: 120000 });
|
|
250
|
-
}
|
|
251
|
-
// 原生启动器:让「运行 coCN 程序」本身也是原生速度
|
|
252
|
-
// 优先静态链接:动态链接的启动器每次 exec 要走 ld.so,实测热路径
|
|
253
|
-
// 多 ~900-1000us;静态版把缓存命中的总时长压回 4ms 以内。
|
|
254
|
-
const launcherSrc = path.join(NATIVE_DIR, '启动器.c');
|
|
255
|
-
const launcherOut = path.join(BIN_DIR, 'cocn');
|
|
256
|
-
if (fs.existsSync(launcherSrc)) {
|
|
257
|
-
// -DCOCN_ABI 把包版本烧进缓存指纹:发版即自动作废上一版产物,
|
|
258
|
-
// 不必靠人记得手改 启动器.c 里的代次串(漏改 = 修复对用户无效)。
|
|
259
|
-
const abi = `-DCOCN_ABI=\"cocn-direct-${VERSION}\"`;
|
|
260
|
-
try {
|
|
261
|
-
execSync(`gcc -O2 -static ${abi} -o "${launcherOut}" "${launcherSrc}"`, { encoding: 'utf8', timeout: 120000 });
|
|
262
|
-
} catch (e) {
|
|
263
|
-
execSync(`gcc -O2 ${abi} -o "${launcherOut}" "${launcherSrc}"`, { encoding: 'utf8', timeout: 120000 });
|
|
264
|
-
}
|
|
265
|
-
fs.chmodSync(launcherOut, 0o755);
|
|
266
|
-
}
|
|
267
|
-
log('原生组件已构建(AOT 加速可用)');
|
|
268
|
-
} catch (e) {
|
|
269
|
-
warn(`原生组件构建失败,回退自解释器模式: ${e.message}`);
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
main().catch(e => {
|
|
274
|
-
error(`安装脚本异常: ${e.message}`);
|
|
275
|
-
process.exit(1);
|
|
276
|
-
});
|
|
@@ -1,419 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* coCN 基础 LSP 服务器(纯 Node.js,无外部依赖)
|
|
4
|
-
*
|
|
5
|
-
* 协议:JSON-RPC 2.0 over stdio,使用 LSP 的 Content-Length 分帧。
|
|
6
|
-
*
|
|
7
|
-
* 支持能力:
|
|
8
|
-
* - textDocument/didOpen / didChange:维护文档内容
|
|
9
|
-
* - textDocument/definition:跳转函数定义(匹配「函数 名」或「名 = 函数」模式)
|
|
10
|
-
* - textDocument/hover:悬停显示标识符附近的类型标注
|
|
11
|
-
* - textDocument/diagnostics:写入临时文件,调用 bin/cocn.js 捕获 stderr
|
|
12
|
-
*
|
|
13
|
-
* 用法:node server.js
|
|
14
|
-
*/
|
|
15
|
-
'use strict';
|
|
16
|
-
|
|
17
|
-
const fs = require('fs');
|
|
18
|
-
const path = require('path');
|
|
19
|
-
const os = require('os');
|
|
20
|
-
const { spawn } = require('child_process');
|
|
21
|
-
|
|
22
|
-
// ---------- 文档存储 ----------
|
|
23
|
-
/** @type {Map<string, {text: string, version: number}>} */
|
|
24
|
-
const documents = new Map();
|
|
25
|
-
|
|
26
|
-
// ---------- 项目根目录检测 ----------
|
|
27
|
-
// server.js 位于 <项目根>/lsp/server.js
|
|
28
|
-
const PROJECT_ROOT = path.resolve(__dirname, '..');
|
|
29
|
-
const COCN_JS = path.join(PROJECT_ROOT, 'bin', 'cocn.js');
|
|
30
|
-
|
|
31
|
-
// ---------- JSON-RPC 分帧(使用 Buffer 按字节处理,兼容中文)----------
|
|
32
|
-
let rpcBuffer = Buffer.alloc(0);
|
|
33
|
-
|
|
34
|
-
process.stdin.on('data', (chunk) => {
|
|
35
|
-
rpcBuffer = Buffer.concat([rpcBuffer, chunk]);
|
|
36
|
-
// 查找 \r\n\r\n(头部与正文的分隔)
|
|
37
|
-
while (true) {
|
|
38
|
-
const sepIdx = rpcBuffer.indexOf('\r\n\r\n');
|
|
39
|
-
if (sepIdx === -1) break;
|
|
40
|
-
const headerSection = rpcBuffer.slice(0, sepIdx).toString('utf8');
|
|
41
|
-
const m = headerSection.match(/Content-Length:\s*(\d+)/i);
|
|
42
|
-
if (!m) {
|
|
43
|
-
// 无效头部,跳过这一段
|
|
44
|
-
rpcBuffer = rpcBuffer.slice(sepIdx + 4);
|
|
45
|
-
continue;
|
|
46
|
-
}
|
|
47
|
-
const contentLength = parseInt(m[1], 10);
|
|
48
|
-
const bodyStart = sepIdx + 4;
|
|
49
|
-
if (rpcBuffer.length < bodyStart + contentLength) break; // 等待更多数据
|
|
50
|
-
const bodyBuf = rpcBuffer.slice(bodyStart, bodyStart + contentLength);
|
|
51
|
-
rpcBuffer = rpcBuffer.slice(bodyStart + contentLength);
|
|
52
|
-
try {
|
|
53
|
-
const msg = JSON.parse(bodyBuf.toString('utf8'));
|
|
54
|
-
handleMessage(msg);
|
|
55
|
-
} catch (e) {
|
|
56
|
-
console.error('[LSP] 解析消息失败:', e.message);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
function sendResponse(id, result) {
|
|
62
|
-
const body = JSON.stringify({ jsonrpc: '2.0', id, result });
|
|
63
|
-
writeRpc(body);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function sendError(id, code, message) {
|
|
67
|
-
const body = JSON.stringify({
|
|
68
|
-
jsonrpc: '2.0',
|
|
69
|
-
id,
|
|
70
|
-
error: { code, message },
|
|
71
|
-
});
|
|
72
|
-
writeRpc(body);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function sendNotification(method, params) {
|
|
76
|
-
const body = JSON.stringify({ jsonrpc: '2.0', method, params });
|
|
77
|
-
writeRpc(body);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function writeRpc(body) {
|
|
81
|
-
const header = `Content-Length: ${Buffer.byteLength(body)}\r\n\r\n`;
|
|
82
|
-
process.stdout.write(header + body);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// ---------- 消息分发 ----------
|
|
86
|
-
function handleMessage(msg) {
|
|
87
|
-
const { id, method, params } = msg;
|
|
88
|
-
|
|
89
|
-
switch (method) {
|
|
90
|
-
case 'initialize':
|
|
91
|
-
sendResponse(id, {
|
|
92
|
-
capabilities: {
|
|
93
|
-
textDocumentSync: 1, // Full sync
|
|
94
|
-
definitionProvider: true,
|
|
95
|
-
hoverProvider: true,
|
|
96
|
-
diagnosticProvider: true,
|
|
97
|
-
},
|
|
98
|
-
serverInfo: { name: 'coCN LSP', version: '0.0.1' },
|
|
99
|
-
});
|
|
100
|
-
break;
|
|
101
|
-
|
|
102
|
-
case 'initialized':
|
|
103
|
-
// 客户端已初始化,无需响应
|
|
104
|
-
break;
|
|
105
|
-
|
|
106
|
-
case 'shutdown':
|
|
107
|
-
sendResponse(id, null);
|
|
108
|
-
break;
|
|
109
|
-
|
|
110
|
-
case 'exit':
|
|
111
|
-
process.exit(0);
|
|
112
|
-
break;
|
|
113
|
-
|
|
114
|
-
case 'textDocument/didOpen':
|
|
115
|
-
didOpen(params);
|
|
116
|
-
break;
|
|
117
|
-
|
|
118
|
-
case 'textDocument/didChange':
|
|
119
|
-
didChange(params);
|
|
120
|
-
break;
|
|
121
|
-
|
|
122
|
-
case 'textDocument/definition':
|
|
123
|
-
handleDefinition(id, params);
|
|
124
|
-
break;
|
|
125
|
-
|
|
126
|
-
case 'textDocument/hover':
|
|
127
|
-
handleHover(id, params);
|
|
128
|
-
break;
|
|
129
|
-
|
|
130
|
-
case 'textDocument/didSave':
|
|
131
|
-
// 保存时触发诊断
|
|
132
|
-
runDiagnostics(params.textDocument.uri);
|
|
133
|
-
break;
|
|
134
|
-
|
|
135
|
-
default:
|
|
136
|
-
// 未知方法,通知客户端
|
|
137
|
-
if (id !== undefined) {
|
|
138
|
-
sendError(id, -32601, `Method not found: ${method}`);
|
|
139
|
-
}
|
|
140
|
-
break;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
// ---------- 文档管理 ----------
|
|
145
|
-
function didOpen(params) {
|
|
146
|
-
const uri = params.textDocument.uri;
|
|
147
|
-
documents.set(uri, {
|
|
148
|
-
text: params.textDocument.text,
|
|
149
|
-
version: params.textDocument.version || 1,
|
|
150
|
-
});
|
|
151
|
-
// 打开时自动跑一次诊断
|
|
152
|
-
runDiagnostics(uri);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
function didChange(params) {
|
|
156
|
-
const uri = params.textDocument.uri;
|
|
157
|
-
const doc = documents.get(uri);
|
|
158
|
-
if (!doc) return;
|
|
159
|
-
// LSP 设置为 Full sync,contentChanges[0].text 是完整文本
|
|
160
|
-
if (params.contentChanges && params.contentChanges.length > 0) {
|
|
161
|
-
doc.text = params.contentChanges[0].text;
|
|
162
|
-
doc.version = params.textDocument.version || doc.version + 1;
|
|
163
|
-
}
|
|
164
|
-
// 防抖诊断:延迟 500ms
|
|
165
|
-
scheduleDiagnostics(uri);
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
// 简单防抖
|
|
169
|
-
const pendingTimers = new Map();
|
|
170
|
-
function scheduleDiagnostics(uri) {
|
|
171
|
-
if (pendingTimers.has(uri)) clearTimeout(pendingTimers.get(uri));
|
|
172
|
-
pendingTimers.set(uri, setTimeout(() => {
|
|
173
|
-
pendingTimers.delete(uri);
|
|
174
|
-
runDiagnostics(uri);
|
|
175
|
-
}, 500));
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
// ---------- 跳转定义 ----------
|
|
179
|
-
/**
|
|
180
|
-
* 解析光标位置的标识符,查找其定义。
|
|
181
|
-
* 匹配模式:
|
|
182
|
-
* 1. 函数 函数名( ... ) —— 函数定义
|
|
183
|
-
* 2. 变量 名 = ... —— 变量声明
|
|
184
|
-
* 3. 常量 名 = ... —— 常量声明
|
|
185
|
-
* 4. 名 = 函数( ... ) —— 匿名函数赋值
|
|
186
|
-
*/
|
|
187
|
-
function handleDefinition(id, params) {
|
|
188
|
-
const uri = params.textDocument.uri;
|
|
189
|
-
const doc = documents.get(uri);
|
|
190
|
-
if (!doc) {
|
|
191
|
-
sendResponse(id, null);
|
|
192
|
-
return;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
const lines = doc.text.split('\n');
|
|
196
|
-
const line = params.position.line;
|
|
197
|
-
const char = params.position.character;
|
|
198
|
-
|
|
199
|
-
// 从当前行提取光标下的标识符
|
|
200
|
-
const currentLine = lines[line] || '';
|
|
201
|
-
const word = extractWordAt(currentLine, char);
|
|
202
|
-
if (!word) {
|
|
203
|
-
sendResponse(id, null);
|
|
204
|
-
return;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
// 在全文中搜索定义
|
|
208
|
-
const def = findDefinition(doc.text, word);
|
|
209
|
-
if (def) {
|
|
210
|
-
sendResponse(id, {
|
|
211
|
-
uri: uri,
|
|
212
|
-
range: {
|
|
213
|
-
start: { line: def.line, character: def.col },
|
|
214
|
-
end: { line: def.line, character: def.col + def.length },
|
|
215
|
-
},
|
|
216
|
-
});
|
|
217
|
-
} else {
|
|
218
|
-
sendResponse(id, null);
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
function extractWordAt(line, char) {
|
|
223
|
-
// 标识符字符:字母、数字、下划线、中文
|
|
224
|
-
const isWordChar = (c) => /[A-Za-z0-9_\u4e00-\u9fff]/.test(c);
|
|
225
|
-
if (char > 0 && !isWordChar(line[char])) char--;
|
|
226
|
-
let start = char;
|
|
227
|
-
let end = char;
|
|
228
|
-
while (start > 0 && isWordChar(line[start - 1])) start--;
|
|
229
|
-
while (end < line.length && isWordChar(line[end])) end++;
|
|
230
|
-
if (start === end) return null;
|
|
231
|
-
return line.substring(start, end);
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
function findDefinition(text, word) {
|
|
235
|
-
const lines = text.split('\n');
|
|
236
|
-
// 模式 1: 函数 函数名(
|
|
237
|
-
const fnRe = new RegExp(`^\\s*函数\\s+${escapeRegex(word)}\\s*\\(`);
|
|
238
|
-
// 模式 2: 变量 名 =
|
|
239
|
-
const varRe = new RegExp(`^\\s*变量\\s+${escapeRegex(word)}\\s*=`);
|
|
240
|
-
// 模式 3: 常量 名 =
|
|
241
|
-
const constRe = new RegExp(`^\\s*常量\\s+${escapeRegex(word)}\\s*=`);
|
|
242
|
-
// 模式 4: 名 = 函数(
|
|
243
|
-
const assignRe = new RegExp(`^\\s*${escapeRegex(word)}\\s*=\\s*函数\\s*\\(`);
|
|
244
|
-
|
|
245
|
-
for (let i = 0; i < lines.length; i++) {
|
|
246
|
-
const line = lines[i];
|
|
247
|
-
let m;
|
|
248
|
-
if ((m = line.match(fnRe))) {
|
|
249
|
-
const idx = line.indexOf(word, line.search(/函数/));
|
|
250
|
-
return { line: i, col: idx, length: word.length };
|
|
251
|
-
}
|
|
252
|
-
if ((m = line.match(varRe))) {
|
|
253
|
-
const idx = line.indexOf(word, line.search(/变量/));
|
|
254
|
-
return { line: i, col: idx, length: word.length };
|
|
255
|
-
}
|
|
256
|
-
if ((m = line.match(constRe))) {
|
|
257
|
-
const idx = line.indexOf(word, line.search(/常量/));
|
|
258
|
-
return { line: i, col: idx, length: word.length };
|
|
259
|
-
}
|
|
260
|
-
if ((m = line.match(assignRe))) {
|
|
261
|
-
const idx = line.indexOf(word);
|
|
262
|
-
return { line: i, col: idx, length: word.length };
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
return null;
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
function escapeRegex(s) {
|
|
269
|
-
return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
// ---------- 悬停 ----------
|
|
273
|
-
function handleHover(id, params) {
|
|
274
|
-
const uri = params.textDocument.uri;
|
|
275
|
-
const doc = documents.get(uri);
|
|
276
|
-
if (!doc) {
|
|
277
|
-
sendResponse(id, { contents: [] });
|
|
278
|
-
return;
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
const lines = doc.text.split('\n');
|
|
282
|
-
const line = params.position.line;
|
|
283
|
-
const char = params.position.character;
|
|
284
|
-
const word = extractWordAt(lines[line] || '', char);
|
|
285
|
-
|
|
286
|
-
if (!word) {
|
|
287
|
-
sendResponse(id, { contents: [] });
|
|
288
|
-
return;
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
// 查找定义,附带类型标注
|
|
292
|
-
const def = findDefinition(doc.text, word);
|
|
293
|
-
if (def) {
|
|
294
|
-
const defLine = lines[def.line] || '';
|
|
295
|
-
// 尝试提取返回类型标注(-> 后面的部分)
|
|
296
|
-
let typeInfo = '';
|
|
297
|
-
const arrowMatch = defLine.match(/->\s*(\S+)/);
|
|
298
|
-
if (arrowMatch) typeInfo = `\n\n返回类型: \`${arrowMatch[1]}\``;
|
|
299
|
-
|
|
300
|
-
// 尝试提取参数类型
|
|
301
|
-
const paramMatch = defLine.match(/\(([^)]*)\)/);
|
|
302
|
-
let paramInfo = '';
|
|
303
|
-
if (paramMatch && paramMatch[1].trim()) {
|
|
304
|
-
paramInfo = `\n参数: \`${paramMatch[1].trim()}\``;
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
sendResponse(id, {
|
|
308
|
-
contents: {
|
|
309
|
-
kind: 'markdown',
|
|
310
|
-
value: `**${word}** (coCN 定义)${paramInfo}${typeInfo}\n\n第 ${def.line + 1} 行`,
|
|
311
|
-
},
|
|
312
|
-
});
|
|
313
|
-
} else {
|
|
314
|
-
// 检查是否是关键字
|
|
315
|
-
const keywords = ['让','变量','常量','函数','返回','如果','否则','若','当','循环','对于','从','到','步长','结束','跳出','继续','真','假','且','或','非','导入','匹配','是','默认','于','在','那么','尝试','捕获','最终','抛出','并发','等待','则','整数','浮点','布尔','字符串','列表','字典','输出','断言','断言相等'];
|
|
316
|
-
if (keywords.includes(word)) {
|
|
317
|
-
sendResponse(id, {
|
|
318
|
-
contents: {
|
|
319
|
-
kind: 'markdown',
|
|
320
|
-
value: `**关键字**: \`${word}\``,
|
|
321
|
-
},
|
|
322
|
-
});
|
|
323
|
-
} else {
|
|
324
|
-
sendResponse(id, { contents: [] });
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
// ---------- 诊断(调用 coCN 解释器)----------
|
|
330
|
-
function runDiagnostics(uri) {
|
|
331
|
-
const doc = documents.get(uri);
|
|
332
|
-
if (!doc) return;
|
|
333
|
-
|
|
334
|
-
// 如果解释器不存在,跳过
|
|
335
|
-
if (!fs.existsSync(COCN_JS)) {
|
|
336
|
-
// 静默跳过,不报错
|
|
337
|
-
return;
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
// 写入临时文件
|
|
341
|
-
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cocn-lsp-'));
|
|
342
|
-
const tmpFile = path.join(tmpDir, 'diagnose.co');
|
|
343
|
-
fs.writeFileSync(tmpFile, doc.text, 'utf-8');
|
|
344
|
-
|
|
345
|
-
// 运行 coCN 解释器
|
|
346
|
-
const child = spawn('node', [COCN_JS, tmpFile], {
|
|
347
|
-
cwd: PROJECT_ROOT,
|
|
348
|
-
env: process.env,
|
|
349
|
-
});
|
|
350
|
-
|
|
351
|
-
let stderr = '';
|
|
352
|
-
let stdout = '';
|
|
353
|
-
|
|
354
|
-
child.stderr.on('data', (d) => (stderr += d.toString()));
|
|
355
|
-
child.stdout.on('data', (d) => (stdout += d.toString()));
|
|
356
|
-
|
|
357
|
-
child.on('exit', (code) => {
|
|
358
|
-
// 清理临时文件
|
|
359
|
-
try { fs.unlinkSync(tmpFile); fs.rmdirSync(tmpDir); } catch (e) {}
|
|
360
|
-
|
|
361
|
-
const diagnostics = [];
|
|
362
|
-
|
|
363
|
-
if (code !== 0 && stderr) {
|
|
364
|
-
// 解析 stderr 中的错误信息
|
|
365
|
-
const lines = stderr.split('\n');
|
|
366
|
-
for (const line of lines) {
|
|
367
|
-
// 尝试匹配行号信息,例如 "第 3 行" 或 "line 3"
|
|
368
|
-
const lineMatch = line.match(/第\s*(\d+)\s*行/);
|
|
369
|
-
if (lineMatch) {
|
|
370
|
-
const errLine = parseInt(lineMatch[1], 10) - 1; // 0-based
|
|
371
|
-
diagnostics.push({
|
|
372
|
-
severity: 1, // Error
|
|
373
|
-
range: {
|
|
374
|
-
start: { line: errLine, character: 0 },
|
|
375
|
-
end: { line: errLine, character: 100 },
|
|
376
|
-
},
|
|
377
|
-
message: line.trim(),
|
|
378
|
-
source: 'coCN',
|
|
379
|
-
});
|
|
380
|
-
} else if (line.trim() && !line.includes('[coCN]')) {
|
|
381
|
-
// 没有行号的错误,放在第 0 行
|
|
382
|
-
diagnostics.push({
|
|
383
|
-
severity: 1,
|
|
384
|
-
range: {
|
|
385
|
-
start: { line: 0, character: 0 },
|
|
386
|
-
end: { line: 0, character: 100 },
|
|
387
|
-
},
|
|
388
|
-
message: line.trim(),
|
|
389
|
-
source: 'coCN',
|
|
390
|
-
});
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
// 发布诊断
|
|
396
|
-
sendNotification('textDocument/publishDiagnostics', {
|
|
397
|
-
uri: uri,
|
|
398
|
-
diagnostics: diagnostics,
|
|
399
|
-
});
|
|
400
|
-
});
|
|
401
|
-
|
|
402
|
-
child.on('error', (err) => {
|
|
403
|
-
try { fs.unlinkSync(tmpFile); fs.rmdirSync(tmpDir); } catch (e) {}
|
|
404
|
-
sendNotification('textDocument/publishDiagnostics', {
|
|
405
|
-
uri: uri,
|
|
406
|
-
diagnostics: [
|
|
407
|
-
{
|
|
408
|
-
severity: 1,
|
|
409
|
-
range: { start: { line: 0, character: 0 }, end: { line: 0, character: 10 } },
|
|
410
|
-
message: `无法运行 coCN 解释器: ${err.message}`,
|
|
411
|
-
source: 'coCN LSP',
|
|
412
|
-
},
|
|
413
|
-
],
|
|
414
|
-
});
|
|
415
|
-
});
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
// ---------- 启动 ----------
|
|
419
|
-
console.error('[coCN LSP] 服务器已启动,监听 stdio');
|