how to parse script expression with go and run the script

233 views
Skip to first unread message

hui zhang

unread,
Feb 15, 2017, 1:48:32 AM2/15/17
to golang-nuts
 I am developing a game engine 
I wish some event could be trigger by script. and this script expression is self-defined.
Like below 
trigger.ini 
----------------
trigger = "power >= 1000"
trigger = "statetype == A || statetype == B "
trigger = "foo() > 1"
-----------------
power and statetype are a struct variable (it also can be defined as global) in go.
foo are a struct method (it also can be defined as global) 


how can I parse the expression like "power >= 1000"
and execute it in go.

I notice there is a  go/parse package ,  but I am not sure how to use it .

howar...@gmail.com

unread,
Feb 15, 2017, 8:35:23 AM2/15/17
to golang-nuts
The go/parse package is not really going to help much, as Go is a compiled language. What you are wanting is something akin to a scripting language, something with an 'eval' type function. While it is just barely feasible that you could accomplish something directly with Go using on-the-fly compilation, dynamic object loading, and plugins, it is going to be a lot of work and not likely to be a comfortable fit.

My recommendation would be to look at simply embedding a script engine in your Go program. There are several Lua implementations for Go, for example.

Have a look at http://awesome-go.com/#embeddable-scripting-languages and see if one of them might work for your situation. 

Jason E. Aten

unread,
Feb 16, 2017, 4:18:37 PM2/16/17
to golang-nuts
Hello Hui,

I would invite you try my scripting language, zygomys. It is 100% Go. It offers both (lisp) and {infix} syntax.

https://github.com/glycerine/zygomys

- Jason
Reply all
Reply to author
Forward
0 new messages