Skip to content

Instantly share code, notes, and snippets.

@droodman
Forked from rafaqz/fortran-julia.jl
Created July 9, 2023 21:40

Revisions

  1. @rafaqz rafaqz revised this gist Oct 30, 2022. 1 changed file with 23 additions and 0 deletions.
    23 changes: 23 additions & 0 deletions fortran-julia.jl
    Original file line number Diff line number Diff line change
    @@ -123,3 +123,26 @@ println(code)
    stem = split(filename, ".")[1]
    outfile = stem * ".jl"
    write(outfile, code)


    #=
    Copyright (c) 2022 Rafael Schouten
    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:
    The above copyright notice and this permission notice shall be included in all
    copies or substantial portions of the Software.
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    SOFTWARE.
    =#
  2. @rafaqz rafaqz revised this gist Oct 19, 2022. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions fortran-julia.jl
    Original file line number Diff line number Diff line change
    @@ -107,14 +107,14 @@ removal = [

    # Load the file from the first command line argument
    filename = string(ARGS[1])
    code = readstring(filename)
    global code = read(filename, String)

    # Process replacements and removals.
    for (f, r) in replacements
    code = replace(code, f, r)
    global code = replace(code, f, r)
    end
    for r in removal
    code = replace(code, r, "")
    global code = replace(code, r, "")
    end
    println(code)

  3. @rafaqz rafaqz revised this gist Feb 12, 2018. 1 changed file with 12 additions and 9 deletions.
    21 changes: 12 additions & 9 deletions fortran-julia.jl
    Original file line number Diff line number Diff line change
    @@ -35,17 +35,17 @@ replacements = OrderedDict(
    # DO loop to for loop
    r"do (.*),(.*)" => s"for \1:\2",
    # Spaces around math operators
    r"([\*\+\/=])(\S)" => s"\1 \2",
    r"(\S)([\*\+\/=])" => s"\1 \2",
    r"([\*\+\/=])(?=\S)" => s"\1 ",
    r"(?<=\S)([\*\+\/=])" => s" \1",
    # Spaces around - operators, except after e
    r"([^e][\-])(\S)" => s"\1 \2",
    r"(\S[^e])([\-])" => s"\1 \2",
    # r"([^e][\-])(\S)" => s"\1 \2",
    r"(?<!\W\de)(\h*\-\h*)" => s" - ",
    # Space after all commas
    r"(,)(\S)" => s"\1 \2",
    # Replace ELSEIF/ELSE IF with elseif
    r"(\s+)else if" => s"\1elseif",
    # Replace IF followed by ( to if (
    r"(\s+)if\(" => s"\1if (",
    r"(\s+)(elseif|if)\(" => s"\1\2 (",
    # Remove THEN
    r"([)\s])then(\s+)" => s"\1\2",
    # Relace END XXXX with end
    @@ -77,12 +77,14 @@ replacements = OrderedDict(
    r"\s*\.ge\.\s*" => " >= ",
    r"\s*\.gt\.\s*" => " > ",
    r"\s*\.lt\.\s*" => " < ",
    # Add end after single line if with an = assignment
    r"if\s*\((.*)\)(.*? = .*?)(\n)" => s"if \1\2 end\3",
    # Remove (expression) brackets after if
    r"if \((.*)\)(\s*\n)" => s"if \1\2",
    # r"if \((.*)\)(\s*\n)" => s"if \1\2",
    # Add end after single line if with an = assignment
    r"if\s*(.*?) = (.*?)(\n)" => s"if \1 = \2 end\3",
    # Format floats as "5.0" not "5."
    r"(\d)\. " => s"\1.0 ",
    r"(\W\d+)\.(\D)" => s"\1.0\2",
    # Tab to 4 spaces
    r"\t" => " ",
    # Relace suberror with error and mark for fixup
    r"(\W)suberror\((.*?),.*?\)" => s"\1 error(\2)",
    # Mark #FIXME the various things this script can't handle
    @@ -116,6 +118,7 @@ for r in removal
    end
    println(code)


    # Write the output to a .jl file with the same filename stem.
    stem = split(filename, ".")[1]
    outfile = stem * ".jl"
  4. @rafaqz rafaqz created this gist Feb 12, 2018.
    122 changes: 122 additions & 0 deletions fortran-julia.jl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,122 @@
    #=
    This julia script converts fortran 90 code into julia.
    It uses naive regex replacements to do as much as possible,
    but the output WILL need further cleanup.
    Known conversion problems such as GOTO are commented and marked with FIXME
    Most variable declaration lines are entirely deleted, which may or
    may not be useful.
    To run from a shell:
    julia fortran-julia.jl filename.f90
    Output is written to filename.jl.
    =#

    using DataStructures

    # Regex/substitution pairs for replace(). Order matters here.
    replacements = OrderedDict(
    # Lowercase everything not commented
    r"^(?!.*!).*"m => lowercase,
    # Lowercase start of lines with comments
    r"^.*!"m => lowercase,
    # Remove '&' multiline continuations
    r"\s*&\s*" => "",
    # Comments use # not !
    "!" => "#",
    # Powers use ^ not **
    "**" => "^",
    # Only double quotes allowed for strings
    "'" => "\"",
    # DO loop to for loop
    r"do (.*),(.*)" => s"for \1:\2",
    # Spaces around math operators
    r"([\*\+\/=])(\S)" => s"\1 \2",
    r"(\S)([\*\+\/=])" => s"\1 \2",
    # Spaces around - operators, except after e
    r"([^e][\-])(\S)" => s"\1 \2",
    r"(\S[^e])([\-])" => s"\1 \2",
    # Space after all commas
    r"(,)(\S)" => s"\1 \2",
    # Replace ELSEIF/ELSE IF with elseif
    r"(\s+)else if" => s"\1elseif",
    # Replace IF followed by ( to if (
    r"(\s+)if\(" => s"\1if (",
    # Remove THEN
    r"([)\s])then(\s+)" => s"\1\2",
    # Relace END XXXX with end
    r"(\s+)end\h*.*" => s"\1end",
    # Replace expnent function
    r"(\W)exp\(" => s"\1exp(",
    # Reorganise functions and doc strings. This may be very project specific.
    r"#\^\^+\s*subroutine\s*(\w+)([^)]+\))\s*(.*?)#\^\^\^+"sm =>
    Base.SubstitutionString("\"\"\"\n\\3\"\"\"\nfunction \\1\\2::Void"),
    r"\#\^\^+\s*real function\s*(\w+)([^)]+\))\s*(.*?)\#\^\^\^+"sm =>
    Base.SubstitutionString("\"\"\"\n\\3\"\"\"\nfunction \\1\\2::Float64"),
    # Don't need CALL
    r"(\s*)call(\h+)" => s"\1",
    # Use real math symbols
    "gamma" => "Γ",
    "theta" => "Θ",
    "epsilon" => "ϵ",
    "lambda" => "λ",
    "alpha" => "α",
    # Swap logical symbols
    ".true." => "true",
    ".false." => "false",
    r"\s*\.or\.\s*" => " || ",
    r"\s*\.and\.\s*" => " && ",
    r"\s*\.not\.\s*" => " ! ",
    r"\s*\.eq\.\s*" => " == ",
    r"\s*\.ne\.\s*" => " != ",
    r"\s*\.le\.\s*" => " <= ",
    r"\s*\.ge\.\s*" => " >= ",
    r"\s*\.gt\.\s*" => " > ",
    r"\s*\.lt\.\s*" => " < ",
    # Add end after single line if with an = assignment
    r"if\s*\((.*)\)(.*? = .*?)(\n)" => s"if \1\2 end\3",
    # Remove (expression) brackets after if
    r"if \((.*)\)(\s*\n)" => s"if \1\2",
    # Format floats as "5.0" not "5."
    r"(\d)\. " => s"\1.0 ",
    # Relace suberror with error and mark for fixup
    r"(\W)suberror\((.*?),.*?\)" => s"\1 error(\2)",
    # Mark #FIXME the various things this script can't handle
    r"(write|goto|while\s)" => s"#FIXME \1",
    )

    # Patterns to remove
    removal = [
    # Trailing whitespace
    r"\h*$"m,
    # Variable declarations
    r"\n\s*real\s.*",
    r"\n\s*real, external\s.*",
    r"\n\s*integer\s.*",
    r"\n\s*implicit none",
    r"\n\s*logical\s.*",
    # Import statements
    r"\n\s*use\s.*",
    ]

    # Load the file from the first command line argument
    filename = string(ARGS[1])
    code = readstring(filename)

    # Process replacements and removals.
    for (f, r) in replacements
    code = replace(code, f, r)
    end
    for r in removal
    code = replace(code, r, "")
    end
    println(code)

    # Write the output to a .jl file with the same filename stem.
    stem = split(filename, ".")[1]
    outfile = stem * ".jl"
    write(outfile, code)