Module: ClassHDL::AssignDefOpertor

Included in:
HDLFunctionIvoke, StructVar, Clock, Integer, Logic, NqString, Numeric, Reset, String, TdlSpace::ArrayChain
Defined in:
lib/tdl/class_hdl/hdl_redefine_opertor.rb

Constant Summary collapse

OP_SYMBOLS =

OP_SYMBOLS = %w- * / % > < >= <= == != << | &

ClassHDL::OP_SYMBOLS
@@included_class =
[]
@@curr_assign_block =

HDLAssignBlock ##HDLAlwaysCombBlock

HDLAssignBlock.new(true)
@@curr_assign_block_stack =

@@curr_assign_block = nil

[HDLAssignBlock.new(true) ]
@@curr_opertor_stack =

@@curr_assign_block_stack = []

[:old]

Class Method Summary collapse

Class Method Details

.curr_assign_blockObject



308
309
310
# File 'lib/tdl/class_hdl/hdl_redefine_opertor.rb', line 308

def self.curr_assign_block
    @@curr_assign_block
end

.curr_assign_block=(b) ⇒ Object



312
313
314
315
316
317
# File 'lib/tdl/class_hdl/hdl_redefine_opertor.rb', line 312

def self.curr_assign_block=(b)
    unless b  
        raise TdlError.new('Assign Block cant be nil')
    end
    @@curr_assign_block = b
end

.curr_assign_block_stackObject



319
320
321
# File 'lib/tdl/class_hdl/hdl_redefine_opertor.rb', line 319

def self.curr_assign_block_stack
    @@curr_assign_block_stack
end

.curr_opertor_stackObject



304
305
306
# File 'lib/tdl/class_hdl/hdl_redefine_opertor.rb', line 304

def self.curr_opertor_stack
    @@curr_opertor_stack
end

.included(mod) ⇒ Object



339
340
341
342
343
# File 'lib/tdl/class_hdl/hdl_redefine_opertor.rb', line 339

def self.included(mod)
    @@included_class.push mod
    mod.extend self
    init_op_methods(mod)
end

.init_op_methods(aclass) ⇒ Object



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/tdl/class_hdl/hdl_redefine_opertor.rb', line 345

def self.init_op_methods(aclass)

    # if aclass.methods.include? :inst
    #     class_inst = aclass.nc_create
    # else
    #     class_inst = aclass.new
    # end
    
    aclass.class_eval do

        def operation_tow(symb,b)
            # puts "aclass #{symb} #{b.class}"
            if b.is_a? OpertorChain
                b.slaver = true 
            end
            ## 当 进行 X < Y 等运算时OpertorChain 需要获取 assign block的 belong_to_module
            if @@curr_assign_block 
                bblm = @@curr_assign_block.belong_to_module
            elsif self.respond_to?(:belong_to_module)
                bblm = self.belong_to_module
            elsif b.respond_to?(:belong_to_module)
                bblm = b.belong_to_module
                
            else
                bblm = nil 
            end

            new_op = OpertorChain.new(nil, bblm) 
            new_op.tree.push([self])
            new_op.tree.push([b,symb])
            if @@curr_assign_block
                @@curr_assign_block.opertor_chains.push(new_op)
            else 
                raise TdlError.new("operation_tow[#{symb}] <#{b}> Error: curr_assign_block is nil ")
            end
            return new_op
        end

    
        ClassHDL::OP_SYMBOLS.each do |symb|
            # if class_inst.respond_to?(symb)
            if self.instance_methods.include?(symb.to_sym)
                alias_method "_old_#{symb}__",symb
            else
                define_method("_old_#{symb}__") do |b|
                    operation_tow(symb,b)
                end
            end
    
            ## define new
    
            define_method("_new_#{symb}__") do |b|
                operation_tow(symb,b)
            end
        end

        ## 定义片选
        def new_slice_cc(*a)
            if a.size == 1
                if a[0].is_a? Range 
                    "#{self}[#{a[0].first}:#{a[1].last}]".to_nq
                else 
                    "#{self}[#{a[0]}]".to_nq
                end
            else 
                "#{self}[#{a[0]}:#{a[1]}]".to_nq
            end
        end

        if self.instance_methods.include?("[]".to_sym)
            alias_method "_old_slice_","[]"
        else 
            alias_method "_old_slice_","new_slice_cc"
        end
    end
end

.use_new_yield_opertorsObject



422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
# File 'lib/tdl/class_hdl/hdl_redefine_opertor.rb', line 422

def self.use_new_yield_opertors
    # NqString.class_exec do
    #     define_method("+") do |a|
    #         "+++++"
    #     end 
    # end
    @@included_class.each do |oc|
        oc.class_eval do
            ClassHDL::OP_SYMBOLS.each do |symb|
                # if symb.eql? "<="
                #     alias_method symb,:_assign_small_and_eq
                #     # define_method(symb,instance_method(:_assign_small_and_eq))
                # else
                #     alias_method symb,"_new_#{symb}__"
                #     # define_method(symb,instance_method("_new_#{symb}__"))
                # end
                alias_method symb,"_new_#{symb}__"
                # define_method(symb,instance_method("_new_#{symb}__"))
                ## 测试用
                # define_method(symb) do |a|
                #     "+++++++"
                # end

            end
        end
    end
end

.use_old_cond_opertorsObject



450
451
452
453
454
455
456
457
458
459
# File 'lib/tdl/class_hdl/hdl_redefine_opertor.rb', line 450

def self.use_old_cond_opertors
    ClassHDL::OP_SYMBOLS.each do |symb|
        @@included_class.each do |oc|
            oc.class_eval do
                alias_method symb,"_old_#{symb}__"
                # define_method(symb,instance_method("_old_#{symb}__"))
            end
        end
    end
end

.with_new_assign_block(na, &block) ⇒ Object



323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/tdl/class_hdl/hdl_redefine_opertor.rb', line 323

def self.with_new_assign_block(na,&block)
    unless na  
        raise TdlError.new('Assign Block cant be nil')
    end
    @@curr_assign_block = na 
    @@curr_assign_block_stack.push(na)
    rels = yield(na)
     
    @@curr_assign_block_stack.pop 
    @@curr_assign_block = @@curr_assign_block_stack.last
    rels
end

.with_new_opertor(&block) ⇒ Object



484
485
486
487
488
489
490
# File 'lib/tdl/class_hdl/hdl_redefine_opertor.rb', line 484

def self.with_new_opertor(&block)
    use_new_yield_opertors
    @@curr_opertor_stack.push :new
    rels = yield
    @@curr_opertor_stack.pop
    rels 
end

.with_normal_opertor(&block) ⇒ Object



492
493
494
495
496
497
498
# File 'lib/tdl/class_hdl/hdl_redefine_opertor.rb', line 492

def self.with_normal_opertor(&block)
    use_old_cond_opertors
    @@curr_opertor_stack.push :old
    rels = yield
    @@curr_opertor_stack.pop
    rels
end

.with_rollback_opertors(use_op, &block) ⇒ Object



461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# File 'lib/tdl/class_hdl/hdl_redefine_opertor.rb', line 461

def self.with_rollback_opertors(use_op,&block)
   
    case(use_op)
    when :new
        rels = with_new_opertor(&block)
    when :old
        rels = with_normal_opertor(&block)
    else 

    end

    case(@@curr_opertor_stack.last)
    when :new
        use_new_yield_opertors
    when :old
        use_old_cond_opertors
    else
        use_old_cond_opertors
    end
    
    rels
end