Class: Adarwin::Preprocessor

Inherits:
Common
  • Object
show all
Defined in:
lib/adarwin/preprocessor.rb

Overview

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

  • Extract the SCoP part from the code (the region of interest)

  • Extract the header code (defines, includes, etc.)

  • Output the original code without pre-processor directives

  • Output the original code minus the SCoP (SCoP to be filled in later)

Constant Summary collapse

WHITESPACE =

Regular expression to identify whitespaces (tabs, spaces).

'\s*'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_code) ⇒ Preprocessor

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.



18
19
20
21
22
23
24
# File 'lib/adarwin/preprocessor.rb', line 18

def initialize(source_code)
  @source_code = source_code
  @header_code = ''
  @parsed_code = ''
  @target_code = ''
  @scop_code = ''
end

Instance Attribute Details

#header_codeObject (readonly)

Returns the value of attribute header_code.



10
11
12
# File 'lib/adarwin/preprocessor.rb', line 10

def header_code
  @header_code
end

#parsed_codeObject (readonly)

Returns the value of attribute parsed_code.



10
11
12
# File 'lib/adarwin/preprocessor.rb', line 10

def parsed_code
  @parsed_code
end

#scop_codeObject (readonly)

Returns the value of attribute scop_code.



10
11
12
# File 'lib/adarwin/preprocessor.rb', line 10

def scop_code
  @scop_code
end

#source_codeObject (readonly)

Returns the value of attribute source_code.



10
11
12
# File 'lib/adarwin/preprocessor.rb', line 10

def source_code
  @source_code
end

#target_codeObject (readonly)

Returns the value of attribute target_code.



10
11
12
# File 'lib/adarwin/preprocessor.rb', line 10

def target_code
  @target_code
end

Instance Method Details

#processObject

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.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/adarwin/preprocessor.rb', line 31

def process
  scop = false
  scop_in_code = false
  
  # Process the file line by line
  @source_code.each_line.with_index do |line,index|
    if line =~ /^#{WHITESPACE}#/
      
      # Keep 'include' statements as header code
      if line =~ /^#{WHITESPACE}#include/
        @header_code += line
        @target_code += line
      
      # Process 'define' statements
      elsif line =~ /^#{WHITESPACE}#define/
        @header_code += line
        @target_code += line
      
      # Found the start of a SCoP
      elsif line =~ /^#{WHITESPACE}#{SCOP_START}/
        scop = true
        scop_in_code = true
        @parsed_code += '{'+NL
        
      # Found the end of a SCoP
      elsif line =~ /^#{WHITESPACE}#{SCOP_END}/
        scop = false
        @parsed_code += '}'+NL
      end
      
    # Nothing special in the code going on here
    else
      @scop_code += line if scop
      @parsed_code += line
      @target_code += line
    end
  end
  
  # Exit if there is no SCoP found
  if !scop_in_code
    raise_error('No "#pragma scop" found in the source code')
  end
end