Pascal on Arduino?

Discussion and questions about programming with Ultibo.
Gavinmc42
Posts: 1595
Joined: Sun Jun 05, 2016 12:38 pm
Location: Brisbane, Australia

Pascal on Arduino?

Postby Gavinmc42 » Wed Jun 12, 2019 2:54 am

One of the guys on the Pi forum showed me a C #define trick.
Since I used Arduino for the Microbit stuff I thought this might be fun.
I have used #define lots of times in C but never to make C look like another language.

Blink example

Code: Select all

#define BEGIN {
#define END }
#define FUNCTION void


FUNCTION setup()
 BEGIN
  pinMode(0, OUTPUT);
 END

FUNCTION loop()
 BEGIN
  digitalWrite(0, HIGH);
  delay(500);
  digitalWrite(0, LOW);
  delay(500);
 END
hippy
Posts: 33
Joined: Tue Jan 02, 2018 5:54 pm
Location: UK

Re: Pascal on Arduino?

Postby hippy » Fri Jun 14, 2019 2:17 pm

Your "FUNCTION" should more properly be "PROCEDURE". The problem I recall from doing similar in the past is remembering any differences in semi-colon use.

The nice thing about GCC is that, the "can't have nested C routines" rule, doesn't apply. Well it does - they aren't strictly nested routines - but it mostly works as expected.

Code: Select all

#define PROCEDURE void
#define BEGIN     {
#define END       }
#include          <stdio.h>

PROCEDURE main()
BEGIN
  PROCEDURE nested()
  BEGIN
    printf("Whoopy!\n");
  END
  nested();
END

Note the nested PROCEDURE comes after the BEGIN. It's nuisances like this which makes it a solution but rather impractical in general.
Gavinmc42
Posts: 1595
Joined: Sun Jun 05, 2016 12:38 pm
Location: Brisbane, Australia

Re: Pascal on Arduino?

Postby Gavinmc42 » Sat Jun 15, 2019 2:28 am

Your "FUNCTION" should more properly be "PROCEDURE".

Thanks for the correction, I was still in C mode thinking.
Keep forgetting Pascal has functions and procedures :D

Nested PROCEDURES? Are they needed if predefined prototyped?

Anyway, real free pascal on Arduino's could probably be done with C lib interfacing.
But too much work for me, got other stuff I want to do first.
hippy
Posts: 33
Joined: Tue Jan 02, 2018 5:54 pm
Location: UK

Re: Pascal on Arduino?

Postby hippy » Sat Jun 15, 2019 6:03 pm

Gavinmc42 wrote:Nested PROCEDURES? Are they needed if predefined prototyped?

Probably not, but they are an intrinsic part of Pascal so, if trying to use a 'minimal effort' way to make Pascal code compile using a C compiler, it helps not to have to refactor the code. If one is going to have to refactor one might as well go the whole hog and code it in C to start with.

Also, the following works as is. If you flattened that out you would have to give the two 'deepnested' routines their own unique names.

Code: Select all

PROCEDURE main()
BEGIN
  PROCEDURE one()
  BEGIN
    PROCEDURE deepnested()
    BEGIN
      printf("deepnested within one\n");
    END
    deepnested();
  END
  PROCEDURE two()
  BEGIN
    PROCEDURE deepnested()
    BEGIN
      printf("deepnested within two\n");
    END
    deepnested(); 
  END
  one();
  two();
END

Return to “General”

Who is online

Users browsing this forum: No registered users and 1 guest