module ClassHDL::AssignDefOpertor

Constants

OP_SYMBOLS

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

Public Class Methods

curr_assign_block() click to toggle source
# File lib/tdl/class_hdl/hdl_redefine_opertor.rb, line 308
def self.curr_assign_block
    @@curr_assign_block
end
curr_assign_block=(b) click to toggle source
# 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_stack() click to toggle source
# File lib/tdl/class_hdl/hdl_redefine_opertor.rb, line 319
def self.curr_assign_block_stack
    @@curr_assign_block_stack
end
curr_opertor_stack() click to toggle source
# File lib/tdl/class_hdl/hdl_redefine_opertor.rb, line 304
def self.curr_opertor_stack
    @@curr_opertor_stack
end
included(mod) click to toggle source
# 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) click to toggle source
# 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_opertors() click to toggle source
# 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_opertors() click to toggle source
# 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) { |na| ... } click to toggle source
# 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() { || ... } click to toggle source
# 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() { || ... } click to toggle source
# 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) click to toggle source
# 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

Public Instance Methods

new_slice_cc(*a) click to toggle source

定义片选

# File lib/tdl/class_hdl/hdl_redefine_opertor.rb, line 402
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
operation_tow(symb,b) click to toggle source
# File lib/tdl/class_hdl/hdl_redefine_opertor.rb, line 355
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