How to resolve the algorithm Extend your language step by step in the Fortran programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Extend your language step by step in the Fortran programming language

Table of Contents

Problem Statement

Some programming languages allow you to extend the language. While this can be done to a certain degree in most languages (e.g. by using macros), other languages go much further. Most notably in the Forth and Lisp families, programming per se is done by extending the language without any formal distinction between built-in and user-defined elements. If your language supports it, show how to introduce a new flow control mechanism. A practical and useful example is a four-way branch: Occasionally, code must be written that depends on two conditions, resulting in up to four branches (depending on whether both, only the first, only the second, or none of the conditions are "true"). In a C-like language this could look like the following: Besides being rather cluttered, the statement(s) for 'condition2isTrue' must be written down twice. If 'condition2isTrue' were a lengthy and involved expression, it would be quite unreadable, and the code generated by the compiler might be unnecessarily large. This can be improved by introducing a new keyword if2. It is similar to if, but takes two conditional statements instead of one, and up to three 'else' statements. One proposal (in pseudo-C syntax) might be: Pick the syntax which suits your language. The keywords 'else1' and 'else2' are just examples. The new conditional expression should look, nest and behave analogously to the language's built-in 'if' statement.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Extend your language step by step in the Fortran programming language

Source code in the fortran programming language

      LOGICAL A,B		!These are allocated the same storage
      INTEGER IA,IB		!As the default integer size.
      EQUIVALENCE (IA,A),(IB,B)	!So, this will cause no overlaps.

      WRITE (6,*) "Boolean tests via integers..."
      DO 199 IA = 0,1	!Two states for A.
        DO 199 IB = 0,1		!Two states for B.
          IF (IA) 666,99,109		!Not four ways, just three.
   99     IF (IB) 666,100,101		!Negative values are surely wrong.
  100     WRITE (6,*) "FF",IA,IB
          GO TO 199
  101     WRITE (6,*) "FT",IA,IB
          GO TO 199
  109     IF (IB) 666,110,111		!A second test.
  110     WRITE (6,*) "TF",IA,IB
          GO TO 199
  111     WRITE (6,*) "TT",IA,IB
  199 CONTINUE		!Both loops finish here.

      WRITE (6,*) "Boolean tests via integers and computed GO TO..."
      DO 299 IA = 0,1	!Two states for A.
        DO 299 IB = 0,1		!Two states for B.
          GO TO (200,201,210,211) 1 + IA*2 + IB	!Counting starts with one.
  200     WRITE (6,*) "FF",IA,IB
          GO TO 299
  201     WRITE (6,*) "FT",IA,IB
          GO TO 299
  210     WRITE (6,*) "TF",IA,IB
          GO TO 299
  211     WRITE (6,*) "TT",IA,IB
  299 CONTINUE		!Both loops finish here.

  300 WRITE (6,301)
  301 FORMAT (/,"Boolean tests via LOGICAL variables...",/
     1 " AB    IA    IB (IA*2 + IB)")
      A = .TRUE.	!Syncopation.
      B = .TRUE.	!Via the .NOT., the first pair will be FF.
      DO I = 0,1	!Step through two states.
        A = .NOT.A		!Thus generate F then T.
        DO J = 0,1		!Step through the second two states.
          B = .NOT.B			!Thus generate FF, FT, TF, TT.
          WRITE (6,302) A,B,IA,IB,IA*2 + IB	!But with strange values.
  302     FORMAT (1X,2L1,2I6,I8)		!Show both types.
        END DO			!Next value for B.
      END DO		!Next value for A.
      GO TO 999

  666 WRITE (6,*) "Huh?"

  999 CONTINUE
      END


      INTEGER FUNCTION IF2(A,B)	!Combine two LOGICAL variables.
       LOGICAL A,B		!These.
        IF2 = 0			!Wasted effort if A is true.
        IF (A) IF2 = 2		!But it avoids IF ... THEN ... ELSE ... END IF blather.
        IF (B) IF2 = IF2 + 1	!This relies on IF2 being a variable. (Standard in F90+)
      END FUNCTION IF2		!Thus produce a four-way result.


      SELECT CASE(IF2(A,B))
       CASE(B"00"); WRITE (6,*) "Both false."
       CASE(B"01"); WRITE (6,*) "B only."
       CASE(B"10"); WRITE (6,*) "A only."
       CASE(B"11"); WRITE (6,*) "Both true."
      END SELECT


  

You may also check:How to resolve the algorithm Multisplit step by step in the C programming language
You may also check:How to resolve the algorithm Comma quibbling step by step in the Lasso programming language
You may also check:How to resolve the algorithm Constrained random points on a circle step by step in the Phix programming language
You may also check:How to resolve the algorithm String concatenation step by step in the Groovy programming language
You may also check:How to resolve the algorithm Knuth shuffle step by step in the Modula-3 programming language