Class: HDLRuby::HDRLoad

Inherits:
Object
  • Object
show all
Defined in:
lib/HDLRuby/hdrcc.rb

Overview

Class for loading HDLRuby files.

Constant Summary collapse

TOP_NAME =

TOP_NAME = "hdr_top_instance"

"__"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(top_system, top_file, dir, *params) ⇒ HDRLoad

Creates a new loader for a +top_system+ system in file +top_file+ from directory +dir+ with generic parameters +params+.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/HDLRuby/hdrcc.rb', line 50

def initialize(top_system,top_file,dir,*params)
    # Sets the top and the looking directory.
    @top_system = top_system.to_s
    @top_file = top_file.to_s
    @dir = dir.to_s
    @params = params

    # The list of required files.
    @requires = []

    # The list of the code texts (the first one should be the one
    # containing the top system).
    @texts = []

    # The list of the code checkers.
    @checks = []

    # The name of the top instance
    @top_name = TOP_NAME
end

Instance Attribute Details

#requiresObject (readonly)

The required files.



46
47
48
# File 'lib/HDLRuby/hdrcc.rb', line 46

def requires
  @requires
end

#top_instanceObject (readonly)

The top instance, only accessible after parsing the files.



43
44
45
# File 'lib/HDLRuby/hdrcc.rb', line 43

def top_instance
  @top_instance
end

Instance Method Details

#check_allObject

Checks the read files.



92
93
94
# File 'lib/HDLRuby/hdrcc.rb', line 92

def check_all
    @checks.each { |check| check.assign_check }
end

#get_topObject

Gets the (first) top system.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/HDLRuby/hdrcc.rb', line 103

def get_top
    # Get all the systems.
    systems = @checks.reduce([]) {|ar,check| ar + check.get_all_systems}
    # puts "First systems=#{systems}"
    # Remove the systems that are instantiated or included
    # (they cannot be tops)
    @checks.each do |check|
        # The instances
        check.get_all_instances(systems).each do |instance|
            systems.delete(check.get_instance_system(instance))
        end
        # The explicitly included systems
        check.get_all_includes(systems).each do |included|
            systems.delete(check.get_include_system(included))
        end
        # The system included when declaring (inheritance)
        check.get_all_inherits(systems).each do |inherit|
            systems -= check.get_inherit_systems(inherit)
        end
    end
    # puts "Now systems=#{systems}"
    # Return the first top of the list.
    return systems[-1]
end

#parseObject

Load the HDLRuby structure from an instance of the top module.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/HDLRuby/hdrcc.rb', line 130

def parse
    # Is there a top system specified yet?
    if @top_system == "" then
        # No, look for it.
        @top_system = get_top
        # puts "@top_system=#{@top_system}"
        unless @top_system then
            # Not found? Error.
            # Maybe it is a parse error, look for it.
            bind = TOPLEVEL_BINDING.clone
            eval("require 'HDLRuby'\n\nconfigure_high\n\n",bind)
            eval(@texts[0],bind,@top_file,1)
            # No parse error found.
            raise "Cannot find a top system." unless @top_system
        end
    end
    # Initialize the environment for processing the hdr file.
    bind = TOPLEVEL_BINDING.clone
    eval("require 'HDLRuby'\n\nconfigure_high\n\n",bind)
    # Process it.
    eval(@texts[0],bind,@top_file,1)
    # Get the resulting instance
    if @params.empty? then
        # There is no generic parameter
        @top_instance = 
            eval("#{@top_system} :#{@top_name}\n#{@top_name}",bind)
    else
        # There are generic parameters
        @top_instance = 
            # eval("#{@top_system} :#{@top_name},#{@params.join(",")}\n#{@top_name}",bind)
            eval("#{@top_system}(#{@params.join(",")}).(:#{@top_name})\n#{@top_name}",bind)
    end
end

#read(file) ⇒ Object

Loads a single +file+.



72
73
74
75
# File 'lib/HDLRuby/hdrcc.rb', line 72

def read(file)
    @texts << File.read(File.join(@dir,file) )
    @checks << Checker.new(@texts[-1],file)
end

#read_all(file = @top_file) ⇒ Object

Loads all the files from +file+.



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/HDLRuby/hdrcc.rb', line 78

def read_all(file = @top_file)
    # puts "read_all with file=#{file}"
    # Read the file
    read(file)
    # Get its required files.
    requires = @checks[-1].get_all_requires
    requires.each do |file|
        read_all(file) if file != "HDLRuby"
    end
    @requires += requires
    @requires.uniq!
end

#show_all(outfile = $stdout) ⇒ Object

Displays the syntax tree of all the files.



97
98
99
100
# File 'lib/HDLRuby/hdrcc.rb', line 97

def show_all(outfile = $stdout)
    # puts "@checks.size=#{@checks.size}"
    @checks.each { |check| check.show(outfile) }
end