38 lines
696 B
Go
38 lines
696 B
Go
|
package repl
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
"os"
|
||
|
"ucl.lmika.dev/ucl"
|
||
|
)
|
||
|
|
||
|
func (r *REPL) EvalAndDisplay(ctx context.Context, expr string) error {
|
||
|
res, err := r.inst.Eval(ctx, expr)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return displayResult(ctx, r.inst, res)
|
||
|
}
|
||
|
|
||
|
func displayResult(ctx context.Context, inst *ucl.Inst, res any) (err error) {
|
||
|
switch v := res.(type) {
|
||
|
case nil:
|
||
|
if _, err = fmt.Fprintln(os.Stdout, "(nil)"); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
case listable:
|
||
|
for i := 0; i < v.Len(); i++ {
|
||
|
if err = displayResult(ctx, inst, v.Index(i)); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
default:
|
||
|
if _, err = fmt.Fprintln(os.Stdout, v.String()); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
return nil
|
||
|
}
|