1
我有自动生成的代码。简化版本:初始化周期错误
package main
// begin of A
func main(){
ParseReader("x")
}
func parseInclude(fileName string) (interface{}, error) {
got, _ := ParseReader(fileName)
return got, nil
}
// end of A
type grammar struct {
pos int
run func(*parser) (interface{}, error)
}
var g = &grammar{
pos: 1,
run: (*parser).callonIncludeOp1,
}
type parser struct {
filename string
cur current
}
func (p *parser) callonIncludeOp1() (interface{}, error) {
return p.cur.onIncludeOp1("x")
}
func (p *parser) parse(g *grammar) (val interface{}, err error) {
return g.pos, nil
}
type current struct {
pos int
}
// B
func (c *current) onIncludeOp1(qfilename interface{}) (interface{}, error) {
got, _ := parseInclude("x")
return got, nil
}
func ParseReader(filename string) (interface{}, error) {
p := &parser{ filename: filename }
return p.parse(g)
}
我有错误编译
./prog.go:19: initialization loop:
/home/gCDfp4/prog.go:19 g refers to
/home/gCDfp4/prog.go:25 (*parser).callonIncludeOp1 refers to
/home/gCDfp4/prog.go:36 (*current).onIncludeOp1 refers to
/home/gCDfp4/prog.go:7 parseInclude refers to
/home/gCDfp4/prog.go:41 ParseReader refers to
/home/gCDfp4/prog.go:19 g
后,我需要在语法做递归调用,因为我有预处理操作符“的#include”解析其他文件。
因为它是自动生成的代码,我只能修改块A或B.功能代码
我怎样才能打破初始化周期?
感谢。我打破与 变种gProxy语法 FUNC的init(){ gProxy =克 } FUNC parseInclude(文件名的字符串)(接口{},错误){ 得到循环,_:= ParseReaderProxy(文件名) 回报得到的, nil } func ParseReaderProxy(filename string)(interface {},error){ p:=&parser {filename:filename} return p。解析(gProxy) } 你的答案是其他方式,但似乎更好。 –
@RedSkotina这确实会起作用。我已经将您的方法纳入答案中,以提高可见性(以及正确的格式) – VonC