class Adarwin::Preprocessor

This is the C99 pre-processor for Adarwin. It has the following tasks:

Constants

WHITESPACE

Regular expression to identify whitespaces (tabs, spaces).

Attributes

header_code[R]
parsed_code[R]
scop_code[R]
source_code[R]
target_code[R]

Public Class Methods

new(source_code) click to toggle source

This is the method which initializes the preprocessor. Initialization requires the target source code to process, which is then set as the class variable +@source_code+.

   # File lib/adarwin/preprocessor.rb
18 def initialize(source_code)
19         @source_code = source_code
20         @header_code = ''
21         @parsed_code = ''
22         @target_code = ''
23         @scop_code = []
24 end

Public Instance Methods

process() click to toggle source

This is the method to perform the actual preprocessing. This method takes care of all the pre-processor tasks. The output is stored in the two attributes header_code, and scop. FIXME: What about multi-line statements? For example, a multi-line comment could have a commented-out SCoP or define or include.

   # File lib/adarwin/preprocessor.rb
31 def process
32         scop = false
33         scop_code = ''
34         scop_in_code = false
35         
36         # Process the file line by line
37         @source_code.each_line.with_index do |line,index|
38                 if line =~ /^#{WHITESPACE}#/
39                         
40                         # Keep 'include' statements as header code
41                         if line =~ /^#{WHITESPACE}#include/
42                                 @header_code += line
43                                 @target_code += line
44                         
45                         # Process 'define' statements
46                         elsif line =~ /^#{WHITESPACE}#define/
47                                 @header_code += line
48                                 @target_code += line
49                         
50                         # Found the start of a SCoP
51                         elsif line =~ /^#{WHITESPACE}#{SCOP_START}/
52                                 scop = true
53                                 scop_in_code = true
54                                 @parsed_code += '{'+NL
55                                 
56                         # Found the end of a SCoP
57                         elsif line =~ /^#{WHITESPACE}#{SCOP_END}/
58                                 scop = false
59                                 @scop_code.push(scop_code)
60                                 scop_code = ''
61                                 @parsed_code += '}'+NL
62                         end
63                         
64                 # Nothing special in the code going on here
65                 else
66                         scop_code += line if scop
67                         @parsed_code += line
68                         @target_code += line
69                 end
70         end
71         
72         # Exit if there is no SCoP found
73         if !scop_in_code
74                 raise_error('No "#pragma scop" found in the source code')
75         end
76 end