Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

capture print/log when using EvalString (for the discord bot) #32

Merged
merged 3 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions eval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package eval

import (
"fmt"
"io"
"math"
"os"
"strings"
Expand All @@ -13,11 +14,13 @@ import (
)

type State struct {
env *object.Environment
env *object.Environment
Out io.Writer
NoLog bool // turn log() into print() (for EvalString)
ldemailly marked this conversation as resolved.
Show resolved Hide resolved
}

func NewState() *State {
return &State{env: object.NewEnvironment()}
return &State{env: object.NewEnvironment(), Out: os.Stdout}
}

// Forward to env to count the number of bindings. Used mostly to know if there are any macros.
Expand Down Expand Up @@ -213,10 +216,19 @@ func (s *State) evalBuiltin(node *ast.Builtin) object.Object {
buf.WriteString(r.Inspect())
}
}
if node.Type == token.PRINT {
os.Stdout.WriteString(buf.String())
doLog := node.Type != token.PRINT
if s.NoLog && doLog {
doLog = false
buf.WriteRune('\n') // log() has a implicit newline when using log.Xxx, print() doesn't.
}
if doLog {
// Consider passing the arguments to log instead of making a string concatenation.
log.Printf("%s", buf.String())
} else {
log.Printf(buf.String())
_, err := s.Out.Write([]byte(buf.String()))
if err != nil {
log.Warnf("print: %v", err)
}
}
return object.NULL
case token.FIRST:
Expand Down
6 changes: 4 additions & 2 deletions repl/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ func EvalAll(s, macroState *eval.State, in io.Reader, out io.Writer, options Opt
func EvalString(what string) string {
s := eval.NewState()
macroState := eval.NewState()
out := strings.Builder{}
EvalOne(s, macroState, what, &out, Options{All: true, ShowEval: true, NoColor: true})
out := &strings.Builder{}
s.Out = out
s.NoLog = true
EvalOne(s, macroState, what, out, Options{All: true, ShowEval: true, NoColor: true})
return out.String()
}

Expand Down
30 changes: 30 additions & 0 deletions repl/repl_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package repl_test

import (
"testing"

"grol.io/grol/repl"
)

func TestEvalString(t *testing.T) {
s := `
fact=func(n) { // function
log("called fact", n) // log (timestamped stderr output)
if (n<=1) {
return 1
}
n*fact(n-1)
}
result = fact(5)
print("Factorial of 5 is", result, "\n") // print to stdout
result`
expected := `called fact 5
called fact 4
called fact 3
called fact 2
called fact 1
Factorial of 5 is 120` + " \n120\n" // there is an extra space before \n that vscode wants to remove
if got := repl.EvalString(s); got != expected {
t.Errorf("EvalString() got\n---\n%s\n---want---\n%s\n---", got, expected)
}
}