A r t i c l e s
Navigation

Note: This site is
a bit older, personal views
may have changed.

M a i n P a g e

D i r e c t o r y

FreePascal Compiler Macros



First, the word "macro" is quite an overused term. More specifically, freepascal
offers compiler macros. Or pre-processing. Compiler macros are basically something
that goes in and replaces all instances of a with b.
Let's pretend your name was long and you were sick of typing Larsabulu Olsonabolgus
out every time you had to spell your name. With a macro, you could use the shortform
LO.

However, generally this can make code unmaintainable in the long term, if you use too
many macros. That's why Pascal didn't offer it in the first place.. But, macros can
be useful.. and they are not just a toy. They could be useful in situations where
you are creating a new language for example, or a shortform syntax.. or say you are
developing something like a regex language. This is very powerful when you think
of situations where it could be useful.


Example 1: ----------- Some C++ or C programmers will gloat about how they can create Pascal in their C compiler by using some defined macros - by replacing { with begin and } with end. In FreePascal, you can do the following: program Project1; {$mode objfpc}{$H+} {$define b:= begin } //let's define some macros {$define e:= end } {$define proc:= procedure } var iLoc:integer; proc testing1; b for iLoc:= 1 to 6 do writeln('test'); e; b testing1; e. Output: test test test test test test So yes, macros not only apply to your code inside your statements, but also the syntax like begin, end, procedure, etc.
Example 2: ----------- program Project1; {$mode objfpc}{$H+} {$define Z:= begin } {$define X:= end } {$define Y:= procedure } {$define A:= for } {$define B:= to } {$define C:= do } {$define D:= writeln } {$define E:= var } {$define F:= := } {$define G:= : } {$define H:= ; } {$define I:= . } {$define J:= integer } {$define K:= iLoc } {$define L:= testing1 } e k g j h Y l h Z a k f 1 b 6 c d('test') h X h Z l h X I Output: test test test test test test
So how does a Pascal programmer make his program look like C code? Well, I haven't figured out that yet, and I care not to spend too much time doing that.. but basically the initial problem is that { is for comments in Pascal and {$define {:= begin } //this works {$define }:= end } //this doesn't work {$define ":= ' } //this doesn't work So, I'm not too sure how you'd do it.. maybe it's not possible yet. But I really don't care, I am more interested in other things, because I find parenthesis is hard to see on my screen. So I wouldn't use that anyway. A new Pascal short-syntax language, maybe could be a useful example of macros. Macros can help when you just want to throw together some ideas for a new language. Other uses will be for those people who really do complain about the verbosity of Pascal.. You can always use b in place of begin, and e in place of end, if you really are that worried (as in example 1). I think it allows you to think up and experiment with possible short form languages for the future.. without you ever having any knowledge of YACC or tokens, or advanced language parsing. It let's you quickly use an existing compiler that is already working, with a new syntax. Another idea would be small Pascal-scripts who are compiled on the fly with PPC386 or FPC. Imagine having something like microsoft's VBA available in your applications. Just ship the Pascal compiler with your applications, and you could have a huge power available to you. If you used a shortform Pascal syntax with a DEFINE file, and included this define file in every script that people compiled you wouldn't need to write a new compiler or interpreter. Just ship the Pascal compiler with your application.. and set up the compiler to use a DEFINE file, invisible to your users. In Example 1, that is a very small and short "script" that a user could write, like a VBA macro. I know that you are thinking "yah, but what's the point?". Remember, there ARE situations like VBA or regexes where shortform is GOOD. We're not changing the Pascal language.. we're just offering a shortform syntax for those situations where it is actually useful (in Excel, it is useful.. in regexes, it is useful.. in Application development and Application design.. it is not useful. You just have to separate useful from useless). Make all the DEFINE's invisible to the user, and now the user has a very short and fast syntax that he could use to compile mini programs. These mini programs could interact with your software application like a VBA. They could either be an on the fly SO or DLL, or an on the fly Exe/ELF. Think about all the work you would have to do to make your own compiler for your mini-scripting language.. OR you could just reuse the freepascal compiler! --L505
Taken from a mailing I sent to the freepascal newsgroups: "You could actually make something useful out of this, if the defines were thought out properly, and the situation fit the need. There are those situations where regex's come handy because they are quick and dirty, and they work in real time environments when you just don't have time to write a real search and replace algorithm. In text editors for example, or in web applications.. people do in fact use regexes. Non-programmers do to. In Excel spreadsheet, people use a shortform language basically... because who has time to write an entire program, just to get data out of Cell A1 and Cell A2? In Excel, =A1&A2 is basically just a shortform for the programming language behind the scenes, if you think about it We might even be able to turn the whole Pascal language into one regex-like looking thing, or Excel-like looking thing, as just purely an OPTION for those times when you need quick syntax. So people could write PascalEx's or PasExcels instead of regexes. Particularly if some standards were set. We can't have each person writing their own define macros.. but if we had some standards.. and some standard Define files which people could download... Don't laugh at it all though..... I use regexes, and they are useful.. I'm serious about this. It's just a matter of figuring out -where- this situation could be most useful. Someone found a useful situation for regexes, and they work. Someone found a useful situation for Excel language, and it worked. But surely there are other situations.. Sometimes I find that regexes limit me in some ways or that regex's are not the answer for some problems... or that regexes are way too slow. Maybe there is an opportunity in those particular cases. Or maybe a solution which has nothing to do with search and replace. Remember, someone might have laughed at regexes too, when they were first thought up. Another idea would be to allow users to make Rapid plugins for your application. Using a shortform syntax, users would be more inclined to write a DLL or .SO plugin for your applications (think like total commander plug-ins). I know larger plugins for applications require time and thinking. so the syntax is really not an issue. But for those situations where you are writing a small plug in for the application, something like a regex syntax might be useful. Remember, in Excel, no one wants to write an entire begin/end program just to get A1 Cell data into B2 Cell data. Since the Pascal compiler is so fast, you could pretty much pretend you were writing a script for your program.. then have the program save your so called script. (think like Microsoft's VBA). Instead of using Java or something, or VBA, you'd just compile your so called "scripts" with the Pascal compiler.. on the fly. Once they were compiled, they'd be "saved" as a "recordable macro script" (think like VBA scripts)."


About
This site is about programming and other things.
_ _ _