[Oberon] FPGA - Simple OOP example

Tomas Kral thomas.kral at email.cz
Tue Aug 14 13:51:55 CEST 2018


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>


More information about the Oberon mailing list