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
30
README.md
30
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.
|
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)
|
- [Hello World](https://github.com/vishapoberon/oberonbyexample/tree/master/examples/hello-world)
|
||||||
- [Types](https://github.com/vishaps/oberonbyexample/tree/master/value-types)
|
- [Types](https://github.com/vishapoberon/oberonbyexample/tree/master/examples/value-types)
|
||||||
- [Variables](https://github.com/vishaps/oberonbyexample/tree/master/variables)
|
- [Variables](https://github.com/vishapoberon/oberonbyexample/tree/master/examples/variables)
|
||||||
- [Constants](https://github.com/vishaps/oberonbyexample/tree/master/constants)
|
- [Constants](https://github.com/vishapoberon/oberonbyexample/tree/master/examples/constants)
|
||||||
- [For](https://github.com/vishaps/oberonbyexample/tree/master/for)
|
- [For](https://github.com/vishapoberon/oberonbyexample/tree/master/examples/for)
|
||||||
- [While](https://github.com/vishaps/oberonbyexample/tree/master/while)
|
- [While](https://github.com/vishapoberon/oberonbyexample/tree/master/examples/while)
|
||||||
- [If/Else](https://github.com/vishaps/oberonbyexample/tree/master/ifelse)
|
- [If/Else](https://github.com/vishapoberon/oberonbyexample/tree/master/examples/ifelse)
|
||||||
- [Case](https://github.com/vishaps/oberonbyexample/tree/master/case)
|
- [Case](https://github.com/vishapoberon/oberonbyexample/tree/master/examples/case)
|
||||||
- [Arrays](https://github.com/vishaps/oberonbyexample/tree/master/arrays)
|
- [Arrays](https://github.com/vishapoberon/oberonbyexample/tree/master/examples/arrays)
|
||||||
- [Procedures](https://github.com/vishaps/oberonbyexample/tree/master/procedures)
|
- [Procedures](https://github.com/vishapoberon/oberonbyexample/tree/master/examples/procedures)
|
||||||
- [Procedure](https://github.com/vishaps/oberonbyexample/tree/master/procedures/procedure)
|
- [Procedure](https://github.com/vishapoberon/oberonbyexample/tree/master/examples/procedures/procedure)
|
||||||
- [Functions](https://github.com/vishaps/oberonbyexample/tree/master/procedures/function-procedure)
|
- [Functions](https://github.com/vishapoberon/oberonbyexample/tree/master/examples/procedures/function-procedure)
|
||||||
- [VAR-Parameters](https://github.com/vishaps/oberonbyexample/tree/master/procedures/var-parameter)
|
- [VAR-Parameters](https://github.com/vishapoberon/oberonbyexample/tree/master/examples/procedures/var-parameter)
|
||||||
- TODO
|
- TODO
|
||||||
|
|
|
||||||
BIN
build/obe
Executable file
BIN
build/obe
Executable file
Binary file not shown.
44
build/obe.c
Normal file
44
build/obe.c
Normal file
|
|
@ -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;
|
||||||
|
}
|
||||||
20
build/obe.dSYM/Contents/Info.plist
Normal file
20
build/obe.dSYM/Contents/Info.plist
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||||
|
<plist version="1.0">
|
||||||
|
<dict>
|
||||||
|
<key>CFBundleDevelopmentRegion</key>
|
||||||
|
<string>English</string>
|
||||||
|
<key>CFBundleIdentifier</key>
|
||||||
|
<string>com.apple.xcode.dsym.obe</string>
|
||||||
|
<key>CFBundleInfoDictionaryVersion</key>
|
||||||
|
<string>6.0</string>
|
||||||
|
<key>CFBundlePackageType</key>
|
||||||
|
<string>dSYM</string>
|
||||||
|
<key>CFBundleSignature</key>
|
||||||
|
<string>????</string>
|
||||||
|
<key>CFBundleShortVersionString</key>
|
||||||
|
<string>1.0</string>
|
||||||
|
<key>CFBundleVersion</key>
|
||||||
|
<string>1</string>
|
||||||
|
</dict>
|
||||||
|
</plist>
|
||||||
BIN
build/obe.dSYM/Contents/Resources/DWARF/obe
Normal file
BIN
build/obe.dSYM/Contents/Resources/DWARF/obe
Normal file
Binary file not shown.
|
|
@ -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 }
|
||||||
|
...
|
||||||
|
|
@ -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.
|
||||||
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.
|
||||||
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.
|
||||||
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.
|
||||||
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.
|
||||||
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.
|
||||||
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.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/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.
|
||||||
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.
|
||||||
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.
|
||||||
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.
|
||||||
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.
|
||||||
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.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.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.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.
|
||||||
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.
|
||||||
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.
|
||||||
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.
|
||||||
26
src/obe.Mod
Normal file
26
src/obe.Mod
Normal file
|
|
@ -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.
|
||||||
Loading…
Add table
Add a link
Reference in a new issue