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_name+ from directory +dir+ with generic parameters +params+.

NOTE: +top_file+ can either be a file or a file name.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/HDLRuby/hdrcc.rb', line 58

def initialize(top_system,top_file,dir,*params)
    # Sets the top and the looking directory.
    @top_system = top_system.to_s
    # @top_file can either be a file or a string giving the file name.
    if top_file.respond_to?(:path) then
        @top_file = top_file
        @top_file_name = top_file.path
    else
        @top_file = nil
        @top_file_name = top_file.to_s
    end
    @dir = dir.to_s
    @params = params

    # The list of the standard library files to exclude for
    # checking.
    # Get the directory of the HDLRuby and Ruby standard libraries.
    @std_dirs = $LOAD_PATH
    # @std_dirs << File.dirname(__FILE__) + "/std"
    # # Gather the files with their path to std.
    # @std_files = Dir[@std_dir + "/*"]

    # 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.



52
53
54
# File 'lib/HDLRuby/hdrcc.rb', line 52

def requires
  @requires
end

#top_instanceObject (readonly)

The top instance, only accessible after parsing the files.



49
50
51
# File 'lib/HDLRuby/hdrcc.rb', line 49

def top_instance
  @top_instance
end

Instance Method Details

#check_allObject

Checks the read files.



155
156
157
# File 'lib/HDLRuby/hdrcc.rb', line 155

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

#get_topObject

Gets the (first) top system.



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/HDLRuby/hdrcc.rb', line 166

def get_top
    # Get all the systems.
    systems = @checks.reduce([]) {|ar,check| ar + check.get_all_systems}
    # show? "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
    # show? "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.



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/HDLRuby/hdrcc.rb', line 193

def parse
    # Is there a top system specified yet?
    if @top_system == "" then
        # No, look for it.
        @top_system = get_top
        # show? "@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_name,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_name,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+.

NOTE: +file+ can either be a file or a file name.



97
98
99
100
101
102
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 97

def read(file)
    # Resolve the file.
    if file.respond_to?(:read) then
        found = file
    else
        found = File.join(@dir,file)
        unless File.exist?(found) then
            founds = Dir.glob(@std_dirs.map do |path|
                File.join(path,file) 
            end)
            if founds.empty? then
                # No standard file with this name, this is an error.
                raise "Unknown required file: #{file}."
            else
                # A standard file is found, skip it since it does not
                # need to be read.
                # show? "Standard files: #{founds}"
                return false
            end
        end
    end
    # Load the file.
    @texts << File.read(found)
    if found.respond_to?(:path) then
        @checks << Checker.new(@texts[-1],found.path)
    else
        @checks << Checker.new(@texts[-1])
    end
    return true
end

#read_all(file = nil) ⇒ Object

Loads all the files from +file+.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/HDLRuby/hdrcc.rb', line 129

def read_all(file = nil)
    unless file then
        if @top_file then
            file = @top_file
        else
            file = @top_file_name
        end
    end
    # show? "read_all with file=#{file}"
    # Read the file
    # read(file)
    unless read(file) then
        # The file is to skip.
        return
    end
    # Get its required files.
    requires = @checks[-1].get_all_requires +
               @checks[-1].get_all_require_relatives
    requires.each do |file|
        read_all(file)
    end
    @requires += requires
    @requires.uniq!
end

#show_all(outfile = $stdout) ⇒ Object

Displays the syntax tree of all the files.



160
161
162
163
# File 'lib/HDLRuby/hdrcc.rb', line 160

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