moved a subset of source file to the kern package
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// bind-go-function.go
|
||||
package kern
|
||||
|
||||
// ---- Linking with Go functions
|
||||
type golangFunctor struct {
|
||||
BaseFunctor
|
||||
f FuncTemplate
|
||||
}
|
||||
|
||||
func NewGolangFunctor(f FuncTemplate) *golangFunctor {
|
||||
return &golangFunctor{f: f}
|
||||
}
|
||||
|
||||
func (functor *golangFunctor) TypeName() string {
|
||||
return "GoFunctor"
|
||||
}
|
||||
|
||||
func (functor *golangFunctor) InvokeNamed(ctx ExprContext, name string, args map[string]any) (result any, err error) {
|
||||
return functor.f(ctx, name, args)
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// common-errors.go
|
||||
package kern
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func ErrMissingParams(funcName string, missing []string) (err error) {
|
||||
return fmt.Errorf("%s(): missing params -- %s", funcName, strings.Join(missing, ", "))
|
||||
}
|
||||
|
||||
func ErrTooFewParams(funcName string, minArgs, maxArgs, argCount int) (err error) {
|
||||
if maxArgs < 0 {
|
||||
err = fmt.Errorf("%s(): too few params -- expected %d or more, got %d", funcName, minArgs, argCount)
|
||||
} else {
|
||||
err = fmt.Errorf("%s(): too few params -- expected %d, got %d", funcName, minArgs, argCount)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func ErrTooManyParams(funcName string, maxArgs, argCount int) (err error) {
|
||||
err = fmt.Errorf("%s(): too many params -- expected %d, got %d", funcName, maxArgs, argCount)
|
||||
return
|
||||
}
|
||||
|
||||
// --- General errors
|
||||
|
||||
func ErrCantConvert(funcName string, value any, kind string) error {
|
||||
return fmt.Errorf("%s(): can't convert %s to %s", funcName, TypeName(value), kind)
|
||||
}
|
||||
|
||||
func ErrExpectedGot(funcName string, kind string, value any) error {
|
||||
return fmt.Errorf("%s(): expected %s, got %s (%v)", funcName, kind, TypeName(value), value)
|
||||
}
|
||||
|
||||
func ErrFuncDivisionByZero(funcName string) error {
|
||||
return fmt.Errorf("%s(): division by zero", funcName)
|
||||
}
|
||||
|
||||
// func ErrDivisionByZero() error {
|
||||
// return fmt.Errorf("division by zero")
|
||||
// }
|
||||
|
||||
// --- Parameter errors
|
||||
|
||||
// func ErrMissingRequiredParameter(funcName, paramName string) error {
|
||||
// return fmt.Errorf("%s(): missing required parameter %q", funcName, paramName)
|
||||
// }
|
||||
|
||||
func ErrInvalidParameterValue(funcName, paramName string, paramValue any) error {
|
||||
return fmt.Errorf("%s(): invalid value %s (%v) for parameter %q", funcName, TypeName(paramValue), paramValue, paramName)
|
||||
}
|
||||
|
||||
func undefArticle(s string) (article string) {
|
||||
if len(s) > 0 && strings.Contains("aeiou", s[0:1]) {
|
||||
article = "an"
|
||||
} else {
|
||||
article = "a"
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func prependUndefArticle(s string) (result string) {
|
||||
return undefArticle(s) + " " + s
|
||||
}
|
||||
|
||||
func ErrWrongParamType(funcName, paramName, paramType string, paramValue any) error {
|
||||
var artWantType, artGotType string
|
||||
gotType := TypeName(paramValue)
|
||||
artGotType = prependUndefArticle(gotType)
|
||||
artWantType = prependUndefArticle(paramType)
|
||||
return fmt.Errorf("%s(): the %q parameter must be %s, got %s (%v)", funcName, paramName, artWantType, artGotType, paramValue)
|
||||
}
|
||||
|
||||
func ErrUnknownParam(funcName, paramName string) error {
|
||||
return fmt.Errorf("%s(): unknown parameter %q", funcName, paramName)
|
||||
}
|
||||
|
||||
func ErrUnknownVar(funcName, varName string) error {
|
||||
return fmt.Errorf("%s(): unknown variable %q", funcName, varName)
|
||||
}
|
||||
|
||||
// --- Operator errors
|
||||
|
||||
func ErrLeftOperandMustBeVariable(leftTerm, opTerm Term) error {
|
||||
return leftTerm.Errorf("left operand of %q must be a variable", opTerm.Source())
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// common-params.go
|
||||
package kern
|
||||
|
||||
const (
|
||||
ParamArgs = "args"
|
||||
ParamCount = "count"
|
||||
ParamItem = "item"
|
||||
ParamIndex = "index"
|
||||
ParamParts = "parts"
|
||||
ParamSeparator = "separator"
|
||||
ParamSource = "source"
|
||||
ParamSuffix = "suffix"
|
||||
ParamPrefix = "prefix"
|
||||
ParamStart = "start"
|
||||
ParamEnd = "end"
|
||||
ParamValue = "value"
|
||||
ParamName = "name"
|
||||
ParamEllipsis = "..."
|
||||
ParamFilepath = "filepath"
|
||||
ParamDirpath = "dirpath"
|
||||
ParamHandle = "handle"
|
||||
ParamResource = "resource"
|
||||
ParamIterator = "iterator"
|
||||
)
|
||||
|
||||
// to be moved in its own source file
|
||||
const (
|
||||
ConstLastIndex = 0xFFFF_FFFF
|
||||
)
|
||||
@@ -0,0 +1,23 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// common-type-names.go
|
||||
package kern
|
||||
|
||||
const (
|
||||
TypeAny = "any"
|
||||
TypeNil = "nil"
|
||||
TypeBoolean = "boolean"
|
||||
TypeFloat = "float"
|
||||
TypeFraction = "fraction"
|
||||
TypeFileHandle = "file-handle"
|
||||
TypeInt = "integer"
|
||||
TypeItem = "item"
|
||||
TypeIterator = "iterator"
|
||||
TypeNumber = "number"
|
||||
TypePair = "pair"
|
||||
TypeString = "string"
|
||||
TypeDict = "dict"
|
||||
TypeListOf = "list-of-"
|
||||
TypeListOfStrings = "list-of-strings"
|
||||
)
|
||||
@@ -0,0 +1,49 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// context-helpers.go
|
||||
package kern
|
||||
|
||||
func CloneContext(sourceCtx ExprContext) (clonedCtx ExprContext) {
|
||||
if sourceCtx != nil {
|
||||
clonedCtx = sourceCtx.Clone()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func exportVar(ctx ExprContext, name string, value any) {
|
||||
if name[0] == '@' {
|
||||
name = name[1:]
|
||||
}
|
||||
ctx.UnsafeSetVar(name, value)
|
||||
}
|
||||
|
||||
func exportFunc(ctx ExprContext, name string, info ExprFunc) {
|
||||
if name[0] == '@' {
|
||||
name = name[1:]
|
||||
}
|
||||
ctx.RegisterFunc(name, info.Functor(), info.ReturnType(), info.Params())
|
||||
}
|
||||
|
||||
func ExportObjects(destCtx, sourceCtx ExprContext) {
|
||||
exportAll := CtrlIsEnabled(sourceCtx, ControlExportAll)
|
||||
// fmt.Printf("Exporting from sourceCtx [%p] to destCtx [%p] -- exportAll=%t\n", sourceCtx, destCtx, exportAll)
|
||||
// Export variables
|
||||
for _, refName := range sourceCtx.EnumVars(func(name string) bool { return (exportAll || name[0] == '@') && !(name[0] == '_') }) {
|
||||
// fmt.Printf("\tExporting %q\n", refName)
|
||||
refValue, _ := sourceCtx.GetVar(refName)
|
||||
exportVar(destCtx, refName, refValue)
|
||||
}
|
||||
// Export functions
|
||||
for _, refName := range sourceCtx.EnumFuncs(func(name string) bool { return exportAll || name[0] == '@' }) {
|
||||
if info, _ := sourceCtx.GetFuncInfo(refName); info != nil {
|
||||
exportFunc(destCtx, refName, info)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func exportObjectsToParent(sourceCtx ExprContext) {
|
||||
if parentCtx := sourceCtx.GetParent(); parentCtx != nil {
|
||||
ExportObjects(parentCtx, sourceCtx)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// control.go
|
||||
package kern
|
||||
|
||||
import "strings"
|
||||
|
||||
// Preset control variables
|
||||
const (
|
||||
ControlPreset = "_preset"
|
||||
ControlLastResult = "last"
|
||||
ControlBoolShortcut = "_bool_shortcut"
|
||||
ControlSearchPath = "_search_path"
|
||||
ControlParentContext = "_parent_context"
|
||||
ControlStdout = "_stdout"
|
||||
)
|
||||
|
||||
// Other control variables
|
||||
const (
|
||||
ControlExportAll = "_export_all"
|
||||
)
|
||||
|
||||
// Initial values
|
||||
const (
|
||||
init_search_path = "~/.local/lib/go-pkg/expr:/usr/local/lib/go-pkg/expr:/usr/lib/go-pkg/expr"
|
||||
)
|
||||
|
||||
func CtrlIsEnabled(ctx ExprContext, name string) (status bool) {
|
||||
var v any
|
||||
var exists bool
|
||||
|
||||
if !strings.HasPrefix(name, "_") {
|
||||
name = "_" + name
|
||||
}
|
||||
|
||||
if v, exists = ctx.GetVar(name); !exists {
|
||||
if globalCtx := ctx.GetGlobal(); globalCtx != nil {
|
||||
v, exists = globalCtx.GetVar(name)
|
||||
}
|
||||
}
|
||||
|
||||
if exists {
|
||||
if b, ok := v.(bool); ok {
|
||||
status = b
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
func SetCtrl(ctx ExprContext, name string, value any) (current any) {
|
||||
current, _ = ctx.GetVar(name)
|
||||
ctx.UnsafeSetVar(name, value)
|
||||
return
|
||||
}
|
||||
|
||||
func InitDefaultVars(ctx ExprContext) {
|
||||
if _, exists := ctx.GetVar(ControlPreset); exists {
|
||||
return
|
||||
}
|
||||
ctx.UnsafeSetVar(ControlPreset, true)
|
||||
ctx.UnsafeSetVar(ControlBoolShortcut, true)
|
||||
ctx.UnsafeSetVar(ControlSearchPath, init_search_path)
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// dict-type.go
|
||||
package kern
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type DictType map[any]any
|
||||
|
||||
func MakeDict() (dict *DictType) {
|
||||
d := make(DictType)
|
||||
dict = &d
|
||||
return
|
||||
}
|
||||
|
||||
func NewDict(dictAny map[any]any) (dict *DictType) {
|
||||
var d DictType
|
||||
if dictAny != nil {
|
||||
d = make(DictType, len(dictAny))
|
||||
for i, item := range dictAny {
|
||||
d[i] = item
|
||||
}
|
||||
} else {
|
||||
d = make(DictType)
|
||||
}
|
||||
dict = &d
|
||||
return
|
||||
}
|
||||
|
||||
func newDict(dictAny map[any]Term) (dict *DictType) {
|
||||
// TODO Change with a call to NewDict()
|
||||
var d DictType
|
||||
if dictAny != nil {
|
||||
d = make(DictType, len(dictAny))
|
||||
for i, item := range dictAny {
|
||||
d[i] = item
|
||||
}
|
||||
} else {
|
||||
d = make(DictType)
|
||||
}
|
||||
dict = &d
|
||||
return
|
||||
}
|
||||
|
||||
func (dict *DictType) toMultiLine(sb *strings.Builder, opt FmtOpt) {
|
||||
indent := GetFormatIndent(opt)
|
||||
flags := GetFormatFlags(opt)
|
||||
//sb.WriteString(strings.Repeat(" ", indent))
|
||||
sb.WriteByte('{')
|
||||
|
||||
if len(*dict) > 0 {
|
||||
innerOpt := MakeFormatOptions(flags, indent+1)
|
||||
nest := strings.Repeat(" ", indent+1)
|
||||
sb.WriteByte('\n')
|
||||
|
||||
first := true
|
||||
for name, value := range *dict {
|
||||
if first {
|
||||
first = false
|
||||
} else {
|
||||
sb.WriteByte(',')
|
||||
sb.WriteByte('\n')
|
||||
}
|
||||
|
||||
sb.WriteString(nest)
|
||||
if key, ok := name.(string); ok {
|
||||
sb.WriteString(string('"') + key + string('"'))
|
||||
} else {
|
||||
sb.WriteString(fmt.Sprintf("%v", name))
|
||||
}
|
||||
sb.WriteString(": ")
|
||||
if f, ok := value.(Formatter); ok {
|
||||
sb.WriteString(f.ToString(innerOpt))
|
||||
} else if _, ok = value.(Functor); ok {
|
||||
sb.WriteString("func(){}")
|
||||
} else {
|
||||
sb.WriteString(fmt.Sprintf("%v", value))
|
||||
}
|
||||
}
|
||||
sb.WriteByte('\n')
|
||||
sb.WriteString(strings.Repeat(" ", indent))
|
||||
}
|
||||
sb.WriteString("}")
|
||||
}
|
||||
|
||||
func (dict *DictType) ToString(opt FmtOpt) string {
|
||||
var sb strings.Builder
|
||||
flags := GetFormatFlags(opt)
|
||||
if flags&MultiLine != 0 {
|
||||
dict.toMultiLine(&sb, opt)
|
||||
} else {
|
||||
sb.WriteByte('{')
|
||||
first := true
|
||||
for key, value := range *dict {
|
||||
if first {
|
||||
first = false
|
||||
} else {
|
||||
sb.WriteString(", ")
|
||||
}
|
||||
if s, ok := key.(string); ok {
|
||||
sb.WriteString(string('"') + s + string('"'))
|
||||
} else {
|
||||
sb.WriteString(fmt.Sprintf("%v", key))
|
||||
}
|
||||
sb.WriteString(": ")
|
||||
if formatter, ok := value.(Formatter); ok {
|
||||
sb.WriteString(formatter.ToString(opt))
|
||||
} else if t, ok := value.(Term); ok {
|
||||
sb.WriteString(t.String())
|
||||
} else {
|
||||
sb.WriteString(fmt.Sprintf("%#v", value))
|
||||
}
|
||||
}
|
||||
sb.WriteByte('}')
|
||||
}
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
func (dict *DictType) String() string {
|
||||
return dict.ToString(0)
|
||||
}
|
||||
|
||||
func (dict *DictType) TypeName() string {
|
||||
return "dict"
|
||||
}
|
||||
|
||||
func (dict *DictType) HasKey(target any) (ok bool) {
|
||||
for key := range *dict {
|
||||
if ok = reflect.DeepEqual(key, target); ok {
|
||||
break
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (dict *DictType) Clone() (c *DictType) {
|
||||
c = newDict(nil)
|
||||
for k, v := range *dict {
|
||||
(*c)[k] = v
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (dict *DictType) Merge(second *DictType) {
|
||||
if second != nil {
|
||||
for k, v := range *second {
|
||||
(*dict)[k] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (dict *DictType) SetItem(key any, value any) (err error) {
|
||||
(*dict)[key] = value
|
||||
return
|
||||
}
|
||||
|
||||
////////////////
|
||||
|
||||
type DictFormat interface {
|
||||
ToDict() *DictType
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// expr-context.go
|
||||
package kern
|
||||
|
||||
// ----Expression Context
|
||||
type ExprContext interface {
|
||||
Clone() ExprContext
|
||||
SetParent(ctx ExprContext)
|
||||
GetParent() (ctx ExprContext)
|
||||
GetGlobal() (ctx ExprContext)
|
||||
GetVar(varName string) (value any, exists bool)
|
||||
GetLast() any
|
||||
SetVar(varName string, value any)
|
||||
UnsafeSetVar(varName string, value any)
|
||||
|
||||
EnumVars(func(name string) (accept bool)) (varNames []string)
|
||||
VarCount() int
|
||||
DeleteVar(varName string)
|
||||
|
||||
EnumFuncs(func(name string) (accept bool)) (funcNames []string)
|
||||
FuncCount() int
|
||||
DeleteFunc(funcName string)
|
||||
|
||||
GetLocalFuncInfo(name string) (info ExprFunc, exists bool)
|
||||
GetFuncInfo(name string) (item ExprFunc, exists bool)
|
||||
Call(name string, args map[string]any) (result any, err error)
|
||||
RegisterFuncInfo(info ExprFunc)
|
||||
RegisterFunc(name string, f Functor, returnType string, param []ExprFuncParam) (funcInfo ExprFunc, err error)
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// expr-function.go
|
||||
package kern
|
||||
|
||||
// ---- Functor interface
|
||||
type Functor interface {
|
||||
Typer
|
||||
InvokeNamed(ctx ExprContext, name string, args map[string]any) (result any, err error)
|
||||
SetFunc(info ExprFunc)
|
||||
GetFunc() ExprFunc
|
||||
GetParams() []ExprFuncParam
|
||||
GetDefinitionContext() ExprContext
|
||||
}
|
||||
|
||||
// ---- Function Param Info
|
||||
type ExprFuncParam interface {
|
||||
Name() string
|
||||
Type() string
|
||||
IsDefault() bool
|
||||
IsOptional() bool
|
||||
IsRepeat() bool
|
||||
DefaultValue() any
|
||||
}
|
||||
|
||||
// ---- Function Info
|
||||
type ExprFunc interface {
|
||||
Formatter
|
||||
Name() string
|
||||
MinArgs() int
|
||||
MaxArgs() int
|
||||
Functor() Functor
|
||||
Params() []ExprFuncParam
|
||||
ParamSpec(paramName string) ExprFuncParam
|
||||
ReturnType() string
|
||||
PrepareCall(name string, actualParams map[string]any) (err error)
|
||||
AllocContext(parentCtx ExprContext) (ctx ExprContext)
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// formatter.go
|
||||
package kern
|
||||
|
||||
import "fmt"
|
||||
|
||||
type FmtOpt uint32 // lower 16 bits hold a bit-mask, higher 16 bits hold an indentation number
|
||||
|
||||
const (
|
||||
TTY FmtOpt = 1 << iota
|
||||
MultiLine
|
||||
Truncate
|
||||
Base2
|
||||
Base8
|
||||
Base10
|
||||
Base16
|
||||
)
|
||||
|
||||
const (
|
||||
TruncateEllipsis = "(...)"
|
||||
MinTruncateSize = 10
|
||||
TruncateSize = MinTruncateSize + 15
|
||||
)
|
||||
|
||||
func TruncateString(s string) (trunc string) {
|
||||
finalPart := len(s) - (MinTruncateSize - len(TruncateEllipsis))
|
||||
trunc = s[0:len(s)-MinTruncateSize] + TruncateEllipsis + s[finalPart:]
|
||||
return
|
||||
}
|
||||
|
||||
func MakeFormatOptions(flags FmtOpt, indent int) FmtOpt {
|
||||
return FmtOpt(indent<<16) | flags
|
||||
}
|
||||
|
||||
func GetFormatFlags(opt FmtOpt) FmtOpt {
|
||||
return opt & 0xFFFF
|
||||
}
|
||||
|
||||
func GetFormatIndent(opt FmtOpt) int {
|
||||
return int(opt >> 16)
|
||||
}
|
||||
|
||||
type Formatter interface {
|
||||
ToString(options FmtOpt) string
|
||||
}
|
||||
|
||||
func GetFormatted(v any, opt FmtOpt) (text string) {
|
||||
if v == nil {
|
||||
text = "(nil)"
|
||||
} else if s, ok := v.(string); ok {
|
||||
text = s
|
||||
} else if formatter, ok := v.(Formatter); ok {
|
||||
text = formatter.ToString(opt)
|
||||
} else {
|
||||
text = fmt.Sprintf("%v", v)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
type Typer interface {
|
||||
TypeName() string
|
||||
}
|
||||
|
||||
func TypeName(v any) (name string) {
|
||||
if v == nil {
|
||||
name = "nil"
|
||||
} else if typer, ok := v.(Typer); ok {
|
||||
name = typer.TypeName()
|
||||
} else if IsInteger(v) {
|
||||
name = "integer"
|
||||
} else if IsFloat(v) {
|
||||
name = "float"
|
||||
} else {
|
||||
name = fmt.Sprintf("%T", v)
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,369 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// fraction-type.go
|
||||
package kern
|
||||
|
||||
//https://www.youmath.it/lezioni/algebra-elementare/lezioni-di-algebra-e-aritmetica-per-scuole-medie/553-dalle-frazioni-a-numeri-decimali.html
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type FractionType struct {
|
||||
num, den int64
|
||||
}
|
||||
|
||||
func NewFraction(num, den int64) *FractionType {
|
||||
num, den = simplifyIntegers(num, den)
|
||||
return &FractionType{num, den}
|
||||
}
|
||||
|
||||
func Float64ToFraction(f float64) (fract *FractionType, err error) {
|
||||
var sign string
|
||||
intPart, decPart := math.Modf(f)
|
||||
if decPart < 0.0 {
|
||||
sign = "-"
|
||||
intPart = -intPart
|
||||
decPart = -decPart
|
||||
}
|
||||
dec := fmt.Sprintf("%.12f", decPart)
|
||||
s := fmt.Sprintf("%s%.f%s", sign, intPart, dec[1:])
|
||||
return MakeGeneratingFraction(s)
|
||||
}
|
||||
|
||||
// Based on https://cs.opensource.google/go/go/+/refs/tags/go1.22.3:src/math/big/rat.go;l=39
|
||||
func MakeGeneratingFraction(s string) (f *FractionType, err error) {
|
||||
var num, den int64
|
||||
var sign int64 = 1
|
||||
var parts []string
|
||||
if len(s) == 0 {
|
||||
goto exit
|
||||
}
|
||||
if s[0] == '-' {
|
||||
sign = int64(-1)
|
||||
s = s[1:]
|
||||
} else if s[0] == '+' {
|
||||
s = s[1:]
|
||||
}
|
||||
// if strings.HasSuffix(s, "()") {
|
||||
// s = s[0 : len(s)-2]
|
||||
// }
|
||||
s = strings.TrimSuffix(s, "()")
|
||||
parts = strings.SplitN(s, ".", 2)
|
||||
if num, err = strconv.ParseInt(parts[0], 10, 64); err != nil {
|
||||
return
|
||||
}
|
||||
if len(parts) == 1 {
|
||||
f = NewFraction(sign*num, 1)
|
||||
} else if len(parts) == 2 {
|
||||
subParts := strings.SplitN(parts[1], "(", 2)
|
||||
if len(subParts) == 1 {
|
||||
den = 1
|
||||
dec := parts[1]
|
||||
lsd := len(dec)
|
||||
for i := lsd - 1; i >= 0 && dec[i] == '0'; i-- {
|
||||
lsd--
|
||||
}
|
||||
for _, c := range dec[0:lsd] {
|
||||
if c < '0' || c > '9' {
|
||||
return nil, ErrExpectedGot("fract", "digit", c)
|
||||
}
|
||||
num = num*10 + int64(c-'0')
|
||||
den = den * 10
|
||||
}
|
||||
f = NewFraction(sign*num, den)
|
||||
} else if len(subParts) == 2 {
|
||||
sub := num
|
||||
mul := int64(1)
|
||||
for _, c := range subParts[0] {
|
||||
if c < '0' || c > '9' {
|
||||
return nil, ErrExpectedGot("fract", "digit", c)
|
||||
}
|
||||
num = num*10 + int64(c-'0')
|
||||
sub = sub*10 + int64(c-'0')
|
||||
mul *= 10
|
||||
}
|
||||
if len(subParts) == 2 {
|
||||
if s[len(s)-1] != ')' {
|
||||
goto exit
|
||||
}
|
||||
p := subParts[1][0 : len(subParts[1])-1]
|
||||
for _, c := range p {
|
||||
if c < '0' || c > '9' {
|
||||
return nil, ErrExpectedGot("fract", "digit", c)
|
||||
}
|
||||
num = num*10 + int64(c-'0')
|
||||
den = den*10 + 9
|
||||
}
|
||||
den *= mul
|
||||
}
|
||||
num -= sub
|
||||
f = NewFraction(sign*num, den)
|
||||
}
|
||||
}
|
||||
exit:
|
||||
if f == nil {
|
||||
err = errors.New("bad syntax")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (f *FractionType) N() int64 {
|
||||
return f.num
|
||||
}
|
||||
|
||||
func (f *FractionType) D() int64 {
|
||||
return f.den
|
||||
}
|
||||
|
||||
func (f *FractionType) ToFloat() float64 {
|
||||
return float64(f.num) / float64(f.den)
|
||||
}
|
||||
|
||||
func (f *FractionType) String() string {
|
||||
return f.ToString(0)
|
||||
}
|
||||
|
||||
func (f *FractionType) ToString(opt FmtOpt) string {
|
||||
var sb strings.Builder
|
||||
if opt&MultiLine == 0 {
|
||||
sb.WriteString(fmt.Sprintf("%d:%d", f.num, f.den))
|
||||
} else {
|
||||
var sign, num string
|
||||
if f.num < 0 && opt&TTY == 0 {
|
||||
num = strconv.FormatInt(-f.num, 10)
|
||||
sign = "-"
|
||||
} else {
|
||||
num = strconv.FormatInt(f.num, 10)
|
||||
}
|
||||
den := strconv.FormatInt(f.den, 10)
|
||||
size := max(len(num), len(den))
|
||||
if opt&TTY != 0 {
|
||||
sNum := fmt.Sprintf("\x1b[4m%[1]*s\x1b[0m\n", -size, fmt.Sprintf("%[1]*s", (size+len(num))/2, sign+num))
|
||||
sb.WriteString(sNum)
|
||||
} else {
|
||||
if len(sign) > 0 {
|
||||
sb.WriteString(" ")
|
||||
}
|
||||
sb.WriteString(fmt.Sprintf("%[1]*s", -size, fmt.Sprintf("%[1]*s", (size+len(num))/2, num)))
|
||||
sb.WriteByte('\n')
|
||||
if len(sign) > 0 {
|
||||
sb.WriteString(sign)
|
||||
sb.WriteByte(' ')
|
||||
}
|
||||
sb.WriteString(strings.Repeat("-", size))
|
||||
sb.WriteByte('\n')
|
||||
if len(sign) > 0 {
|
||||
sb.WriteString(" ")
|
||||
}
|
||||
}
|
||||
sDen := fmt.Sprintf("%[1]*s", size, fmt.Sprintf("%[1]*s", (size+len(den))/2, den))
|
||||
sb.WriteString(sDen)
|
||||
}
|
||||
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
func (f *FractionType) TypeName() string {
|
||||
return "fraction"
|
||||
}
|
||||
|
||||
// -------- fraction utility functions
|
||||
|
||||
// greatest common divider
|
||||
func Gcd(a, b int64) (g int64) {
|
||||
if a < 0 {
|
||||
a = -a
|
||||
}
|
||||
if b < 0 {
|
||||
b = -b
|
||||
}
|
||||
if a < b {
|
||||
a, b = b, a
|
||||
}
|
||||
r := a % b
|
||||
for r > 0 {
|
||||
a, b = b, r
|
||||
r = a % b
|
||||
}
|
||||
g = b
|
||||
return
|
||||
}
|
||||
|
||||
// lower common multiple
|
||||
func lcm(a, b int64) (l int64) {
|
||||
g := Gcd(a, b)
|
||||
l = a * b / g
|
||||
return
|
||||
}
|
||||
|
||||
// Sum two fractions
|
||||
func SumFract(f1, f2 *FractionType) (sum *FractionType) {
|
||||
m := lcm(f1.den, f2.den)
|
||||
sum = NewFraction(f1.num*(m/f1.den)+f2.num*(m/f2.den), m)
|
||||
return
|
||||
}
|
||||
|
||||
// Multiply two fractions
|
||||
func MulFract(f1, f2 *FractionType) (prod *FractionType) {
|
||||
prod = NewFraction(f1.num*f2.num, f1.den*f2.den)
|
||||
return
|
||||
}
|
||||
|
||||
func anyToFract(v any) (f *FractionType, err error) {
|
||||
var ok bool
|
||||
if f, ok = v.(*FractionType); !ok {
|
||||
if n, ok := v.(int64); ok {
|
||||
f = intToFraction(n)
|
||||
}
|
||||
}
|
||||
if f == nil {
|
||||
err = ErrExpectedGot("fract", TypeFraction, v)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func anyPairToFract(v1, v2 any) (f1, f2 *FractionType, err error) {
|
||||
if f1, err = anyToFract(v1); err != nil {
|
||||
return
|
||||
}
|
||||
if f2, err = anyToFract(v2); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func SumAnyFract(af1, af2 any) (sum any, err error) {
|
||||
var f1, f2 *FractionType
|
||||
if f1, f2, err = anyPairToFract(af1, af2); err != nil {
|
||||
return
|
||||
}
|
||||
f := SumFract(f1, f2)
|
||||
if f.num == 0 {
|
||||
sum = 0
|
||||
} else {
|
||||
sum = simplifyFraction(f)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Returns
|
||||
//
|
||||
// <0 if af1 < af2
|
||||
// =0 if af1 == af2
|
||||
// >0 if af1 > af2
|
||||
// err if af1 or af2 is not convertible to fraction
|
||||
func CmpAnyFract(af1, af2 any) (result int, err error) {
|
||||
var f1, f2 *FractionType
|
||||
if f1, f2, err = anyPairToFract(af1, af2); err != nil {
|
||||
return
|
||||
}
|
||||
result = cmpFract(f1, f2)
|
||||
return
|
||||
}
|
||||
|
||||
// Returns
|
||||
//
|
||||
// <0 if af1 < af2
|
||||
// =0 if af1 == af2
|
||||
// >0 if af1 > af2
|
||||
func cmpFract(f1, f2 *FractionType) (result int) {
|
||||
f2.num = -f2.num
|
||||
f := SumFract(f1, f2)
|
||||
if f.num < 0 {
|
||||
result = -1
|
||||
} else if f.num > 0 {
|
||||
result = 1
|
||||
} else {
|
||||
result = 0
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func SubAnyFract(af1, af2 any) (sum any, err error) {
|
||||
var f1, f2 *FractionType
|
||||
if f1, f2, err = anyPairToFract(af1, af2); err != nil {
|
||||
return
|
||||
}
|
||||
f2.num = -f2.num
|
||||
f := SumFract(f1, f2)
|
||||
if f.num == 0 {
|
||||
sum = 0
|
||||
} else {
|
||||
sum = simplifyFraction(f)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func MulAnyFract(af1, af2 any) (prod any, err error) {
|
||||
var f1, f2 *FractionType
|
||||
if f1, f2, err = anyPairToFract(af1, af2); err != nil {
|
||||
return
|
||||
}
|
||||
if f1.num == 0 || f2.num == 0 {
|
||||
prod = 0
|
||||
} else {
|
||||
f := &FractionType{f1.num * f2.num, f1.den * f2.den}
|
||||
prod = simplifyFraction(f)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func DivAnyFract(af1, af2 any) (quot any, err error) {
|
||||
var f1, f2 *FractionType
|
||||
if f1, f2, err = anyPairToFract(af1, af2); err != nil {
|
||||
return
|
||||
}
|
||||
if f2.num == 0 {
|
||||
err = errors.New("division by zero")
|
||||
return
|
||||
}
|
||||
if f1.num == 0 || f2.den == 0 {
|
||||
quot = 0
|
||||
} else {
|
||||
f := &FractionType{f1.num * f2.den, f1.den * f2.num}
|
||||
quot = simplifyFraction(f)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func simplifyFraction(f *FractionType) (v any) {
|
||||
f.num, f.den = simplifyIntegers(f.num, f.den)
|
||||
if f.den == 1 {
|
||||
v = f.num
|
||||
} else {
|
||||
v = &FractionType{f.num, f.den}
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func simplifyIntegers(num, den int64) (a, b int64) {
|
||||
if num == 0 {
|
||||
return 0, 1
|
||||
}
|
||||
if den == 0 {
|
||||
panic("fraction with denominator == 0")
|
||||
}
|
||||
if den < 0 {
|
||||
den = -den
|
||||
num = -num
|
||||
}
|
||||
g := Gcd(num, den)
|
||||
a = num / g
|
||||
b = den / g
|
||||
return
|
||||
}
|
||||
|
||||
func intToFraction(n int64) *FractionType {
|
||||
return &FractionType{n, 1}
|
||||
}
|
||||
|
||||
func IsFraction(v any) (ok bool) {
|
||||
_, ok = v.(*FractionType)
|
||||
return ok
|
||||
}
|
||||
@@ -0,0 +1,273 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// function.go
|
||||
package kern
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// ---- Function templates
|
||||
type FuncTemplate func(ctx ExprContext, name string, args map[string]any) (result any, err error)
|
||||
|
||||
type DeepFuncTemplate func(a, b any) (eq bool, err error)
|
||||
|
||||
// ---- Common functor definition
|
||||
type BaseFunctor struct {
|
||||
info ExprFunc
|
||||
}
|
||||
|
||||
func (functor *BaseFunctor) ToString(opt FmtOpt) (s string) {
|
||||
if functor.info != nil {
|
||||
s = functor.info.ToString(opt)
|
||||
} else {
|
||||
s = "func(){}"
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func (functor *BaseFunctor) GetParams() (params []ExprFuncParam) {
|
||||
if functor.info != nil {
|
||||
return functor.info.Params()
|
||||
} else {
|
||||
return []ExprFuncParam{}
|
||||
}
|
||||
}
|
||||
|
||||
func (functor *BaseFunctor) SetFunc(info ExprFunc) {
|
||||
functor.info = info
|
||||
}
|
||||
|
||||
func (functor *BaseFunctor) GetFunc() ExprFunc {
|
||||
return functor.info
|
||||
}
|
||||
|
||||
func (functor *BaseFunctor) GetDefinitionContext() ExprContext {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ---- Function Parameters
|
||||
type paramFlags uint16
|
||||
|
||||
const (
|
||||
PfDefault paramFlags = 1 << iota
|
||||
PfOptional
|
||||
PfRepeat
|
||||
)
|
||||
|
||||
type funcParamInfo struct {
|
||||
name string
|
||||
flags paramFlags
|
||||
defaultValue any
|
||||
}
|
||||
|
||||
func NewFuncParam(name string) ExprFuncParam {
|
||||
return &funcParamInfo{name: name}
|
||||
}
|
||||
|
||||
func NewFuncParamFlag(name string, flags paramFlags) ExprFuncParam {
|
||||
return &funcParamInfo{name: name, flags: flags}
|
||||
}
|
||||
|
||||
func NewFuncParamFlagDef(name string, flags paramFlags, defValue any) *funcParamInfo {
|
||||
return &funcParamInfo{name: name, flags: flags, defaultValue: defValue}
|
||||
}
|
||||
|
||||
func (param *funcParamInfo) Name() string {
|
||||
return param.name
|
||||
}
|
||||
|
||||
func (param *funcParamInfo) Type() string {
|
||||
return TypeAny
|
||||
}
|
||||
|
||||
func (param *funcParamInfo) IsDefault() bool {
|
||||
return (param.flags & PfDefault) != 0
|
||||
}
|
||||
|
||||
func (param *funcParamInfo) IsOptional() bool {
|
||||
return (param.flags & PfOptional) != 0
|
||||
}
|
||||
|
||||
func (param *funcParamInfo) IsRepeat() bool {
|
||||
return (param.flags & PfRepeat) != 0
|
||||
}
|
||||
|
||||
func (param *funcParamInfo) DefaultValue() any {
|
||||
return param.defaultValue
|
||||
}
|
||||
|
||||
func initActualParams(ctx ExprContext, info ExprFunc, callTerm Term) (actualParams map[string]any, err error) {
|
||||
var varArgs []any
|
||||
var varName string
|
||||
|
||||
namedParamsStarted := false
|
||||
|
||||
formalParams := info.Params()
|
||||
actualParams = make(map[string]any, len(formalParams))
|
||||
if callTerm == nil {
|
||||
return
|
||||
}
|
||||
|
||||
childCount := callTerm.GetChildCount()
|
||||
for i := range childCount {
|
||||
tree := callTerm.GetChild(i)
|
||||
// for i, tree := range callTerm.Children() {
|
||||
var paramValue any
|
||||
paramCtx := ctx.Clone()
|
||||
if paramValue, err = tree.Compute(paramCtx); err != nil {
|
||||
break
|
||||
}
|
||||
if paramName, namedParam := GetAssignVarName(tree); namedParam {
|
||||
if info.ParamSpec(paramName) == nil {
|
||||
err = fmt.Errorf("%s(): unknown param %q", info.Name(), paramName)
|
||||
break
|
||||
}
|
||||
actualParams[paramName] = paramValue
|
||||
namedParamsStarted = true
|
||||
} else if !namedParamsStarted {
|
||||
if varArgs != nil {
|
||||
varArgs = append(varArgs, paramValue)
|
||||
} else if i < len(formalParams) {
|
||||
spec := formalParams[i]
|
||||
if spec.IsRepeat() {
|
||||
varArgs = make([]any, 0, childCount-i)
|
||||
varArgs = append(varArgs, paramValue)
|
||||
varName = spec.Name()
|
||||
} else {
|
||||
actualParams[spec.Name()] = paramValue
|
||||
}
|
||||
} else {
|
||||
err = ErrTooManyParams(info.Name(), len(formalParams), childCount)
|
||||
break
|
||||
}
|
||||
} else {
|
||||
err = fmt.Errorf("%s(): positional param nr %d not allowed after named params", info.Name(), i+1)
|
||||
break
|
||||
}
|
||||
}
|
||||
if err == nil {
|
||||
if varArgs != nil {
|
||||
actualParams[varName] = varArgs
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// func (info *funcInfo) PrepareCall(name string, actualParams map[string]any) (err error) {
|
||||
// passedCount := len(actualParams)
|
||||
// if info.MinArgs() > passedCount {
|
||||
// err = ErrTooFewParams(name, info.MinArgs(), info.MaxArgs(), passedCount)
|
||||
// return
|
||||
// }
|
||||
|
||||
// if passedCount < len(info.formalParams) {
|
||||
// for _, p := range info.formalParams {
|
||||
// if _, exists := actualParams[p.Name()]; !exists {
|
||||
// if !p.IsDefault() {
|
||||
// break
|
||||
// }
|
||||
// if p.IsRepeat() {
|
||||
// varArgs := make([]any, 1)
|
||||
// varArgs[0] = p.DefaultValue()
|
||||
// actualParams[p.Name()] = varArgs
|
||||
// } else {
|
||||
// actualParams[p.Name()] = p.DefaultValue()
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// if info.MaxArgs() >= 0 && info.MaxArgs() < len(actualParams) {
|
||||
// err = ErrTooManyParams(name, info.MaxArgs(), len(actualParams))
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
|
||||
// ----- Call a function ---
|
||||
|
||||
// func getAssignVarName(t *term) (name string, ok bool) {
|
||||
// if ok = t.symbol() == SymEqual; ok {
|
||||
// name = t.children[0].source()
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
|
||||
func GetAssignVarName(t Term) (name string, ok bool) {
|
||||
if ok = t.IsAssign(); ok {
|
||||
name = t.GetChildSource(0)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func CallFunctionByTerm(parentCtx ExprContext, name string, callTerm Term) (result any, err error) {
|
||||
var actualParams map[string]any
|
||||
if info, exists := parentCtx.GetFuncInfo(name); exists {
|
||||
if actualParams, err = initActualParams(parentCtx, info, callTerm); err == nil {
|
||||
ctx := info.AllocContext(parentCtx)
|
||||
if err = info.PrepareCall(name, actualParams); err == nil {
|
||||
functor := info.Functor()
|
||||
result, err = functor.InvokeNamed(ctx, name, actualParams)
|
||||
exportObjectsToParent(ctx)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
err = fmt.Errorf("unknown function %s()", name)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func CallFunctionByArgs(parentCtx ExprContext, name string, args []any) (result any, err error) {
|
||||
var actualParams map[string]any
|
||||
if info, exists := parentCtx.GetFuncInfo(name); exists {
|
||||
functor := info.Functor()
|
||||
actualParams = BindActualParams(functor, args)
|
||||
ctx := info.AllocContext(parentCtx)
|
||||
if err = info.PrepareCall(name, actualParams); err == nil {
|
||||
result, err = functor.InvokeNamed(ctx, name, actualParams)
|
||||
exportObjectsToParent(ctx)
|
||||
}
|
||||
} else {
|
||||
err = fmt.Errorf("unknown function %s()", name)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func CallFunctionByParams(parentCtx ExprContext, name string, actualParams map[string]any) (result any, err error) {
|
||||
//var actualParams map[string]any
|
||||
if info, exists := parentCtx.GetFuncInfo(name); exists {
|
||||
functor := info.Functor()
|
||||
ctx := info.AllocContext(parentCtx)
|
||||
if err = info.PrepareCall(name, actualParams); err == nil {
|
||||
result, err = functor.InvokeNamed(ctx, name, actualParams)
|
||||
exportObjectsToParent(ctx)
|
||||
}
|
||||
} else {
|
||||
err = fmt.Errorf("unknown function %s()", name)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func GetParam(args map[string]any, paramName string, paramNum int) (value any, exists bool) {
|
||||
if value, exists = args[paramName]; !exists {
|
||||
if paramNum > 0 && paramNum <= len(args) {
|
||||
value, exists = args["arg"+strconv.Itoa(paramNum)]
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func BindActualParams(functor Functor, args []any) (actualParams map[string]any) {
|
||||
formalParams := functor.GetParams()
|
||||
actualParams = make(map[string]any, len(args))
|
||||
for i, arg := range args {
|
||||
if i < len(formalParams) {
|
||||
actualParams[formalParams[i].Name()] = arg
|
||||
} else {
|
||||
actualParams["arg"+strconv.Itoa(i+1)] = arg
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// iterator.go
|
||||
package kern
|
||||
|
||||
import (
|
||||
// "errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Operator names
|
||||
|
||||
const (
|
||||
InitName = "init"
|
||||
CleanName = "clean"
|
||||
ResetName = "reset"
|
||||
NextName = "next"
|
||||
CurrentName = "current"
|
||||
IndexName = "index"
|
||||
CountName = "count"
|
||||
FilterName = "filter"
|
||||
MapName = "map"
|
||||
KeyName = "key"
|
||||
ValueName = "value"
|
||||
)
|
||||
|
||||
type Iterator interface {
|
||||
Typer
|
||||
fmt.Stringer
|
||||
Next() (item any, err error) // must return io.EOF after the last item
|
||||
Current() (item any, err error)
|
||||
Index() int
|
||||
Count() int
|
||||
HasOperation(name string) bool
|
||||
CallOperation(name string, args map[string]any) (value any, err error)
|
||||
}
|
||||
|
||||
type ExtIterator interface {
|
||||
Iterator
|
||||
Reset() error
|
||||
Clean() error
|
||||
}
|
||||
|
||||
func ErrNoOperation(name string) error {
|
||||
return fmt.Errorf("no %s() function defined in the data-source", name)
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// list-type.go
|
||||
package kern
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ListType []any
|
||||
|
||||
func NewListA(listAny ...any) (list *ListType) {
|
||||
if listAny == nil {
|
||||
listAny = []any{}
|
||||
}
|
||||
return NewList(listAny)
|
||||
}
|
||||
|
||||
func NewList(listAny []any) (list *ListType) {
|
||||
if listAny != nil {
|
||||
ls := make(ListType, len(listAny))
|
||||
copy(ls, listAny)
|
||||
list = &ls
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func MakeList(length, capacity int) (list *ListType) {
|
||||
if capacity < length {
|
||||
capacity = length
|
||||
}
|
||||
ls := make(ListType, length, capacity)
|
||||
list = &ls
|
||||
return
|
||||
}
|
||||
|
||||
func ListFromStrings(stringList []string) (list *ListType) {
|
||||
list = MakeList(len(stringList), 0)
|
||||
for i, s := range stringList {
|
||||
(*list)[i] = s
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (ls *ListType) ToString(opt FmtOpt) (s string) {
|
||||
indent := GetFormatIndent(opt)
|
||||
flags := GetFormatFlags(opt)
|
||||
|
||||
var sb strings.Builder
|
||||
sb.WriteByte('[')
|
||||
if len(*ls) > 0 {
|
||||
innerOpt := MakeFormatOptions(flags, indent+1)
|
||||
nest := strings.Repeat(" ", indent+1)
|
||||
|
||||
if flags&MultiLine != 0 {
|
||||
sb.WriteByte('\n')
|
||||
sb.WriteString(nest)
|
||||
}
|
||||
for i, item := range []any(*ls) {
|
||||
if i > 0 {
|
||||
if flags&MultiLine != 0 {
|
||||
sb.WriteString(",\n")
|
||||
sb.WriteString(nest)
|
||||
} else {
|
||||
sb.WriteString(", ")
|
||||
}
|
||||
}
|
||||
if s, ok := item.(string); ok {
|
||||
sb.WriteByte('"')
|
||||
sb.WriteString(s)
|
||||
sb.WriteByte('"')
|
||||
} else if formatter, ok := item.(Formatter); ok {
|
||||
sb.WriteString(formatter.ToString(innerOpt))
|
||||
} else {
|
||||
sb.WriteString(fmt.Sprintf("%v", item))
|
||||
}
|
||||
}
|
||||
if flags&MultiLine != 0 {
|
||||
sb.WriteByte('\n')
|
||||
sb.WriteString(strings.Repeat(" ", indent))
|
||||
}
|
||||
}
|
||||
sb.WriteByte(']')
|
||||
s = sb.String()
|
||||
if flags&Truncate != 0 && len(s) > TruncateSize {
|
||||
s = TruncateString(s)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (ls *ListType) String() string {
|
||||
return ls.ToString(0)
|
||||
}
|
||||
|
||||
func (ls *ListType) TypeName() string {
|
||||
return "list"
|
||||
}
|
||||
|
||||
func (ls *ListType) Contains(t *ListType) (answer bool) {
|
||||
if len(*ls) >= len(*t) {
|
||||
answer = true
|
||||
for _, item := range *t {
|
||||
if answer = ls.IndexDeepSameCmp(item) >= 0; !answer {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (ls1 *ListType) Equals(ls2 ListType) (answer bool) {
|
||||
if ls2 != nil && len(*ls1) == len(ls2) {
|
||||
answer = true
|
||||
for index, i1 := range *ls1 {
|
||||
// if i1 != (ls2)[index] {
|
||||
if !reflect.DeepEqual(i1, ls2[index]) {
|
||||
answer = false
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (list *ListType) IndexDeepSameCmp(target any) (index int) {
|
||||
var eq bool
|
||||
var err error
|
||||
index = -1
|
||||
for i, item := range *list {
|
||||
if eq, err = deepSame(item, target, SameContent); err != nil {
|
||||
break
|
||||
} else if eq {
|
||||
index = i
|
||||
break
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func SameContent(a, b any) (same bool, err error) {
|
||||
la, _ := a.(*ListType)
|
||||
lb, _ := b.(*ListType)
|
||||
if len(*la) == len(*lb) {
|
||||
same = true
|
||||
for _, item := range *la {
|
||||
if pos := lb.IndexDeepSameCmp(item); pos < 0 {
|
||||
same = false
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func deepSame(a, b any, deepCmp DeepFuncTemplate) (eq bool, err error) {
|
||||
if IsNumOrFract(a) && IsNumOrFract(b) {
|
||||
if IsNumber(a) && IsNumber(b) {
|
||||
if IsInteger(a) && IsInteger(b) {
|
||||
li, _ := a.(int64)
|
||||
ri, _ := b.(int64)
|
||||
eq = li == ri
|
||||
} else {
|
||||
eq = NumAsFloat(a) == NumAsFloat(b)
|
||||
}
|
||||
} else {
|
||||
var cmp int
|
||||
if cmp, err = CmpAnyFract(a, b); err == nil {
|
||||
eq = cmp == 0
|
||||
}
|
||||
}
|
||||
} else if deepCmp != nil && IsList(a) && IsList(b) {
|
||||
eq, err = deepCmp(a, b)
|
||||
} else {
|
||||
eq = reflect.DeepEqual(a, b)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (list *ListType) SetItem(index int64, value any) (err error) {
|
||||
if index >= 0 && index < int64(len(*list)) {
|
||||
(*list)[index] = value
|
||||
} else {
|
||||
err = fmt.Errorf("index %d out of bounds (0, %d)", index, len(*list)-1)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (list *ListType) AppendItem(value any) {
|
||||
*list = append(*list, value)
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// term.go
|
||||
package kern
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Term interface {
|
||||
fmt.Stringer
|
||||
// Children() []Term
|
||||
Source() string
|
||||
GetChildCount() (count int)
|
||||
GetChild(index int) Term
|
||||
GetChildSource(index int) string
|
||||
Compute(ctx ExprContext) (result any, err error)
|
||||
IsAssign() bool
|
||||
Errorf(template string, args ...any) (err error)
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
//go:build unix
|
||||
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// utils-unix.go
|
||||
package kern
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/user"
|
||||
"path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func ExpandPath(sourcePath string) (expandedPath string, err error) {
|
||||
for expandedPath = os.ExpandEnv(sourcePath); expandedPath != sourcePath; expandedPath = os.ExpandEnv(sourcePath) {
|
||||
sourcePath = expandedPath
|
||||
}
|
||||
|
||||
if strings.HasPrefix(sourcePath, "~") {
|
||||
var home, userName, remainder string
|
||||
|
||||
slashPos := strings.IndexRune(sourcePath, '/')
|
||||
if slashPos > 0 {
|
||||
userName = sourcePath[1:slashPos]
|
||||
remainder = sourcePath[slashPos:]
|
||||
} else {
|
||||
userName = sourcePath[1:]
|
||||
}
|
||||
|
||||
if len(userName) == 0 {
|
||||
home, err = os.UserHomeDir()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
var userInfo *user.User
|
||||
userInfo, err = user.Lookup(userName)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
home = userInfo.HomeDir
|
||||
}
|
||||
expandedPath = path.Join(home, remainder)
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
//go:build windows
|
||||
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// utils-unix.go
|
||||
package kern
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
func ExpandPath(sourcePath string) (expandedPath string, err error) {
|
||||
for expandedPath = os.ExpandEnv(sourcePath); expandedPath != sourcePath; expandedPath = os.ExpandEnv(sourcePath) {
|
||||
sourcePath = expandedPath
|
||||
}
|
||||
return
|
||||
}
|
||||
+232
@@ -0,0 +1,232 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// utils.go
|
||||
package kern
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func IsString(v any) (ok bool) {
|
||||
_, ok = v.(string)
|
||||
return ok
|
||||
}
|
||||
|
||||
func IsInteger(v any) (ok bool) {
|
||||
_, ok = v.(int64)
|
||||
return ok
|
||||
}
|
||||
|
||||
func IsFloat(v any) (ok bool) {
|
||||
_, ok = v.(float64)
|
||||
return ok
|
||||
}
|
||||
|
||||
func IsBool(v any) (ok bool) {
|
||||
_, ok = v.(bool)
|
||||
return ok
|
||||
}
|
||||
|
||||
func IsList(v any) (ok bool) {
|
||||
_, ok = v.(*ListType)
|
||||
return ok
|
||||
}
|
||||
|
||||
func IsDict(v any) (ok bool) {
|
||||
_, ok = v.(*DictType)
|
||||
return ok
|
||||
}
|
||||
|
||||
func IsFract(v any) (ok bool) {
|
||||
_, ok = v.(*FractionType)
|
||||
return ok
|
||||
}
|
||||
|
||||
func IsRational(v any) (ok bool) {
|
||||
if _, ok = v.(*FractionType); !ok {
|
||||
_, ok = v.(int64)
|
||||
}
|
||||
return ok
|
||||
}
|
||||
|
||||
func IsNumber(v any) (ok bool) {
|
||||
return IsFloat(v) || IsInteger(v)
|
||||
}
|
||||
|
||||
func IsNumOrFract(v any) (ok bool) {
|
||||
return IsFloat(v) || IsInteger(v) || IsFraction(v)
|
||||
}
|
||||
|
||||
func IsNumberString(v any) (ok bool) {
|
||||
return IsString(v) || IsNumber(v)
|
||||
}
|
||||
|
||||
func IsFunctor(v any) (ok bool) {
|
||||
_, ok = v.(Functor)
|
||||
return
|
||||
}
|
||||
|
||||
func IsIterator(v any) (ok bool) {
|
||||
_, ok = v.(Iterator)
|
||||
return
|
||||
}
|
||||
|
||||
func NumAsFloat(v any) (f float64) {
|
||||
var ok bool
|
||||
if f, ok = v.(float64); !ok {
|
||||
if fract, ok := v.(*FractionType); ok {
|
||||
f = fract.ToFloat()
|
||||
} else {
|
||||
i, _ := v.(int64)
|
||||
f = float64(i)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func ToBool(v any) (b bool, ok bool) {
|
||||
ok = true
|
||||
switch x := v.(type) {
|
||||
case string:
|
||||
b = len(x) > 0
|
||||
case float64:
|
||||
b = x != 0.0
|
||||
case int64:
|
||||
b = x != 0
|
||||
case bool:
|
||||
b = x
|
||||
default:
|
||||
ok = false
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func IsFunc(v any) bool {
|
||||
return reflect.TypeOf(v).Kind() == reflect.Func
|
||||
}
|
||||
|
||||
func AnyInteger(v any) (i int64, ok bool) {
|
||||
ok = true
|
||||
switch intval := v.(type) {
|
||||
case int:
|
||||
i = int64(intval)
|
||||
case uint8:
|
||||
i = int64(intval)
|
||||
case uint16:
|
||||
i = int64(intval)
|
||||
case uint64:
|
||||
i = int64(intval)
|
||||
case uint32:
|
||||
i = int64(intval)
|
||||
case int8:
|
||||
i = int64(intval)
|
||||
case int16:
|
||||
i = int64(intval)
|
||||
case int32:
|
||||
i = int64(intval)
|
||||
case int64:
|
||||
i = intval
|
||||
default:
|
||||
ok = false
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func FromGenericAny(v any) (exprAny any, ok bool) {
|
||||
if v != nil {
|
||||
if exprAny, ok = v.(bool); ok {
|
||||
return
|
||||
}
|
||||
if exprAny, ok = v.(string); ok {
|
||||
return
|
||||
}
|
||||
if exprAny, ok = AnyInteger(v); ok {
|
||||
return
|
||||
}
|
||||
if exprAny, ok = AnyFloat(v); ok {
|
||||
return
|
||||
}
|
||||
if exprAny, ok = v.(*DictType); ok {
|
||||
return
|
||||
}
|
||||
if exprAny, ok = v.(*ListType); ok {
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func AnyFloat(v any) (float float64, ok bool) {
|
||||
ok = true
|
||||
switch floatval := v.(type) {
|
||||
case float32:
|
||||
float = float64(floatval)
|
||||
case float64:
|
||||
float = floatval
|
||||
default:
|
||||
ok = false
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func CopyMap[K comparable, V any](dest, source map[K]V) map[K]V {
|
||||
for k, v := range source {
|
||||
dest[k] = v
|
||||
}
|
||||
return dest
|
||||
}
|
||||
|
||||
// func CloneMap[K comparable, V any](source map[K]V) map[K]V {
|
||||
// dest := make(map[K]V, len(source))
|
||||
// return CopyMap(dest, source)
|
||||
// }
|
||||
|
||||
func CopyFilteredMap[K comparable, V any](dest, source map[K]V, filter func(key K) (accept bool)) map[K]V {
|
||||
// fmt.Printf("--- Clone with filter %p\n", filter)
|
||||
if filter == nil {
|
||||
return CopyMap(dest, source)
|
||||
} else {
|
||||
for k, v := range source {
|
||||
if filter(k) {
|
||||
// fmt.Printf("\tClone var %q\n", k)
|
||||
dest[k] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
return dest
|
||||
}
|
||||
|
||||
func CloneFilteredMap[K comparable, V any](source map[K]V, filter func(key K) (accept bool)) map[K]V {
|
||||
dest := make(map[K]V, len(source))
|
||||
return CopyFilteredMap(dest, source, filter)
|
||||
}
|
||||
|
||||
func ToGoInt(value any, description string) (i int, err error) {
|
||||
if valueInt64, ok := value.(int64); ok {
|
||||
i = int(valueInt64)
|
||||
} else if valueInt, ok := value.(int); ok {
|
||||
i = valueInt
|
||||
} else {
|
||||
err = fmt.Errorf("%s expected integer, got %s (%v)", description, TypeName(value), value)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func ToGoString(value any, description string) (s string, err error) {
|
||||
if s, ok := value.(string); ok {
|
||||
return s, nil
|
||||
} else {
|
||||
err = fmt.Errorf("%s expected string, got %s (%v)", description, TypeName(value), value)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func ForAll[T, V any](ts []T, fn func(T) V) []V {
|
||||
result := make([]V, len(ts))
|
||||
for i, t := range ts {
|
||||
result[i] = fn(t)
|
||||
}
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user