Skip to content

Commit 9522cee

Browse files
authored
Merge pull request #22 from tangentstorm/claude/port-oberon-free-pascal-1Wi4I
start on retro pascal (oberonc) targeting b4vm bytecode
2 parents 304ec56 + b939c39 commit 9522cee

23 files changed

Lines changed: 3477 additions & 3 deletions

pas/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ b4ix
55
colortest
66
mkoptbl
77
disk.b4
8+
*.b4x
9+
*.b4a
10+
*.smb
11+
*.b4x

pas/Makefile

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ B4IX = b4ix$(EXE)
1515
B4A = b4a$(EXE)
1616
B4D = b4d$(EXE)
1717
MKOPTBL = mkoptbl$(EXE)
18-
BINS = $(B4) $(B4X) $(B4I) $(B4H) $(B4IX) $(B4A) $(B4D) $(MKOPTBL)
18+
OBERONC = oberonc$(EXE)
19+
BINS = $(B4) $(B4X) $(B4I) $(B4H) $(B4IX) $(B4A) $(B4D) $(MKOPTBL) $(OBERONC)
1920

2021
main:
2122
@echo 'usage : make <target> ARGS="..."'
@@ -32,6 +33,7 @@ main:
3233
@echo ' runix: build/run b4ix (fancy interpreter)'
3334
@echo ' runa : build/run b4a (batch assembler)'
3435
@echo ' rund : build/run b4d (disassembler)'
36+
@echo ' oberonc : compile the Oberon-07 to b4vm compiler'
3537
@echo ' colortest: build/run colortest'
3638

3739
always:
@@ -107,5 +109,15 @@ colortest: uhw_vt.pas colortest.pas
107109
$(FPC) -B colortest.pas
108110
./colortest
109111

112+
$(OBERONC): ob/oberonc.pas ob/orp.pas ob/org.pas ob/orb.pas ob/ors.pas ob/org2.inc ob/org3.inc ob/org4.inc ob/orp2.inc
113+
$(FPC) -o$(OBERONC) -Fuob -B ob/oberonc.pas
114+
115+
oberonc: $(OBERONC)
116+
117+
test-oberonc: $(OBERONC) $(B4)
118+
./$(OBERONC) ob/test.ob
119+
echo "--- running on b4vm ---"
120+
./$(B4) -l test.b4x
121+
110122
clean:
111-
@rm -f *.o *.ppu *.exe $(BINS) link*.res *~
123+
@rm -f *.o *.ppu *.exe $(BINS) link*.res *~ ob/*.o ob/*.ppu

pas/ob/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
oberonc
2+
*.o
3+
*.ppu
4+
*.b4x
5+
link*.res
6+
*.b4a
7+
*.smb

pas/ob/examples/adder.ob

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
MODULE Adder;
2+
IMPORT In, Out;
3+
VAR a, b: INTEGER;
4+
BEGIN
5+
In.Int(a);
6+
In.Int(b);
7+
Out.Int(a + b, 0); Out.Ln
8+
END Adder.

pas/ob/examples/fibonacci.ob

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
MODULE Fibonacci;
2+
VAR i, n: INTEGER;
3+
4+
PROCEDURE Fib(n: INTEGER): INTEGER;
5+
VAR a, b, t: INTEGER;
6+
BEGIN
7+
a := 0; b := 1;
8+
WHILE n > 0 DO
9+
t := b;
10+
b := a + b;
11+
a := t;
12+
DEC(n)
13+
END;
14+
RETURN a
15+
END Fib;
16+
17+
BEGIN
18+
(* compute first 12 Fibonacci numbers *)
19+
(* results stored in memory starting at global n *)
20+
i := 0;
21+
WHILE i <= 12 DO
22+
n := Fib(i);
23+
INC(i)
24+
END
25+
(* n ends up as Fib(12) = 144 *)
26+
END Fibonacci.

pas/ob/examples/gcd.ob

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
MODULE GCD;
2+
VAR result: INTEGER;
3+
4+
PROCEDURE Gcd(a, b: INTEGER): INTEGER;
5+
BEGIN
6+
WHILE a # b DO
7+
IF a > b THEN a := a - b
8+
ELSE b := b - a
9+
END
10+
END;
11+
RETURN a
12+
END Gcd;
13+
14+
BEGIN
15+
result := Gcd(36, 24)
16+
(* result = 12 *)
17+
END GCD.

pas/ob/examples/hello.ob

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module Hello;
2+
import Out;
3+
var i, f: INTEGER;
4+
5+
procedure Fact(n: INTEGER): INTEGER;
6+
var r: INTEGER;
7+
begin r := 1; while n > 1 do r := r * n; DEC(n) end; return r
8+
end Fact;
9+
10+
begin
11+
i := 1;
12+
while i <= 10 do
13+
f := Fact(i);
14+
Out.Int(f, 0); Out.Char(32);
15+
INC(i)
16+
end;
17+
Out.Ln
18+
end Hello.

pas/ob/examples/power.ob

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
MODULE Power;
2+
(* Integer exponentiation by squaring *)
3+
VAR result: INTEGER;
4+
5+
PROCEDURE Pow(base, exp: INTEGER): INTEGER;
6+
VAR r: INTEGER;
7+
BEGIN
8+
r := 1;
9+
WHILE exp > 0 DO
10+
IF ODD(exp) THEN r := r * base END;
11+
base := base * base;
12+
exp := exp DIV 2
13+
END;
14+
RETURN r
15+
END Pow;
16+
17+
BEGIN
18+
result := Pow(2, 10)
19+
(* result = 1024 *)
20+
END Power.

pas/ob/examples/records.ob

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
MODULE Records;
2+
(* Demonstrate record types *)
3+
TYPE
4+
Point = RECORD x, y: INTEGER END;
5+
6+
VAR
7+
p: Point;
8+
sum: INTEGER;
9+
10+
BEGIN
11+
p.x := 3;
12+
p.y := 4;
13+
sum := p.x + p.y
14+
(* sum = 7 *)
15+
END Records.

pas/ob/examples/sieve.ob

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
MODULE Sieve;
2+
(* Sieve of Eratosthenes - find primes up to 100 *)
3+
CONST N = 100;
4+
VAR
5+
sieve: ARRAY N OF BOOLEAN;
6+
i, j, count: INTEGER;
7+
8+
BEGIN
9+
(* initialize: assume all are prime *)
10+
i := 2;
11+
WHILE i < N DO
12+
sieve[i] := TRUE;
13+
INC(i)
14+
END;
15+
16+
(* sieve *)
17+
i := 2;
18+
WHILE i * i < N DO
19+
IF sieve[i] THEN
20+
j := i * i;
21+
WHILE j < N DO
22+
sieve[j] := FALSE;
23+
j := j + i
24+
END
25+
END;
26+
INC(i)
27+
END;
28+
29+
(* count primes *)
30+
count := 0;
31+
i := 2;
32+
WHILE i < N DO
33+
IF sieve[i] THEN INC(count) END;
34+
INC(i)
35+
END
36+
(* count = 25, the number of primes below 100 *)
37+
END Sieve.

0 commit comments

Comments
 (0)