[Oberon] FPGA - Simple OOP example
Jan de Kruyf
jan.de.kruyf at gmail.com
Tue Aug 14 16:43:59 CEST 2018
Thomas
I am no great specialist anymore on oberon, but as a general rule you could
have the Term module totally abstract and let the extensions define the
write procedure.
So then you have only 1 VAR t*: Term in the abstact Module; and assign the
needed PROCEDURE Write( ) to it from the extension module.
But in your example you might hit other issues. i.e. it is not an ideal
example.
Also read chapter 12 in "Programming in Oberon" Reiser and Wirth. (The
green book)
It has a beautiful example of a drawing program.
Cheers,
j.
On Tue, Aug 14, 2018 at 1:51 PM, Tomas Kral <thomas.kral at email.cz> wrote:
> Hi,
>
> Just reading Oberon book on OOP by Hanspeter Mosenbok. I would like
> to set a reusable code pattern for that, not sure how close I am, my
> try below. There is also other way of doing it, using so called
> `message' handler.
>
> Cheers Tomas
>
> MODULE Term; (* TK 14.8.2018 lower/capital case terminal, simple OOP
> example *)
> IMPORT Texts, Oberon;
>
> TYPE
> Term* = POINTER TO TermDesc;
> TermDesc* = RECORD
> write*: PROCEDURE(t: Term; ch: CHAR)
> END ;
>
> VAR
> W: Texts.Writer; t*: Term;
>
> (* Generic output routine accepting any Term based type *)
>
> PROCEDURE Out*(t: Term; ch: CHAR);
> BEGIN t.write(t, ch)
> END Out;
>
> PROCEDURE Write(t: Term; ch: CHAR);
> BEGIN
> Texts.Write(W, ch); Texts.Append(Oberon.Log, W.buf)
> END Write;
>
> BEGIN Texts.OpenWriter(W); NEW(t); t.write := Write;
> END Term.
>
>
> MODULE CapTerm; (* capital terminal *)
> IMPORT Term, Texts, Oberon;
>
> TYPE
> CapTerm* = POINTER TO CapTermDesc;
> CapTermDesc* = RECORD (Term.TermDesc) END ;
>
> VAR
> W: Texts.Writer; ct*: CapTerm;
>
> PROCEDURE CAP(ch: CHAR): CHAR;
> VAR up: CHAR;
> BEGIN
> up := CHR(ORD(ch) + ORD("A") - ORD("a"));
> RETURN up END CAP;
>
> PROCEDURE Write(t: Term.Term; ch: CHAR);
> BEGIN
> IF (ch >= "a") OR (ch <= "z") THEN ch := CAP(ch) END ;
> Texts.Write(W, ch); Texts.Append(Oberon.Log, W.buf)
> END Write;
>
> BEGIN Texts.OpenWriter(W); NEW(ct); ct.write := Write;
> END CapTerm.
>
>
> MODULE TestTerm;
> IMPORT Term, CapTerm, Texts, Oberon;
>
> VAR
> W: Texts.Writer; t: Term.Term; ct: CapTerm.CapTerm;
>
> PROCEDURE Run*;
> VAR S: Texts.Scanner;
> BEGIN
> Texts.OpenScanner(S, Oberon.Par.text, Oberon.Par.pos);
> Texts.Scan(S);
> WHILE S.class = Texts.Name DO
> Term.Out(t, S.s[0]); Term.Out(ct, S.s[0]);
> Texts.Scan(S)
> END ;
> Texts.WriteLn(W); Texts.Append(Oberon.Log, W.buf)
> END Run;
>
> BEGIN Texts.OpenWriter(W); t := Term.t; ct := CapTerm.ct;
> END TestTerm.
>
> TestTerm.Run a b c ~
>
>
>
> --
> Tomas Kral <thomas.kral at email.cz>
> --
> Oberon at lists.inf.ethz.ch mailing list for ETH Oberon and related systems
> https://lists.inf.ethz.ch/mailman/listinfo/oberon
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.inf.ethz.ch/pipermail/oberon/attachments/20180814/00be0db4/attachment.html>
More information about the Oberon
mailing list