我正在Go中编写一个解析器,并且我有以下用于ast构造的代码:
type NODE interface {
GetPosition() (int, int)
}
type EXPRESSION_NODE interface {
NODE
expressionNode()
}
// binary node
type BINARY_EXPRESSION struct {
Operator string
Left, Right EXPRESSION_NODE
Position
}
// implementing the EXPRESSION_NODE interface for BINARY_EXPRESSION
func (s BINARY_EXPRESSION) expressionNode()
func (s BINARY_EXPRESSION) GetPosition() (int, int) { return s.Line, s.Column }
所以我有一个接口EXPRESSION_NODE,BINARY_EXPRESSION结构,这个结构实现了所有的接口方法。
这是我的困惑:
func (self *Parser) ParseBinaryExpression(
operators []string,
parser ExpressionParser,
) (*EXPRESSION_NODE, error) {
self.SetPosition()
result, err := parser()
if err != nil {
return nil, ChainErrs(self.Err(BINARY_EXPRESSION_ERROR), err)
}
for Includes(operators, self.stream.Peek().Literal) {
operator := self.stream.Next().Literal
right, err := parser()
if err != nil {
return nil, ChainErrs(self.Err(BINARY_EXPRESSION_ERROR), err)
//ERROR: cannot use &(BINARY_EXPRESSION literal) (value of type *BINARY_EXPRESSION) as *EXPRESSION_NODE value in assignment
result = &BINARY_EXPRESSION{operator, result, right, self.position}
}
}
return result, nil
}
所以这条线
result = &BINARY_EXPRESSION{operator, result, right, self.position}
给我以下错误:cannot use &(BINARY_EXPRESSION literal) (value of type *BINARY_EXPRESSION) as *EXPRESSION_NODE value in assignment
所以当我这样做
var a EXPRESSION_NODE = &BINARY_EXPRESSION{}
没有指针,一切似乎都可以,但有指针...问题是我实际上需要从我的函数中返回*EXPRESSION_NODE
慕雪6442864
相关分类