In article <43FAC87C.8000009 / capitain.de>,
Pit Capitain  <pit / capitain.de> wrote:
>Phil Tomson schrieb:
>> Well, it's a bit strange in the example above.  Really what I want is for it to 
>> be method-level, I suppose.  Recall that I showed an alternative where the user 
>> actually defines the initialize method explicitly:
>> 
>>    #....
>>    def initialize
>>       count = 0
>>       define_behvior {
>>         #...do something with count ...
>>       }
>>    end
>
>Hi Phil, sorry for the late reply. I had to implement part of the code 
>to understand why you can't use accessor methods for variables. Accessor 
>methods for inputs and outputs are no problem because you just read from 
>the inputs and set the outputs with a method call (<<). The problem with 
>variables is that you want to be able to set them via assignment, and 
>for Ruby
>
>   variable = value
>
>never is a method call. You could write
>
>   self.variable = value
>
>or use other methods to set the value of a variable, but with assignment 
>  you have to use local variables, like you've shown in the explicit 
>initialize method above.
>

Right.  Here's what I finally came up with:

  class AndGate < RHDL
    inputs :a, :b
    outputs :out
    init {
      somevar = 0
      define_behavior {
        out << (a & b)
        puts "somevar is: #{somevar}"
        somevar +=1
      }
    }
  end

So the 'init' method allows a place to 'declare' variables which will also be 
available to the define_behavior block.  The advantage of this over the 
explicit initialize is that there is no repetition of information (you'd have 
to pass the inputs and outputs to the initialize method) as I generate the 
initialize method so that given the above description, the AndGate could be 
used as:

  sa = Sig.new(Bit(0))
  sb = Sig.new(Bit(0))
  sout= Sig.new(Bit())

  a = AndGate.new(sa,sb,sout) #initialize was automatically generated

also, the 'init' block is optional.  If you have no variables to initialize 
then you don't need it:

  class AndGate < RHDL
    inputs  :a, :b
    outputs :out
    define_behavior {
      out << (a & b)
    }
  end

BTW: I'm thinking of using '<=' for assignment instead of '<<' since '<=' has 
lower precedence than '<<' and that would allow:
  out <= a & b  #parens not required

I still wish we had a ':=' operator that could be defined that would have the 
same precedense as '='.

Phil