2024-04-21 14:24:56 +02:00
|
|
|
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
|
|
|
// All rights reserved.
|
|
|
|
|
|
|
|
// operand-dict.go
|
|
|
|
package expr
|
|
|
|
|
2024-05-19 01:20:04 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DictType map[any]any
|
|
|
|
|
|
|
|
func newDict(dictAny []any) (dict *DictType) {
|
|
|
|
if dictAny != nil {
|
|
|
|
d := make(DictType, len(dictAny))
|
|
|
|
for i, item := range dictAny {
|
|
|
|
d[i] = item
|
|
|
|
}
|
|
|
|
dict = &d
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dict *DictType) toMultiLine(sb *strings.Builder, indent int) {
|
|
|
|
first := true
|
|
|
|
for name, value := range *dict {
|
|
|
|
if first {
|
|
|
|
first = false
|
|
|
|
} else {
|
|
|
|
sb.WriteByte(',')
|
|
|
|
sb.WriteByte('\n')
|
|
|
|
}
|
|
|
|
|
|
|
|
sb.WriteString(strings.Repeat("\t", indent+1))
|
|
|
|
if key, ok := name.(string); ok {
|
|
|
|
sb.WriteString(key)
|
|
|
|
} else {
|
|
|
|
sb.WriteString(fmt.Sprintf("%v", name))
|
|
|
|
}
|
|
|
|
sb.WriteString(": ")
|
|
|
|
if f, ok := value.(Formatter); ok {
|
|
|
|
sb.WriteString(f.ToString(0))
|
|
|
|
} else if _, ok = value.(Functor); ok {
|
|
|
|
sb.WriteString("func(){}")
|
|
|
|
} else {
|
|
|
|
sb.WriteString(fmt.Sprintf("%v", value))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sb.WriteString(strings.Repeat("\t", indent))
|
|
|
|
sb.WriteString("\n}\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dict *DictType) ToString(opt FmtOpt) string {
|
|
|
|
var sb strings.Builder
|
|
|
|
if opt&MultiLine != 0 {
|
|
|
|
dict.toMultiLine(&sb, 0)
|
|
|
|
}
|
|
|
|
return sb.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dict *DictType) hasKey(d map[any]any, target any) (ok bool) {
|
|
|
|
for key := range *dict {
|
|
|
|
if ok = reflect.DeepEqual(key, target); ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-21 14:24:56 +02:00
|
|
|
// -------- dict term
|
|
|
|
func newDictTerm(args map[any]*term) *term {
|
|
|
|
return &term{
|
|
|
|
tk: *NewValueToken(0, 0, SymDict, "{}", args),
|
|
|
|
parent: nil,
|
|
|
|
children: nil,
|
|
|
|
position: posLeaf,
|
|
|
|
priority: priValue,
|
|
|
|
evalFunc: evalDict,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------- dict func
|
|
|
|
func evalDict(ctx ExprContext, self *term) (v any, err error) {
|
|
|
|
dict, _ := self.value().(map[any]*term)
|
|
|
|
items := make(map[any]any, len(dict))
|
|
|
|
for key, tree := range dict {
|
|
|
|
var param any
|
|
|
|
if param, err = tree.compute(ctx); err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
items[key] = param
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
v = items
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|