Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.forth > #3639
| From | Chris Hinsley <chris.hinsley@gmail.com> |
|---|---|
| Newsgroups | comp.lang.forth |
| Date | 2011-06-29 19:32 +0100 |
| Message-ID | <2011062919320387546-chrishinsley@gmailcom> (permalink) |
| References | (3 earlier) <20110628185151666-chrishinsley@gmailcom> <2011062819022446632-chrishinsley@gmailcom> <2011062819111570841-chrishinsley@gmailcom> <iufj50$tct$1@dont-email.me> <iufm7g$kf4$1@dont-email.me> |
| Subject | Re: macro expansion ? |
On 2011-06-29 18:09:49 +0100, Gerry Jackson said: > On 29/06/2011 17:17, Gerry Jackson wrote: >> On 28/06/2011 19:11, Chris Hinsley wrote: >>> On 2011-06-28 19:02:24 +0100, Chris Hinsley said: >>> >> >> [...] >> >>>> How about a macro that did greatest common divider ? >>>> >>>> Function gcd(a As Integer, b As Integer) As Integer >>>> If b = 0 Then >>>> gcd = a >>>> Else >>>> gcd = gcd(b,a) >>>> End If >>>> End Function >>>> >>>> Chris >>> >>> I think I'm rapidly getting into the 'but you wouldn't do it like that' >>> teritory. You'd just define a word that did the calcutation, and then >>> use it with somthing like [ 15 5 GCD ] LITERAL. >>> >>> But I'm pushing the issue of how you'd do it with a macro called GCD to >>> expose the limits. >>> >> >> Andrew & Anton have given you good reasons not to do it via a macro, but >> if you really want to have a gcd macro it's not difficult e.g an >> interpret time macro for gcd is: >> >> macro gcd ?dup [if] tuck mod gcd [then]" >> cr 17 118 * 17 62 * gcd . \ Should display 34 >> >> To use it in a colon definition you can do >> : t1 [ 17 118 * 17 62 * gcd ] literal ; >> t1 . \ Should display 34 >> >> To pass the arguments into the colon definition requires a little more >> trickery (for systems such as GForth that place control information on >> the data stack and other systems that require a balanced stack between : >> and ;) e.g.: >> >> : pass >r ' execute r> ; immediate >> 17 118 * 17 62 * pass pass : t2 [ gcd ] literal ; >> t2 . \ Should display 34 >> >> Alternatively [gcd] can be defined as a macro: >> >> macro [gcd] [ ?dup [if] tuck mod [gcd] [else] ] [then]" >> 17 177 * 17 93 * pass pass : t3 [gcd] literal ; >> t3 . \ should display 51 >> >> It all works with GForth >> > > Incidentally the gcd calculation can be done in interpret mode without > a macro e.g. > > 51 59 * 51 31 * > ?dup [if] tuck mod 0 >in ! [then] . Neat ! This sort of thing is the mind bending stuff I like about Forth. ! Chris
Back to comp.lang.forth | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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