class Frepl::FortranFile

Constants

BEGIN_PROGRAM_STATEMENT
END_PROGRAM_STATEMENT
FUNC_SUBROUTINE_HEADER
IMPLICIT_STATEMENT

Public Class Methods

new() click to toggle source
# File lib/frepl/fortran_file.rb, line 10
def initialize
  @all_statements = []
  @declarations = []
  @derived_types = []
  @assignments = []
  @execution = nil
  @allocations = []
  @subroutines = []
  @functions = []
  @wheres = []
end

Public Instance Methods

add(line_obj) click to toggle source
# 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
run() click to toggle source
# 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!() click to toggle source
# 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) click to toggle source
# File lib/frepl/fortran_file.rb, line 108
def visit_allocation(a)
  @allocations << a
end
visit_assignment(a) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/frepl/fortran_file.rb, line 112
def visit_execution(e)
  @execution = e
  run
end
visit_function(fn) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/frepl/fortran_file.rb, line 117
def visit_repl_command(cmd)
  cmd.run(self)
end
visit_standalone_variable(sv) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/frepl/fortran_file.rb, line 147
def visit_where(w)
  @assignments << w
end