mirror of
https://github.com/yhirose/cpp-peglib.git
synced 2024-12-22 11:55:30 +00:00
Added samples for PL/0.
This commit is contained in:
parent
2b2a4f3816
commit
5d6ffe8d89
30
language/pl0/samples/fib.pas
Normal file
30
language/pl0/samples/fib.pas
Normal file
@ -0,0 +1,30 @@
|
||||
VAR i, x, r;
|
||||
|
||||
PROCEDURE fib;
|
||||
VAR xx, r1, r2;
|
||||
BEGIN
|
||||
xx := x;
|
||||
IF xx = 0 THEN r := 1;
|
||||
IF xx = 1 THEN r := 1;
|
||||
IF xx >= 2 THEN BEGIN
|
||||
x := xx - 2;
|
||||
CALL fib;
|
||||
r1 := r;
|
||||
|
||||
x := xx - 1;
|
||||
CALL fib;
|
||||
r2 := r;
|
||||
r := r1 + r2;
|
||||
END
|
||||
END;
|
||||
|
||||
BEGIN
|
||||
i := 0;
|
||||
WHILE i < 30 DO BEGIN
|
||||
x := i;
|
||||
CALL fib;
|
||||
write i;
|
||||
write r;
|
||||
i := i + 1;
|
||||
END
|
||||
END.
|
61
language/pl0/samples/gcd.pas
Normal file
61
language/pl0/samples/gcd.pas
Normal file
@ -0,0 +1,61 @@
|
||||
CONST
|
||||
m = 7,
|
||||
n = 85;
|
||||
|
||||
VAR
|
||||
x, y, z, q, r;
|
||||
|
||||
PROCEDURE multiply;
|
||||
VAR a, b;
|
||||
BEGIN
|
||||
a := x;
|
||||
b := y;
|
||||
z := 0;
|
||||
WHILE b > 0 DO BEGIN
|
||||
IF ODD b THEN z := z + a;
|
||||
a := 2 * a;
|
||||
b := b / 2;
|
||||
END
|
||||
END;
|
||||
|
||||
PROCEDURE divide;
|
||||
VAR w;
|
||||
BEGIN
|
||||
r := x;
|
||||
q := 0;
|
||||
w := y;
|
||||
WHILE w <= r DO w := 2 * w;
|
||||
WHILE w > y DO BEGIN
|
||||
q := 2 * q;
|
||||
w := w / 2;
|
||||
IF w <= r THEN BEGIN
|
||||
r := r - w;
|
||||
q := q + 1
|
||||
END
|
||||
END
|
||||
END;
|
||||
|
||||
PROCEDURE gcd;
|
||||
VAR f, g;
|
||||
BEGIN
|
||||
f := x;
|
||||
g := y;
|
||||
WHILE f # g DO BEGIN
|
||||
IF f < g THEN g := g - f;
|
||||
IF g < f THEN f := f - g;
|
||||
END;
|
||||
z := f
|
||||
END;
|
||||
|
||||
BEGIN
|
||||
x := m;
|
||||
y := n;
|
||||
CALL multiply;
|
||||
x := 25;
|
||||
y := 3;
|
||||
CALL divide;
|
||||
x := 84;
|
||||
y := 36;
|
||||
CALL gcd;
|
||||
write z;
|
||||
END.
|
Loading…
Reference in New Issue
Block a user