File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 55colortest
66mkoptbl
77disk.b4
8+ * .b4x
9+ * .b4a
10+ * .smb
11+ * .b4x
Original file line number Diff line number Diff line change @@ -15,7 +15,8 @@ B4IX = b4ix$(EXE)
1515B4A = b4a$(EXE )
1616B4D = b4d$(EXE )
1717MKOPTBL = 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
2021main :
2122 @echo ' usage : make <target> ARGS="..."'
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
3739always :
@@ -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+
110122clean :
111- @rm -f * .o * .ppu * .exe $(BINS ) link* .res * ~
123+ @rm -f * .o * .ppu * .exe $(BINS ) link* .res * ~ ob/ * .o ob/ * .ppu
Original file line number Diff line number Diff line change 1+ oberonc
2+ * .o
3+ * .ppu
4+ * .b4x
5+ link * .res
6+ * .b4a
7+ * .smb
Original file line number Diff line number Diff line change 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.
Original file line number Diff line number Diff line change 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.
Original file line number Diff line number Diff line change 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.
Original file line number Diff line number Diff line change 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.
Original file line number Diff line number Diff line change 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.
Original file line number Diff line number Diff line change 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.
Original file line number Diff line number Diff line change 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.
You can’t perform that action at this time.
0 commit comments