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

// operand-literal.go
package expr

import (
	"git.portale-stac.it/go-pkg/expr/kern"
	"git.portale-stac.it/go-pkg/expr/scan"
	"git.portale-stac.it/go-pkg/expr/sym"
	"git.portale-stac.it/go-pkg/expr/types/fract"
)

// -------- literal term
func newLiteralTerm(tk *scan.Token) *scan.Term {

	if tk.Sym == sym.SymFraction {
		if v, isString := tk.Value.(string); isString {
			var err error
			if tk.Value, err = fract.MakeGeneratingFraction(v); err != nil {
				return nil
			}
		}
	}

	return &scan.Term{
		Tk:       *tk,
		Parent:   nil,
		Children: nil,
		Position: scan.PosLeaf,
		Priority: scan.PriValue,
		EvalFunc: evalLiteral,
	}
}

// -------- eval func
func evalLiteral(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
	v = opTerm.Tk.Value
	return
}

// init
func init() {
	scan.RegisterTermConstructor(sym.SymString, newLiteralTerm)
	scan.RegisterTermConstructor(sym.SymInteger, newLiteralTerm)
	scan.RegisterTermConstructor(sym.SymFloat, newLiteralTerm)
	scan.RegisterTermConstructor(sym.SymFraction, newLiteralTerm)
	scan.RegisterTermConstructor(sym.SymBool, newLiteralTerm)
	scan.RegisterTermConstructor(sym.SymKwNil, newLiteralTerm)
}
