mirror of
https://github.com/vishapoberon/oberonbyexample.git
synced 2026-04-05 21:02:25 +00:00
Merge branch 'mv'
This commit is contained in:
commit
840a24f3a7
6 changed files with 217 additions and 0 deletions
11
arguments/unixstyle_oberon_traditional/makefile
Normal file
11
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
arguments/unixstyle_oberon_traditional/partest.Mod
Normal file
63
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.
|
||||
47
arguments/unixstyle_oberon_traditional/readme.md
Normal file
47
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.
|
||||
68
enums_example/Days.Mod
Normal file
68
enums_example/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.
|
||||
6
enums_example/readme.md
Normal file
6
enums_example/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
enums_example/test.Mod
Normal file
22
enums_example/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.
|
||||
Loading…
Add table
Add a link
Reference in a new issue