* * *

Author Topic: Redefining existing keywords  (Read 1822 times)

Bofer

  • Newbie
  • Posts: 4
Redefining existing keywords
« on: April 08, 2018, 09:51:21 pm »
Is it possible to use {$define} in FPC to redefine existing reserved symbols? Quick trivial example:


Code: Pascal  [Select]
  1. {$define program := main}
  2. {$define begin := {}
  3. {$define end := }}
  4. {$define <> := !=}
  5.  
  6. main someRandomProgram;
  7.  
  8. var
  9.    s : string;
  10.  
  11. {
  12.    s := 'Some random string.';
  13.    writeln(s);
  14.    readln
  15. }
  16.  
  17.  
  18.  

(Perhaps in the example one must also take more care regarding the curly brackets. In any case, you get the idea.)

Blaazen

  • Hero Member
  • *****
  • Posts: 2588
  • POKE 54296,15
    • Eye-Candy Controls
Re: Redefining existing keywords
« Reply #1 on: April 08, 2018, 09:55:38 pm »
 :D

Welcome to Free Pascal forum!
Lazarus 1.9.0 r58554 FPC 3.1.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.11.1, Plasma 5.13.2
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.11

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Bofer

  • Newbie
  • Posts: 4
Re: Redefining existing keywords
« Reply #2 on: April 08, 2018, 10:17:01 pm »
:D

Welcome to Free Pascal forum!

Hah hah, yes, thank you (but you did not answer the question, heh heh heh).

I was just wondering if what I described is possible and how it all works. One reason for this is to learn FPC more and to possibly change some of the Pascal  syntax redundancies. One thing I want to do is to remove the begin keyword wherever it is used outside of the main program entry point. Is this at all possible in FPC by simply using {$define} and the necessary code, and, if yes, how would it work?

Can one also, for example, change the <> operator? Just wondering.

Edson

  • Hero Member
  • *****
  • Posts: 920
Re: Redefining existing keywords
« Reply #3 on: April 08, 2018, 11:20:55 pm »
Symbols/Operators cannot be redefined that way. Delimiters { } are only for comments in Pascal.

It's better you get used to Pascal syntax, instead of thinking of C. Using the IDE facilities can help a lot.

Lazarus 1.6 - FPC 3.0.0 - x86_64-win64 on  Windows 7

Bart

  • Hero Member
  • *****
  • Posts: 3038
    • Bart en Mariska's Webstek
Re: Redefining existing keywords
« Reply #4 on: April 08, 2018, 11:29:21 pm »
Reminds me of this classic artice #include pascal.h on TheDailyWTF.

Bart

Bofer

  • Newbie
  • Posts: 4
Re: Redefining existing keywords
« Reply #5 on: April 09, 2018, 01:13:15 am »
Symbols/Operators cannot be redefined that way. Delimiters { } are only for comments in Pascal.

It's better you get used to Pascal syntax, instead of thinking of C. Using the IDE facilities can help a lot.

Then how, if using compiler directives? Or must one necessarily go to a deeper level in this case?

(Why assume some of those things which you seemingly assumed? I am already used to Pascal syntax and it is even the programming language I prefer (and the first programming language I was exposed to). (I generally prefer Wirth-like imperative structured languages.) (But it is not for no good reason that Wirth himself invented other languages that improved upon Pascal.) And I am already using the IDE facilities, and even looked for information on the IDE on how to set up certain things which seemed to be tucked away. That example code (with or without C-like symbols) was trivial and was merely intended to get the thread going so that I may learn how this part of FPC works, if it can at all be done in such a way.)

jamie

  • Hero Member
  • *****
  • Posts: 786
Re: Redefining existing keywords
« Reply #6 on: April 09, 2018, 01:56:24 am »
Fpc does have a couple of things from C

*=
+=
/=
-=

Does that bring a smile to your face ? ;)

Bofer

  • Newbie
  • Posts: 4
Re: Redefining existing keywords
« Reply #7 on: April 09, 2018, 05:20:05 pm »
Fpc does have a couple of things from C

*=
+=
/=
-=

Does that bring a smile to your face ? ;)

Actually, it does not, nor do you.

It seems to me that no one among you so far is willing to answer properly, but you rather seem to be willing to respond fallaciously. You probably even misunderstood some of my words and made conclusions you were not rationally allowed to make. I suppose it is a waste of time to further visit this forum.

Thaddy

  • Hero Member
  • *****
  • Posts: 6542
Re: Redefining existing keywords
« Reply #8 on: April 09, 2018, 05:28:11 pm »
You can define some but not all keywords using the macro facility.
You can override operators as well.

What is the exact question? You have to be more exact.

 I demo'd a crude "Dutch Pascal: here on the forum using macro's... works like a charm...
See this: http://forum.lazarus.freepascal.org/index.php/topic,36887.msg246265.html#msg246265
« Last Edit: April 09, 2018, 05:30:36 pm by Thaddy »
Ada's daddy wrote this:"Fools are my theme, let satire be my song."

jamie

  • Hero Member
  • *****
  • Posts: 786
Re: Redefining existing keywords
« Reply #9 on: April 10, 2018, 04:03:20 am »
@bofer

  Thanks, you made my day, you actually put a smile on my face, really!  :)

 You know you could always use FPC as a back end and write a language of your choice!

 I did something like that years ago for a friend that knew fortran very well but didn't want to learn anything
else, so I wrote a simple IDE using TP and used TP as a backend and had the IDE convert all of his fortran codings
to object pascal at compile time and then had TP compile that!.

 It's your ball game.




 

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 797
    • Burdjia
Re: Redefining existing keywords
« Reply #10 on: April 10, 2018, 01:17:10 pm »
:D

Welcome to Free Pascal forum!

Hah hah, yes, thank you (but you did not answer the question, heh heh heh).
Actually he did.


I was just wondering if what I described is possible and how it all works. One reason for this is to learn FPC more and to possibly change some of the Pascal  syntax redundancies.
Redundancies?  Pascal is one of the less redundant language ever! Or the Wirth's initial description was, right now thanks to operator orverloading and generics it is a bit more redundant but still less redundant than C and much less than C++.

Except you mean "redundant -> superfluous stuff".  Pascal has so much superfluous stuff as many other high level language (and C is not a high level language).  May be more than Python, but who would use a language that uses indentation to define code blocks?  That's crazy!

One thing I want to do is to remove the begin keyword wherever it is used outside of the main program entry point. Is this at all possible in FPC by simply using {$define} and the necessary code, and, if yes, how would it work?
Why?

Can one also, for example, change the <> operator? Just wondering.
Use operator overloading.
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

rvk

  • Hero Member
  • *****
  • Posts: 3449
Re: Redefining existing keywords
« Reply #11 on: April 10, 2018, 01:43:42 pm »
I demo'd a crude "Dutch Pascal: here on the forum using macro's... works like a charm...
See this: http://forum.lazarus.freepascal.org/index.php/topic,36887.msg246265.html#msg246265
Yes, that was a really lovely example :D :D

What keywords can't be redefined? Because even begin and end can be redefined.
The only thing is that you can't redefined it TO { and } (as OP wanted).

But this works.

Code: Pascal  [Select]
  1. {$macro on}
  2. {$define main := program}
  3. {$define al := begin}
  4. {$define la := end}
  5. main someRandomProgram;
  6. var
  7.    s : string;
  8. al
  9.    s := 'Some random string.';
  10.    writeln(s);
  11.    readln
  12. la.

OP just mixed up the defines (before the := and after) and you can't use { and } obviously.

But yeah, I agree with the others... this is a fun exercise but don't go programming like this.
« Last Edit: April 10, 2018, 01:45:23 pm by rvk »

PascalDragon

  • Full Member
  • ***
  • Posts: 159
  • Compiler Developer
Re: Redefining existing keywords
« Reply #12 on: April 10, 2018, 04:53:04 pm »
Is it possible to use {$define} in FPC to redefine existing reserved symbols? Quick trivial example:

No, you can't and the compiler even tells you that (once you add {$macro on} at the beginning):

Code: [Select]
Free Pascal Compiler version 3.0.4 [2017/12/03] for i386
Copyright (c) 1993-2017 by Florian Klaempfl and others
Target OS: Win32 for i386
Compiling .\fpctests\tdefine.pas
tdefine.pas(3,3) Error: Keyword redefined as macro has no effect
tdefine.pas(14,2) Fatal: Unexpected end of file
tdefine.pas(14,2) Fatal: Compilation aborted

Thaddy

  • Hero Member
  • *****
  • Posts: 6542
Re: Redefining existing keywords
« Reply #13 on: April 10, 2018, 05:04:27 pm »
@PascalDragon
Indeed, no reserved symbols, but keywords? Yes. As I demo'd...
If it is an operator you can overload it in many cases. So yes, but...

(I can make up a "Sven" language if you like  :D :D )
« Last Edit: April 10, 2018, 05:06:34 pm by Thaddy »
Ada's daddy wrote this:"Fools are my theme, let satire be my song."

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus