// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
// All rights reserved.

// operand-list.go
package expr

// -------- list term
func newListTermA(args ...*term) *term {
	return newListTerm(0, 0, args)
}

func newListTerm(row, col int, args []*term) *term {
	return &term{
		tk:       *NewValueToken(row, col, SymList, "[]", args),
		parent:   nil,
		children: nil,
		position: posLeaf,
		priority: priValue,
		evalFunc: evalList,
	}
}

// -------- list func
func evalList(ctx ExprContext, self *term) (v any, err error) {
	list, _ := self.value().([]*term)
	items := make(ListType, len(list))
	for i, tree := range list {
		var param any
		if param, err = tree.compute(ctx); err != nil {
			break
		}
		items[i] = param
	}
	if err == nil {
		v = &items
	}
	return
}
