2017-02-11 06:06:45 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/yuin/gopher-lua"
|
|
|
|
)
|
|
|
|
|
2017-06-16 20:55:22 -04:00
|
|
|
const luaTabulationTypeName = "tabulation"
|
2017-02-11 06:06:45 -05:00
|
|
|
const luaSeriesTypeName = "series"
|
|
|
|
|
2017-06-16 20:55:22 -04:00
|
|
|
func luaRegisterTabulations(L *lua.LState) {
|
|
|
|
mtr := L.NewTypeMetatable(luaTabulationTypeName)
|
|
|
|
L.SetGlobal("tabulation", mtr)
|
|
|
|
L.SetField(mtr, "new", L.NewFunction(luaTabulationNew))
|
|
|
|
L.SetField(mtr, "__index", L.NewFunction(luaTabulation__index))
|
2017-02-11 06:06:45 -05:00
|
|
|
L.SetField(mtr, "__metatable", lua.LString("protected"))
|
|
|
|
|
|
|
|
mts := L.NewTypeMetatable(luaSeriesTypeName)
|
|
|
|
L.SetGlobal("series", mts)
|
|
|
|
L.SetField(mts, "__index", L.NewFunction(luaSeries__index))
|
|
|
|
L.SetField(mts, "__metatable", lua.LString("protected"))
|
|
|
|
}
|
|
|
|
|
2017-06-16 20:55:22 -04:00
|
|
|
// Checks whether the first lua argument is a *LUserData with *Tabulation and returns *Tabulation
|
|
|
|
func luaCheckTabulation(L *lua.LState, n int) *Tabulation {
|
2017-02-11 06:06:45 -05:00
|
|
|
ud := L.CheckUserData(n)
|
2017-06-16 20:55:22 -04:00
|
|
|
if tabulation, ok := ud.Value.(*Tabulation); ok {
|
|
|
|
return tabulation
|
2017-02-11 06:06:45 -05:00
|
|
|
}
|
2017-06-16 20:55:22 -04:00
|
|
|
L.ArgError(n, "tabulation expected")
|
2017-02-11 06:06:45 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks whether the first lua argument is a *LUserData with *Series and returns *Series
|
|
|
|
func luaCheckSeries(L *lua.LState, n int) *Series {
|
|
|
|
ud := L.CheckUserData(n)
|
|
|
|
if series, ok := ud.Value.(*Series); ok {
|
|
|
|
return series
|
|
|
|
}
|
|
|
|
L.ArgError(n, "series expected")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-16 20:55:22 -04:00
|
|
|
func luaTabulationNew(L *lua.LState) int {
|
2017-02-11 06:06:45 -05:00
|
|
|
numvalues := L.CheckInt(1)
|
|
|
|
ud := L.NewUserData()
|
2017-06-16 20:55:22 -04:00
|
|
|
ud.Value = &Tabulation{
|
2017-02-11 06:06:45 -05:00
|
|
|
Labels: make([]string, numvalues),
|
|
|
|
Series: make(map[string]*Series),
|
|
|
|
}
|
2017-06-16 20:55:22 -04:00
|
|
|
L.SetMetatable(ud, L.GetTypeMetatable(luaTabulationTypeName))
|
2017-02-11 06:06:45 -05:00
|
|
|
L.Push(ud)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-06-16 20:55:22 -04:00
|
|
|
func luaTabulation__index(L *lua.LState) int {
|
2017-02-11 06:06:45 -05:00
|
|
|
field := L.CheckString(2)
|
|
|
|
|
|
|
|
switch field {
|
|
|
|
case "Label", "label":
|
2017-06-16 20:55:22 -04:00
|
|
|
L.Push(L.NewFunction(luaTabulationLabel))
|
2017-02-11 06:06:45 -05:00
|
|
|
case "Series", "series":
|
2017-06-16 20:55:22 -04:00
|
|
|
L.Push(L.NewFunction(luaTabulationSeries))
|
2017-02-11 10:12:04 -05:00
|
|
|
case "Title", "title":
|
2017-06-16 20:55:22 -04:00
|
|
|
L.Push(L.NewFunction(luaTabulationTitle))
|
2017-02-11 10:12:04 -05:00
|
|
|
case "Subtitle", "subtitle":
|
2017-06-16 20:55:22 -04:00
|
|
|
L.Push(L.NewFunction(luaTabulationSubtitle))
|
|
|
|
case "Units", "units":
|
|
|
|
L.Push(L.NewFunction(luaTabulationUnits))
|
2017-02-11 06:06:45 -05:00
|
|
|
default:
|
2017-06-16 20:55:22 -04:00
|
|
|
L.ArgError(2, "unexpected tabulation attribute: "+field)
|
2017-02-11 06:06:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-06-16 20:55:22 -04:00
|
|
|
func luaTabulationLabel(L *lua.LState) int {
|
|
|
|
tabulation := luaCheckTabulation(L, 1)
|
2017-02-11 06:06:45 -05:00
|
|
|
labelnumber := L.CheckInt(2)
|
|
|
|
label := L.CheckString(3)
|
|
|
|
|
2017-06-16 20:55:22 -04:00
|
|
|
if labelnumber > cap(tabulation.Labels) || labelnumber < 1 {
|
2017-02-11 06:06:45 -05:00
|
|
|
L.ArgError(2, "Label index must be between 1 and the number of data points, inclusive")
|
|
|
|
}
|
2017-06-16 20:55:22 -04:00
|
|
|
tabulation.Labels[labelnumber-1] = label
|
2017-02-11 06:06:45 -05:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2017-06-16 20:55:22 -04:00
|
|
|
func luaTabulationSeries(L *lua.LState) int {
|
|
|
|
tabulation := luaCheckTabulation(L, 1)
|
2017-02-11 06:06:45 -05:00
|
|
|
name := L.CheckString(2)
|
|
|
|
ud := L.NewUserData()
|
|
|
|
|
2017-06-16 20:55:22 -04:00
|
|
|
s, ok := tabulation.Series[name]
|
2017-02-11 06:06:45 -05:00
|
|
|
if ok {
|
|
|
|
ud.Value = s
|
|
|
|
} else {
|
2017-06-16 20:55:22 -04:00
|
|
|
tabulation.Series[name] = &Series{
|
2017-02-17 10:01:31 -05:00
|
|
|
Series: make(map[string]*Series),
|
2017-06-16 20:55:22 -04:00
|
|
|
Values: make([]float64, cap(tabulation.Labels)),
|
2017-02-11 06:06:45 -05:00
|
|
|
}
|
2017-06-16 20:55:22 -04:00
|
|
|
ud.Value = tabulation.Series[name]
|
2017-02-11 06:06:45 -05:00
|
|
|
}
|
|
|
|
L.SetMetatable(ud, L.GetTypeMetatable(luaSeriesTypeName))
|
|
|
|
L.Push(ud)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-06-16 20:55:22 -04:00
|
|
|
func luaTabulationTitle(L *lua.LState) int {
|
|
|
|
tabulation := luaCheckTabulation(L, 1)
|
2017-02-11 10:12:04 -05:00
|
|
|
|
|
|
|
if L.GetTop() == 2 {
|
2017-06-16 20:55:22 -04:00
|
|
|
tabulation.Title = L.CheckString(2)
|
2017-02-11 10:12:04 -05:00
|
|
|
return 0
|
|
|
|
}
|
2017-06-16 20:55:22 -04:00
|
|
|
L.Push(lua.LString(tabulation.Title))
|
2017-02-11 10:12:04 -05:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-06-16 20:55:22 -04:00
|
|
|
func luaTabulationSubtitle(L *lua.LState) int {
|
|
|
|
tabulation := luaCheckTabulation(L, 1)
|
2017-02-11 10:12:04 -05:00
|
|
|
|
|
|
|
if L.GetTop() == 2 {
|
2017-06-16 20:55:22 -04:00
|
|
|
tabulation.Subtitle = L.CheckString(2)
|
2017-02-11 10:12:04 -05:00
|
|
|
return 0
|
|
|
|
}
|
2017-06-16 20:55:22 -04:00
|
|
|
L.Push(lua.LString(tabulation.Subtitle))
|
2017-02-11 10:12:04 -05:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-06-16 20:55:22 -04:00
|
|
|
func luaTabulationUnits(L *lua.LState) int {
|
|
|
|
tabulation := luaCheckTabulation(L, 1)
|
2017-02-11 10:12:04 -05:00
|
|
|
|
|
|
|
if L.GetTop() == 2 {
|
2017-06-16 20:55:22 -04:00
|
|
|
tabulation.Units = L.CheckString(2)
|
2017-02-11 10:12:04 -05:00
|
|
|
return 0
|
|
|
|
}
|
2017-06-16 20:55:22 -04:00
|
|
|
L.Push(lua.LString(tabulation.Units))
|
2017-02-11 10:12:04 -05:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-02-11 06:06:45 -05:00
|
|
|
func luaSeries__index(L *lua.LState) int {
|
|
|
|
field := L.CheckString(2)
|
|
|
|
|
|
|
|
switch field {
|
|
|
|
case "Value", "value":
|
|
|
|
L.Push(L.NewFunction(luaSeriesValue))
|
2017-02-17 10:01:31 -05:00
|
|
|
case "Series", "series":
|
|
|
|
L.Push(L.NewFunction(luaSeriesSeries))
|
2017-02-11 06:06:45 -05:00
|
|
|
default:
|
|
|
|
L.ArgError(2, "unexpected series attribute: "+field)
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func luaSeriesValue(L *lua.LState) int {
|
|
|
|
series := luaCheckSeries(L, 1)
|
|
|
|
valuenumber := L.CheckInt(2)
|
|
|
|
value := float64(L.CheckNumber(3))
|
|
|
|
|
|
|
|
if valuenumber > cap(series.Values) || valuenumber < 1 {
|
|
|
|
L.ArgError(2, "value index must be between 1 and the number of data points, inclusive")
|
|
|
|
}
|
|
|
|
series.Values[valuenumber-1] = value
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2017-02-17 10:01:31 -05:00
|
|
|
func luaSeriesSeries(L *lua.LState) int {
|
2017-02-11 06:06:45 -05:00
|
|
|
parent := luaCheckSeries(L, 1)
|
|
|
|
name := L.CheckString(2)
|
|
|
|
ud := L.NewUserData()
|
|
|
|
|
2017-02-17 10:01:31 -05:00
|
|
|
s, ok := parent.Series[name]
|
2017-02-11 06:06:45 -05:00
|
|
|
if ok {
|
|
|
|
ud.Value = s
|
|
|
|
} else {
|
2017-02-17 10:01:31 -05:00
|
|
|
parent.Series[name] = &Series{
|
|
|
|
Series: make(map[string]*Series),
|
|
|
|
Values: make([]float64, cap(parent.Values)),
|
2017-02-11 06:06:45 -05:00
|
|
|
}
|
2017-02-17 10:01:31 -05:00
|
|
|
ud.Value = parent.Series[name]
|
2017-02-11 06:06:45 -05:00
|
|
|
}
|
|
|
|
L.SetMetatable(ud, L.GetTypeMetatable(luaSeriesTypeName))
|
|
|
|
L.Push(ud)
|
|
|
|
return 1
|
|
|
|
}
|