Skip to content

C code looks so much like Fortran you can feel it.

July 6, 2012

Here is some C code. It looks like Fortran, but it is C.

/*  -*- fortran -*-   */
#include <stdio.h>
#include "fort.h"
C /* This will look like Fortran code!! */

C      line(l000)
       if(N EQ 0) THEN
 l000=     FORMAT(" Cannot find the reciprocal of 0\n");
           WRITE(stdout, l000)                         ;
           CALL EXIT                                   

       return(1/N)                ;

C        line(l100)  line(l200) line(l300)
       INTEGER I, J                 ;
       REAL    N                    ;
       DO (I=1;I LE 5; I=I+1) {
 l100=     FORMAT(" I is %i\n")     ;
           WRITE(stdout, l100, I)   ;
 l200= FORMAT(" Loop finished\n");
       if (I EQ 6) THEN
           WRITE(stdout, l200);
 l300= FORMAT(" Reciprocal of %f is %f\n")   ;
       WRITE(stdout, l300, 5., RECIPROCAL(5.))  ;
       CALL EXIT

Here is the header file that makes it all possible

/* required include files:
#include <stdio.h>

#define LT <
#define GT >
#define LE <=
#define GE >=
#define EQ ==
#define NE !=
#define NOT !
#define AND &&
#define OR  ||
#define WRITE fprintf
#define READ  fscanf
#define CONTINUE /* */
#define EQUIVALENCE union
#define PROGRAM int main(int argc, char * argv[]) {
#define SUBROUTINE void
#define FUNCTION /* */
#define CALL (void)
#define INTEGER int
#define CHARACTER char
#define REAL double
#define DO for
#define ENDDO }
#define FORMAT /* */
#define EXIT exit(0);
#define EXIT1(a) exit((a));
#define END }
#define THEN {
#define ENDIF }
#define C /* */
#define line(a) char * a;

/* to use:
   Include this file
   Put /* -*- fortran -*- * / at the top so emacs will use fortran-mode.
   put line() declarations at the top for every format. 
   Use "thousand series" line numbers by using a lower case l (el)
   FORMAT statements are really printf format strings and must appear before the corresponding WRITE.
   WRITE statements must include the variables in the parenthases
   DO statements are really C for(;;) statements, and the DO line must terminate with "{"
   logical operators cannot be surrounded by periods
   Lines must end in ";", except line(), PROGRAM, END..., and EXIT.
   CHARACTER operations cannot be overloaded on "=" (strcpy function)
   see fort.c for an example.

/* To do:
   need to add lexicals

From → Technology

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: