new operator '111123' that returns the content of the current context or the context of an iterator

This commit is contained in:
Celestino Amoroso 2024-04-27 06:12:30 +02:00
parent 894b1884eb
commit 10eec286fa
3 changed files with 55 additions and 3 deletions

49
operator-context.go Normal file
View File

@ -0,0 +1,49 @@
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
// All rights reserved.
// operator-context-value.go
package expr
//-------- context term
func newContextTerm(tk *Token) (inst *term) {
return &term{
tk: *tk,
children: make([]*term, 0, 1),
position: posPrefix,
priority: priPrePost,
evalFunc: evalContextValue,
}
}
func evalContextValue(ctx ExprContext, self *term) (v any, err error) {
var childValue any
var sourceCtx ExprContext
if len(self.children) == 0 {
sourceCtx = ctx
} else if childValue, err = self.evalPrefix(ctx); err == nil {
if dc, ok := childValue.(*dataCursor); ok {
sourceCtx = dc.ctx
}
} else {
return
}
if sourceCtx != nil {
keys := sourceCtx.EnumVars(func(name string) bool { return name[0] != '_' })
d := make(map[string]any)
for _, key := range keys {
d[key], _ = sourceCtx.GetVar(key)
}
v = d
} else {
err = self.errIncompatibleType(childValue)
}
return
}
// init
func init() {
registerTermConstructor(SymDoubleDollar, newContextTerm)
}

View File

@ -242,6 +242,8 @@ func (self *scanner) fetchNextToken() (tk *Token) {
if next, _ := self.peek(); next == '(' { if next, _ := self.peek(); next == '(' {
tk = self.moveOn(SymDollarRound, ch, next) tk = self.moveOn(SymDollarRound, ch, next)
tk.source += ")" tk.source += ")"
} else if next == '$' {
tk = self.moveOn(SymDoubleDollar, ch, next)
} else { } else {
tk = self.makeToken(SymDollar, ch) tk = self.makeToken(SymDollar, ch)
} }
@ -249,8 +251,8 @@ func (self *scanner) fetchNextToken() (tk *Token) {
if next, _ := self.peek(); next == ')' { if next, _ := self.peek(); next == ')' {
tk = self.moveOn(SymOpenClosedRound, ch, next) tk = self.moveOn(SymOpenClosedRound, ch, next)
} else { } else {
tk = self.makeToken(SymOpenRound, ch) tk = self.makeToken(SymOpenRound, ch)
} }
case ')': case ')':
tk = self.makeToken(SymClosedRound, ch) tk = self.makeToken(SymClosedRound, ch)
case '[': case '[':

View File

@ -63,7 +63,8 @@ const (
SymAppend // 52: '<<' SymAppend // 52: '<<'
SymCaret // 53: '^' SymCaret // 53: '^'
SymDollarRound // 54: '$(' SymDollarRound // 54: '$('
SymOpenClosedRound // 55: '()' SymOpenClosedRound // 55: '()'
SymDoubleDollar // 56: '$$
SymChangeSign SymChangeSign
SymUnchangeSign SymUnchangeSign
SymIdentifier SymIdentifier