MCPcopy Index your code
hub / github.com/expr-lang/expr / MemberNode

Method MemberNode

compiler/compiler.go:682–759  ·  view source on GitHub ↗
(node *ast.MemberNode)

Source from the content-addressed store, hash-verified

680}
681
682func (c *compiler) MemberNode(node *ast.MemberNode) {
683 var env Nature
684 if c.config != nil {
685 env = c.config.Env
686 }
687
688 if ok, index, name := checker.MethodIndex(c.ntCache, env, node); ok {
689 c.compile(node.Node)
690 c.emit(OpMethod, c.addConstant(&runtime.Method{
691 Name: name,
692 Index: index,
693 }))
694 return
695 }
696 op := OpFetch
697 base := node.Node
698
699 ok, index, nodeName := checker.FieldIndex(c.ntCache, env, node)
700 path := []string{nodeName}
701
702 if ok {
703 op = OpFetchField
704 for !node.Optional {
705 if ident, isIdent := base.(*ast.IdentifierNode); isIdent {
706 if ok, identIndex, name := checker.FieldIndex(c.ntCache, env, ident); ok {
707 index = append(identIndex, index...)
708 path = append([]string{name}, path...)
709 c.emitLocation(ident.Location(), OpLoadField, c.addConstant(
710 &runtime.Field{Index: index, Path: path},
711 ))
712 return
713 }
714 }
715
716 if member, isMember := base.(*ast.MemberNode); isMember {
717 if ok, memberIndex, name := checker.FieldIndex(c.ntCache, env, member); ok {
718 index = append(memberIndex, index...)
719 path = append([]string{name}, path...)
720 node = member
721 base = member.Node
722 } else {
723 break
724 }
725 } else {
726 break
727 }
728 }
729 }
730
731 c.compile(base)
732 // If the field is optional, we need to jump over the fetch operation.
733 // If no ChainNode (none c.chains) is used, do not compile the optional fetch.
734 if node.Optional && len(c.chains) > 0 {
735 ph := c.emit(OpJumpIfNil, placeholder)
736 c.chains[len(c.chains)-1] = append(c.chains[len(c.chains)-1], ph)
737 }
738
739 if op == OpFetch {

Callers 1

compileMethod · 0.95

Calls 11

compileMethod · 0.95
emitMethod · 0.95
addConstantMethod · 0.95
emitLocationMethod · 0.95
derefInNeededMethod · 0.95
MethodIndexFunction · 0.92
FieldIndexFunction · 0.92
KeyMethod · 0.80
AssignableToMethod · 0.80
LocationMethod · 0.65
TypeMethod · 0.65

Tested by

no test coverage detected