Skip to content

Commit 7e9757c

Browse files
miaobyteclaude
andcommitted
refactor: load command and parser improvements
Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 549a2e4 commit 7e9757c

2 files changed

Lines changed: 17 additions & 7 deletions

File tree

cmd/kvlang/load.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,12 @@ func runCode(name string, rc io.Reader, dsn string, debug bool) {
101101
}
102102
allCalls := df.TopLevelCalls
103103
if len(df.InitBody) > 0 {
104-
initFn := ast.Func{Sig: ast.FuncSig{Name: "__init__"}, Body: df.InitBody}
104+
initFn := ast.Func{Sig: ast.FuncSig{Name: "init"}, Body: df.InitBody}
105105
layoutcode.WriteFunc(kv, "main", lower.Func(&initFn))
106-
allCalls = append([]*ast.Instruction{{Expr: ast.Call("__init__")}}, allCalls...)
107106
}
108-
layoutcode.WriteFunc(kv, "main", makeInitFunc(allCalls))
107+
if len(allCalls) > 0 {
108+
layoutcode.WriteFunc(kv, "main", makeInitFunc(allCalls))
109+
}
109110
kv.Set(keytree.LibMain, kvspace.Str(`{"entry":"init","reads":[],"writes":[]}`))
110111
executeEntry(kv, debug)
111112
}
@@ -120,7 +121,10 @@ func loadFunctions(kv kvspace.KVSpace, files []string) bool {
120121
_loadFile(kv, f, &allCalls, &anyCode, loaded)
121122
}
122123
if !anyCode && len(allCalls) == 0 { return false }
123-
layoutcode.WriteFunc(kv, "main", makeInitFunc(allCalls))
124+
// InitBody 已写入入口 init(fix-037),仅当 allCalls 非空时才补 makeInitFunc
125+
if len(allCalls) > 0 {
126+
layoutcode.WriteFunc(kv, "main", makeInitFunc(allCalls))
127+
}
124128
kv.Set(keytree.LibMain, kvspace.Str(`{"entry":"init","reads":[],"writes":[]}`))
125129
return true
126130
}
@@ -140,15 +144,18 @@ func _loadFile(kv kvspace.KVSpace, f string, allCalls *[]*ast.Instruction, anyCo
140144
layoutcode.WriteFunc(kv, pkg, lower.Func(&df.Funcs[i]))
141145
*anyCode = true
142146
}
143-
// init { ... } 体包装为函数经 lower 展开(fix-036:支持 if/while/for 控制流
147+
// init { ... } 体 / 裸控制流 → 入口 init() 函数体内联(fix-036/037
144148
if len(df.InitBody) > 0 {
145-
initFn := ast.Func{Sig: ast.FuncSig{Name: "__init__"}, Body: df.InitBody}
149+
initFn := ast.Func{Sig: ast.FuncSig{Name: "init"}, Body: df.InitBody}
146150
layoutcode.WriteFunc(kv, pkg, lower.Func(&initFn))
147-
*allCalls = append(*allCalls, &ast.Instruction{Expr: ast.Call("__init__")})
148151
*anyCode = true
149152
}
150153
*allCalls = append(*allCalls, df.TopLevelCalls...)
151154
if len(df.TopLevelCalls) > 0 { *anyCode = true }
155+
// InitBody 存在时,入口 init 已由 initFn 接管——不再用 makeInitFunc 生成空壳
156+
if len(df.InitBody) > 0 && len(*allCalls) == 0 {
157+
*anyCode = true // 确保 loadFunctions 不跳过
158+
}
152159
}
153160

154161
// collectKVFiles 收集 path(文件或目录)下所有 .kv 文件路径。

internal/parser/parser.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,9 @@ if p.peek().Kind == Ident && p.peek().Value == "def" {
230230
fn := p.parseFunc()
231231
fn.Comments = comments
232232
f.Funcs = append(f.Funcs, fn)
233+
} else if p.peek().Kind == If || p.peek().Kind == While || p.peek().Kind == For {
234+
// 顶层裸控制流 → 自动封装为隐式 def __init__() -> () { … }(fix-037)
235+
f.InitBody = p.parseBody()
233236
} else {
234237
prevPos := p.pos
235238
inst := p.parseInst()

0 commit comments

Comments
 (0)