Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #3615

Re: macro expansion ?

From Chris Hinsley <chris.hinsley@gmail.com>
Newsgroups comp.lang.forth
Date 2011-06-28 21:34 +0100
Message-ID <2011062821345429323-chrishinsley@gmailcom> (permalink)
References <2011062814435882049-chrishinsley@gmailcom> <o56dncRbu8HmuZfTnZ2dnUVZ_uidnZ2d@supernews.com> <2011062820391536125-chrishinsley@gmailcom>
Subject Re: macro expansion ?

Show all headers | View raw


On 2011-06-28 20:39:15 +0100, Chris Hinsley said:

> On 2011-06-28 19:59:06 +0100, Elizabeth D Rather said:
> 
>> On 6/28/11 3:43 AM, Chris Hinsley wrote:
>>> From the recent thread on IMMEDIATE I quote this word:
>>> 
>>> \ macros
>>> : MACRO ( "name" <ccccc"> -- )
>>> CREATE ," IMMEDIATE DOES> COUNT EVALUATE ;
>>> 
>>> When I first added this word to my Forth project I belived it would
>>> allow me to define words that were 'macro expanded' in a text
>>> substitution manner.
>>> 
>>> Is this indead the case ? The call to EVALUATE made me think this would
>>> act as if it was text substitution rather than just a IMMIDEATE word
>>> that would execute during compilation. My intention was somthing that
>>> would be expanded during compilation and therefore may get optimised as
>>> inlined code into the word curently deing defined, so no subroutine call
>>> overhead.
>> 
>> Whether it compiles something would depend on whether it's used in 
>> compile state, obviously.  The use of EVALUATE has a potential 
>> side-effect in that if a word in the string has been redefined, you'll 
>> get the most recent version, whereas when you compile a definition you 
>> always have the version that was current at the time it was compiled.
> 
> I see the possibility of redefintion during expansion as a feature, not 
> a problem. !
> 
>> 
>>> Clearly it suffers from haveing a single " being scanned for the end of
>>> the evaluated string, but that could be improved maybe ?
>>> 
>>> But this led me on to thinking how would I do a more Lisp like macro ?
>>> Somthing that would be expanded and then that expantion expanded till
>>> there was no more expantions. One of the thoughs that's bothering me on
>>> this is what happens if the expansion leads to new words needing to be
>>> defined within a partial expansion, and then on the next expantion pass
>>> they are executed, all this while not yet finishing the definition of
>>> the original word that used the macro in the first place ?
>>> 
>>> I would like to resolve these thoughts with any help from the Forth
>>> GURU's out there. :)
>> 
>> My big question is, why do you want to do this?  If it's an academic 
>> exercise, to just see if it's possible, that's one thing: enjoy. But 
>> I'm not very enamored of macros in general, because they're often a 
>> substitute for "programming by copy/paste" which is a pernicious 
>> practice.  People do it in languages where a CALL (and surrounding 
>> setup, etc.) is costly, but given that the entire architecture of Forth 
>> is designed to minimize the cost of a call, I really don't see the 
>> point as a practical matter.
>> 
>> Cheers,
>> Elizabeth
> 
> It's not an academic exersise ! At least not the way I see it. I 
> disagree that CALL is cheap ! It is on your own Forth CPU, it's not on 
> a general RISC chip.
> 
> I would like to have the ability to have a powerfull macro system that 
> works at the source level. Somthing like what LISP has, or (forgive me 
> mentioning it) C++ templates. Before the Forth compiler starts to lay 
> down the definition of a word I wish to have text substitution happen, 
> I want the full power of Forth to be available during the expansion to 
> manipulate the source. I don't seam to be able to do this in a simple 
> way useing what's available within the language itself ?
> 
> Chris

I'll try to ilustrate why this is important to me.

In assembler language I've written a _lot_ of pixel bashing code over 
the years while codeing arcade games, I've also written around 5 GUI 
libraries during the last 15 years, again all in assembler not C.

So I certainly didn't have an optimising compiler to do the hard work 
of producing optimal pixel conversion code (for example) for blit 
routines from one surface type to another. After doing my n'th hand 
coded assembler inner loop for some new customer graphics card surface 
format I thought, 'stuff this for a game of soilders I'm going to write 
a macro to do this for me'. I put all the smarts of how to shift and 
mask and up/down convert colour channels, detect if there was an 
unneeded channel or if I could omit an and if it was the top bits of a 
channel, plus it would shedule the sequence of channel operations to 
allow for results not being available for 1 and somtimes 2 cycles etc.

After I'd done this macro, I then recoded every surface type blit class 
to just a few macro calls, removeing huge gobs of repetative code, 
along with minnor bugs too I might add. And we had a system that would 
produce optimal _quality_ code, passed all the test suits, was now 
about 20 lines of source per surface class. And any old idiot (sorry 
any Tao guys out there reading this, cause none of you where idiots :) 
) could knock up a new format for a customer without any need to know 
more than a few equates to define the channel layout.

Now I find myself in a similar situation with Forth. I like the 
language a lot, I've ported and improved a simple compiler, but I 
really don't think I'm going to expend all the energy I need to do a 
fully optimising compiler. But I don't think I should need to ! 5% of 
the code (sucking eggs time guys) get's executted 90% of the time. I 
really don't care much about 95% of the code being optimised or not. 
But what I do care about is if I can create a macro in Forth that has 
the smarts my assembler macro had, and produces inline code for each 
surface to surface blit function without haveing a call for each 
operation on each colour channel for each pixel !

Chris

Back to comp.lang.forth | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

macro expansion ? Chris Hinsley <chris.hinsley@gmail.com> - 2011-06-28 14:43 +0100
  Re: macro expansion ? Andrew Haley <andrew29@littlepinkcloud.invalid> - 2011-06-28 10:58 -0500
    Re: macro expansion ? Chris Hinsley <chris.hinsley@gmail.com> - 2011-06-28 18:34 +0100
      Re: macro expansion ? Chris Hinsley <chris.hinsley@gmail.com> - 2011-06-28 18:51 +0100
        Re: macro expansion ? Chris Hinsley <chris.hinsley@gmail.com> - 2011-06-28 19:02 +0100
          Re: macro expansion ? Chris Hinsley <chris.hinsley@gmail.com> - 2011-06-28 19:11 +0100
            Re: macro expansion ? Chris Hinsley <chris.hinsley@gmail.com> - 2011-06-28 19:17 +0100
              Re: macro expansion ? Chris Hinsley <chris.hinsley@gmail.com> - 2011-06-28 19:47 +0100
                Re: macro expansion ? Josh Grams <josh@qualdan.com> - 2011-06-29 15:34 +0000
                Re: macro expansion ? Chris Hinsley <chris.hinsley@gmail.com> - 2011-06-29 19:21 +0100
            Re: macro expansion ? Andrew Haley <andrew29@littlepinkcloud.invalid> - 2011-06-29 03:15 -0500
              Re: macro expansion ? Chris Hinsley <chris.hinsley@gmail.com> - 2011-06-29 13:11 +0100
            Re: macro expansion ? Gerry Jackson <gerry@jackson9000.fsnet.co.uk> - 2011-06-29 17:17 +0100
              Re: macro expansion ? Gerry Jackson <gerry@jackson9000.fsnet.co.uk> - 2011-06-29 18:09 +0100
                Re: macro expansion ? Chris Hinsley <chris.hinsley@gmail.com> - 2011-06-29 19:32 +0100
                Re: macro expansion ? Gerry Jackson <gerry@jackson9000.fsnet.co.uk> - 2011-07-02 08:48 +0100
    Re: macro expansion ? Chris Hinsley <chris.hinsley@gmail.com> - 2011-06-29 00:44 +0100
    Re: macro expansion ? Gerry Jackson <gerry@jackson9000.fsnet.co.uk> - 2011-06-29 20:39 +0100
  Re: macro expansion ? Elizabeth D Rather <erather@forth.com> - 2011-06-28 08:59 -1000
    Re: macro expansion ? Chris Hinsley <chris.hinsley@gmail.com> - 2011-06-28 20:39 +0100
      Re: macro expansion ? Chris Hinsley <chris.hinsley@gmail.com> - 2011-06-28 21:34 +0100
        Re: macro expansion ? Andrew Haley <andrew29@littlepinkcloud.invalid> - 2011-06-29 03:26 -0500
  Re: macro expansion ? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2011-06-29 11:25 +0000
    Re: macro expansion ? Chris Hinsley <chris.hinsley@gmail.com> - 2011-06-29 13:24 +0100
      Re: macro expansion ? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2011-07-03 14:02 +0000
    Re: macro expansion ? Albert van der Horst <albert@spenarnc.xs4all.nl> - 2011-06-29 18:48 +0000
  Re: macro expansion ? BruceMcF <agila61@netscape.net> - 2011-06-30 11:11 -0700
  Re: macro expansion ? Ian Osgood <iano@quirkster.com> - 2011-07-02 16:22 -0700
  Re: macro expansion ? Hugh Aguilar <hughaguilar96@yahoo.com> - 2011-07-05 17:35 -0700

csiph-web