Class: Cyclotone::MiniNotation::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/cyclotone/mini_notation/compiler.rb

Constant Summary collapse

MAX_EXPANSION =
4096

Instance Method Summary collapse

Instance Method Details

#compile(node) ⇒ Object



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
# File 'lib/cyclotone/mini_notation/compiler.rb', line 8

def compile(node)
  case node
  when AST::Atom
    compile_atom(node)
  when AST::Rest
    Pattern.silence
  when AST::Sequence
    compile_sequence(node)
  when AST::Stack
    Pattern.stack(node.patterns.map { |pattern| compile(pattern) })
  when AST::Alternating
    compile_alternating(node)
  when AST::Repeat
    validate_expansion_count(node.count, "repeat count")
    compile(node.pattern).fast(node.count)
  when AST::Replicate
    validate_expansion_count(node.count, "replicate count")
    Pattern.timecat(Array.new(node.count) { [1, compile(node.pattern)] })
  when AST::Slow, AST::Elongate
    compile(node.pattern).slow(node.amount)
  when AST::Degrade
    compile(node.pattern).degrade_by(node.probability)
  when AST::Choice
    Pattern.randcat(node.patterns.map { |pattern| compile(pattern) })
  when AST::Euclidean
    compile_euclidean(node)
  when AST::Polymetric
    compile_polymetric(node)
  else
    raise ArgumentError, "unsupported AST node #{node.class}"
  end
end