Method: HDLRuby::Viz::Node#place_and_route_statement_vertically

Defined in:
lib/HDLRuby/hruby_viz.rb

#place_and_route_statement_vertically(stmnt, x, y, cond_succ = nil) ⇒ Object

Place statements vertically from statement +stmnt+ at position +x+, +y+.

+cond_succ+ the upper conditional successor if any.

Returns the width and height of the enclosing block.



3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
# File 'lib/HDLRuby/hruby_viz.rb', line 3194

def place_and_route_statement_vertically(stmnt,x,y,cond_succ=nil)
# def place_and_route_statement_vertically(stmnt,x,y)
  puts "place_and_route_statement_vertically with type=#{stmnt.type} x=#{x} y=#{y}"
  # Set the current statement y position.
  stmnt.ypos = y
  stmnt.xpos = x
  w = x + stmnt.width + 1
  # Depending of the kind of statement.
  case stmnt.type
  when :seq
    cur_w, last_y = 
      place_and_route_statement_vertically(stmnt.branches[0],x,y)
    w = cur_w if cur_w > w
    # Ensure seq has an height
    last_y = y+3 if last_y <= y
    # Update the size of the block.
    stmnt.height = last_y - y - 1
    stmnt.width = w - x - 1
    # Update the y position.
    y = last_y
  when :par
    stmnt.place_and_route_par(x,y)
    last_y = stmnt.height + y + 2
    w = stmnt.width + x + 1
    # Update the y position.
    y = last_y-1
  when :if, :case, :repeat
    fs = stmnt.type != :case ? 1 : 2 # First statement position.
    last_y = y
    cur_x = x
    cur_w = w
    last_y += 3
    # The yes is left.
    branch = stmnt.branches[fs]
    cur_w, cur_y = place_and_route_statement_vertically(branch,
                                                        # cur_x+stmnt.width+2,y)
                                                        cur_w+2,y)
    # And connect it with an arrow.
    @arrows << [x+stmnt.width, y+stmnt.height/2, 
                # cur_x+stmnt.width+2, y+stmnt.height/2]
                cur_w-branch.width-1, y+stmnt.height/2]
    # And starts the connection it to the successor if any.
    # But maybe there is a new successor, check it.
    cond_succ = stmnt.successor if stmnt.successor 
    if cond_succ and stmnt.type != :repeat then
      @arrows << [cur_w-1, cur_y-stmnt.height/2,
                  cur_w,   cur_y-stmnt.height/2]
    end
    # The no and elsif are down
    fy = y + 2
    last_y = cur_y if cur_y > last_y
    w = cur_w if cur_w > w
    stmnt.branches[fs+1..-1].each do |branch|
      # Place and route the branch
      cur_w, cur_y = 
        place_and_route_statement_vertically(branch,x,cur_y,
                                             stmnt.successor)
        # place_and_route_statement_vertically(branch,x,cur_y)
      # And connect it with an arrow.
      # @arrows << [x+stmnt.width/2, last_y-1, 
      @arrows << [x+stmnt.width/2, fy, 
                  x+stmnt.width/2, last_y]
      fy = last_y + 2
      # Update the width and height from the branch size.
      w = cur_w if cur_w > w
      last_y = cur_y if cur_y > last_y
      # if branch.type != :if and branch.type != :case then
      #   # End of if/case.
      #   last_y += 1
      # end
    end
    if fs+1 == stmnt.branches.size and stmnt.successor then
      # There were no "no" branches, add an arrow up to the successor.
      @arrows << [x+stmnt.width/2, fy, 
                  x+stmnt.width/2, last_y]
    end

    # Update the width and height from the branch size.
    w = cur_w if cur_w > w
    # cur_x += cur_w + 1
    cur_x += cur_w
    last_y = cur_y if cur_y > last_y
    # Prepare the next step. 
    y = last_y
  when :empty
     w -= 2
    # @arrows << [x+stmnt.width/2, y, 
    #             x+stmnt.width/2, y+3]
    # else
    # y += 3
    # end
  else
    unless stmnt.type == :assign or stmnt.type == :wait or 
        stmnt.type == :print or stmnt.type == :terminate then
      raise "Unknown statement: #{stmnt.type}"
    end
    y += 3
  end
  # Recurse on successor if any.
  if stmnt.successor then
    puts "For stmnt type: #{stmnt.type} Successor is #{stmnt.successor.name}"
    py = y
    nw, y = place_and_route_statement_vertically(stmnt.successor,x,y)
    w = nw if nw > w
    # And connect it with an arrow.
    # @arrows << [x + self.width/2, py-1, x + stmnt.width/2, py]
    @arrows << [x + self.width/2, py-1, x + self.width/2, py]
    # And connect it to the other branches if condition.
    if stmnt.type == :if or stmnt.type == :case then
      # @arrows << [w, stmnt.ypos+stmnt.height/2,
      @arrows << [w, stmnt.branches[fs].ypos+stmnt.branches[fs].height,
                  x+self.width, py+stmnt.successor.height/2]
    end
  end
  return [ w, y ]
end