Skip to content

Commit

Permalink
Merge pull request #9 from julianrubisch/6-row-to-multiple
Browse files Browse the repository at this point in the history
6 row to multiple
  • Loading branch information
julianrubisch authored Jun 14, 2018
2 parents d64c39c + c7773ff commit fd59d8d
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 14 deletions.
25 changes: 16 additions & 9 deletions lib/maxy/gen/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,34 @@ def generate_node(node, id)
@patch['patcher']['boxes'] << make_box(node, id)
@object_count += 1

node.child_nodes.each do |child_node|
node.child_nodes.each_with_index do |child_node, index|
child_id = "obj_#{@object_count}"
generate_node(child_node, child_id)
@patch['patcher']['lines'] << make_line(id, child_id)
if node.flags.include? :connect_children_individually
@patch['patcher']['lines'] << make_line(id, child_id, index, 0)
else
@patch['patcher']['lines'] << make_line(id, child_id)
end
end
end

def make_box(node, id)
box = @library[:objects][node.name].dup
box['id'] = id
box['patching_rect'] = [OFFSET_X + (node.x_rank - 1) * STEP_X, OFFSET_Y + (node.y_rank - 1) * STEP_Y, box['width'] || WIDTH, box['height'] || HEIGHT]
unless box['text'].nil?
box['text'] += " #{node.args}"
end
box['text'] += " #{node.args}" unless box['text'].nil?

box
end

def make_line(parent_id, child_id)
{ patchline: { destination: [child_id, 0], source: [parent_id, 0]} }
def make_line(parent_id, child_id, parent_outlet = 0, child_inlet = 0)
{
patchline:
{
destination: [child_id, child_inlet],
source: [parent_id, parent_outlet]
}
}
end

def align_tree(node, x_rank = 1, y_rank = 1)
Expand All @@ -71,6 +79,5 @@ def align_tree(node, x_rank = 1, y_rank = 1)
node
end
end

end
end
end
15 changes: 14 additions & 1 deletion lib/maxy/gen/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def parse(parent_node=@tree, closing_group=false)
else
parse_begin_group parent_node
child_node = parse_identifier parent_node
parse_equals child_node
parse_dash child_node
end

Expand Down Expand Up @@ -92,9 +93,21 @@ def parse_dash(obj_node)
parse(obj_node)
end
end

def parse_equals(obj_node)
if peek(:equals)
consume(:equals)
obj_node.flags << :connect_children_individually
parse(obj_node)
end
end
end

ObjectNode = Struct.new(:name, :args, :child_nodes, :x_rank, :y_rank)
ObjectNode = Struct.new(:name, :args, :child_nodes, :x_rank, :y_rank, :flags) do
def initialize(name, args, child_nodes, x_rank=0, y_rank=0, flags=[])
super
end
end
RootNode = Struct.new(:child_nodes)

end
Expand Down
5 changes: 3 additions & 2 deletions lib/maxy/gen/tokenizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ module Gen
class Tokenizer
TOKEN_TYPES = [
[:arguments, /({[^{}]*})/],
[:escaped_identifier, /(\\[\S][^-+{}()\\]*)(?=[-+*{}])?/],
[:identifier, /([^-+{}()\\]+)(?=[-+*{}])?/],
[:escaped_identifier, /(\\[\S][^-+={}()\\]*)(?=[-+=*{}])?/],
[:identifier, /([^-+={}()\\]+)(?=[-+=*{}])?/],
[:oparen, /(\()/],
[:cparen, /(\))/],
[:plus, /(\+)/],
[:equals, /(=)/],
[:dash, /(-)/]
]

Expand Down
2 changes: 1 addition & 1 deletion lib/maxy/gen/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Maxy
module Gen
VERSION = "0.3.0"
VERSION = "0.3.1"
end
end
23 changes: 22 additions & 1 deletion spec/maxy/generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,26 @@
expect(duplicate_keys).to be_empty
end

it 'should connect children individually if asked to do so' do
tree = RootNode.new([
ObjectNode.new('trigger', 'b b b', [
ObjectNode.new('int', '', []),
ObjectNode.new('int', '', []),
ObjectNode.new('int', '', [])
], 0, 0, [:connect_children_individually])
])
generator = Generator.new
generated = generator.generate(tree)

lines = JSON.parse(generated)['patcher']['lines']

end
expect(JSON.parse(generated)['patcher']['boxes']).not_to be_empty
expect(lines).not_to be_empty

expect(JSON.parse(generated)['patcher']['boxes'].size).to eq(4)
expect(lines.size).to eq(3)
expect(lines[0]['patchline']['source']).to eq(['obj_1', 0])
expect(lines[1]['patchline']['source']).to eq(['obj_1', 1])
expect(lines[2]['patchline']['source']).to eq(['obj_1', 2])
end
end
20 changes: 20 additions & 0 deletions spec/maxy/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,24 @@
expect { Parser.new(tokens).parse }.to raise_error(RuntimeError)
end

it 'should parse a row to multiple expression (=) ' do
tokens = [Token.new(:identifier, 'trigger'),
Token.new(:arguments, '{b b b}'),
Token.new(:equals, '='),
Token.new(:oparen, '('),
Token.new(:identifier, 'int'),
Token.new(:plus, '+'),
Token.new(:identifier, 'int'),
Token.new(:plus, '+'),
Token.new(:identifier, 'int'),
Token.new(:cparen, ')')]
tree = Parser.new(tokens).parse

expect(tree.child_nodes[0].name).to eq('trigger')
expect(tree.child_nodes[0].child_nodes.size).to eq(3)
expect(tree.child_nodes[0].flags).to include(:connect_children_individually)
expect(tree.child_nodes[0].child_nodes[0]).to eq(ObjectNode.new('int', '', [], 0, 0, []))
expect(tree.child_nodes[0].child_nodes[2]).to eq(ObjectNode.new('int', '', [], 0, 0, []))
end

end
6 changes: 6 additions & 0 deletions spec/maxy/tokenizer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,10 @@
expect(tokens.map(&:type)).to eq([:identifier, :dash, :oparen, :escaped_identifier, :dash, :identifier, :cparen, :plus, :oparen, :identifier, :dash, :identifier, :cparen])
expect(tokens.map(&:value)).to eq(%w(inlet - ( \* - outlet ) + ( trigger - outlet )))
end

it 'should tokenize an equals sign' do
tokens = Tokenizer.new('trigger{b b b}=(int+int+int)').tokenize
expect(tokens.map(&:type)).to eq([:identifier, :arguments, :equals, :oparen, :identifier, :plus, :identifier, :plus, :identifier, :cparen])
expect(tokens.map(&:value)).to eq(%w(trigger {b\ b\ b} = ( int + int + int ) ))
end
end

0 comments on commit fd59d8d

Please sign in to comment.