Skip to content

Commit

Permalink
tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
guregu committed Aug 7, 2022
1 parent e58234a commit 3336451
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 24 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func main() {

- Andrew Davison ([@infradig](https://github.com/infradig)) and other contributors to [Trealla Prolog](https://github.com/trealla-prolog/trealla).
- Jos De Roo ([@josd](https://github.com/josd)) for test cases and encouragement.
- Aram Panasenco ([@panasenco](https://github.com/panasenco)) for his JSON library.

## License

Expand Down
30 changes: 14 additions & 16 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,17 @@ func BenchmarkQuery(b *testing.B) {
}
}

// func BenchmarkTak(b *testing.B) {
// pl, err := New(WithPreopenDir("./testdata"))
// if err != nil {
// b.Fatal(err)
// }
// ctx := context.Background()
// b.ResetTimer()
// for i := 0; i < b.N; i++ {
// ans, err := pl.Query(ctx, "consult(tak), run")
// println("a")
// _ = ans
// if err != nil {
// b.Fatal(err)
// }
// }
// }
func BenchmarkTak(b *testing.B) {
pl, err := New(WithPreopenDir("testdata"))
if err != nil {
b.Fatal(err)
}
ctx := context.Background()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := pl.Query(ctx, "consult('testdata/tak'), run")
if err != nil {
b.Fatal(err)
}
}
}
22 changes: 22 additions & 0 deletions prolog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,25 @@ func TestQuery(t *testing.T) {
}

}

func TestThrow(t *testing.T) {
pl, err := New(WithPreopenDir("testdata"))
if err != nil {
t.Fatal(err)
}

ctx := context.Background()
ans, err := pl.Query(ctx, `throw(ball).`)
if err != nil {
// TODO: might want to make this an error in the future instead of a status
t.Fatal(err)
}

if ans.Result != ResultError {
t.Error("unexpected result. want:", ResultError, "got:", ans.Result)
}

if ans.Error != "ball" {
t.Error(`unexpected error value. want: "ball" got:`, ans.Error)
}
}
8 changes: 8 additions & 0 deletions term.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
)

// Term is a Prolog term.
//
// One of the following types:
// - string
// - int64
Expand All @@ -21,6 +22,7 @@ type Term = any
// In other words, it's one answer to a query.
type Solution map[string]Term

// UnmarshalJSON implements the encoding/json.Marshaler interface.
func (sol *Solution) UnmarshalJSON(bs []byte) error {
var raws map[string]json.RawMessage
dec := json.NewDecoder(bytes.NewReader(bs))
Expand Down Expand Up @@ -138,6 +140,12 @@ type Compound struct {
Args []Term
}

// Indicator returns the procedure indicator of this compound in Functor/Arity format.
func (c Compound) Indicator() string {
// TODO: escape
return fmt.Sprintf("%s/%d", c.Functor, len(c.Args))
}

// String returns a Prolog-ish representation of this Compound.
func (c Compound) String() string {
if len(c.Args) == 0 {
Expand Down
4 changes: 4 additions & 0 deletions term_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ func TestCompound(t *testing.T) {
if c0.String() != want {
t.Errorf("bad string. want: %v got: %v", want, c0.String())
}
pi := "foo/2"
if c0.Indicator() != pi {
t.Errorf("bad indicator. want: %v got: %v", pi, c0.Indicator())
}
}
19 changes: 11 additions & 8 deletions trealla.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
//go:embed tpl.wasm
var tplWASM []byte

var wasmEngine = wasmer.NewEngine()

// Prolog is a Prolog interpreter.
type Prolog interface {
// Query executes a query.
Expand All @@ -30,7 +32,7 @@ type prolog struct {

// New creates a new Prolog interpreter.
func New(opts ...Option) (Prolog, error) {
pl, err := newEngine()
pl, err := newProlog()
if err != nil {
return nil, err
}
Expand All @@ -40,15 +42,14 @@ func New(opts ...Option) (Prolog, error) {
return pl, nil
}

func newEngine() (*prolog, error) {
engine := wasmer.NewEngine()
store := wasmer.NewStore(engine)
func newProlog() (*prolog, error) {
store := wasmer.NewStore(wasmEngine)
module, err := wasmer.NewModule(store, tplWASM)
if err != nil {
return nil, err
}
return &prolog{
engine: engine,
engine: wasmEngine,
store: store,
module: module,
}, nil
Expand Down Expand Up @@ -100,6 +101,7 @@ type Answer struct {
Output string
}

// Result is the status of a query answer.
type Result string

// Result values.
Expand Down Expand Up @@ -142,7 +144,8 @@ func (pl *prolog) ask(ctx context.Context, query string) (string, error) {
if err != nil {
return "", err
}
ch := make(chan error, 1)

ch := make(chan error, 2)
go func() {
defer func() {
if ex := recover(); ex != nil {
Expand All @@ -155,7 +158,7 @@ func (pl *prolog) ask(ctx context.Context, query string) (string, error) {

select {
case <-ctx.Done():
return "", fmt.Errorf("trealla: canceled: %x", ctx.Err())
return "", fmt.Errorf("trealla: canceled: %w", ctx.Err())
case err := <-ch:
stdout := string(wasiEnv.ReadStdout())
return stdout, err
Expand All @@ -171,7 +174,7 @@ func escapeQuery(query string) string {
type Option func(*prolog)

// WithPreopenDir sets the preopen directory to dir, granting access to it. Calling this again will overwrite it.
// More or less equivalent to WithMapDir(dir, dir).
// More or less equivalent to `WithMapDir(dir, dir)`.
func WithPreopenDir(dir string) Option {
return func(pl *prolog) {
pl.preopen = dir
Expand Down

0 comments on commit 3336451

Please sign in to comment.