glycerine/zygomys

Calling existing code

lenw opened this issue · 2 comments

lenw commented

Having an example of calling existing code from a script would be very helpful ?

Hi @lenw,

The easiest way is to define a new Go function that becomes a new lisp function. Have a look at repl/functions.go, and search for the BuiltinFunctions array that lets you add new functions to the language itself. For example "gob" is the last one defined there, and that ties it to the definition of GobEncodeFunction which lives in repl/gob.go. From inside your newly added Go function, you can call any existing Go code you'd like.

It is also possible to call methods on go structs entirely using reflection, after only registering the struct.

The script here gives an example (script:)

https://github.com/glycerine/zygomys/blob/master/tests/methodcall.zy

of calling into methods on the demo structs defined here:

https://github.com/glycerine/zygomys/blob/master/repl/demo_go_structs.go

Also the unit tests in
https://github.com/glycerine/zygomys/blob/master/repl/callgo_test.go
provide additional examples of the reflection only approach.

Last but not least, the API docs are here:
https://github.com/glycerine/zygomys/wiki/Go-API

Let me know if you have any other questions.

@lenw closing this as answered; feel free to re-open or open a new issue if you have any other questions, Len.
Jason