<script setup lang="ts">

</script>

<template>
  <div>
    <h1 class="text-3xl">Hello World In Fortran 90</h1>
    <hr>
    <p></p>

    <div>
      <pre><code class="text-sm">
program hello
  implicit none
  print *, 'Hello world!'
end program hello
      </code></pre>
    </div>

    <p>In the good ol' days (before FORTRAN 77), when printing to console, you would write <span><code>write(*,*)</code></span>,
      but with the release of FORTRAN 77, that became redundant, as you could use the newest keyword, <span><code>print</code></span>.</p>
    <br>

    <p>With modern compilers, running the newest Fortran standards, <span><code>write(*,*) 'Hello world!'</code> and <code>print *, 'Hello world!'</code></span>
      will compile to the exact same assembly. So if you only need to print something to stdout, just use <span><code>print *</code></span>
      as it conveys the meaning of the code better. Plus we don't need the full functionality of <span><code>write(*,*)</code></span>
      in this example.</p>
    <br>

    <div>
      <pre><code class="text-sm">
$ gfortran -o hello main.f90
$ ./hello
 Hello world!
      </code></pre>
    </div>

    <p>Now, hear me out, why not spice up the program a little bit more? Maybe we want to print to stdout, or what if
      we suddenly need to print an error message, and want that to be handled correctly by the terminal emulator?</p>
    <br>
    <p>Well, fret not my friend, because I have just the solution for you then. All we gotta do is to read from the
      terminal, and then print out the value.</p>
    <br>

    <div>
      <pre><code class="text-sm">
program hello
  implicit none

  character(1000) :: usertxt
  integer :: ios

  print *, 'Input text here: '

  read(*,"(A)", iostat=ios) usertxt
  if(is_iostat_end(ios)) stop

  print *, trim(usertxt)

end program hello
      </code></pre>
    </div>

    <p>So what we're seeing here, is that we have created two variables: <span><code>usertxt</code> and <code>ios</code></span>.
      <span><code>usertxt</code></span> is the input we're reading from the terminal. Although since Fortran 90 doesn't
      have an explicit string type, doesn't mean we can't read or write text, as the <span><code>character</code></span>
      type is just an array of 1000 characters long. It doesn't have to be 1000 characters, but it's what I chose on a
      whim.</p>
    <br>

    <p>The other variable, <span><code>ios</code></span>, I probably don't need to introduce, as it's just an integer.
      But what control-flow this value holds, I feel is quite important, as a lot of file-based business logic makes
      use of it.</p>
    <br>

    <p>Introducing, <span><code>iostat</code></span>. AKA, Input/Output Status Specifier. It indicates the status of the
      I/O operation. If the integer is positive, it indicates an error code. If it's negative, it indicates an end of file
      condition. If it's zero, then it does not indicate anything yet. No error, no End Of File, no particular condition
      has triggered yet.</p>
    <br>

    <p>So in this case, where we check if <span><code>ios</code></span> is End Of File, then that means if we input an
      End Of File key combination (Ctrl+D on Unix/Linux, and Ctrl+Z on Windows), then the program would stop immediately.
      We use the <span><code>trim()</code></span> function to remove any trailing whitespaces. That's because, if you
      make a character bigger than the actual text, it will be fille with whitespace after the text. So now the output
      looks like this:</p>
    <br>

    <div>
      <pre><code class="text-sm">
$ gfortran -o hello main.f90
$ ./hello
 Input text here:
Hello world :D
 Hello world :D
      </code></pre>
    </div>
  </div>
</template>