Anyone for Object Oriented coding on the QL?

Anything QL Software or Programming Related.
User avatar
NormanDunbar
Forum Moderator
Posts: 1604
Joined: Tue Dec 14, 2010 9:04 am
Location: Leeds, West Yorkshire, UK
Contact:

Anyone for Object Oriented coding on the QL?

Postby NormanDunbar » Mon Apr 05, 2021 12:00 pm

Tony Tebby may well be upset, but we now have classes and Object Oriented programming on the QL.

This code is written in Free Pascal and cross compiled for the QL:

Code: Select all

{
  Program to demonstrate classes on the QL.

  This example blatently copied from:
  https://www.tutorialspoint.com/pascal/pascal_classes.htm

  A few minor amendments and extra comments by Norm.
}


{$mode objfpc}    // directive to be used for defining classes
{$m+}             // directive to be used for using constructor


{
  Define the class...
}
type
   Rectangle = class
   const
      minWidth = 2;
      minHeight = 2;

   private
      height, width: integer;
      procedure drawTopBottom;
      procedure drawMiddle;
   
   public
      constructor create(h, w: integer);
      procedure setheight(h: integer);
     
      function getheight(): integer;
      procedure setwidth(w: integer);
     
      function getwidth(): integer;
      procedure draw;
end;


var
   {
     r1 is a reference to a Rectangle class instance
   }
   r1: Rectangle;


{
  We need a constructor for the class. Create is "standard". Mostly!
}
constructor Rectangle.create(h, w: integer);
begin
   {
     We can just assign the passed values to height and
     width, but we would like to carry out some validation
     to keep them withing acceptable bounds.
   }
   setheight(h);
   setwidth(w);
end;

{
  We have a few functions to help set stuff.
}
procedure Rectangle.setheight(h: integer);
begin
   if (height < minHeight) then
      height := minHeight
   else
      height := h;
end;

procedure Rectangle.setwidth(w: integer);
begin
   if (width < minWidth) then
      width := minWidth
   else
      width := w;
end;

{
  We have a few functions to help get stuff.
}
function Rectangle.getheight(): integer;
begin
   getheight := height;
end;

function Rectangle.getwidth(): integer;
begin
   getwidth := width;
end;

{
  And a few procedures to draw a rectangle. I say "draw"....
}

procedure Rectangle.drawTopBottom;
var i: integer;
begin
   write('+');
   for i := 1 to width - 2 do
      write('-');
   writeln('+');
end;

procedure Rectangle.drawMiddle;
var i, j: integer;
begin
   for i := 1 to height - 2 do
   begin
      write('|');
      for j := 1 to width - 2 do
         write(' ');
      writeln('|');
   end;
end;

procedure Rectangle.draw;
begin
   { Top line. }
   drawTopBottom;

   { Middle of rectangle. }
   drawMiddle;

   { Bottom line. }
   drawTopBottom;
      
end;

{
  The actual program starts here.
  We create a new rectangle and assign it to r1, it gets its
  width and height from the constructor. After drawing it,
  change the width and height and draw the new rectangle.
}
begin
   {
     This will be adjusted to 2 by 2. Minimum sizes apply.
   }
   r1:= Rectangle.create(1, 1);
   writeln(' Draw Rectangle: ', r1.getheight(), ' by ' , r1.getwidth());
   r1.draw;

   r1.setheight(5);
   r1.setwidth(7);
   writeln(' Draw Rectangle: ', r1.getheight(), ' by ' , r1.getwidth());
   r1.draw;

   r1.setwidth(10);
   writeln(' Draw Rectangle: ', r1.getheight(), ' by ' , r1.getwidth());
   r1.draw;
end.


When it runs, you get this output in a 512 by 256 window:

Code: Select all

 Draw Rectangle: 2 by 2
++
++
 Draw Rectangle: 5 by 7
+-----+
|     |
|     |
|     |
+-----+
 Draw Rectangle: 5 by 10
+--------+
|        |
|        |
|        |
+--------+
Press any key to exit.


It's early days yet, obviously, there's a lot more work required to get this working for all QL functions and features, and then, hopefully, it can be used to compile itself and run natively on a QL.


Cheers,
Norm.


Why do they put lightning conductors on churches?
Author of Arduino Software Internals - https://www.amazon.co.uk/Arduino-Softwa ... 1484257898, https://www.apress.com/gb/book/9781484257890
User avatar
vanpeebles
Commissario Pebbli
Posts: 2613
Joined: Sat Nov 20, 2010 7:13 pm
Location: North East UK

Re: Anyone for Object Oriented coding on the QL?

Postby vanpeebles » Mon Apr 05, 2021 1:37 pm

Yuck! :lol:


User avatar
NormanDunbar
Forum Moderator
Posts: 1604
Joined: Tue Dec 14, 2010 9:04 am
Location: Leeds, West Yorkshire, UK
Contact:

Re: Anyone for Object Oriented coding on the QL?

Postby NormanDunbar » Mon Apr 05, 2021 1:38 pm

Har har har!


Why do they put lightning conductors on churches?
Author of Arduino Software Internals - https://www.amazon.co.uk/Arduino-Softwa ... 1484257898, https://www.apress.com/gb/book/9781484257890
Derek_Stewart
Font of All Knowledge
Posts: 2354
Joined: Mon Dec 20, 2010 11:40 am
Location: Runcorn, Cheshire, UK

Re: Anyone for Object Oriented coding on the QL?

Postby Derek_Stewart » Mon Apr 05, 2021 1:52 pm

HI,

I have never done any OOP programming, I am not sure where to start as a beginner....

I do not like Cross Compiling, as it makes me angry?


Regards,

Derek
User avatar
vanpeebles
Commissario Pebbli
Posts: 2613
Joined: Sat Nov 20, 2010 7:13 pm
Location: North East UK

Re: Anyone for Object Oriented coding on the QL?

Postby vanpeebles » Mon Apr 05, 2021 2:11 pm

OO, will make you even angrier :D


User avatar
NormanDunbar
Forum Moderator
Posts: 1604
Joined: Tue Dec 14, 2010 9:04 am
Location: Leeds, West Yorkshire, UK
Contact:

Re: Anyone for Object Oriented coding on the QL?

Postby NormanDunbar » Mon Apr 05, 2021 2:16 pm

Derek_Stewart wrote:I have never done any OOP programming, I am not sure where to start as a beginner....

That depends. Are you proficient in Pascal? C++? Something else? For the first there is the Free Pascal (cross) compiler, for the others, well, who knows!

Derek_Stewart wrote:I do not like Cross Compiling, as it makes me angry?

I much prefer native compiling myself, but at the moment, that isn't possible. If things work out, there might be a self hosted version of the Free Pascal compiler for the QL. It's a bit of a faff at present, but this is my working process when testing the Pascal stuff I've been doing:


  • Ssh to my development "server" -- It's a VM running Mint Linux 20, 64 bit. Cd to where the source code for the QL programs lives
  • Open a (local) terminal session and cd to my directory where I have DOS1_ mapped to, in QPC.
  • Open QPC.

So that's the faff I mentioned! I am using a VM to do the development work so that I can throw it away and start again, if I really mess up. It saves messing up my local system which I need to be safe for normal work.

I cross compile a QL program as follows:

  • Write the code! Using the ssh session.
  • Execute "fpc-ql -Tsinclairql -al program_name.pas"
  • In the local session, where dos1_ points, scp the compiled program_name.exe to the current directory.
  • Run a small program in the QL to copy from dos1_ to ram1_, xall xtcc to set the data space, and execute the program.

The QL program looks like this:

Code: Select all

110 exe$ = 'program_name'
120 dos$ = 'dos1_' & exe$ & '.exe'
130 ram$ = 'ram1_' & exe$
140 :
150 copy_o dos$ to ram$
160 ex xtcc_xtcc_bin, ram$
170 :
180 cls
190 ex ram$
200 stop


If I need to run it again, I can just "EX ram$" to do so.

Now, it could be simpler as I am using a VM to do the development work which keeps stuff separate from my main machine. So I have to ssh into the VM to do the compiling, then scp the compiled binary "back" to my local machine for use by the QL. However, with my terminal session open, I have everything in separate tabs, and it's easy. If I change the name of the program I'm testing, I only have one line in the SuperBASIC program to change and it's done.

But, I do agree that cross compiling is a right PITA from time to time. If you think this is bad, wait till you start doing development work on the RTL for the QL. :D


Cheers,
Norm.


Why do they put lightning conductors on churches?
Author of Arduino Software Internals - https://www.amazon.co.uk/Arduino-Softwa ... 1484257898, https://www.apress.com/gb/book/9781484257890
User avatar
NormanDunbar
Forum Moderator
Posts: 1604
Joined: Tue Dec 14, 2010 9:04 am
Location: Leeds, West Yorkshire, UK
Contact:

Re: Anyone for Object Oriented coding on the QL?

Postby NormanDunbar » Mon Apr 05, 2021 2:16 pm

vanpeebles wrote:OO, will make you even angrier :D


Everyone is entitled to an opinion, as long as it's mine! :D :D :D :D


Why do they put lightning conductors on churches?
Author of Arduino Software Internals - https://www.amazon.co.uk/Arduino-Softwa ... 1484257898, https://www.apress.com/gb/book/9781484257890
Derek_Stewart
Font of All Knowledge
Posts: 2354
Joined: Mon Dec 20, 2010 11:40 am
Location: Runcorn, Cheshire, UK

Re: Anyone for Object Oriented coding on the QL?

Postby Derek_Stewart » Mon Apr 05, 2021 4:29 pm

Hi Norm,

I can read C++ okay, I did a little Pascal in the past. But nothing serious.

Why not use Github for source code development, maybe Git client could be made for the QPC2 or internet aware QL based systems.

Derek_Stewart wrote:I do not like Cross Compiling, as it makes me angry?


This was meant as a joke, seriousily I have no problem cross compilation.


Regards,

Derek
User avatar
Sparrowhawk
Gold Card
Posts: 368
Joined: Wed Dec 15, 2010 12:33 pm
Location: @131072
Contact:

Re: Anyone for Object Oriented coding on the QL?

Postby Sparrowhawk » Mon Apr 05, 2021 7:46 pm

I'm a big fan of OOP, probably because the first OOP language I learnt was Smalltalk (on OS/2) which is so elegant.

My day job is currently writing oodles of oop with Python (and of course connecting to DBs with SQL). So yes, as a programming approach goes, I very much think in an OOP manner.

The main problem with OOP as I see it is that some people insist on massively extended class hierarchies instead of using loosely coupled object composition. Gives OOP a bad name.

Not sure it's right for system level programming (I'm no expert there), but for business apps, games etc, it's great.

Out of interest, what are the current limitations of FreePascal for the QL? Does user input work yet?


a.k.a. Jean-Yves !
User avatar
NormanDunbar
Forum Moderator
Posts: 1604
Joined: Tue Dec 14, 2010 9:04 am
Location: Leeds, West Yorkshire, UK
Contact:

Re: Anyone for Object Oriented coding on the QL?

Postby NormanDunbar » Mon Apr 05, 2021 8:29 pm

sparrowHawk wrote:Out of interest, what are the current limitations of FreePascal for the QL? Does user input work yet?


At the moment, it's working on thebasics. I've not tried readln() to any great effect, but every program ends with a call to it to "press any key to quit". So that's working.

There are a few of the internal traps working, creating and deleting dirs and files works, assign(), reset(), rewrite() and append() work. At least for text files. I'm working yhrough stuff as and when at the moment.

What did you have in mind?

Cheers,
Norm.


Why do they put lightning conductors on churches?
Author of Arduino Software Internals - https://www.amazon.co.uk/Arduino-Softwa ... 1484257898, https://www.apress.com/gb/book/9781484257890

Who is online

Users browsing this forum: No registered users and 12 guests