diff --git a/README.md b/README.md index a51173b..5d316d3 100644 --- a/README.md +++ b/README.md @@ -8,21 +8,21 @@ Oberon By Example is a hands-on introduction to Oberon-2 using examples. This repository is under heavy modification to make it better for beginners. -To start, make sure you have [Vishap Oberon Compiler](https://github.com/vishaps/voc) +To start, make sure you have [Vishap Oberon Compiler](https://github.com/vishapoberon/voc) -Check out the [hello-world](https://github.com/vishaps/oberonbyexample/tree/master/hello-world) example or browse the full list below. +Check out the [hello-world](https://github.com/vishapoberon/oberonbyexample/tree/master/examples/hello-world) example or browse the full list below. -- [Hello World](https://github.com/vishaps/oberonbyexample/tree/master/hello-world) -- [Types](https://github.com/vishaps/oberonbyexample/tree/master/value-types) -- [Variables](https://github.com/vishaps/oberonbyexample/tree/master/variables) -- [Constants](https://github.com/vishaps/oberonbyexample/tree/master/constants) -- [For](https://github.com/vishaps/oberonbyexample/tree/master/for) -- [While](https://github.com/vishaps/oberonbyexample/tree/master/while) -- [If/Else](https://github.com/vishaps/oberonbyexample/tree/master/ifelse) -- [Case](https://github.com/vishaps/oberonbyexample/tree/master/case) -- [Arrays](https://github.com/vishaps/oberonbyexample/tree/master/arrays) -- [Procedures](https://github.com/vishaps/oberonbyexample/tree/master/procedures) - - [Procedure](https://github.com/vishaps/oberonbyexample/tree/master/procedures/procedure) - - [Functions](https://github.com/vishaps/oberonbyexample/tree/master/procedures/function-procedure) - - [VAR-Parameters](https://github.com/vishaps/oberonbyexample/tree/master/procedures/var-parameter) +- [Hello World](https://github.com/vishapoberon/oberonbyexample/tree/master/examples/hello-world) +- [Types](https://github.com/vishapoberon/oberonbyexample/tree/master/examples/value-types) +- [Variables](https://github.com/vishapoberon/oberonbyexample/tree/master/examples/variables) +- [Constants](https://github.com/vishapoberon/oberonbyexample/tree/master/examples/constants) +- [For](https://github.com/vishapoberon/oberonbyexample/tree/master/examples/for) +- [While](https://github.com/vishapoberon/oberonbyexample/tree/master/examples/while) +- [If/Else](https://github.com/vishapoberon/oberonbyexample/tree/master/examples/ifelse) +- [Case](https://github.com/vishapoberon/oberonbyexample/tree/master/examples/case) +- [Arrays](https://github.com/vishapoberon/oberonbyexample/tree/master/examples/arrays) +- [Procedures](https://github.com/vishapoberon/oberonbyexample/tree/master/examples/procedures) + - [Procedure](https://github.com/vishapoberon/oberonbyexample/tree/master/examples/procedures/procedure) + - [Functions](https://github.com/vishapoberon/oberonbyexample/tree/master/examples/procedures/function-procedure) + - [VAR-Parameters](https://github.com/vishapoberon/oberonbyexample/tree/master/examples/procedures/var-parameter) - TODO diff --git a/build/obe b/build/obe new file mode 100755 index 0000000..bcc41eb Binary files /dev/null and b/build/obe differ diff --git a/build/obe.c b/build/obe.c new file mode 100644 index 0000000..d604519 --- /dev/null +++ b/build/obe.c @@ -0,0 +1,44 @@ +/* voc 2.1.0 [2022/05/16] for clang LP64 on darwin xtpam */ + +#define SHORTINT INT8 +#define INTEGER INT16 +#define LONGINT INT32 +#define SET UINT32 + +#include "SYSTEM.h" +#include "Files.h" +#include "Out.h" +#include "Platform.h" + + + + +static void obe_CheckDirectories (void); +static void obe_GenIndex (void); + + +static void obe_CheckDirectories (void) +{ + Files_File fd = NIL; + fd = Files_New((CHAR*)"public", 7); +} + +static void obe_GenIndex (void) +{ + Files_File outputfd = NIL, templfd = NIL, examplesfd = NIL; + Files_Rider outputrd, templrd, examplesrd; +} + + +export int main(int argc, char **argv) +{ + __INIT(argc, argv); + __MODULE_IMPORT(Files); + __MODULE_IMPORT(Out); + __MODULE_IMPORT(Platform); + __REGMAIN("obe", 0); +/* BEGIN */ + obe_CheckDirectories(); + obe_GenIndex(); + __FINI; +} diff --git a/build/obe.dSYM/Contents/Info.plist b/build/obe.dSYM/Contents/Info.plist new file mode 100644 index 0000000..204e264 --- /dev/null +++ b/build/obe.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleIdentifier + com.apple.xcode.dsym.obe + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + dSYM + CFBundleSignature + ???? + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/build/obe.dSYM/Contents/Resources/DWARF/obe b/build/obe.dSYM/Contents/Resources/DWARF/obe new file mode 100644 index 0000000..456f4bd Binary files /dev/null and b/build/obe.dSYM/Contents/Resources/DWARF/obe differ diff --git a/build/obe.dSYM/Contents/Resources/Relocations/aarch64/obe.yml b/build/obe.dSYM/Contents/Resources/Relocations/aarch64/obe.yml new file mode 100644 index 0000000..ada74a5 --- /dev/null +++ b/build/obe.dSYM/Contents/Resources/Relocations/aarch64/obe.yml @@ -0,0 +1,10 @@ +--- +triple: 'arm64-apple-darwin' +binary-path: obe +relocations: + - { offsetInCU: 0x26, offset: 0x26, size: 0x8, addend: 0x0, symName: _main, symObjAddr: 0x0, symBinAddr: 0x100003E2C, symSize: 0x7C } + - { offsetInCU: 0x33, offset: 0x33, size: 0x8, addend: 0x0, symName: _main, symObjAddr: 0x0, symBinAddr: 0x100003E2C, symSize: 0x7C } + - { offsetInCU: 0x58, offset: 0x58, size: 0x8, addend: 0x0, symName: _main.m, symObjAddr: 0xA00, symBinAddr: 0x100008000, symSize: 0x0 } + - { offsetInCU: 0xA6, offset: 0xA6, size: 0x8, addend: 0x0, symName: _obe_CheckDirectories, symObjAddr: 0x7C, symBinAddr: 0x100003EA8, symSize: 0x30 } + - { offsetInCU: 0xCA, offset: 0xCA, size: 0x8, addend: 0x0, symName: _obe_GenIndex, symObjAddr: 0xAC, symBinAddr: 0x100003ED8, symSize: 0x58 } +... diff --git a/arguments/unixstyle_oberon_traditional/makefile b/examples/arguments/unixstyle_oberon_traditional/makefile similarity index 100% rename from arguments/unixstyle_oberon_traditional/makefile rename to examples/arguments/unixstyle_oberon_traditional/makefile diff --git a/arguments/unixstyle_oberon_traditional/partest.Mod b/examples/arguments/unixstyle_oberon_traditional/partest.Mod similarity index 100% rename from arguments/unixstyle_oberon_traditional/partest.Mod rename to examples/arguments/unixstyle_oberon_traditional/partest.Mod diff --git a/examples/arguments/unixstyle_oberon_traditional/partest.Mod.ob2 b/examples/arguments/unixstyle_oberon_traditional/partest.Mod.ob2 new file mode 100644 index 0000000..b15b545 --- /dev/null +++ b/examples/arguments/unixstyle_oberon_traditional/partest.Mod.ob2 @@ -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. diff --git a/arguments/unixstyle_oberon_traditional/readme.md b/examples/arguments/unixstyle_oberon_traditional/readme.md similarity index 100% rename from arguments/unixstyle_oberon_traditional/readme.md rename to examples/arguments/unixstyle_oberon_traditional/readme.md diff --git a/arrays/Arrays.Mod b/examples/arrays/Arrays.Mod similarity index 100% rename from arrays/Arrays.Mod rename to examples/arrays/Arrays.Mod diff --git a/examples/arrays/Arrays.Mod.ob2 b/examples/arrays/Arrays.Mod.ob2 new file mode 100644 index 0000000..50ef185 --- /dev/null +++ b/examples/arrays/Arrays.Mod.ob2 @@ -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. diff --git a/arrays/Makefile b/examples/arrays/Makefile similarity index 100% rename from arrays/Makefile rename to examples/arrays/Makefile diff --git a/case/Case.Mod b/examples/case/Case.Mod similarity index 100% rename from case/Case.Mod rename to examples/case/Case.Mod diff --git a/examples/case/Case.Mod.ob2 b/examples/case/Case.Mod.ob2 new file mode 100644 index 0000000..ab6ad5b --- /dev/null +++ b/examples/case/Case.Mod.ob2 @@ -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. diff --git a/case/Makefile b/examples/case/Makefile similarity index 100% rename from case/Makefile rename to examples/case/Makefile diff --git a/constants/Constants.Mod b/examples/constants/Constants.Mod similarity index 100% rename from constants/Constants.Mod rename to examples/constants/Constants.Mod diff --git a/examples/constants/Constants.Mod.ob2 b/examples/constants/Constants.Mod.ob2 new file mode 100644 index 0000000..47dc4c4 --- /dev/null +++ b/examples/constants/Constants.Mod.ob2 @@ -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. diff --git a/constants/Makefile b/examples/constants/Makefile similarity index 100% rename from constants/Makefile rename to examples/constants/Makefile diff --git a/enums_example/0/Days.Mod b/examples/enums_example/0/Days.Mod similarity index 100% rename from enums_example/0/Days.Mod rename to examples/enums_example/0/Days.Mod diff --git a/examples/enums_example/0/Days.Mod.ob2 b/examples/enums_example/0/Days.Mod.ob2 new file mode 100644 index 0000000..8611a21 --- /dev/null +++ b/examples/enums_example/0/Days.Mod.ob2 @@ -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. diff --git a/enums_example/0/readme.md b/examples/enums_example/0/readme.md similarity index 100% rename from enums_example/0/readme.md rename to examples/enums_example/0/readme.md diff --git a/enums_example/0/test.Mod b/examples/enums_example/0/test.Mod similarity index 100% rename from enums_example/0/test.Mod rename to examples/enums_example/0/test.Mod diff --git a/enums_example/1/test.Mod b/examples/enums_example/0/test.Mod.ob2 similarity index 100% rename from enums_example/1/test.Mod rename to examples/enums_example/0/test.Mod.ob2 diff --git a/enums_example/1/Days.Mod b/examples/enums_example/1/Days.Mod similarity index 100% rename from enums_example/1/Days.Mod rename to examples/enums_example/1/Days.Mod diff --git a/examples/enums_example/1/Days.Mod.ob2 b/examples/enums_example/1/Days.Mod.ob2 new file mode 100644 index 0000000..a8ab1ad --- /dev/null +++ b/examples/enums_example/1/Days.Mod.ob2 @@ -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. diff --git a/enums_example/1/readme.md b/examples/enums_example/1/readme.md similarity index 100% rename from enums_example/1/readme.md rename to examples/enums_example/1/readme.md diff --git a/examples/enums_example/1/test.Mod b/examples/enums_example/1/test.Mod new file mode 100644 index 0000000..8d49b34 --- /dev/null +++ b/examples/enums_example/1/test.Mod @@ -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. diff --git a/examples/enums_example/1/test.Mod.ob2 b/examples/enums_example/1/test.Mod.ob2 new file mode 100644 index 0000000..8d49b34 --- /dev/null +++ b/examples/enums_example/1/test.Mod.ob2 @@ -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. diff --git a/for/For.Mod b/examples/for/For.Mod similarity index 100% rename from for/For.Mod rename to examples/for/For.Mod diff --git a/examples/for/For.Mod.ob2 b/examples/for/For.Mod.ob2 new file mode 100644 index 0000000..8e110bd --- /dev/null +++ b/examples/for/For.Mod.ob2 @@ -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. diff --git a/for/Makefile b/examples/for/Makefile similarity index 100% rename from for/Makefile rename to examples/for/Makefile diff --git a/hello-world/Console/Hello.Mod b/examples/hello-world/Console/Hello.Mod similarity index 100% rename from hello-world/Console/Hello.Mod rename to examples/hello-world/Console/Hello.Mod diff --git a/examples/hello-world/Console/Hello.Mod.ob2 b/examples/hello-world/Console/Hello.Mod.ob2 new file mode 100644 index 0000000..479a6ea --- /dev/null +++ b/examples/hello-world/Console/Hello.Mod.ob2 @@ -0,0 +1,10 @@ +MODULE hello; + + +IMPORT Console; + + +BEGIN + Console.String("Hello World!"); + Console.Ln +END hello. diff --git a/hello-world/Console/Makefile b/examples/hello-world/Console/Makefile similarity index 100% rename from hello-world/Console/Makefile rename to examples/hello-world/Console/Makefile diff --git a/hello-world/Out/Hello.Mod b/examples/hello-world/Out/Hello.Mod similarity index 100% rename from hello-world/Out/Hello.Mod rename to examples/hello-world/Out/Hello.Mod diff --git a/examples/hello-world/Out/Hello.Mod.ob2 b/examples/hello-world/Out/Hello.Mod.ob2 new file mode 100644 index 0000000..80346a2 --- /dev/null +++ b/examples/hello-world/Out/Hello.Mod.ob2 @@ -0,0 +1,9 @@ +MODULE hello; + + +IMPORT Out; + + +BEGIN + Out.String("Hello, World"); Out.Ln +END hello. diff --git a/hello-world/Out/Makefile b/examples/hello-world/Out/Makefile similarity index 100% rename from hello-world/Out/Makefile rename to examples/hello-world/Out/Makefile diff --git a/ifelse/IfElse.Mod b/examples/ifelse/IfElse.Mod similarity index 100% rename from ifelse/IfElse.Mod rename to examples/ifelse/IfElse.Mod diff --git a/examples/ifelse/IfElse.Mod.ob2 b/examples/ifelse/IfElse.Mod.ob2 new file mode 100644 index 0000000..81ca1d7 --- /dev/null +++ b/examples/ifelse/IfElse.Mod.ob2 @@ -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. diff --git a/ifelse/Makefile b/examples/ifelse/Makefile similarity index 100% rename from ifelse/Makefile rename to examples/ifelse/Makefile diff --git a/procedures/function-procedure/Makefile b/examples/procedures/function-procedure/Makefile similarity index 100% rename from procedures/function-procedure/Makefile rename to examples/procedures/function-procedure/Makefile diff --git a/procedures/function-procedure/Square.Mod b/examples/procedures/function-procedure/Square.Mod similarity index 100% rename from procedures/function-procedure/Square.Mod rename to examples/procedures/function-procedure/Square.Mod diff --git a/examples/procedures/function-procedure/Square.Mod.ob2 b/examples/procedures/function-procedure/Square.Mod.ob2 new file mode 100644 index 0000000..f972edf --- /dev/null +++ b/examples/procedures/function-procedure/Square.Mod.ob2 @@ -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. diff --git a/procedures/procedure/Makefile b/examples/procedures/procedure/Makefile similarity index 100% rename from procedures/procedure/Makefile rename to examples/procedures/procedure/Makefile diff --git a/procedures/procedure/Procedure.Mod b/examples/procedures/procedure/Procedure.Mod similarity index 100% rename from procedures/procedure/Procedure.Mod rename to examples/procedures/procedure/Procedure.Mod diff --git a/examples/procedures/procedure/Procedure.Mod.ob2 b/examples/procedures/procedure/Procedure.Mod.ob2 new file mode 100644 index 0000000..c832008 --- /dev/null +++ b/examples/procedures/procedure/Procedure.Mod.ob2 @@ -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. diff --git a/procedures/var-parameter/Makefile b/examples/procedures/var-parameter/Makefile similarity index 100% rename from procedures/var-parameter/Makefile rename to examples/procedures/var-parameter/Makefile diff --git a/procedures/var-parameter/VarParam.Mod b/examples/procedures/var-parameter/VarParam.Mod similarity index 100% rename from procedures/var-parameter/VarParam.Mod rename to examples/procedures/var-parameter/VarParam.Mod diff --git a/examples/procedures/var-parameter/VarParam.Mod.ob2 b/examples/procedures/var-parameter/VarParam.Mod.ob2 new file mode 100644 index 0000000..19d1d8a --- /dev/null +++ b/examples/procedures/var-parameter/VarParam.Mod.ob2 @@ -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. diff --git a/records/Records.Mod b/examples/records/Records.Mod similarity index 100% rename from records/Records.Mod rename to examples/records/Records.Mod diff --git a/examples/records/Records.Mod.ob2 b/examples/records/Records.Mod.ob2 new file mode 100644 index 0000000..8dc22a2 --- /dev/null +++ b/examples/records/Records.Mod.ob2 @@ -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. diff --git a/recursion/Fib.Mod b/examples/recursion/Fib.Mod similarity index 100% rename from recursion/Fib.Mod rename to examples/recursion/Fib.Mod diff --git a/examples/recursion/Fib.Mod.ob2 b/examples/recursion/Fib.Mod.ob2 new file mode 100644 index 0000000..e5f5a27 --- /dev/null +++ b/examples/recursion/Fib.Mod.ob2 @@ -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. diff --git a/recursion/Gcd.Mod b/examples/recursion/Gcd.Mod similarity index 100% rename from recursion/Gcd.Mod rename to examples/recursion/Gcd.Mod diff --git a/examples/recursion/Gcd.Mod.ob2 b/examples/recursion/Gcd.Mod.ob2 new file mode 100644 index 0000000..2cc283d --- /dev/null +++ b/examples/recursion/Gcd.Mod.ob2 @@ -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. diff --git a/value-types/Makefile b/examples/value-types/Makefile similarity index 100% rename from value-types/Makefile rename to examples/value-types/Makefile diff --git a/value-types/Values.Mod b/examples/value-types/Values.Mod similarity index 100% rename from value-types/Values.Mod rename to examples/value-types/Values.Mod diff --git a/examples/value-types/Values.Mod.ob2 b/examples/value-types/Values.Mod.ob2 new file mode 100644 index 0000000..b8b976b --- /dev/null +++ b/examples/value-types/Values.Mod.ob2 @@ -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. diff --git a/variables/Makefile b/examples/variables/Makefile similarity index 100% rename from variables/Makefile rename to examples/variables/Makefile diff --git a/variables/Variables.Mod b/examples/variables/Variables.Mod similarity index 100% rename from variables/Variables.Mod rename to examples/variables/Variables.Mod diff --git a/examples/variables/Variables.Mod.ob2 b/examples/variables/Variables.Mod.ob2 new file mode 100644 index 0000000..3630f19 --- /dev/null +++ b/examples/variables/Variables.Mod.ob2 @@ -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. diff --git a/while/Makefile b/examples/while/Makefile similarity index 100% rename from while/Makefile rename to examples/while/Makefile diff --git a/while/While.Mod b/examples/while/While.Mod similarity index 100% rename from while/While.Mod rename to examples/while/While.Mod diff --git a/examples/while/While.Mod.ob2 b/examples/while/While.Mod.ob2 new file mode 100644 index 0000000..30946f2 --- /dev/null +++ b/examples/while/While.Mod.ob2 @@ -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. diff --git a/src/obe.Mod b/src/obe.Mod new file mode 100644 index 0000000..8026b9e --- /dev/null +++ b/src/obe.Mod @@ -0,0 +1,26 @@ +MODULE obe; + +IMPORT + Files, + Platform, + Out; + +PROCEDURE CheckDirectories; +BEGIN +END CheckDirectories; + +PROCEDURE GenIndex; +CONST + templpath = "../templates/index.html"; + outputpath = "../public/index.html"; + examplespath = "../examples.txt"; +VAR + outputfd, templfd, examplesfd: Files.File; + outputrd, templrd, examplesrd: Files.Rider; +BEGIN +END GenIndex; + +BEGIN + CheckDirectories; + GenIndex; +END obe.