项目作者: hjaremko

项目描述 :
Simple Fortran interpreter
高级语言: Haskell
项目地址: git://github.com/hjaremko/fi.git
创建时间: 2021-02-05T10:43:32Z
项目社区:https://github.com/hjaremko/fi

开源协议:

下载


Simple Fortran interpreter - Functional programming 2020/2021

This project implements some of the constructs from historical FORTRAN, written purely in Haskell.
Parser combinators inspired by this paper.

Compilation and usage

  1. $ git clone https://github.com/hjaremko/fi.git && cd fi
  2. $ ghc --make -o fi src/Main.hs -isrc
  3. $ ./fi <filename>

Supported statements

Assumptions

  • Every variable is typed float.
  • Variables are initialized on the first use with zero.
  • Whitespaces should be ignored.

Output

  1. WRITE "Hello world! ", (2*4 + 2)

It is possible to print multiple expressions separeted by comma.

Input

  1. READ A

Reads a float value from standard input.

Labels

Statements can be labeled with integer values. Labels should be unique.

Arithmetic IFs

  1. IF (102 .EQ. 120) 40, 50, 60
  2. 40 WRITE "Negative"
  3. 50 WRITE "False"
  4. 60 WRITE "True"

Three-way arithmetic conditional statement. Jumps to three different branches depending on whether the result of an expression is negative, zero, or positive.

DO loops

  1. DO (label) I=(init value), (stop value) [, (step value)]
  2. WRITE I
  3. (label) CONTINUE

Simple loop, correct labels are required. Step value can be omitted (default is 1).

Assignments

  1. I = 123
  2. I = I * (200 + 10)

Jumps

  1. GOTO 13
  2. 12 WRITE "This will be omitted"
  3. 13 WRITE "Hello world"

Arithmetic and logic expressions

Supported arithmetic operators: +, -, *, /, unary -, SQRT and (, ).
Supported logic operators: .EQ. (equals), .NE. (not equals), .LT. (less than), .LE. (less equals), .GT. (greater than), .GE. (greater equals).

True is indicated by value 1.0 and false by 0.0.

Example code

  1. WRITE "Square root calculator"
  2. DO 300 N=0,2
  3. WRITE "Calculation no. ", N
  4. READ A
  5. READ B
  6. READ C
  7. D=B*B-4*A*C
  8. WRITE "Delta: ", D
  9. IF (D) 101,102,103
  10. 101 WRITE "No roots"
  11. GOTO 200
  12. 102 X=2
  13. WRITE "Root: ",X
  14. GOTO 200
  15. 103 X1=(-B-SQRT(D))/(2*A)
  16. X2=(-B+SQRT(D))/(2*A)
  17. WRITE "Root no. 1: ",X1
  18. WRITE "Root no. 2: ",X2
  19. 200 WRITE "End of calculation no. ", N
  20. 300 CONTINUE
  21. DO 600 N=0, 30, 10
  22. WRITE "N: ", N
  23. DO 700 K=0, 2
  24. WRITE "Hello from inner loop"
  25. 700 CONTINUE
  26. 600 CONTINUE
  27. WRITE "Hello World!"
  28. IF (102.EQ.120) 40, 50, 60
  29. 40 WRITE "Negative"
  30. 50 WRITE "False"
  31. 60 WRITE "True"
  32. WRITE 10-1 .EQ. -9
  33. WRITE (1 + 1)+1.EQ.3
  34. Expr = 1 + 5 * 2 .EQ. 12 - 1
  35. WRITE "Expr: ", Expr
  36. WRITE "-50: ", -50
  37. WRITE Expr, " <= -50: ", Expr .LE. -50
  38. WRITE Expr, " > -50: ", Expr .GT. -50
  39. IF (Expr .LE. -50) 11, 11, 12
  40. 11 WRITE "False"
  41. GOTO 13
  42. 12 WRITE "True"
  43. 13 WRITE "End"