Skip to content

Instantly share code, notes, and snippets.

@trentgill
Created April 29, 2021 22:47
Show Gist options
  • Save trentgill/4d8f83a294bc89d86b0033e74f5d9e7a to your computer and use it in GitHub Desktop.
Save trentgill/4d8f83a294bc89d86b0033e74f5d9e7a to your computer and use it in GitHub Desktop.
support more native-style crow syntax on norns
--- norns library for crow namespace support
-- enables full syntax support for calling crow functions, and setting crow values
-- does *not* support directly querying values or function responses from crow
--- helper fns for quoting (aka stringifying) lua constructs
quotekey = function(ix)
-- stringify table keys with [] style
local fstr = (type(ix)=='number') and '[%g]' or '[%q]'
return string.format(fstr, ix)
end
quote = function(value)
-- stringify anything so it can be read as lua code
if type(value) == 'string' then return string.format('%q',value)
elseif type(value) ~= 'table' then return tostring(value)
else -- recur per table element
local t = {}
for k,v in pairs(value) do
table.insert(t, quotekey(k) .. '=' .. quote(v))
end
return string.format('{%s}', table.concat(t, ','))
end
end
quotevarargs = function(...)
local t = {}
for _,v in ipairs{...} do
table.insert(t, quote(v) )
end
return table.concat(t, ',')
end
-- metamethods for building up strings of underlying call
-- and calling / setting values on crow
-- note: fn calls & value queries don't return anything
-- specifically means ASL construction won't work
crowSub = {
__index = function(self, ix)
-- build up the table access key chain
self.str = self.str .. quotekey(ix)
return self
end,
__newindex = function(self, ix, val)
-- set a table value on crow
self.send(self.str .. quotekey(ix) .. '=' .. val)
end,
__call = function(self, ...)
local qt = quotevarargs(...)
self.send(self.str .. '(' .. qt .. ')')
-- TODO future. return a clock object (?) that is invoked when value returns from crow
end,
}
--- crow table
-- defines fns that relate to crow but run on norns
-- defines ^^response event handlers
-- anything else is forwarded to crow via metamethods
crow = {
-- set global var on crow
-- TODO can probably forward directly to crowSub?
__newindex = function(self, ix, val) crow.send(ix .. '=' .. quote(val)) end,
-- create a table that will resolve to a crow.send call in crowSub
__index = function(self, ix) return setmetatable({send=self.send, str=ix}, crowSub) end,
}
setmetatable(crow, crow)
crow.send = function(...) print('crow.send', ...) end
-- crow.send = function(cmd)
-- if norns.crow.dev then
-- _norns.crow_send(norns.crow.dev, cmd)
-- end
-- end
--- TEST code
-- just run this file to see what is sent to crow
-- set global crow var
crow.myvar = 1
-- set table var on crow with string index
crow.tab.key = 2
-- set nested table var on crow with numeric index
crow.tab[3].key = 4
-- call global crow fn no arguments
crow.fn()
-- call global crow fn with arguments
crow.fn(6)
-- call table fn
crow.tab.fn(8,'string',9)
-- call table fn with table argument
crow.tab.fn{1,2,3,'string',3.2,-12389.00}
-- call table fn with k,v pair args
crow.tab.fn{a=1, b=2, c=3}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment