Genie Algol68g
Implementation of programming language Algol68Wikipedia: ALGOL 68G or Algol 68 Genie is an ALGOL 68 interpreter. ALGOL 68G is a nearly full implementation of ALGOL 68 as defined by the Revised Report and also implements partial parametrisation, which is an extension of ALGOL 68.
After successful parsing of an entire source program, the syntax tree, that serves as an intermediate program representation, is interpreted. The interpreter performs many runtime checks.
Examples:
Hello, World!:
Example for versions Algol68g-1.18.0Note the use of printf with the formatting being described between dollars. eg $gl$ — meaning “g”eneral pattern, then new “l”ine.
(
printf(($gl$,"Hello, world!"))
)
Factorial:
Example for versions Algol68g-1.18.0, Algol68toc-1.8This example uses recursive factorial definition with first 7 numbers precomputed.
PROC factorial = (INT n)LONG LONG INT:
CASE n+1 IN
1,1,2,6,24,120,720 # a brief lookup #
OUT
n*factorial(n-1)
ESAC
;
Quadratic equation:
Example for versions Algol68g-1.18.0A direct translation of the C++ example.
PROC print root = (INT ind, LONG COMPL x)VOID:
( print(("x", ind, " := "));
IF ABS(im OF x) < 1E-6 THEN
print((re OF x, new line))
ELSE print((x, new line))
FI
);
main:
( LONG COMPL a, b, c, d, x1, x2;
print(("a := "));
read((a));
IF ABS a <1E-3 THEN
print(( "Not a quadratic equation", new line));
stop
FI;
print(("b := "));
read((b));
print(("c := "));
read((c));
a *:= 2;
d := b*b-a*c*2.0;
IF ABS d <1E-3 THEN
print(("x := ", re OF (-b/a)))
ELSE
print root(1, (-b+long complex sqrt(d))/a);
print root(2, (-b-long complex sqrt(d))/a)
FI
)
Fibonacci numbers:
Example for versions Algol68g-1.18.0, Algol68toc-1.8Generative method. This code specimen uses a callback to generate the sequence on demand.
MODE YIELDINT = PROC(INT)VOID;
PROC fibonacci = (INT n, YIELDINT yield)VOID: (
INT even:=0, odd:=1;
yield(even);
yield(odd);
FOR i FROM odd+1 TO n DO
yield( (ODD i|odd|even) := odd + even )
OD
);
main:(
# FOR INT n IN # fibonacci(16, # ) DO ( #
## (INT n)VOID:(
print((" ",whole(n,0)))
)); # OD #
print(new line)
)
Fibonacci numbers:
Example for versions Algol68g-1.18.0, Algol68toc-1.8Analytic method
PROC analytic fibonacci = (INT n)INT:(
REAL sqrt 5 = sqrt(5);
REAL p = (1 + sqrt 5) / 2;
REAL q = 1/p;
ENTIER( (p**n + q**n) / sqrt 5 )
);
FOR i FROM 0 TO 16 WHILE
print(whole(analytic fibonacci(i),0));
# WHILE # i /= 16 DO
print(", ")
OD;
print(new line)
Comments
]]>blog comments powered by Disqus
]]>