the new ListType type takes the of []any

This commit is contained in:
Celestino Amoroso 2024-05-01 21:47:27 +02:00
parent 2d0d03b975
commit 49904f9097
3 changed files with 57 additions and 32 deletions

15
formatter.go Normal file
View File

@ -0,0 +1,15 @@
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
// All rights reserved.
// formatter.go
package expr
type FmtOpt uint16
const (
tty FmtOpt = 1 << iota
)
type Formatter interface {
ToString(options FmtOpt) string
}

View File

@ -7,11 +7,11 @@ package expr
import "io"
type FlatArrayIterator struct {
a []any
a ListType
index int
}
func NewFlatArrayIterator(array []any) *FlatArrayIterator {
func NewFlatArrayIterator(array ListType) *FlatArrayIterator {
return &FlatArrayIterator{a: array, index: 0}
}

View File

@ -4,6 +4,44 @@
// operand-list.go
package expr
import (
"fmt"
"strings"
)
type ListType []any
func (ls *ListType) ToString(opt FmtOpt) string {
var sb strings.Builder
sb.WriteByte('[')
if len(*ls) > 0 {
if opt&MultiLine != 0 {
sb.WriteString("\n ")
}
for i, item := range []any(*ls) {
if i > 0 {
if opt&MultiLine != 0 {
sb.WriteString(",\n ")
} else {
sb.WriteString(", ")
}
}
if s, ok := item.(string); ok {
sb.WriteByte('"')
sb.WriteString(s)
sb.WriteByte('"')
} else {
sb.WriteString(fmt.Sprintf("%v", item))
}
}
if opt&MultiLine != 0 {
sb.WriteByte('\n')
}
}
sb.WriteByte(']')
return sb.String()
}
// -------- list term
func newListTermA(args ...*term) *term {
return newListTerm(args)
@ -23,7 +61,7 @@ func newListTerm(args []*term) *term {
// -------- list func
func evalList(ctx ExprContext, self *term) (v any, err error) {
list, _ := self.value().([]*term)
items := make([]any, len(list))
items := make(ListType, len(list))
for i, tree := range list {
var param any
if param, err = tree.compute(ctx); err != nil {
@ -32,35 +70,7 @@ func evalList(ctx ExprContext, self *term) (v any, err error) {
items[i] = param
}
if err == nil {
v = items
v = &items
}
return
}
// // -------- list term
// func newListTerm(args []*term) *term {
// return &term{
// tk: *NewToken(0, 0, SymList, "[]"),
// parent: nil,
// children: args,
// position: posLeaf,
// priority: priValue,
// evalFunc: evalList,
// }
// }
// // -------- list func
// func evalList(ctx ExprContext, self *term) (v any, err error) {
// items := make([]any, len(self.children))
// for i, tree := range self.children {
// var param any
// if param, err = tree.compute(ctx); err != nil {
// break
// }
// items[i] = param
// }
// if err == nil {
// v = items
// }
// return
// }