glycerine / zygomys

Zygo is a Lisp interpreter written in 100% Go. Central use case: dynamically compose Go struct trees in a zygo script, then invoke compiled Go functions on those trees. Makes Go reflection easy.
https://github.com/glycerine/zygomys/wiki
BSD 2-Clause "Simplified" License
1.71k stars 81 forks source link

how to get function return value? #28

Closed taliszhou closed 6 years ago

taliszhou commented 6 years ago

hi~~ zygo is so cool!!! i m try to write a web mode REPL, like this:

`var env *zygo.Zlisp

func main() { env = zygo.NewZlisp() env.StandardSetup() }

func listenLispCmd(session WebSession, msg InComeCmd) { expr, err := env.EvalString(msg.Content) if err != nil { session.Output(env.GetStackTrace(err)) } else { session.Output(expr.SexpString(nil)) } }`

the program is running.. when i sent a cmd: (def hi "hello") and sent hi

it work ok, show "hello" in my web page textbox, but send the cmd: (defn sayhi [name] (printf "hi %v" name)) and sent (sayhi "talis")

it will output nil in my textbox, but in the logs, have a right result hi talis

what exactly am I doing wrong? thanks!!!

glycerine commented 6 years ago

The last value evaluated in a function is what it returns, and note that the (printf) function returns nil after doing its side effect of printing. So sayhi is returning nil -- that sounds like what you are seeing, and what I expect. Does that make sense? (sprintf) will return the string it constructs, if you want that.