Class: Frepl::FortranFile

Inherits:
Object
  • Object
show all
Defined in:
lib/frepl/fortran_file.rb

Constant Summary collapse

BEGIN_PROGRAM_STATEMENT =
"program frepl_out\n"
END_PROGRAM_STATEMENT =
"end program frepl_out\n"
FUNC_SUBROUTINE_HEADER =
"  contains\n"
IMPLICIT_STATEMENT =
"  implicit none\n"

Instance Method Summary collapse

Constructor Details

#initializeFortranFile

Returns a new instance of FortranFile.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/frepl/fortran_file.rb', line 10

def initialize
  @all_statements = []
  @declarations = []
  @derived_types = []
  @assignments = []
  @execution = nil
  @allocations = []
  @subroutines = []
  @functions = []
  @wheres = []
end

Instance Method Details

#add(line_obj) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/frepl/fortran_file.rb', line 65

def add(line_obj)
  line_obj.accept(self)
  @all_statements << line_obj
  Frepl.log("added")
  Frepl.log("declarations: #{@declarations}")
  Frepl.log("assignments: #{@assignments}")
end

#runObject



22
23
24
25
26
27
28
29
30
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
# File 'lib/frepl/fortran_file.rb', line 22

def run
  File.open('frepl_out.f90', 'w+') do |f|
    f << BEGIN_PROGRAM_STATEMENT
    f << IMPLICIT_STATEMENT

    @derived_types.each do |dt|
      f.write(dt.output)
    end

    @declarations.each do |d|
      f.write(d.output)
    end

    @allocations.each do |a|
      f.write(a.output)
    end

    @assignments.each do |a|
      f.write(a.output)
    end

    if @execution
      f.write(@execution.output)
    end

    if @subroutines.any? || @functions.any?
      f << FUNC_SUBROUTINE_HEADER

      @subroutines.each do |sub|
        f.write(sub.output)
      end

      @functions.each do |fn|
        f.write(fn.output)
      end
    end

    f << END_PROGRAM_STATEMENT
  end
  o = `#{Frepl.compiler} frepl_out.f90 -o frepl_out && ./frepl_out`
  Frepl.output(o)
end

#undo_last!Object



73
74
75
76
77
# File 'lib/frepl/fortran_file.rb', line 73

def undo_last!
  last_statement = @all_statements.last
  ivar = instance_variable_get("@#{last_statement.class.to_s.demodulize.underscore.pluralize}")
  ivar.pop
end

#visit_allocation(a) ⇒ Object



108
109
110
# File 'lib/frepl/fortran_file.rb', line 108

def visit_allocation(a)
  @allocations << a
end

#visit_assignment(a) ⇒ Object



96
97
98
99
100
101
# File 'lib/frepl/fortran_file.rb', line 96

def visit_assignment(a)
  @assignments << a
  e = Execution.new(a.expressionize)
  visit_execution(e)
  Frepl.log("assignment name: #{a.variable_name}")
end

#visit_declaration(declaration) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/frepl/fortran_file.rb', line 79

def visit_declaration(declaration)
  if i = @declarations.find_index { |d| d == declaration }
    @declarations[i] = declaration
    if j = @assignments.find_index { |a| a.variable_name == declaration.variable_name }
      @assignments.slice!(j)
    end
  else
    @declarations << declaration
  end
end

#visit_derived_type(dt) ⇒ Object



151
152
153
154
155
156
157
# File 'lib/frepl/fortran_file.rb', line 151

def visit_derived_type(dt)
  if i = @derived_types.find_index { |v| v == dt }
    @derived_types[i] = dt
  else
    @derived_types << dt
  end
end

#visit_do_loop(d) ⇒ Object



142
143
144
145
# File 'lib/frepl/fortran_file.rb', line 142

def visit_do_loop(d)
  e = Execution.new(d.output)
  visit_execution(e)
end

#visit_execution(e) ⇒ Object



112
113
114
115
# File 'lib/frepl/fortran_file.rb', line 112

def visit_execution(e)
  @execution = e
  run
end

#visit_function(fn) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/frepl/fortran_file.rb', line 121

def visit_function(fn)
  if i = @functions.find_index { |f| f == fn }
    @functions[i] = fn
  else
    @functions << fn
  end
end

#visit_ifstatement(i) ⇒ Object



137
138
139
140
# File 'lib/frepl/fortran_file.rb', line 137

def visit_ifstatement(i)
  e = Execution.new(i.output)
  visit_execution(e)
end

#visit_multi_declaration(multi_declaration) ⇒ Object



90
91
92
93
94
# File 'lib/frepl/fortran_file.rb', line 90

def visit_multi_declaration(multi_declaration)
  multi_declaration.declarations.each do |d|
    visit_declaration(d)
  end
end

#visit_repl_command(cmd) ⇒ Object



117
118
119
# File 'lib/frepl/fortran_file.rb', line 117

def visit_repl_command(cmd)
  cmd.run(self)
end

#visit_standalone_variable(sv) ⇒ Object



103
104
105
106
# File 'lib/frepl/fortran_file.rb', line 103

def visit_standalone_variable(sv)
  e = Execution.new(sv.expressionize)
  visit_execution(e)
end

#visit_subroutine(sub) ⇒ Object



129
130
131
132
133
134
135
# File 'lib/frepl/fortran_file.rb', line 129

def visit_subroutine(sub)
  if i = @subroutines.find_index { |s| s == sub }
    @subroutines[i] = sub
  else
    @subroutines << sub
  end
end

#visit_where(w) ⇒ Object



147
148
149
# File 'lib/frepl/fortran_file.rb', line 147

def visit_where(w)
  @assignments << w
end