94 lines
2.2 KiB
Go
94 lines
2.2 KiB
Go
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
|
// All rights reserved.
|
|
|
|
// operator-post-inc-dec.go
|
|
package expr
|
|
|
|
// -------- post increment term
|
|
|
|
func newPostIncTerm(tk *Token) *term {
|
|
return &term{
|
|
tk: *tk,
|
|
parent: nil,
|
|
children: make([]*term, 0, 1),
|
|
position: posPostfix,
|
|
priority: priIncDec,
|
|
evalFunc: evalPostInc,
|
|
}
|
|
}
|
|
|
|
func evalPostInc(ctx ExprContext, opTerm *term) (v any, err error) {
|
|
var childValue any
|
|
if childValue, err = opTerm.evalPrefix(ctx); err != nil {
|
|
return
|
|
}
|
|
|
|
if it, ok := childValue.(Iterator); ok {
|
|
var namePrefix string
|
|
v, err = it.Next()
|
|
|
|
if opTerm.children[0].symbol() == SymVariable {
|
|
namePrefix = opTerm.children[0].source()
|
|
}
|
|
ctx.UnsafeSetVar(namePrefix+"_index", it.Index())
|
|
if c, err1 := it.Current(); err1 == nil {
|
|
ctx.UnsafeSetVar(namePrefix+"_current", c)
|
|
}
|
|
|
|
if it.HasOperation(KeyName) {
|
|
if k, err1 := it.CallOperation(KeyName, nil); err1 == nil {
|
|
ctx.UnsafeSetVar(namePrefix+"_key", k)
|
|
}
|
|
}
|
|
if it.HasOperation(ValueName) {
|
|
if v1, err1 := it.CallOperation(ValueName, nil); err1 == nil {
|
|
ctx.UnsafeSetVar(namePrefix+"_value", v1)
|
|
}
|
|
}
|
|
} else if IsInteger(childValue) && opTerm.children[0].symbol() == SymVariable {
|
|
v = childValue
|
|
i, _ := childValue.(int64)
|
|
ctx.SetVar(opTerm.children[0].source(), i+1)
|
|
} else {
|
|
err = opTerm.errIncompatibleType(childValue)
|
|
}
|
|
return
|
|
}
|
|
|
|
// -------- post decrement term
|
|
|
|
func newPostDecTerm(tk *Token) *term {
|
|
return &term{
|
|
tk: *tk,
|
|
parent: nil,
|
|
children: make([]*term, 0, 1),
|
|
position: posPostfix,
|
|
priority: priIncDec,
|
|
evalFunc: evalPostDec,
|
|
}
|
|
}
|
|
|
|
func evalPostDec(ctx ExprContext, opTerm *term) (v any, err error) {
|
|
var childValue any
|
|
if childValue, err = opTerm.evalPrefix(ctx); err != nil {
|
|
return
|
|
}
|
|
|
|
/* if it, ok := childValue.(Iterator); ok {
|
|
v, err = it.Next()
|
|
} else */if IsInteger(childValue) && opTerm.children[0].symbol() == SymVariable {
|
|
v = childValue
|
|
i, _ := childValue.(int64)
|
|
ctx.SetVar(opTerm.children[0].source(), i-1)
|
|
} else {
|
|
err = opTerm.errIncompatibleType(childValue)
|
|
}
|
|
return
|
|
}
|
|
|
|
// init
|
|
func init() {
|
|
registerTermConstructor(SymDoublePlus, newPostIncTerm)
|
|
registerTermConstructor(SymDoubleMinus, newPostDecTerm)
|
|
}
|