wdte: github.com/DeedleFake/wdte/repl Index | Examples | Files

package repl

import "github.com/DeedleFake/wdte/repl"

Package repl provides a layer intended to help with the development of a read-eval-print loop.


package main

import (

    _ "github.com/DeedleFake/wdte/std/all"

var (
    mode = ">>> "

func next(lr *liner.State) repl.NextFunc {
    return func() ([]byte, error) {
        line, err := lr.Prompt(mode)
        return []byte(line + "\n"), err

func main() {
    lr := liner.NewLiner()
    defer lr.Close()

    r := repl.New(next(lr), std.Import, nil, std.Scope)

    for {
        ret, err := r.Next()
        if err != nil {
            if err == repl.ErrIncomplete {
                mode = "... "

            if err == liner.ErrPromptAborted {
                mode = ">>> "

            fmt.Fprintf(os.Stderr, "Error: %v\n", err)
        if ret == nil {

        switch reflect.Indirect(reflect.ValueOf(ret)).Kind() {
        case reflect.Struct:
            fmt.Printf(": complex value\n")

            fmt.Printf(": %v\n", ret)

        mode = ">>> "



Package Files

doc.go repl.go


var (
    // ErrIncomplete is returned by REPL.Next() if it expects more input
    // before it will begin evaluation.
    ErrIncomplete = errors.New("input incomplete")

func Partial Uses

func Partial(r io.Reader, stack []string, macros scanner.MacroMap) ([]string, bool)

Partial checks if an expression, read from r, is incomplete. The initial value of stack should be nil, and subsequent values should be the value of the first return. The second return is true if the expression was incomplete.


stack, partial := repl.Partial(strings.NewReader("let io =>"), nil, nil)

stack, partial = repl.Partial(strings.NewReader("import 'io'"), stack, nil)

stack, partial = repl.Partial(strings.NewReader(";"), stack, nil)



type NextFunc Uses

type NextFunc func() ([]byte, error)

A NextFunc returns the next piece of code to be interpreted. When reading from stdin, this is likely the next line entered.

If NextFunc should not be called again, it should return nil, io.EOF.

func SimpleNext Uses

func SimpleNext(r io.Reader) NextFunc

SimpleNext returns a next func that scans lines from r.

type REPL Uses

type REPL struct {
    // Scope is the scope that the next line will be executed in. It is
    // automatically updated every time an executed line changes the
    // scope.
    Scope *wdte.Scope
    // contains filtered or unexported fields

REPL provides a means to track a global state and interpret separate lines of a WDTE script, allowing for the implementation of a read-eval-print loop.

func New Uses

func New(next NextFunc, im wdte.Importer, macros scanner.MacroMap, start *wdte.Scope) *REPL

New creates a new REPL which reads with next, imports using im, and executes the first line with the scope start.

func (*REPL) Cancel Uses

func (r *REPL) Cancel()

Cancel cancels a partial expression. This is useful if, for example, a user sends an interrupt to a command-line REPL while entering a subsequent line of a multi-line expression.

If a partial expression is not in progress, this has no effect.

func (*REPL) Next Uses

func (r *REPL) Next() (ret wdte.Func, err error)

Next reads and evaluates the next line of input. It returns the value returned from that line, or an error if one is encountered. If the end of the input has been reached, it will return nil, nil.

If an input ends in a partial expression, such as a single line of a mult-line expression, nil, ErrIncomplete is returned.

Package repl imports 6 packages (graph). Updated 2019-02-27. Refresh now. Tools for package owners.