Module: Ribosome

Defined in:
lib/swift_generator/code_generation/swift_file_template.rb

Overview

The initial part of this file belongs to the ribosome project.

Copyright © 2014 Martin Sustrik All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Defined Under Namespace

Classes: Block

Class Method Summary collapse

Class Method Details

.add(line, bind) ⇒ Object

Adds one . line from the DNA file.



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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/swift_generator/code_generation/swift_file_template.rb', line 197

def Ribosome.add(line, bind)

    # If there is no previous line, add one.
    if(@stack.last.empty?)
        @stack.last << Block.new("")
    end

    # In this block we will accumulate the expanded line.
    block = @stack.last.last

    # Traverse the line and convert it into a block.
    i = 0
    while true
        j = line.index(/[@&][1-9]?\{/, i)
        j = line.size if j == nil

        # Process constant block of text.
        if (i != j)
            block.add_right(Block.new(line[i..j - 1]))
        end

        break if line.size == j

        # Process an embedded expression.
        i = j
        j += 1
        level = 0
        if (line[j] >= ?1 && line[j] <= ?9)
            level = line[j].to_i
            j += 1
        end

        # Find corresponding }.
        par = 0;
        while true
            if(line[j] == ?{)
                par += 1
            elsif(line[j] == ?})
                par -= 1
            end
            break if par == 0
            j += 1
            if j >= line.size
                raise SyntaxError.new("Unmatched {")
            end
        end

        # Expression of higher indirection levels are simply brought
        # down by one level.
        if(level > 0)
            if line [i + 1] == ?1
                block.add_right(Block.new("@" + line[i + 2..j]))
            else
                line[i + 1] = (line [i + 1].to_i - 1).chr
                block.add_right(Block.new(line[i..j]))
            end
            i = j + 1
            next
        end

        # We are at the lowest level of embeddedness so we have to
        # evaluate the embedded expression straight away.
        expr = line[(level == 0 ? i + 2 : i + 3)..j - 1]
        @stack.push([])
        val = eval(expr, bind)
        top = @stack.pop()
        if(top.empty?)
            val = Block.new(val.to_s)
        else
            val = Block.new("")
            for b in top
                val.add_bottom(b)
            end
        end
        val.trim if line[i] == ?@
        block.add_right(val)
        i = j + 1
    end
end

.align(line, bind) ⇒ Object

Adds newline followed by leading whitespace copied from the previous line and one line from the DNA file.



285
286
287
288
289
290
291
292
293
294
# File 'lib/swift_generator/code_generation/swift_file_template.rb', line 285

def Ribosome.align(line, bind)
    if @stack.last.empty?
        n = 0
    else
        n = @stack.last.last.last_offset
    end
    @stack.last << Block.new("")
    add(" " * n, nil)
    add(line, bind)
end

.append(filename) ⇒ Object

Redirects output to the specified file. New stuff is added to the existing content of the file.



169
170
171
172
173
# File 'lib/swift_generator/code_generation/swift_file_template.rb', line 169

def Ribosome.append(filename)
    close()
    @outisafile = true
    @out = File.open(filename, "a")
end

.closeObject

Flush the data to the currently open file and close it.



188
189
190
191
192
193
194
# File 'lib/swift_generator/code_generation/swift_file_template.rb', line 188

def Ribosome.close()
    for b in @stack.last
        b.write(@out, @tabsize)
    end
    @stack = [[]]
    @out.close() if @outisafile
end

.dot(line, bind) ⇒ Object

Adds newline followed by one . line from the DNA file.



278
279
280
281
# File 'lib/swift_generator/code_generation/swift_file_template.rb', line 278

def Ribosome.dot(line, bind)
    @stack.last << Block.new("")
    add(line, bind)
end

.output(filename) ⇒ Object

Redirects output to the specified file.



161
162
163
164
165
# File 'lib/swift_generator/code_generation/swift_file_template.rb', line 161

def Ribosome.output(filename)
    close()
    @outisafile = true
    @out = File.open(filename, "w")
end

.rethrow(e, rnafile, linemap) ⇒ Object

Report an error that happened when executing RNA file.



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/swift_generator/code_generation/swift_file_template.rb', line 297

def Ribosome.rethrow(e, rnafile, linemap)
    i = 0
    for i in 0..e.backtrace.size - 1
        l = e.backtrace[i]
        if l.start_with?(rnafile + ":")
            stop = l.index(":", rnafile.size + 1) || l.size
            num = l[rnafile.size + 1..stop - 1].to_i
            for j in 0..linemap.size - 1
                break if linemap[j][0] == nil || linemap[j][0] > num
            end
            j -= 1
            num = num - linemap[j][0] + linemap[j][2]
            l = "#{linemap[j][1]}:#{num}#{l[stop..-1]}"
            e.backtrace[i] = l
        end
    end
    raise e
end

.stdoutObject

Redirects output to the stdout.



176
177
178
179
180
# File 'lib/swift_generator/code_generation/swift_file_template.rb', line 176

def Ribosome.stdout()
    close()
    @outisafile = false
    @out = $stdout
end

.tabsize(size) ⇒ Object

Sets the size of the tab.



183
184
185
# File 'lib/swift_generator/code_generation/swift_file_template.rb', line 183

def Ribosome.tabsize(size)
    @tabsize = size
end