'Macro compiler' ideas - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

'Macro compiler' ideas

I've been impressed by the power FASM's macros. You can create a lot of other things besides programs with them. One macro created a Mandelbrot fractal in the TGA format. But you are limited on how you provide the paramaters for the macro. So I tried creating a macro compiler but didn't get very far because I ran out of ideas on how the make the macro definition sytanx that allows your macro to define and process any type of argument format. Here is what I tried. It's kind of like BNF. The example script here outputs asm source but could just as easily create the actual binary file. Has somebody already created a macro compiler like this? Do you have any ideas about the how to define the macros?
Thanks


[code]
[size=2]
macro asm
{

// The 'db' command will put bytes in the output file
db '; open with Fasm.win32exe', 13, 10, 13, 10,
'format PE GUI 4.0', 13, 10,
'entry START', 13, 10, 13, 10,
'START:', 13, 10,
' push -1', 13, 10, 13, 10

// This always matches the semi-colon
';'

// The brackets ( '[' and ']' ) mean that the string is optional
// The astrisk means that it can be repeated
// The colon means there is an action associated with the 'nop'
['nop']*:
{

db ' nop'

// This allows a semi-colon but gives a warning
[';']: db ' ; Hint: The semi-colon is optional'

db 13, 10

}


['eax']*:
{

db ' mov eax, '

'='

// Macro definitions can be nested as shown here
['eax']: db 'eax'
['ebx']: db 'ebx'
['ecx']: db 'ecx'
['[']:
{

db '['

['eax']: db 'eax'
['ebx']: db 'ebx'

']'

db ']'

}

db 13, 10

}

'end':
{

db 13, 10, 'EXIT:', 13, 10,
' add esp, 4', 13, 10,
' mov eax, 0', 13, 10,
' ret'
}

}

// Now use the macros that were defined
asm;

nop
nop;

eax = ebx
eax = eax
eax = [eax]
eax = [ebx]

end

// output file created by this macro
-----testPPC.asm, 222 bytes------

format PE GUI 4.0
entry START

START:
push -1

nop
nop
nop
nop
nop
nop
mov eax, ebx
mov eax, eax
mov eax, [eax]
mov eax, [ebx]

EXIT:
add esp, 4
mov eax, 0
ret

[/size]
[/code]

Comments

  • AlexandrescuAlexandrescu Posts: 66Member
    : [...] But you are limited on how you provide the paramaters for the macro. [...]

    Please describe the limitations you were thinking at and I'll help. But for the moment I can't, because I don't understand your problem.

    (Is that about the arguments order?)
    ================================================
    ((cons(car X)(cdr X))X)
    holds(X,P):-P(X);holds(Y,P),IsA(X,Y).

    Any (more) questions? SHOOT!

  • Ptr_082004Ptr_082004 Posts: 31Member
    I guess I didn't make it clear. I'm working on a 'macro compiler' and hadn't gotten stuck or anything. I just was wondering if somebody else had tried doing that. Or if people thought that it wouldn't work. What I mean by a 'macro compiler' is a program that you can define a programming langauge in and then use it. In the code I posted the first section is the definitions for the 'new programming language', the second section is an example of what those definitions would read, the third section is what the program outputs. I posted this here to see if anybody had some suggestions on how to define the macros. I'm sorry if I'm not explaining this clearly. I was wanting more of a disscussion, then a solution to a problem. Some questions.

    Has anybody created a macro processer that is powerfull enough to make a modest progamming language out of?

    Do you think a program like this would be very useful?

    Do you know of a good way to associate actions ( script ) with BNF syntax?

    I'd like to hear any comments you have about this. Even if you say That's stupid!, because if everybody says that I might rethink my approach to this program.

    Thanks
  • AlexandrescuAlexandrescu Posts: 66Member
    Yes - Pelle Orinius implemented C99 using some mecro-assembler, called HLA. You can check the following address for details on HLA, which can be used to develop industrial compilers (like PellesC - that C99 one, made by Pelle Orinius, who happends to be a member of programmersheaven)

    For that HLA thing, check:
    http://webster.cs.ucr.edu/AsmTools/HLA/index.html

    Learning HLA will enlight you about how to build real compilers quite esily and in a single, uniform environment (no YAC, no LEX... ): HIDE (HLA IDE)

    HIDE things:
    http://www.geocities.com/kahlinor/HIDE.html

    For that PellesC thing, check:
    http://www.christian-heffner.de/
    and
    http://www.smorgasbordet.com/pellesc/

    BTW:
    1- PellesC is fatser than gcc, generates a very efficient code and it complies better to the ISO-C99 standard. The bad thing is it only works on Windows (any Win32).
    2- There is a Linux HLA too - perhaps a linux HIDE as well, but I an not sure about HIDE.

    As for the utility of your topic... [b][size=5]isn't good compilers what we all want so badly?![/size][/b]

    ================================================
    ((cons(car X)(cdr X))X)
    holds(X,P):-P(X);holds(Y,P),IsA(X,Y).

    Any (more) questions? SHOOT!

  • htakeuchihtakeuchi Posts: 19Member
    : What I mean by a 'macro compiler' is a program that you can define a programming langauge in and then use it. In the code I posted the first section is the definitions for the 'new programming language', the second section is an example of what those definitions would read, the third section is what the program outputs. I posted this here to see if anybody had some suggestions on how to define the macros. I'm sorry if I'm not explaining this clearly. I was wanting more of a disscussion, then a solution to a problem. Some questions.
    :
    : Has anybody created a macro processer that is powerfull enough to make a modest progamming language out of?
    :
    : Do you think a program like this would be very useful?
    :
    : Do you know of a good way to associate actions ( script ) with BNF syntax?
    :

    Sounds like you want a compiler-compiler like YACC, Bison, Antlr, Coco, and a number of others.

    On the other hand, if you want to embed the macro language in an existing language, you are looking at macro preprocessors, where the macros are expanded in a separate step, with a separate program. You may be thinking of general purpose macro preprocessors like m4 (or was it m6?) in Unix, or the one described in Software Tools by Kernighan and Plauger.

    The hardest part of *using* a general purpose macro preprocessor is the "quoting" or "escaping" of characters when you have nested macro calls.
  • 684867684867 Posts: 110Member
    Interesting.

    Three or four years ago I setup a mock experiment with some dirty code while playing with the CBOOP/X-language design. The idea was to create an expression-based language. In the course I started considering making the language very loosely defined. In fact, only the delimiters and comment tokens would be hard coded into the language.

    The course I took in defining this prototype of my parser read a markup language. Recall that this was prior to XML being seriously considered. What I came up with looked a lot like HTML. However, now with XML being pretty near prolific, you might consider defining a DTD for programming language definition. A lexical analyzer/parser could then read an XML file that is dynamically defined by the lexical analyzer/parser or an external editor to process a separate source file.

    Defining languages on the fly with XML might create a means of specializing languages to a project and create a means of optimizing source code.

    "Unconventional Thinking, Unconventional Success"

    : I guess I didn't make it clear. I'm working on a 'macro compiler' and hadn't gotten stuck or anything. I just was wondering if somebody else had tried doing that. Or if people thought that it wouldn't work. What I mean by a 'macro compiler' is a program that you can define a programming langauge in and then use it. In the code I posted the first section is the definitions for the 'new programming language', the second section is an example of what those definitions would read, the third section is what the program outputs. I posted this here to see if anybody had some suggestions on how to define the macros. I'm sorry if I'm not explaining this clearly. I was wanting more of a disscussion, then a solution to a problem. Some questions.
    :
    : Has anybody created a macro processer that is powerfull enough to make a modest progamming language out of?
    :
    : Do you think a program like this would be very useful?
    :
    : Do you know of a good way to associate actions ( script ) with BNF syntax?
    :
    : I'd like to hear any comments you have about this. Even if you say That's stupid!, because if everybody says that I might rethink my approach to this program.
    :
    : Thanks
    :


    ****************************************
    Excellence Breeds! Go Hard or Go Home.

    Let Penguins rule the earth.
    Break some windows today.

Sign In or Register to comment.