aboutsummaryrefslogtreecommitdiff
path: root/examples/lexpr.pd_lua
blob: 487c87e28decef0236ef97bf3812a0e18f8d8731 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
local lexpr = pd.Class:new():register("lexpr")

local function sandbox(e, f) -- only supports nullary f() with one return
  local g = getfenv(f)
  setfenv(f, e)
  local r = f()
  setfenv(f, g)
  return r
end

local lexpr_globals = {
  abs    = math.abs,
  acos   = math.acos,
  asin   = math.asin,
  atan   = math.atan,
  atan2  = math.atan2,
  ceil   = math.ceil,
  cos    = math.cos,
  cosh   = math.cosh,
  deg    = math.deg,
  exp    = math.exp,
  floor  = math.floor,
  fmod   = math.fmod,
  log    = math.log,
  log10  = math.log10,
  max    = math.max,
  min    = math.min,
  int    = function (x) i,f = math.modf(x) ; return i end,
  wrap   = function (x) i,f = math.modf(x) ; return f end,
  pi     = math.pi,
  pow    = math.pow,
  rad    = math.rad,
  sin    = math.sin,
  sinh   = math.sinh,
  sqrt   = math.sqrt,
  tan    = math.tan,
  tanh   = math.tanh,
  val    = function (s) return (pd.getvalue(s) or 0) end,
  choose = function (b,t,f) if b then return t else return f end end
}

function lexpr:readexpr(atoms)
  local vname = { }
  local context = { }
  local expr
  local i
  local k
  local v
  local j = 1
  local inlets
  local f
  local phase = "vars"
  for k,v in pairs(lexpr_globals) do
    context[k] = v
  end
  for i,v in ipairs(atoms) do
    if phase == "vars" then        -- create variables
      if v == "->" then
        inlets = i - 1
        phase = "expr"
        expr = ""
      else
        if type(v) == "string" then
          vname[j] = v
          context[v] = 0
          j = j + 1
        else
          self:error("lexpr: variable names must be symbols")
          return -1
        end
      end
    else if phase == "expr" then  -- build string
      expr = expr .. " " .. v
    else
      self:error("lexpr: internal error parsing expression")
      return -1
    end end
  end
  f = assert(loadstring("return {" .. expr .. " }"))
  local outlets = #(sandbox(context, f))
  return inlets, vname, context, f, outlets
end

function lexpr:initialize(sel, atoms)
  self.vname = { }
  self.context = { }
  self.hot = { }
  self.f = function () return 0 end
  function self:in_1_bang()
    local r = sandbox(self.context, self.f)
    local i
    for i = self.outlets,1,-1 do
      if type(r[i]) == "number" then
        self:outlet(i, "float", { r[i] })
      else if type(r[i]) == "string" then
        self:outlet(i, "symbol", { r[i] })
      else
        self:error("calculated a " .. type(r[i]) .. " but expected a number or a string")
      end end
    end
  end
  function self:in_n_float(i, f)
    self.context[self.vname[i]] = f
    if self.hot[i] then self:in_1_bang() end
  end
  function self:in_n_symbol(i, s)
    self.context[self.vname[i]] = s
    if self.hot[i] then self:in_1_bang() end
  end
  function self:in_n_hot(i, atoms)
    if type(atoms[1]) == "number" then
      self.hot[i] = atoms[1] ~= 0
    else
      self:error("hot method expects a float")
    end
  end
  function self:in_1_lexpr(atoms)
    local inlets
    local vname
    local context
    local f
    local outlets
    inlets, vname, context, f, outlets = self:readexpr(atoms)
    if (inlets == self.inlets) and (outlets == self.outlets) then
      self.vname = vname
      self.context = context
      self.f = f
    else
      self:error("new expression has different inlet/outlet count")
    end
  end
  self.inlets, self.vname, self.context, self.f, self.outlets = self:readexpr(atoms)
  if self.inlets < 1 then
    pd.post("lexpr: error: would have no inlets")
    return false
  end
  if self.outlets < 1 then
    pd.post("lexpr: error: would have no outlets")
    return false
  end
  for i = 1,self.inlets,1 do
    self.hot[i] = i == 1
  end
  return true
end