Skip to content

Commit

Permalink
fix(logic): fix incorrect handling of empty list
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Nov 7, 2023
1 parent 157ff21 commit 25e476d
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions x/logic/util/prolog.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ var (

// AtomEmpty is the term used to represent empty.
AtomEmpty = engine.NewAtom("")

// AtomEmptyList is the term used to represent an empty list.
AtomEmptyList = engine.NewAtom("[]")
)

// StringToTerm converts a string to a term.
Expand Down Expand Up @@ -51,9 +54,24 @@ func PredicateMatches(this string) func(string) bool {
}
}

// IsList returns true if the given compound is a list.
func IsList(compound engine.Compound) bool {
return compound.Functor() == AtomDot && compound.Arity() == 2
// IsList returns true if the given term is a list.
func IsList(term engine.Term) bool {
switch v := term.(type) {
case engine.Compound:
return v.Functor() == AtomDot && v.Arity() == 2
case engine.Atom:
return v == AtomEmptyList
}

return false
}

// IsEmptyList returns true if the given term is an empty list.
func IsEmptyList(term engine.Term) bool {
if v, ok := term.(engine.Atom); ok {
return v == AtomEmptyList
}
return false
}

// GetOption returns the value of the first option with the given name in the given options.
Expand Down Expand Up @@ -82,9 +100,12 @@ func GetOption(name engine.Atom, options engine.Term, env *engine.Env) (engine.T

resolvedTerm := env.Resolve(options)

compound, ok := resolvedTerm.(engine.Compound)
if ok && IsList(compound) {
iter := engine.ListIterator{List: compound, Env: env}
if IsEmptyList(resolvedTerm) {
return nil, nil
}

if IsList(resolvedTerm) {
iter := engine.ListIterator{List: resolvedTerm, Env: env}

for iter.Next() {
opt := env.Resolve(iter.Current())
Expand Down

0 comments on commit 25e476d

Please sign in to comment.