For those interested in how to use the eval package to create a simple
Go shell (not a POSIX shell, a mini-go cmd line like python has):
func shell(stdin io.Reader, stdout io.Writer, stderr io.Writer) (int)
{
r := bufio.NewReader(stdin);
o := bufio.NewWriter(stdout);
e := bufio.NewWriter(stderr);
world := eval.NewWorld();
prompt := func () {
o.WriteString(">>> ");
o.Flush();
};
readLine := func () string {
ret, err := r.ReadString('\n');
if err != nil {
os.Exit(1);
}
return ret;
};
execCmd := func (cmd string) int {
code, err := world.Compile(cmd); // parse and compile
if err != nil {
fmt.Fprint(e,err);
return 1;
}
typ := code.Type(); // check what type the code will return
if typ != nil {
fmt.Fprintf(o,"(%s) ",typ);
}
val, er := code.Run(); // run the code
if er != nil {
fmt.Fprint(e,err);
return 1;
}
if val != nil {
o.WriteString(val.String());
}
o.WriteString("\n");
return 0;
};
ret := 0;
cmd := "";
for {
prompt();
cmd = readLine();
ret = execCmd(cmd);
}
return ret;
}
It's limited currently to what the eval package can provide, which
precludes using imports (game breaker right now for it to be really
useful).
On Nov 15, 4:18 pm, Marcelo Castellani <
marc...@hypequino.com> wrote:
> An Go REPL is excellent. I love tools like irb, for example.
>
> On 15/11/09 16:56, "Adam Langley" <
a...@golang.org> wrote:
>
>
>
> > On Sun, Nov 15, 2009 at 10:54 AM, michael <
michaelhusm...@googlemail.com>