mirror of
https://github.com/vishapoberon/oberonbyexample.git
synced 2026-04-05 21:02:25 +00:00
post rebranding
This commit is contained in:
parent
4169aecd5b
commit
95b512baf4
66 changed files with 716 additions and 15 deletions
11
examples/arguments/unixstyle_oberon_traditional/makefile
Normal file
11
examples/arguments/unixstyle_oberon_traditional/makefile
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
|
||||
VOC = /opt/voc/bin/voc
|
||||
|
||||
|
||||
all:
|
||||
$(VOC) -m partest.Mod
|
||||
|
||||
|
||||
test:
|
||||
./partest -str aaa -int 111
|
||||
./partest -str 1 -int 111
|
||||
63
examples/arguments/unixstyle_oberon_traditional/partest.Mod
Normal file
63
examples/arguments/unixstyle_oberon_traditional/partest.Mod
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
MODULE partest;
|
||||
|
||||
IMPORT Oberon, Texts;
|
||||
|
||||
CONST
|
||||
argStr0 = "str"; (* we only have two types of args, one string and one int *)
|
||||
argInt0 = "int"; (* i. e. -str somestring -int somenumber *)
|
||||
|
||||
VAR
|
||||
W: Texts.Writer; (* for console output *)
|
||||
S: Texts.Scanner; T: Texts.Text;
|
||||
BEGIN
|
||||
Texts.OpenWriter(W);
|
||||
Texts.WriteString(W, "hello, world, let's see which arguments do we get"); Texts.WriteLn(W);
|
||||
|
||||
(* open arguments scanner *)
|
||||
Texts.OpenScanner(S, Oberon.Par.text, Oberon.Par.pos);
|
||||
|
||||
WHILE ~S.eot DO
|
||||
Texts.Scan(S);
|
||||
|
||||
IF S.class = Texts.Char THEN (* do we get '-' sign ? *)
|
||||
IF S.c = "-" THEN
|
||||
Texts.Scan(S);
|
||||
IF S.class = Texts.Name THEN (* we got the key *)
|
||||
Texts.WriteString(W, "key: "); Texts.WriteString(W, S.s); Texts.WriteLn(W);
|
||||
(* now get the value *)
|
||||
IF S.s = argStr0 THEN
|
||||
Texts.Scan(S);
|
||||
IF S.class = Texts.Name THEN
|
||||
Texts.WriteString(W, "value: "); Texts.WriteString (W, S.s); Texts.WriteLn(W); Texts.Append(Oberon.Log, W.buf);
|
||||
ELSE
|
||||
Texts.WriteString(W, "string expected"); Texts.WriteLn(W);
|
||||
Texts.Append(Oberon.Log, W.buf);
|
||||
HALT(1);
|
||||
END;
|
||||
ELSIF S.s = argInt0 THEN
|
||||
Texts.Scan(S);
|
||||
IF S.class = Texts.Int THEN
|
||||
Texts.WriteString(W, "value: "); Texts.WriteInt (W, S.i, 0); Texts.WriteLn(W); Texts.Append(Oberon.Log, W.buf);
|
||||
ELSE
|
||||
Texts.WriteString(W, "integer expected"); Texts.WriteLn(W);
|
||||
Texts.Append(Oberon.Log, W.buf);
|
||||
HALT(1);
|
||||
END;
|
||||
END;
|
||||
ELSE
|
||||
(* we were expecting characters after the '-' sign *)
|
||||
Texts.WriteString(W, "key name expected"); Texts. WriteLn(W);
|
||||
Texts.Append(Oberon.Log, W.buf);
|
||||
HALT(1);
|
||||
END;
|
||||
END
|
||||
ELSE
|
||||
Texts.WriteString(W, "key option must start with '-' sign "); Texts.WriteLn(W);
|
||||
HALT(1);
|
||||
END; (* if got '-' *)
|
||||
Oberon.Par.pos := Texts.Pos(S);
|
||||
Texts.Append(Oberon.Log, W.buf)
|
||||
END; (* while *)
|
||||
|
||||
|
||||
END partest.
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
MODULE partest;
|
||||
|
||||
IMPORT Oberon, Texts;
|
||||
|
||||
CONST
|
||||
argStr0 = "str"; (* we only have two types of args, one string and one int *)
|
||||
argInt0 = "int"; (* i. e. -str somestring -int somenumber *)
|
||||
|
||||
VAR
|
||||
W: Texts.Writer; (* for console output *)
|
||||
S: Texts.Scanner; T: Texts.Text;
|
||||
BEGIN
|
||||
Texts.OpenWriter(W);
|
||||
Texts.WriteString(W, "hello, world, let's see which arguments do we get"); Texts.WriteLn(W);
|
||||
|
||||
(* open arguments scanner *)
|
||||
Texts.OpenScanner(S, Oberon.Par.text, Oberon.Par.pos);
|
||||
|
||||
WHILE ~S.eot DO
|
||||
Texts.Scan(S);
|
||||
|
||||
IF S.class = Texts.Char THEN (* do we get '-' sign ? *)
|
||||
IF S.c = "-" THEN
|
||||
Texts.Scan(S);
|
||||
IF S.class = Texts.Name THEN (* we got the key *)
|
||||
Texts.WriteString(W, "key: "); Texts.WriteString(W, S.s); Texts.WriteLn(W);
|
||||
(* now get the value *)
|
||||
IF S.s = argStr0 THEN
|
||||
Texts.Scan(S);
|
||||
IF S.class = Texts.Name THEN
|
||||
Texts.WriteString(W, "value: "); Texts.WriteString (W, S.s); Texts.WriteLn(W); Texts.Append(Oberon.Log, W.buf);
|
||||
ELSE
|
||||
Texts.WriteString(W, "string expected"); Texts.WriteLn(W);
|
||||
Texts.Append(Oberon.Log, W.buf);
|
||||
HALT(1);
|
||||
END;
|
||||
ELSIF S.s = argInt0 THEN
|
||||
Texts.Scan(S);
|
||||
IF S.class = Texts.Int THEN
|
||||
Texts.WriteString(W, "value: "); Texts.WriteInt (W, S.i, 0); Texts.WriteLn(W); Texts.Append(Oberon.Log, W.buf);
|
||||
ELSE
|
||||
Texts.WriteString(W, "integer expected"); Texts.WriteLn(W);
|
||||
Texts.Append(Oberon.Log, W.buf);
|
||||
HALT(1);
|
||||
END;
|
||||
END;
|
||||
ELSE
|
||||
(* we were expecting characters after the '-' sign *)
|
||||
Texts.WriteString(W, "key name expected"); Texts. WriteLn(W);
|
||||
Texts.Append(Oberon.Log, W.buf);
|
||||
HALT(1);
|
||||
END;
|
||||
END
|
||||
ELSE
|
||||
Texts.WriteString(W, "key option must start with '-' sign "); Texts.WriteLn(W);
|
||||
HALT(1);
|
||||
END; (* if got '-' *)
|
||||
Oberon.Par.pos := Texts.Pos(S);
|
||||
Texts.Append(Oberon.Log, W.buf)
|
||||
END; (* while *)
|
||||
|
||||
|
||||
END partest.
|
||||
47
examples/arguments/unixstyle_oberon_traditional/readme.md
Normal file
47
examples/arguments/unixstyle_oberon_traditional/readme.md
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
|
||||
example shows how to implement unix style arguments parsing by using traditional oberon functions from modules Texts and Oberon.
|
||||
|
||||
compile
|
||||
=======
|
||||
|
||||
```
|
||||
make
|
||||
```
|
||||
|
||||
run
|
||||
===
|
||||
|
||||
```
|
||||
make test
|
||||
```
|
||||
|
||||
or type
|
||||
|
||||
```
|
||||
./partest -str aaa -int 111
|
||||
```
|
||||
|
||||
that should produce the following output
|
||||
|
||||
```
|
||||
hello, world, let's see which arguments do we get
|
||||
key: str
|
||||
value: aaa
|
||||
key: int
|
||||
value: 111
|
||||
```
|
||||
|
||||
```
|
||||
./partest -str 000 -int 111
|
||||
```
|
||||
|
||||
the output will be
|
||||
|
||||
```
|
||||
hello, world, let's see which arguments do we get
|
||||
key: str
|
||||
string expected
|
||||
Terminated by Halt(1).
|
||||
```
|
||||
|
||||
that's all folks.
|
||||
42
examples/arrays/Arrays.Mod
Normal file
42
examples/arrays/Arrays.Mod
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
MODULE arrays;
|
||||
|
||||
IMPORT Out;
|
||||
|
||||
VAR
|
||||
tmp : INTEGER;
|
||||
matrix : ARRAY 3 OF ARRAY 3 OF INTEGER;
|
||||
i, v, k : INTEGER;
|
||||
|
||||
BEGIN
|
||||
v := 1;
|
||||
FOR i := 0 TO LEN(matrix) - 1 DO
|
||||
FOR k := 0 TO LEN(matrix[i]) - 1 DO
|
||||
matrix[i][k] := v;
|
||||
INC(v);
|
||||
END;
|
||||
END;
|
||||
|
||||
FOR i := 0 TO LEN(matrix) - 1 DO
|
||||
FOR k := 0 TO LEN(matrix[i]) - 1 DO
|
||||
Out.Int(matrix[i][k], 0); Out.String(" ");
|
||||
END;
|
||||
Out.Ln;
|
||||
END;
|
||||
|
||||
FOR i := 0 TO LEN(matrix) - 1 DO
|
||||
FOR k := i + 1 TO LEN(matrix[i]) - 1 DO
|
||||
tmp := matrix[i][k];
|
||||
matrix[i][k] := matrix[k][i];
|
||||
matrix[k][i] := tmp;
|
||||
END;
|
||||
END;
|
||||
|
||||
Out.Ln; Out.Ln;
|
||||
|
||||
FOR i := 0 TO LEN(matrix) - 1 DO
|
||||
FOR k := 0 TO LEN(matrix[i]) - 1 DO
|
||||
Out.Int(matrix[i][k], 0); Out.String(" ");
|
||||
END;
|
||||
Out.Ln;
|
||||
END;
|
||||
END arrays.
|
||||
42
examples/arrays/Arrays.Mod.ob2
Normal file
42
examples/arrays/Arrays.Mod.ob2
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
MODULE arrays;
|
||||
|
||||
IMPORT Out;
|
||||
|
||||
VAR
|
||||
tmp : INTEGER;
|
||||
matrix : ARRAY 3 OF ARRAY 3 OF INTEGER;
|
||||
i, v, k : INTEGER;
|
||||
|
||||
BEGIN
|
||||
v := 1;
|
||||
FOR i := 0 TO LEN(matrix) - 1 DO
|
||||
FOR k := 0 TO LEN(matrix[i]) - 1 DO
|
||||
matrix[i][k] := v;
|
||||
INC(v);
|
||||
END;
|
||||
END;
|
||||
|
||||
FOR i := 0 TO LEN(matrix) - 1 DO
|
||||
FOR k := 0 TO LEN(matrix[i]) - 1 DO
|
||||
Out.Int(matrix[i][k], 0); Out.String(" ");
|
||||
END;
|
||||
Out.Ln;
|
||||
END;
|
||||
|
||||
FOR i := 0 TO LEN(matrix) - 1 DO
|
||||
FOR k := i + 1 TO LEN(matrix[i]) - 1 DO
|
||||
tmp := matrix[i][k];
|
||||
matrix[i][k] := matrix[k][i];
|
||||
matrix[k][i] := tmp;
|
||||
END;
|
||||
END;
|
||||
|
||||
Out.Ln; Out.Ln;
|
||||
|
||||
FOR i := 0 TO LEN(matrix) - 1 DO
|
||||
FOR k := 0 TO LEN(matrix[i]) - 1 DO
|
||||
Out.Int(matrix[i][k], 0); Out.String(" ");
|
||||
END;
|
||||
Out.Ln;
|
||||
END;
|
||||
END arrays.
|
||||
10
examples/arrays/Makefile
Normal file
10
examples/arrays/Makefile
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
VOC = /opt/voc/bin/voc
|
||||
|
||||
all:
|
||||
$(VOC) -m Arrays.Mod
|
||||
|
||||
clean:
|
||||
rm *.c
|
||||
rm *.h
|
||||
rm *.o
|
||||
rm *.sym
|
||||
14
examples/case/Case.Mod
Normal file
14
examples/case/Case.Mod
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
MODULE case;
|
||||
|
||||
|
||||
IMPORT Out, Modules;
|
||||
|
||||
|
||||
BEGIN
|
||||
CASE Modules.ArgCount - 1 OF
|
||||
0 : Out.String("There are no arguments");
|
||||
| 1 : Out.String("There is one argument");
|
||||
| 2 : Out.String("There are two arguments");
|
||||
ELSE Out.String("There are more than two arguments") END;
|
||||
Out.Ln;
|
||||
END case.
|
||||
14
examples/case/Case.Mod.ob2
Normal file
14
examples/case/Case.Mod.ob2
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
MODULE case;
|
||||
|
||||
|
||||
IMPORT Out, Modules;
|
||||
|
||||
|
||||
BEGIN
|
||||
CASE Modules.ArgCount - 1 OF
|
||||
0 : Out.String("There are no arguments");
|
||||
| 1 : Out.String("There is one argument");
|
||||
| 2 : Out.String("There are two arguments");
|
||||
ELSE Out.String("There are more than two arguments") END;
|
||||
Out.Ln;
|
||||
END case.
|
||||
10
examples/case/Makefile
Normal file
10
examples/case/Makefile
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
VOC = /opt/voc/bin/voc
|
||||
|
||||
all:
|
||||
$(VOC) -m Case.Mod
|
||||
|
||||
clean:
|
||||
rm *.c
|
||||
rm *.h
|
||||
rm *.o
|
||||
rm *.sym
|
||||
14
examples/constants/Constants.Mod
Normal file
14
examples/constants/Constants.Mod
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
MODULE constants;
|
||||
|
||||
IMPORT Out;
|
||||
|
||||
CONST
|
||||
s = "if it moves, compile it!";
|
||||
n = 42;
|
||||
m = n * 2;
|
||||
|
||||
BEGIN
|
||||
Out.String(s); Out.Ln;
|
||||
Out.Int(n, 0); Out.Ln;
|
||||
Out.Int(m, 0); Out.Ln;
|
||||
END constants.
|
||||
14
examples/constants/Constants.Mod.ob2
Normal file
14
examples/constants/Constants.Mod.ob2
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
MODULE constants;
|
||||
|
||||
IMPORT Out;
|
||||
|
||||
CONST
|
||||
s = "if it moves, compile it!";
|
||||
n = 42;
|
||||
m = n * 2;
|
||||
|
||||
BEGIN
|
||||
Out.String(s); Out.Ln;
|
||||
Out.Int(n, 0); Out.Ln;
|
||||
Out.Int(m, 0); Out.Ln;
|
||||
END constants.
|
||||
10
examples/constants/Makefile
Normal file
10
examples/constants/Makefile
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
VOC = /opt/voc/bin/voc
|
||||
|
||||
all:
|
||||
$(VOC) -m Constants.Mod
|
||||
|
||||
clean:
|
||||
rm *.c
|
||||
rm *.h
|
||||
rm *.o
|
||||
rm *.sym
|
||||
68
examples/enums_example/0/Days.Mod
Normal file
68
examples/enums_example/0/Days.Mod
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
MODULE Days;
|
||||
|
||||
TYPE
|
||||
Day* = POINTER TO DayDesc;
|
||||
DayDesc = RECORD
|
||||
next, prev : Day
|
||||
END;
|
||||
Week = ARRAY 7 OF Day;
|
||||
|
||||
VAR
|
||||
sun*, mon*, tue*, wed*, thu*, fri*, sat* : Day;
|
||||
week: Week;
|
||||
|
||||
PROCEDURE Next*(d : Day): Day;
|
||||
BEGIN
|
||||
RETURN d.next
|
||||
END Next;
|
||||
|
||||
PROCEDURE Prev*(d: Day): Day;
|
||||
BEGIN
|
||||
RETURN d.prev;
|
||||
END Prev;
|
||||
|
||||
PROCEDURE inc(VAR j: SHORTINT);
|
||||
BEGIN
|
||||
IF j = 6 THEN
|
||||
j := 0
|
||||
ELSE
|
||||
INC(j)
|
||||
END
|
||||
END inc;
|
||||
|
||||
PROCEDURE dec(VAR j: SHORTINT);
|
||||
BEGIN
|
||||
IF j = 0 THEN
|
||||
j := 6
|
||||
ELSE
|
||||
DEC(j)
|
||||
END
|
||||
END dec;
|
||||
|
||||
PROCEDURE init(VAR w : Week);
|
||||
VAR
|
||||
i,j : SHORTINT;
|
||||
BEGIN
|
||||
i := 0;
|
||||
REPEAT
|
||||
j := i; inc(j);
|
||||
w[i].next := w[j];
|
||||
j := i; dec(j);
|
||||
w[i].prev := w[j];
|
||||
INC(i)
|
||||
UNTIL i > 6;
|
||||
END init;
|
||||
|
||||
BEGIN
|
||||
NEW(sun); NEW(mon); NEW(tue); NEW(wed); NEW(thu); NEW(fri); NEW(sat);
|
||||
week[0] := sun;
|
||||
week[1] := mon;
|
||||
week[2] := tue;
|
||||
week[3] := wed;
|
||||
week[4] := thu;
|
||||
week[5] := fri;
|
||||
week[6] := sat;
|
||||
|
||||
init(week);
|
||||
|
||||
END Days.
|
||||
68
examples/enums_example/0/Days.Mod.ob2
Normal file
68
examples/enums_example/0/Days.Mod.ob2
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
MODULE Days;
|
||||
|
||||
TYPE
|
||||
Day* = POINTER TO DayDesc;
|
||||
DayDesc = RECORD
|
||||
next, prev : Day
|
||||
END;
|
||||
Week = ARRAY 7 OF Day;
|
||||
|
||||
VAR
|
||||
sun*, mon*, tue*, wed*, thu*, fri*, sat* : Day;
|
||||
week: Week;
|
||||
|
||||
PROCEDURE Next*(d : Day): Day;
|
||||
BEGIN
|
||||
RETURN d.next
|
||||
END Next;
|
||||
|
||||
PROCEDURE Prev*(d: Day): Day;
|
||||
BEGIN
|
||||
RETURN d.prev;
|
||||
END Prev;
|
||||
|
||||
PROCEDURE inc(VAR j: SHORTINT);
|
||||
BEGIN
|
||||
IF j = 6 THEN
|
||||
j := 0
|
||||
ELSE
|
||||
INC(j)
|
||||
END
|
||||
END inc;
|
||||
|
||||
PROCEDURE dec(VAR j: SHORTINT);
|
||||
BEGIN
|
||||
IF j = 0 THEN
|
||||
j := 6
|
||||
ELSE
|
||||
DEC(j)
|
||||
END
|
||||
END dec;
|
||||
|
||||
PROCEDURE init(VAR w : Week);
|
||||
VAR
|
||||
i,j : SHORTINT;
|
||||
BEGIN
|
||||
i := 0;
|
||||
REPEAT
|
||||
j := i; inc(j);
|
||||
w[i].next := w[j];
|
||||
j := i; dec(j);
|
||||
w[i].prev := w[j];
|
||||
INC(i)
|
||||
UNTIL i > 6;
|
||||
END init;
|
||||
|
||||
BEGIN
|
||||
NEW(sun); NEW(mon); NEW(tue); NEW(wed); NEW(thu); NEW(fri); NEW(sat);
|
||||
week[0] := sun;
|
||||
week[1] := mon;
|
||||
week[2] := tue;
|
||||
week[3] := wed;
|
||||
week[4] := thu;
|
||||
week[5] := fri;
|
||||
week[6] := sat;
|
||||
|
||||
init(week);
|
||||
|
||||
END Days.
|
||||
6
examples/enums_example/0/readme.md
Normal file
6
examples/enums_example/0/readme.md
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
|
||||
|
||||
Example aimed to show how to survive without enumerations in Oberon.
|
||||
|
||||
This way is even cooler than enumerations. (:
|
||||
|
||||
22
examples/enums_example/0/test.Mod
Normal file
22
examples/enums_example/0/test.Mod
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
MODULE test;
|
||||
|
||||
IMPORT Days, Out;
|
||||
|
||||
VAR today, yesterday, tomorrow: Days.Day;
|
||||
|
||||
BEGIN
|
||||
today := Days.mon; (*init*)
|
||||
|
||||
yesterday := Days.Prev(today);
|
||||
IF yesterday = Days.sun
|
||||
THEN
|
||||
Out.String("it works!"); Out.Ln
|
||||
END;
|
||||
tomorrow := Days.Next(today);
|
||||
|
||||
IF tomorrow = Days.tue
|
||||
THEN
|
||||
Out.String("it works!"); Out.Ln
|
||||
END;
|
||||
|
||||
END test.
|
||||
22
examples/enums_example/0/test.Mod.ob2
Normal file
22
examples/enums_example/0/test.Mod.ob2
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
MODULE test;
|
||||
|
||||
IMPORT Days, Out;
|
||||
|
||||
VAR today, yesterday, tomorrow: Days.Day;
|
||||
|
||||
BEGIN
|
||||
today := Days.mon; (*init*)
|
||||
|
||||
yesterday := Days.Prev(today);
|
||||
IF yesterday = Days.sun
|
||||
THEN
|
||||
Out.String("it works!"); Out.Ln
|
||||
END;
|
||||
tomorrow := Days.Next(today);
|
||||
|
||||
IF tomorrow = Days.tue
|
||||
THEN
|
||||
Out.String("it works!"); Out.Ln
|
||||
END;
|
||||
|
||||
END test.
|
||||
33
examples/enums_example/1/Days.Mod
Normal file
33
examples/enums_example/1/Days.Mod
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
MODULE Days;
|
||||
|
||||
TYPE
|
||||
Day* = POINTER TO DayDesc;
|
||||
DayDesc = RECORD
|
||||
num: INTEGER
|
||||
END;
|
||||
Week* = ARRAY 7 OF Day;
|
||||
VAR
|
||||
sun*, mon*, tue*, wed*, thu*, fri*, sat* : Day;
|
||||
week: Week;
|
||||
|
||||
PROCEDURE Next*(d : Day): Day;
|
||||
BEGIN RETURN week[(d.num + 1) MOD 7];
|
||||
END Next;
|
||||
|
||||
PROCEDURE Prev*(d: Day): Day;
|
||||
BEGIN RETURN week[(d.num - 1) MOD 7];
|
||||
END Prev;
|
||||
|
||||
PROCEDURE day(VAR d: Day; num: INTEGER);
|
||||
BEGIN NEW(d); d.num := num; week[num] := d;
|
||||
END day;
|
||||
|
||||
BEGIN
|
||||
day(sun, 0);
|
||||
day(mon, 1);
|
||||
day(tue, 2);
|
||||
day(wed, 3);
|
||||
day(thu, 4);
|
||||
day(fri, 5);
|
||||
day(sat, 6);
|
||||
END Days.
|
||||
33
examples/enums_example/1/Days.Mod.ob2
Normal file
33
examples/enums_example/1/Days.Mod.ob2
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
MODULE Days;
|
||||
|
||||
TYPE
|
||||
Day* = POINTER TO DayDesc;
|
||||
DayDesc = RECORD
|
||||
num: INTEGER
|
||||
END;
|
||||
Week* = ARRAY 7 OF Day;
|
||||
VAR
|
||||
sun*, mon*, tue*, wed*, thu*, fri*, sat* : Day;
|
||||
week: Week;
|
||||
|
||||
PROCEDURE Next*(d : Day): Day;
|
||||
BEGIN RETURN week[(d.num + 1) MOD 7];
|
||||
END Next;
|
||||
|
||||
PROCEDURE Prev*(d: Day): Day;
|
||||
BEGIN RETURN week[(d.num - 1) MOD 7];
|
||||
END Prev;
|
||||
|
||||
PROCEDURE day(VAR d: Day; num: INTEGER);
|
||||
BEGIN NEW(d); d.num := num; week[num] := d;
|
||||
END day;
|
||||
|
||||
BEGIN
|
||||
day(sun, 0);
|
||||
day(mon, 1);
|
||||
day(tue, 2);
|
||||
day(wed, 3);
|
||||
day(thu, 4);
|
||||
day(fri, 5);
|
||||
day(sat, 6);
|
||||
END Days.
|
||||
5
examples/enums_example/1/readme.md
Normal file
5
examples/enums_example/1/readme.md
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
|
||||
|
||||
Example aimed to show how to survive without enumerations in Oberon.
|
||||
|
||||
This example is contributed by @pdewacht.
|
||||
22
examples/enums_example/1/test.Mod
Normal file
22
examples/enums_example/1/test.Mod
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
MODULE test;
|
||||
|
||||
IMPORT Days, Out;
|
||||
|
||||
VAR today, yesterday, tomorrow: Days.Day;
|
||||
|
||||
BEGIN
|
||||
today := Days.mon; (*init*)
|
||||
|
||||
yesterday := Days.Prev(today);
|
||||
IF yesterday = Days.sun
|
||||
THEN
|
||||
Out.String("it works!"); Out.Ln
|
||||
END;
|
||||
tomorrow := Days.Next(today);
|
||||
|
||||
IF tomorrow = Days.tue
|
||||
THEN
|
||||
Out.String("it works!"); Out.Ln
|
||||
END;
|
||||
|
||||
END test.
|
||||
22
examples/enums_example/1/test.Mod.ob2
Normal file
22
examples/enums_example/1/test.Mod.ob2
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
MODULE test;
|
||||
|
||||
IMPORT Days, Out;
|
||||
|
||||
VAR today, yesterday, tomorrow: Days.Day;
|
||||
|
||||
BEGIN
|
||||
today := Days.mon; (*init*)
|
||||
|
||||
yesterday := Days.Prev(today);
|
||||
IF yesterday = Days.sun
|
||||
THEN
|
||||
Out.String("it works!"); Out.Ln
|
||||
END;
|
||||
tomorrow := Days.Next(today);
|
||||
|
||||
IF tomorrow = Days.tue
|
||||
THEN
|
||||
Out.String("it works!"); Out.Ln
|
||||
END;
|
||||
|
||||
END test.
|
||||
19
examples/for/For.Mod
Normal file
19
examples/for/For.Mod
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
MODULE for;
|
||||
|
||||
IMPORT Out;
|
||||
|
||||
VAR
|
||||
i : INTEGER;
|
||||
|
||||
|
||||
BEGIN
|
||||
Out.String("i is "); Out.Int(i, 0); Out.Ln;
|
||||
Out.String("For loop started"); Out.Ln;
|
||||
FOR i := 0 TO 10 DO
|
||||
Out.String("i : "); Out.Int(i, 0); Out.Ln;
|
||||
END;
|
||||
Out.String("For-By loop started"); Out.Ln;
|
||||
FOR i := 0 TO 10 BY 2 DO
|
||||
Out.String("i : "); Out.Int(i, 0); Out.Ln;
|
||||
END;
|
||||
END for.
|
||||
19
examples/for/For.Mod.ob2
Normal file
19
examples/for/For.Mod.ob2
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
MODULE for;
|
||||
|
||||
IMPORT Out;
|
||||
|
||||
VAR
|
||||
i : INTEGER;
|
||||
|
||||
|
||||
BEGIN
|
||||
Out.String("i is "); Out.Int(i, 0); Out.Ln;
|
||||
Out.String("For loop started"); Out.Ln;
|
||||
FOR i := 0 TO 10 DO
|
||||
Out.String("i : "); Out.Int(i, 0); Out.Ln;
|
||||
END;
|
||||
Out.String("For-By loop started"); Out.Ln;
|
||||
FOR i := 0 TO 10 BY 2 DO
|
||||
Out.String("i : "); Out.Int(i, 0); Out.Ln;
|
||||
END;
|
||||
END for.
|
||||
10
examples/for/Makefile
Normal file
10
examples/for/Makefile
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
VOC = /opt/voc/bin/voc
|
||||
|
||||
all:
|
||||
$(VOC) -m For.Mod
|
||||
|
||||
clean:
|
||||
rm *.c
|
||||
rm *.h
|
||||
rm *.o
|
||||
rm *.sym
|
||||
10
examples/hello-world/Console/Hello.Mod
Normal file
10
examples/hello-world/Console/Hello.Mod
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
MODULE hello;
|
||||
|
||||
|
||||
IMPORT Console;
|
||||
|
||||
|
||||
BEGIN
|
||||
Console.String("Hello World!");
|
||||
Console.Ln
|
||||
END hello.
|
||||
10
examples/hello-world/Console/Hello.Mod.ob2
Normal file
10
examples/hello-world/Console/Hello.Mod.ob2
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
MODULE hello;
|
||||
|
||||
|
||||
IMPORT Console;
|
||||
|
||||
|
||||
BEGIN
|
||||
Console.String("Hello World!");
|
||||
Console.Ln
|
||||
END hello.
|
||||
10
examples/hello-world/Console/Makefile
Normal file
10
examples/hello-world/Console/Makefile
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
VOC = /opt/voc/bin/voc
|
||||
|
||||
all:
|
||||
$(VOC) -m Hello.Mod
|
||||
|
||||
clean:
|
||||
rm *.c
|
||||
rm *.h
|
||||
rm *.o
|
||||
rm *.sym
|
||||
9
examples/hello-world/Out/Hello.Mod
Normal file
9
examples/hello-world/Out/Hello.Mod
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
MODULE hello;
|
||||
|
||||
|
||||
IMPORT Out;
|
||||
|
||||
|
||||
BEGIN
|
||||
Out.String("Hello, World"); Out.Ln
|
||||
END hello.
|
||||
9
examples/hello-world/Out/Hello.Mod.ob2
Normal file
9
examples/hello-world/Out/Hello.Mod.ob2
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
MODULE hello;
|
||||
|
||||
|
||||
IMPORT Out;
|
||||
|
||||
|
||||
BEGIN
|
||||
Out.String("Hello, World"); Out.Ln
|
||||
END hello.
|
||||
10
examples/hello-world/Out/Makefile
Normal file
10
examples/hello-world/Out/Makefile
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
VOC = /opt/voc/bin/voc
|
||||
|
||||
all:
|
||||
$(VOC) -m Hello.Mod
|
||||
|
||||
clean:
|
||||
rm *.c
|
||||
rm *.h
|
||||
rm *.o
|
||||
rm *.sym
|
||||
49
examples/ifelse/IfElse.Mod
Normal file
49
examples/ifelse/IfElse.Mod
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
MODULE ifelse;
|
||||
|
||||
|
||||
IMPORT Out;
|
||||
|
||||
VAR n, m : INTEGER;
|
||||
|
||||
|
||||
BEGIN
|
||||
n := 8; m := 4;
|
||||
|
||||
IF n MOD m = 0 THEN
|
||||
Out.Int(n,0); Out.String(" is divisible by "); Out.Int(m,0); Out.Ln;
|
||||
END;
|
||||
|
||||
n := 7; m := 6;
|
||||
|
||||
IF n * m = 42 THEN
|
||||
Out.Int(n,0); Out.String(" times "); Out.Int(m,0); Out.String(" equals 42"); Out.Ln;
|
||||
END;
|
||||
|
||||
IF n # m THEN Out.Int(n,0); Out.String(" does not equal "); Out.Int(m,0); Out.Ln; END;
|
||||
|
||||
IF ODD(n)
|
||||
THEN
|
||||
Out.Int(n,0); Out.String(" is odd"); Out.Ln;
|
||||
ELSE
|
||||
Out.Int(n,0); Out.String(" is even"); Out.Ln;
|
||||
END;
|
||||
|
||||
IF ~ODD(m)
|
||||
THEN
|
||||
Out.Int(m,0); Out.String(" is even"); Out.Ln;
|
||||
ELSE
|
||||
Out.Int(m,0); Out.String(" is odd"); Out.Ln;
|
||||
END;
|
||||
|
||||
n := 9;
|
||||
|
||||
IF n < 0
|
||||
THEN
|
||||
Out.Int(n, 0); Out.String(" is negative"); Out.Ln;
|
||||
ELSIF n < 10
|
||||
THEN
|
||||
Out.Int(n, 0); Out.String(" has 1 digit"); Out.Ln;
|
||||
ELSE
|
||||
Out.Int(n, 0); Out.String(" has multiple digits"); Out.Ln;
|
||||
END;
|
||||
END ifelse.
|
||||
49
examples/ifelse/IfElse.Mod.ob2
Normal file
49
examples/ifelse/IfElse.Mod.ob2
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
MODULE ifelse;
|
||||
|
||||
|
||||
IMPORT Out;
|
||||
|
||||
VAR n, m : INTEGER;
|
||||
|
||||
|
||||
BEGIN
|
||||
n := 8; m := 4;
|
||||
|
||||
IF n MOD m = 0 THEN
|
||||
Out.Int(n,0); Out.String(" is divisible by "); Out.Int(m,0); Out.Ln;
|
||||
END;
|
||||
|
||||
n := 7; m := 6;
|
||||
|
||||
IF n * m = 42 THEN
|
||||
Out.Int(n,0); Out.String(" times "); Out.Int(m,0); Out.String(" equals 42"); Out.Ln;
|
||||
END;
|
||||
|
||||
IF n # m THEN Out.Int(n,0); Out.String(" does not equal "); Out.Int(m,0); Out.Ln; END;
|
||||
|
||||
IF ODD(n)
|
||||
THEN
|
||||
Out.Int(n,0); Out.String(" is odd"); Out.Ln;
|
||||
ELSE
|
||||
Out.Int(n,0); Out.String(" is even"); Out.Ln;
|
||||
END;
|
||||
|
||||
IF ~ODD(m)
|
||||
THEN
|
||||
Out.Int(m,0); Out.String(" is even"); Out.Ln;
|
||||
ELSE
|
||||
Out.Int(m,0); Out.String(" is odd"); Out.Ln;
|
||||
END;
|
||||
|
||||
n := 9;
|
||||
|
||||
IF n < 0
|
||||
THEN
|
||||
Out.Int(n, 0); Out.String(" is negative"); Out.Ln;
|
||||
ELSIF n < 10
|
||||
THEN
|
||||
Out.Int(n, 0); Out.String(" has 1 digit"); Out.Ln;
|
||||
ELSE
|
||||
Out.Int(n, 0); Out.String(" has multiple digits"); Out.Ln;
|
||||
END;
|
||||
END ifelse.
|
||||
10
examples/ifelse/Makefile
Normal file
10
examples/ifelse/Makefile
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
VOC = /opt/voc/bin/voc
|
||||
|
||||
all:
|
||||
$(VOC) -m IfElse.Mod
|
||||
|
||||
clean:
|
||||
rm *.c
|
||||
rm *.h
|
||||
rm *.o
|
||||
rm *.sym
|
||||
10
examples/procedures/function-procedure/Makefile
Normal file
10
examples/procedures/function-procedure/Makefile
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
VOC = /opt/voc/bin/voc
|
||||
|
||||
all:
|
||||
$(VOC) -m Square.Mod
|
||||
|
||||
clean:
|
||||
rm *.c
|
||||
rm *.h
|
||||
rm *.o
|
||||
rm *.sym
|
||||
20
examples/procedures/function-procedure/Square.Mod
Normal file
20
examples/procedures/function-procedure/Square.Mod
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
MODULE square;
|
||||
|
||||
|
||||
IMPORT Out;
|
||||
|
||||
|
||||
VAR s : INTEGER;
|
||||
|
||||
|
||||
PROCEDURE squared(x : INTEGER): INTEGER;
|
||||
BEGIN
|
||||
RETURN x * x
|
||||
END squared;
|
||||
|
||||
|
||||
BEGIN
|
||||
s := squared(7);
|
||||
Out.Int(s, 0); Out.Ln;
|
||||
Out.Int(squared(8), 0); Out.Ln;
|
||||
END square.
|
||||
20
examples/procedures/function-procedure/Square.Mod.ob2
Normal file
20
examples/procedures/function-procedure/Square.Mod.ob2
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
MODULE square;
|
||||
|
||||
|
||||
IMPORT Out;
|
||||
|
||||
|
||||
VAR s : INTEGER;
|
||||
|
||||
|
||||
PROCEDURE squared(x : INTEGER): INTEGER;
|
||||
BEGIN
|
||||
RETURN x * x
|
||||
END squared;
|
||||
|
||||
|
||||
BEGIN
|
||||
s := squared(7);
|
||||
Out.Int(s, 0); Out.Ln;
|
||||
Out.Int(squared(8), 0); Out.Ln;
|
||||
END square.
|
||||
10
examples/procedures/procedure/Makefile
Normal file
10
examples/procedures/procedure/Makefile
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
VOC = /opt/voc/bin/voc
|
||||
|
||||
all:
|
||||
$(VOC) -m Procedure.Mod
|
||||
|
||||
clean:
|
||||
rm *.c
|
||||
rm *.h
|
||||
rm *.o
|
||||
rm *.sym
|
||||
15
examples/procedures/procedure/Procedure.Mod
Normal file
15
examples/procedures/procedure/Procedure.Mod
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
MODULE proc;
|
||||
|
||||
|
||||
IMPORT Out;
|
||||
|
||||
|
||||
PROCEDURE printSum(a, b : INTEGER);
|
||||
BEGIN
|
||||
Out.Int(a + b, 0); Out.Ln
|
||||
END printSum;
|
||||
|
||||
|
||||
BEGIN
|
||||
printSum(6, 9)
|
||||
END proc.
|
||||
15
examples/procedures/procedure/Procedure.Mod.ob2
Normal file
15
examples/procedures/procedure/Procedure.Mod.ob2
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
MODULE proc;
|
||||
|
||||
|
||||
IMPORT Out;
|
||||
|
||||
|
||||
PROCEDURE printSum(a, b : INTEGER);
|
||||
BEGIN
|
||||
Out.Int(a + b, 0); Out.Ln
|
||||
END printSum;
|
||||
|
||||
|
||||
BEGIN
|
||||
printSum(6, 9)
|
||||
END proc.
|
||||
10
examples/procedures/var-parameter/Makefile
Normal file
10
examples/procedures/var-parameter/Makefile
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
VOC = /opt/voc/bin/voc
|
||||
|
||||
all:
|
||||
$(VOC) -m VarParam.Mod
|
||||
|
||||
clean:
|
||||
rm *.c
|
||||
rm *.h
|
||||
rm *.o
|
||||
rm *.sym
|
||||
24
examples/procedures/var-parameter/VarParam.Mod
Normal file
24
examples/procedures/var-parameter/VarParam.Mod
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
MODULE varparam;
|
||||
|
||||
|
||||
IMPORT Out;
|
||||
|
||||
|
||||
VAR a, b : INTEGER;
|
||||
|
||||
|
||||
PROCEDURE swapVals(VAR x, y : INTEGER);
|
||||
VAR tmp : INTEGER;
|
||||
BEGIN
|
||||
tmp := x; x := y; y := tmp;
|
||||
END swapVals;
|
||||
|
||||
|
||||
BEGIN
|
||||
a := 6; b := 9;
|
||||
Out.String("initial "); Out.Ln;
|
||||
Out.String("a : "); Out.Int(a, 0); Out.String("; b : "); Out.Int(b, 0); Out.Ln;
|
||||
swapVals(a, b);
|
||||
Out.String("after swap"); Out.Ln;
|
||||
Out.String("a : "); Out.Int(a, 0); Out.String("; b : "); Out.Int(b, 0); Out.Ln;
|
||||
END varparam.
|
||||
24
examples/procedures/var-parameter/VarParam.Mod.ob2
Normal file
24
examples/procedures/var-parameter/VarParam.Mod.ob2
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
MODULE varparam;
|
||||
|
||||
|
||||
IMPORT Out;
|
||||
|
||||
|
||||
VAR a, b : INTEGER;
|
||||
|
||||
|
||||
PROCEDURE swapVals(VAR x, y : INTEGER);
|
||||
VAR tmp : INTEGER;
|
||||
BEGIN
|
||||
tmp := x; x := y; y := tmp;
|
||||
END swapVals;
|
||||
|
||||
|
||||
BEGIN
|
||||
a := 6; b := 9;
|
||||
Out.String("initial "); Out.Ln;
|
||||
Out.String("a : "); Out.Int(a, 0); Out.String("; b : "); Out.Int(b, 0); Out.Ln;
|
||||
swapVals(a, b);
|
||||
Out.String("after swap"); Out.Ln;
|
||||
Out.String("a : "); Out.Int(a, 0); Out.String("; b : "); Out.Int(b, 0); Out.Ln;
|
||||
END varparam.
|
||||
53
examples/records/Records.Mod
Normal file
53
examples/records/Records.Mod
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
MODULE record;
|
||||
|
||||
|
||||
IMPORT Out;
|
||||
|
||||
|
||||
CONST
|
||||
Male = 1;
|
||||
Female = 2;
|
||||
|
||||
|
||||
TYPE
|
||||
SexDesc = INTEGER;
|
||||
Person = RECORD
|
||||
Name : ARRAY 32 OF CHAR;
|
||||
Age : INTEGER;
|
||||
Title : ARRAY 64 OF CHAR;
|
||||
Sex : SexDesc;
|
||||
END;
|
||||
|
||||
|
||||
VAR
|
||||
i : INTEGER;
|
||||
employer : Person;
|
||||
employee : ARRAY 2 OF Person;
|
||||
|
||||
|
||||
PROCEDURE dumpPerson ( p : Person );
|
||||
BEGIN
|
||||
Out.String("Meet "); Out.String(p.Name);
|
||||
IF p.Sex = Male THEN Out.String(". He is ") END;
|
||||
IF p.Sex = Female THEN Out.String(". She is ") END;
|
||||
Out.Int(p.Age, 0); Out.String(" years old and a "); Out.String(p.Title); Out.Ln;
|
||||
END dumpPerson;
|
||||
|
||||
|
||||
|
||||
BEGIN
|
||||
(* define people *)
|
||||
employer.Name := "Bing"; employer.Age := 42; employer.Title := "CEO"; employer.Sex := Male;
|
||||
|
||||
employee[0].Name := "Bob"; employee[0].Age := 26;
|
||||
employee[0].Title := "SysAdmin"; employee[0].Sex := Male;
|
||||
|
||||
employee[1].Name := "Alice" ; employee[1].Age := 22;
|
||||
employee[1].Title := "Programmer"; employee[1].Sex := Female;
|
||||
|
||||
(* print people *)
|
||||
dumpPerson(employer);
|
||||
FOR i := 0 TO LEN(employee) - 1 DO
|
||||
dumpPerson(employee[i]);
|
||||
END;
|
||||
END record.
|
||||
53
examples/records/Records.Mod.ob2
Normal file
53
examples/records/Records.Mod.ob2
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
MODULE record;
|
||||
|
||||
|
||||
IMPORT Out;
|
||||
|
||||
|
||||
CONST
|
||||
Male = 1;
|
||||
Female = 2;
|
||||
|
||||
|
||||
TYPE
|
||||
SexDesc = INTEGER;
|
||||
Person = RECORD
|
||||
Name : ARRAY 32 OF CHAR;
|
||||
Age : INTEGER;
|
||||
Title : ARRAY 64 OF CHAR;
|
||||
Sex : SexDesc;
|
||||
END;
|
||||
|
||||
|
||||
VAR
|
||||
i : INTEGER;
|
||||
employer : Person;
|
||||
employee : ARRAY 2 OF Person;
|
||||
|
||||
|
||||
PROCEDURE dumpPerson ( p : Person );
|
||||
BEGIN
|
||||
Out.String("Meet "); Out.String(p.Name);
|
||||
IF p.Sex = Male THEN Out.String(". He is ") END;
|
||||
IF p.Sex = Female THEN Out.String(". She is ") END;
|
||||
Out.Int(p.Age, 0); Out.String(" years old and a "); Out.String(p.Title); Out.Ln;
|
||||
END dumpPerson;
|
||||
|
||||
|
||||
|
||||
BEGIN
|
||||
(* define people *)
|
||||
employer.Name := "Bing"; employer.Age := 42; employer.Title := "CEO"; employer.Sex := Male;
|
||||
|
||||
employee[0].Name := "Bob"; employee[0].Age := 26;
|
||||
employee[0].Title := "SysAdmin"; employee[0].Sex := Male;
|
||||
|
||||
employee[1].Name := "Alice" ; employee[1].Age := 22;
|
||||
employee[1].Title := "Programmer"; employee[1].Sex := Female;
|
||||
|
||||
(* print people *)
|
||||
dumpPerson(employer);
|
||||
FOR i := 0 TO LEN(employee) - 1 DO
|
||||
dumpPerson(employee[i]);
|
||||
END;
|
||||
END record.
|
||||
33
examples/recursion/Fib.Mod
Normal file
33
examples/recursion/Fib.Mod
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
MODULE fibonacci;
|
||||
|
||||
|
||||
IMPORT Out, Modules;
|
||||
|
||||
|
||||
VAR
|
||||
n : LONGINT;
|
||||
Arg0 : LONGINT;
|
||||
|
||||
|
||||
PROCEDURE getFib (n : LONGINT) : LONGINT;
|
||||
VAR result : LONGINT;
|
||||
BEGIN
|
||||
IF n = 0 THEN
|
||||
result := 0
|
||||
ELSIF n = 1 THEN
|
||||
result:= 1
|
||||
ELSE
|
||||
result := getFib(n-1) + getFib(n-2)
|
||||
END;
|
||||
RETURN result
|
||||
END getFib;
|
||||
|
||||
|
||||
BEGIN
|
||||
IF Modules.ArgCount # 2 THEN
|
||||
Out.String("one argument needed"); Out.Ln;
|
||||
HALT(1);
|
||||
END;
|
||||
Modules.GetIntArg(1, Arg0);
|
||||
Out.Int(getFib(Arg0), 0); Out.Ln;
|
||||
END fibonacci.
|
||||
33
examples/recursion/Fib.Mod.ob2
Normal file
33
examples/recursion/Fib.Mod.ob2
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
MODULE fibonacci;
|
||||
|
||||
|
||||
IMPORT Out, Modules;
|
||||
|
||||
|
||||
VAR
|
||||
n : LONGINT;
|
||||
Arg0 : LONGINT;
|
||||
|
||||
|
||||
PROCEDURE getFib (n : LONGINT) : LONGINT;
|
||||
VAR result : LONGINT;
|
||||
BEGIN
|
||||
IF n = 0 THEN
|
||||
result := 0
|
||||
ELSIF n = 1 THEN
|
||||
result:= 1
|
||||
ELSE
|
||||
result := getFib(n-1) + getFib(n-2)
|
||||
END;
|
||||
RETURN result
|
||||
END getFib;
|
||||
|
||||
|
||||
BEGIN
|
||||
IF Modules.ArgCount # 2 THEN
|
||||
Out.String("one argument needed"); Out.Ln;
|
||||
HALT(1);
|
||||
END;
|
||||
Modules.GetIntArg(1, Arg0);
|
||||
Out.Int(getFib(Arg0), 0); Out.Ln;
|
||||
END fibonacci.
|
||||
31
examples/recursion/Gcd.Mod
Normal file
31
examples/recursion/Gcd.Mod
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
MODULE gcd;
|
||||
|
||||
|
||||
IMPORT Out, Modules;
|
||||
|
||||
|
||||
VAR
|
||||
gcd : INTEGER;
|
||||
arg0, arg1 : LONGINT;
|
||||
|
||||
|
||||
PROCEDURE getGCD(a, b : INTEGER): INTEGER;
|
||||
VAR ret : INTEGER;
|
||||
BEGIN
|
||||
IF a = 0 THEN ret := b;
|
||||
ELSIF b = 0 THEN ret := a;
|
||||
ELSIF b > a THEN ret := getGCD(b, a);
|
||||
ELSE ret := getGCD(b, a MOD b) END;
|
||||
RETURN ret;
|
||||
END getGCD;
|
||||
|
||||
|
||||
BEGIN
|
||||
IF Modules.ArgCount # 3 THEN
|
||||
Out.String("enter two integers to get GCD"); Out.Ln;
|
||||
HALT(1)
|
||||
END;
|
||||
Modules.GetIntArg(1, arg0); Modules.GetIntArg(2, arg1);
|
||||
gcd := getGCD(SHORT(arg0), SHORT(arg1));
|
||||
Out.Int(gcd, 0); Out.Ln;
|
||||
END gcd.
|
||||
31
examples/recursion/Gcd.Mod.ob2
Normal file
31
examples/recursion/Gcd.Mod.ob2
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
MODULE gcd;
|
||||
|
||||
|
||||
IMPORT Out, Modules;
|
||||
|
||||
|
||||
VAR
|
||||
gcd : INTEGER;
|
||||
arg0, arg1 : LONGINT;
|
||||
|
||||
|
||||
PROCEDURE getGCD(a, b : INTEGER): INTEGER;
|
||||
VAR ret : INTEGER;
|
||||
BEGIN
|
||||
IF a = 0 THEN ret := b;
|
||||
ELSIF b = 0 THEN ret := a;
|
||||
ELSIF b > a THEN ret := getGCD(b, a);
|
||||
ELSE ret := getGCD(b, a MOD b) END;
|
||||
RETURN ret;
|
||||
END getGCD;
|
||||
|
||||
|
||||
BEGIN
|
||||
IF Modules.ArgCount # 3 THEN
|
||||
Out.String("enter two integers to get GCD"); Out.Ln;
|
||||
HALT(1)
|
||||
END;
|
||||
Modules.GetIntArg(1, arg0); Modules.GetIntArg(2, arg1);
|
||||
gcd := getGCD(SHORT(arg0), SHORT(arg1));
|
||||
Out.Int(gcd, 0); Out.Ln;
|
||||
END gcd.
|
||||
10
examples/value-types/Makefile
Normal file
10
examples/value-types/Makefile
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
VOC = /opt/voc/bin/voc
|
||||
|
||||
all:
|
||||
$(VOC) -m Values.Mod
|
||||
|
||||
clean:
|
||||
rm *.c
|
||||
rm *.h
|
||||
rm *.o
|
||||
rm *.sym
|
||||
12
examples/value-types/Values.Mod
Normal file
12
examples/value-types/Values.Mod
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
MODULE values;
|
||||
|
||||
|
||||
IMPORT Out;
|
||||
|
||||
|
||||
BEGIN
|
||||
Out.String("Oberon has types, for example, I am a string type (ARRAY OF CHAR);"); Out.Ln;
|
||||
Out.String("There are also other types, e.g. INTEGERs and BOOLEANs"); Out.Ln;
|
||||
Out.Int(42, 0);
|
||||
Out.Ln
|
||||
END values.
|
||||
12
examples/value-types/Values.Mod.ob2
Normal file
12
examples/value-types/Values.Mod.ob2
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
MODULE values;
|
||||
|
||||
|
||||
IMPORT Out;
|
||||
|
||||
|
||||
BEGIN
|
||||
Out.String("Oberon has types, for example, I am a string type (ARRAY OF CHAR);"); Out.Ln;
|
||||
Out.String("There are also other types, e.g. INTEGERs and BOOLEANs"); Out.Ln;
|
||||
Out.Int(42, 0);
|
||||
Out.Ln
|
||||
END values.
|
||||
10
examples/variables/Makefile
Normal file
10
examples/variables/Makefile
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
VOC = /opt/voc/bin/voc
|
||||
|
||||
all:
|
||||
$(VOC) -m Variables.Mod
|
||||
|
||||
clean:
|
||||
rm *.c
|
||||
rm *.h
|
||||
rm *.o
|
||||
rm *.sym
|
||||
31
examples/variables/Variables.Mod
Normal file
31
examples/variables/Variables.Mod
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
MODULE variables;
|
||||
|
||||
IMPORT Out;
|
||||
|
||||
|
||||
VAR
|
||||
s : ARRAY 32 OF CHAR;
|
||||
i : REAL;
|
||||
n, m : INTEGER;
|
||||
|
||||
|
||||
BEGIN
|
||||
s := "Initial";
|
||||
Out.String(s); Out.Ln;
|
||||
i := 3.14;
|
||||
n := 64;
|
||||
m := 42;
|
||||
Out.Int(m, 0); Out.Ln;
|
||||
Out.Int(n, 0); Out.Ln;
|
||||
Out.Real(i, 0); Out.Ln;
|
||||
|
||||
s := "assigning new values";
|
||||
Out.String(s); Out.Ln;
|
||||
i := 2.71;
|
||||
n := 128;
|
||||
m := 84;
|
||||
Out.Int(m, 0); Out.Ln;
|
||||
Out.Int(n, 0); Out.Ln;
|
||||
Out.Real(i, 0);
|
||||
Out.Ln
|
||||
END variables.
|
||||
31
examples/variables/Variables.Mod.ob2
Normal file
31
examples/variables/Variables.Mod.ob2
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
MODULE variables;
|
||||
|
||||
IMPORT Out;
|
||||
|
||||
|
||||
VAR
|
||||
s : ARRAY 32 OF CHAR;
|
||||
i : REAL;
|
||||
n, m : INTEGER;
|
||||
|
||||
|
||||
BEGIN
|
||||
s := "Initial";
|
||||
Out.String(s); Out.Ln;
|
||||
i := 3.14;
|
||||
n := 64;
|
||||
m := 42;
|
||||
Out.Int(m, 0); Out.Ln;
|
||||
Out.Int(n, 0); Out.Ln;
|
||||
Out.Real(i, 0); Out.Ln;
|
||||
|
||||
s := "assigning new values";
|
||||
Out.String(s); Out.Ln;
|
||||
i := 2.71;
|
||||
n := 128;
|
||||
m := 84;
|
||||
Out.Int(m, 0); Out.Ln;
|
||||
Out.Int(n, 0); Out.Ln;
|
||||
Out.Real(i, 0);
|
||||
Out.Ln
|
||||
END variables.
|
||||
10
examples/while/Makefile
Normal file
10
examples/while/Makefile
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
VOC = /opt/voc/bin/voc
|
||||
|
||||
all:
|
||||
$(VOC) -m While.Mod
|
||||
|
||||
clean:
|
||||
rm *.c
|
||||
rm *.h
|
||||
rm *.o
|
||||
rm *.sym
|
||||
17
examples/while/While.Mod
Normal file
17
examples/while/While.Mod
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
MODULE while;
|
||||
|
||||
IMPORT Out;
|
||||
|
||||
|
||||
VAR
|
||||
i : INTEGER;
|
||||
|
||||
|
||||
BEGIN
|
||||
i := 0;
|
||||
Out.String("WHILE loop started"); Out.Ln;
|
||||
WHILE i < 10 DO
|
||||
i := i + 1;
|
||||
Out.Int(i, 0); Out.Ln;
|
||||
END;
|
||||
END while.
|
||||
17
examples/while/While.Mod.ob2
Normal file
17
examples/while/While.Mod.ob2
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
MODULE while;
|
||||
|
||||
IMPORT Out;
|
||||
|
||||
|
||||
VAR
|
||||
i : INTEGER;
|
||||
|
||||
|
||||
BEGIN
|
||||
i := 0;
|
||||
Out.String("WHILE loop started"); Out.Ln;
|
||||
WHILE i < 10 DO
|
||||
i := i + 1;
|
||||
Out.Int(i, 0); Out.Ln;
|
||||
END;
|
||||
END while.
|
||||
Loading…
Add table
Add a link
Reference in a new issue