-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathivy.go
More file actions
100 lines (87 loc) · 2.25 KB
/
Copy pathivy.go
File metadata and controls
100 lines (87 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2022 Frederik Zipp. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bufio"
"fmt"
"io"
"os"
"os/exec"
"strings"
)
type Ivy struct {
cmd *exec.Cmd
in io.Writer
out *bufio.Reader
internalPrompt byte
}
func NewIvy() (*Ivy, error) {
cmd := exec.Command("ivy")
in, err := cmd.StdinPipe()
if err != nil {
return nil, fmt.Errorf("could not connect to ivy input: %w", err)
}
outPipe, err := cmd.StdoutPipe()
if err != nil {
return nil, fmt.Errorf("could not connect to ivy output: %w", err)
}
cmd.Stderr = cmd.Stdout
out := bufio.NewReader(outPipe)
return &Ivy{cmd: cmd, in: in, out: out, internalPrompt: '\x17'}, nil
}
func (ivy *Ivy) Start() error {
err := ivy.cmd.Start()
if err != nil {
return fmt.Errorf("could not start ivy: %w", err)
}
return ivy.setInternalPrompt()
}
func (ivy *Ivy) Exec(stmt string) (string, error) {
_, err := fmt.Fprintln(ivy.in, stmt)
if err != nil {
return "", fmt.Errorf("could not send input to ivy: %w", err)
}
return ivy.readResponse()
}
func (ivy *Ivy) Ops() ([]string, error) {
return ivy.listDefs("user-defined operators", ")op")
}
func (ivy *Ivy) Vars() ([]string, error) {
return ivy.listDefs("defined variables", ")var")
}
func (ivy *Ivy) listDefs(description, command string) ([]string, error) {
output, err := ivy.Exec(command)
if err != nil {
return nil, fmt.Errorf("could not get list of %s: %w", description, err)
}
lines := strings.Split(output, "\n")
var defs []string
for _, line := range lines {
if line != "" && line[0] == '\t' {
defs = append(defs, line[1:])
}
}
return defs, nil
}
func (ivy *Ivy) Quit() error {
err := ivy.cmd.Process.Signal(os.Kill)
if err != nil {
return fmt.Errorf("could not quit ivy process: %w", err)
}
return ivy.cmd.Wait()
}
func (ivy *Ivy) setInternalPrompt() error {
_, err := ivy.Exec(fmt.Sprintf(`)prompt "%c"`, ivy.internalPrompt))
if err != nil {
return fmt.Errorf("could not set internal prompt: %w", err)
}
return nil
}
func (ivy *Ivy) readResponse() (string, error) {
s, err := ivy.out.ReadString(ivy.internalPrompt)
if err != nil {
return "", fmt.Errorf("could not read response from ivy: %w", err)
}
return s[:len(s)-2], nil
}