Skip to content

Commit

Permalink
fix(logic): make source_file return multiple results instead of list
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Mar 16, 2023
1 parent 6e7d27c commit d7e9526
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
29 changes: 18 additions & 11 deletions x/logic/predicate/file.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package predicate

import (
"context"
"fmt"
"reflect"
"sort"
Expand All @@ -20,21 +21,27 @@ func SourceFile(vm *engine.VM, file engine.Term, cont engine.Cont, env *engine.E
if _, ok := loaded[*inputFile]; ok {
return engine.Unify(vm, file, engine.NewAtom(*inputFile), cont, env)
}
return engine.Unify(vm, file, engine.List(), cont, env)
return engine.Delay()
}

result := make([]engine.Term, 0, len(loaded))
for _, filename := range sortLoadedSources(loaded) {
result = append(result, engine.NewAtom(filename))
promises := make([]func(ctx context.Context) *engine.Promise, 0, len(loaded))
sortedSource := sortLoadedSources(loaded)
for i := range sortedSource {
term := engine.NewAtom(sortedSource[i])
promises = append(
promises,
func(ctx context.Context) *engine.Promise {
return engine.Unify(
vm,
file,
term,
cont,
env,
)
})
}

return engine.Unify(
vm,
file,
engine.List(result...),
cont,
env,
)
return engine.Delay(promises...)
}

func getLoadedSources(vm *engine.VM) map[string]interface{} {
Expand Down
5 changes: 2 additions & 3 deletions x/logic/predicate/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,11 @@ func TestSourceFile(t *testing.T) {
},
{
query: "source_file(X).",
wantResult: []types.TermResults{{"X": "[]"}},
wantSuccess: true,
wantSuccess: false,
},
{
query: "consult(file1), consult(file2), source_file(X).",
wantResult: []types.TermResults{{"X": "[file1,file2]"}},
wantResult: []types.TermResults{{"X": "file1"}, {"X": "file2"}},
wantSuccess: true,
},
{
Expand Down

0 comments on commit d7e9526

Please sign in to comment.