-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
automoji: add lua scripting interface
- Loading branch information
Showing
7 changed files
with
308 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package automoji | ||
|
||
import ( | ||
"regexp" | ||
|
||
lua "github.com/yuin/gopher-lua" | ||
) | ||
|
||
func registerEmojiSetType(L *lua.LState) { | ||
mt := L.NewTypeMetatable("emojiset") | ||
L.SetGlobal("emojiset", mt) | ||
L.SetField(mt, "__index", L.SetFuncs(L.NewTable(), emojiSetMethods)) | ||
} | ||
|
||
func setGlobalEmojiSet(L *lua.LState, name string, es *emojiSet) int { | ||
ud := L.NewUserData() | ||
ud.Value = es | ||
L.SetMetatable(ud, L.GetTypeMetatable("emojiset")) | ||
L.SetGlobal(name, ud) | ||
return 1 | ||
} | ||
|
||
func checkEmojiSet(L *lua.LState) *emojiSet { | ||
ud := L.CheckUserData(1) | ||
if v, ok := ud.Value.(*emojiSet); ok { | ||
return v | ||
} | ||
L.ArgError(1, "emojiSet expected") | ||
return nil | ||
} | ||
|
||
var emojiSetMethods = map[string]lua.LGFunction{ | ||
"hasoptional": func(L *lua.LState) int { | ||
es := checkEmojiSet(L) | ||
e := L.CheckString(2) | ||
L.Push(lua.LBool(es.optional[e])) | ||
return 1 | ||
}, | ||
"addoptional": func(L *lua.LState) int { | ||
es := checkEmojiSet(L) | ||
e := L.CheckString(2) | ||
es.optional[e] = true | ||
return 0 | ||
}, | ||
"removeoptional": func(L *lua.LState) int { | ||
es := checkEmojiSet(L) | ||
e := L.CheckString(2) | ||
delete(es.optional, e) | ||
return 0 | ||
}, | ||
|
||
"hasrequired": func(L *lua.LState) int { | ||
es := checkEmojiSet(L) | ||
e := L.CheckString(2) | ||
for _, s := range es.required { | ||
if s == e { | ||
L.Push(lua.LBool(true)) | ||
return 1 | ||
} | ||
} | ||
L.Push(lua.LBool(false)) | ||
return 1 | ||
}, | ||
"addrequired": func(L *lua.LState) int { | ||
es := checkEmojiSet(L) | ||
e := L.CheckString(2) | ||
es.required = append(es.required, e) | ||
return 0 | ||
}, | ||
"removerequired": func(L *lua.LState) int { | ||
es := checkEmojiSet(L) | ||
e := L.CheckString(2) | ||
newRequired := es.required[:0] // share backing array | ||
for _, v := range es.required { | ||
if v == e { | ||
continue | ||
} | ||
newRequired = append(newRequired, v) | ||
} | ||
es.required = newRequired | ||
return 0 | ||
}, | ||
|
||
"message": func(L *lua.LState) int { | ||
es := checkEmojiSet(L) | ||
L.Push(lua.LString(es.message)) | ||
return 1 | ||
}, | ||
"messagematches": func(L *lua.LState) int { | ||
es := checkEmojiSet(L) | ||
e := L.CheckString(2) | ||
re := regexp.MustCompile(e) | ||
L.Push(lua.LBool(re.MatchString(es.message))) | ||
return 1 | ||
}, | ||
|
||
"hasword": func(L *lua.LState) int { | ||
es := checkEmojiSet(L) | ||
e := L.CheckString(2) | ||
for _, s := range es.words { | ||
if s == e { | ||
L.Push(lua.LBool(true)) | ||
return 1 | ||
} | ||
} | ||
L.Push(lua.LBool(false)) | ||
return 1 | ||
}, | ||
} | ||
|
||
func luaEval(es *emojiSet, code string) error { | ||
L := lua.NewState() | ||
defer L.Close() | ||
|
||
registerEmojiSetType(L) | ||
registerTurtleType(L) | ||
setGlobalEmojiSet(L, "es", es) | ||
|
||
return L.DoString(code) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package automoji | ||
|
||
import ( | ||
"github.com/hackebrot/turtle" | ||
lua "github.com/yuin/gopher-lua" | ||
) | ||
|
||
func registerTurtleType(L *lua.LState) { | ||
mt := L.NewTypeMetatable("turtleemoji") | ||
L.SetGlobal("turtleemoji", mt) | ||
L.SetField(mt, "findbyname", L.NewFunction(findTurtleByName)) | ||
L.SetField(mt, "findbychar", L.NewFunction(findTurtleByChar)) | ||
L.SetField(mt, "__index", L.SetFuncs(L.NewTable(), turtleMethods)) | ||
} | ||
|
||
func findTurtleByName(L *lua.LState) int { | ||
name := L.CheckString(1) | ||
|
||
ud := L.NewUserData() | ||
ud.Value = turtle.Emojis[name] | ||
L.SetMetatable(ud, L.GetTypeMetatable("turtleemoji")) | ||
L.Push(ud) | ||
return 1 | ||
} | ||
|
||
func findTurtleByChar(L *lua.LState) int { | ||
name := L.CheckString(1) | ||
|
||
ud := L.NewUserData() | ||
ud.Value = turtle.EmojisByChar[name] | ||
L.SetMetatable(ud, L.GetTypeMetatable("turtleemoji")) | ||
L.Push(ud) | ||
return 1 | ||
} | ||
|
||
func checkTurtle(L *lua.LState) *turtle.Emoji { | ||
ud := L.CheckUserData(1) | ||
if v, ok := ud.Value.(*turtle.Emoji); ok { | ||
return v | ||
} | ||
L.ArgError(1, "turtle.Emoji expected") | ||
return nil | ||
} | ||
|
||
var turtleMethods = map[string]lua.LGFunction{ | ||
"name": func(L *lua.LState) int { | ||
c := checkTurtle(L) | ||
|
||
L.Push(lua.LString(c.Name)) | ||
|
||
return 1 | ||
}, | ||
"category": func(L *lua.LState) int { | ||
c := checkTurtle(L) | ||
|
||
L.Push(lua.LString(c.Category)) | ||
|
||
return 1 | ||
}, | ||
"char": func(L *lua.LState) int { | ||
c := checkTurtle(L) | ||
|
||
L.Push(lua.LString(c.Char)) | ||
|
||
return 1 | ||
}, | ||
"haskeyword": func(L *lua.LState) int { | ||
c := checkTurtle(L) | ||
e := L.CheckString(2) | ||
|
||
for _, s := range c.Keywords { | ||
if s == e { | ||
L.Push(lua.LBool(true)) | ||
return 1 | ||
} | ||
} | ||
L.Push(lua.LBool(false)) | ||
return 1 | ||
}, | ||
} |