Skip to content

Instantly share code, notes, and snippets.

@aybabtme
Created July 27, 2015 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aybabtme/dec676c445d20d0463c1 to your computer and use it in GitHub Desktop.
Save aybabtme/dec676c445d20d0463c1 to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"flag"
"github.com/Shopify/go-lua"
"github.com/kortschak/zalgo"
"io"
"log"
"os"
"text/template"
)
type tmpFields struct {
Equation string
}
var (
luaUpTemplate = `function up(x1, x2) return {{.Equation}} end`
luaMiddleTemplate = `function middle(x1, x2) return {{.Equation}} end`
luaDownTemplate = `function down(x1, x2) return {{.Equation}} end`
upVal float64
midVal float64
downVal float64
luaUp string
luaMiddle string
luaDown string
)
func main() {
flag.Float64Var(&upVal, "up", 0.001, "initial value of up")
flag.Float64Var(&midVal, "mid", 0.001, "initial value of mid")
flag.Float64Var(&downVal, "down", 0.001, "initial value of down")
flag.StringVar(&luaUp, "up-func", "x1+0.01, x2+0.01", "body of a lua function of the form `"+luaUpTemplate+"`")
flag.StringVar(&luaMiddle, "mid-func", "x1+0.01, x2+0.01", "body of a lua function of the form `"+luaMiddleTemplate+"`")
flag.StringVar(&luaDown, "down-func", "x1+0.01, x2+0.01", "body of a lua function of the form `"+luaDownTemplate+"`")
flag.Parse()
l := lua.NewState()
lua.OpenLibraries(l)
registerFunc(l, "up-func", toLuaFunction(luaUpTemplate, tmpFields{luaUp}))
registerFunc(l, "mid-func", toLuaFunction(luaMiddleTemplate, tmpFields{luaMiddle}))
registerFunc(l, "down-func", toLuaFunction(luaDownTemplate, tmpFields{luaDown}))
z := zalgo.NewCorrupter(os.Stdout)
x := 0
z.Zalgo = func(n int, z *zalgo.Corrupter) {
z.Up = upFunc(l, z.Up)
z.Middle = midFunc(l, z.Middle)
z.Down = downFunc(l, z.Down)
x++
}
z.Up = complex(upVal, upVal)
z.Middle = complex(midVal, midVal)
z.Down = complex(downVal, downVal)
_, _ = io.Copy(z, os.Stdin)
}
func toLuaFunction(tmpStr string, fields tmpFields) string {
buf := bytes.NewBuffer(nil)
t := template.Must(template.New("lua").Parse(tmpStr))
err := t.Execute(buf, fields)
if err != nil {
panic(err)
}
return buf.String()
}
func registerFunc(l *lua.State, name, program string) {
if err := lua.LoadString(l, program); err != nil {
log.Fatalf("%s: %v", name, err)
}
if err := lua.ProtectedCall(l, 0, 0, 0); err != nil {
log.Fatalf("%s: %v", name, err)
}
}
func upFunc(l *lua.State, x complex128) complex128 { return invokeFunc(l, x, "up") }
func midFunc(l *lua.State, x complex128) complex128 { return invokeFunc(l, x, "middle") }
func downFunc(l *lua.State, x complex128) complex128 { return invokeFunc(l, x, "down") }
func invokeFunc(l *lua.State, x complex128, funcName string) complex128 {
lua.Global(l, funcName)
lua.PushNumber(l, real(x))
lua.PushNumber(l, imag(x))
lua.Call(l, 2, 2)
y := complex(lua.CheckNumber(l, 1), lua.CheckNumber(l, 2))
lua.Pop(l, 2)
return y
}
func pSt(l *lua.State) {
log.Printf("stack %d elems", lua.Top(l))
for i := lua.Top(l); i > 0; i-- {
log.Printf("\t%d (%d): %#v", i, i-lua.Top(l)-1, lua.ToValue(l, i))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment