Guest User

Untitled

a guest
Feb 24th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. #=
  2. This julia script converts fortran 90 code into julia.
  3. It uses naive regex replacements to do as much as possible,
  4. but the output WILL need further cleanup.
  5.  
  6. Known conversion problems such as GOTO are commented and marked with FIXME
  7.  
  8. Most variable declaration lines are entirely deleted, which may or
  9. may not be useful.
  10.  
  11.  
  12. To run from a shell:
  13.  
  14. julia fortran-julia.jl filename.f90
  15.  
  16. Output is written to filename.jl.
  17. =#
  18.  
  19. using DataStructures
  20.  
  21. # Regex/substitution pairs for replace(). Order matters here.
  22. replacements = OrderedDict(
  23. # Lowercase everything not commented
  24. r"^(?!.*!).*"m => lowercase,
  25. # Lowercase start of lines with comments
  26. r"^.*!"m => lowercase,
  27. # Remove '&' multiline continuations
  28. r"\s*&\s*" => "",
  29. # Comments use # not !
  30. "!" => "#",
  31. # Powers use ^ not **
  32. "**" => "^",
  33. # Only double quotes allowed for strings
  34. "'" => "\"",
  35. # DO loop to for loop
  36. r"do (.*),(.*)" => s"for \1:\2",
  37. # Spaces around math operators
  38. r"([\*\+\/=])(?=\S)" => s"\1 ",
  39. r"(?<=\S)([\*\+\/=])" => s" \1",
  40. # Spaces around - operators, except after e
  41. # r"([^e][\-])(\S)" => s"\1 \2",
  42. r"(?<!\W\de)(\h*\-\h*)" => s" - ",
  43. # Space after all commas
  44. r"(,)(\S)" => s"\1 \2",
  45. # Replace ELSEIF/ELSE IF with elseif
  46. r"(\s+)else if" => s"\1elseif",
  47. # Replace IF followed by ( to if (
  48. r"(\s+)(elseif|if)\(" => s"\1\2 (",
  49. # Remove THEN
  50. r"([)\s])then(\s+)" => s"\1\2",
  51. # Relace END XXXX with end
  52. r"(\s+)end\h*.*" => s"\1end",
  53. # Replace expnent function
  54. r"(\W)exp\(" => s"\1exp(",
  55. # Reorganise functions and doc strings. This may be very project specific.
  56. r"#\^\^+\s*subroutine\s*(\w+)([^)]+\))\s*(.*?)#\^\^\^+"sm =>
  57. Base.SubstitutionString("\"\"\"\n\\3\"\"\"\nfunction \\1\\2::Void"),
  58. r"\#\^\^+\s*real function\s*(\w+)([^)]+\))\s*(.*?)\#\^\^\^+"sm =>
  59. Base.SubstitutionString("\"\"\"\n\\3\"\"\"\nfunction \\1\\2::Float64"),
  60. # Don't need CALL
  61. r"(\s*)call(\h+)" => s"\1",
  62. # Use real math symbols
  63. "gamma" => "Γ",
  64. "theta" => "Θ",
  65. "epsilon" => "ϵ",
  66. "lambda" => "λ",
  67. "alpha" => "α",
  68. # Swap logical symbols
  69. ".true." => "true",
  70. ".false." => "false",
  71. r"\s*\.or\.\s*" => " || ",
  72. r"\s*\.and\.\s*" => " && ",
  73. r"\s*\.not\.\s*" => " ! ",
  74. r"\s*\.eq\.\s*" => " == ",
  75. r"\s*\.ne\.\s*" => " != ",
  76. r"\s*\.le\.\s*" => " <= ",
  77. r"\s*\.ge\.\s*" => " >= ",
  78. r"\s*\.gt\.\s*" => " > ",
  79. r"\s*\.lt\.\s*" => " < ",
  80. # Remove (expression) brackets after if
  81. # r"if \((.*)\)(\s*\n)" => s"if \1\2",
  82. # Add end after single line if with an = assignment
  83. r"if\s*(.*?) = (.*?)(\n)" => s"if \1 = \2 end\3",
  84. # Format floats as "5.0" not "5."
  85. r"(\W\d+)\.(\D)" => s"\1.0\2",
  86. # Tab to 4 spaces
  87. r"\t" => " ",
  88. # Relace suberror with error and mark for fixup
  89. r"(\W)suberror\((.*?),.*?\)" => s"\1 error(\2)",
  90. # Mark #FIXME the various things this script can't handle
  91. r"(write|goto|while\s)" => s"#FIXME \1",
  92. )
  93.  
  94. # Patterns to remove
  95. removal = [
  96. # Trailing whitespace
  97. r"\h*$"m,
  98. # Variable declarations
  99. r"\n\s*real\s.*",
  100. r"\n\s*real, external\s.*",
  101. r"\n\s*integer\s.*",
  102. r"\n\s*implicit none",
  103. r"\n\s*logical\s.*",
  104. # Import statements
  105. r"\n\s*use\s.*",
  106. ]
  107.  
  108. # Load the file from the first command line argument
  109. filename = string(ARGS[1])
  110. code = readstring(filename)
  111.  
  112. # Process replacements and removals.
  113. for (f, r) in replacements
  114. code = replace(code, f, r)
  115. end
  116. for r in removal
  117. code = replace(code, r, "")
  118. end
  119. println(code)
  120.  
  121.  
  122. # Write the output to a .jl file with the same filename stem.
  123. stem = split(filename, ".")[1]
  124. outfile = stem * ".jl"
  125. write(outfile, code)
Add Comment
Please, Sign In to add comment