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


Groups > comp.lang.forth > #31208 > unrolled thread

Compiler Tricks

Started byJason Damisch <jasondamisch@yahoo.com>
First post2014-06-05 11:00 -0700
Last post2014-06-05 19:32 -0700
Articles 11 — 6 participants

Back to article view | Back to comp.lang.forth


Contents

  Compiler Tricks Jason Damisch <jasondamisch@yahoo.com> - 2014-06-05 11:00 -0700
    Re: Compiler Tricks anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2014-06-05 18:21 +0000
      Re: Compiler Tricks Jason Damisch <jasondamisch@yahoo.com> - 2014-06-05 13:43 -0700
      Re: Compiler Tricks Gerry Jackson <gerry@jackson9000.fsnet.co.uk> - 2014-06-06 09:16 +0100
    Re: Compiler Tricks "Rod Pemberton" <dont_use_email@xnothavet.cqm> - 2014-06-05 15:36 -0400
      Re: Compiler Tricks Jason Damisch <jasondamisch@yahoo.com> - 2014-06-05 13:48 -0700
        Re: Compiler Tricks "Rod Pemberton" <dont_use_email@xnothavet.cqm> - 2014-06-05 17:30 -0400
          Re: Compiler Tricks Jason Damisch <jasondamisch@yahoo.com> - 2014-06-05 14:50 -0700
    Compiler Tricks Julian Fondren <ayrnieu@gmail.com> - 2014-06-05 13:50 -0700
      Re: Compiler Tricks Jason Damisch <jasondamisch@yahoo.com> - 2014-06-05 14:03 -0700
    Re: Compiler Tricks "Clyde W. Phillips Jr." <cwpjr02@gmail.com> - 2014-06-05 19:32 -0700

#31208 — Compiler Tricks

FromJason Damisch <jasondamisch@yahoo.com>
Date2014-06-05 11:00 -0700
SubjectCompiler Tricks
Message-ID<e83ffde9-78ab-49f9-bd0b-2b83137d6a51@googlegroups.com>
Hi Guys ( and Gals ),

I want to ask a question.  I want to have a word which compiles a string but instead of returning the address to the string, instead returns an XT which when executed returns the address of the string. 

I have SwiftForth.  So I used the handy dandy world LOCATE to rummage around in SwiftForth to see what it provides for me.

LOCATE :NONAME  ( as a clue on what to do )

Which gives me the following on my sometimes dandy mock Teletype thingy.


: ; ( -- )
   POSTPONE EXIT (;) -SMUDGE ;  IMMEDIATE

: : ( -- ) \ Usage: : <name>
   (:)  HEADER +SMUDGE ;

: :NONAME ( -- xt )
   $FF C, 0 C,  HERE DUP LAST CELL+ CELL+ !  CODE>  (:) ;


After scratching my head a bit I came up with the following

: +XT
   $FF C, 0 C,  HERE DUP LAST CELL+ CELL+ !  CODE>
   ;

: -XT
   POSTPONE ;
   ;

: TEST"  +XT   POSTPONE (C") ,"  -XT  ;

Which sure seems to do the trick.  

My question, is there a more portable way of doing this?

Jason

[toc] | [next] | [standalone]


#31211

Fromanton@mips.complang.tuwien.ac.at (Anton Ertl)
Date2014-06-05 18:21 +0000
Message-ID<2014Jun5.202121@mips.complang.tuwien.ac.at>
In reply to#31208
Jason Damisch <jasondamisch@yahoo.com> writes:
>Hi Guys ( and Gals ),
>
>I want to ask a question.  I want to have a word which compiles a string but instead of returning the address to the string, instead returns an XT which when executed returns the address of the string. 
...
>My question, is there a more portable way of doing this?

Hmm, if you want a word that works in interpretation mode, the
following is standard:

: string-xt ( c-addr u -- xt )
  2>r :noname 2r> postpone sliteral postpone ; ;
: test" ( "name" -- xt )
  [char] " parse string-xt ;

test" bla bla" .s execute type

If you want a word that works inside a colon definition, I don't think
that's possible in standard Forth.  It may be possible with quotations
(but the following does not work currently with Gforth):

: string-xt1 ( c-addr u -- xt )
  2>r postpone [: 2r> postpone sliteral  postpone ;] ;

: [test]" '"' parse string-xt1 postpone literal ; immediate

: foo [test]" bla bla" ;
foo execute type

- anton
-- 
M. Anton Ertl  http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
     New standard: http://www.forth200x.org/forth200x.html
   EuroForth 2014: http://www.euroforth.org/ef14/

[toc] | [prev] | [next] | [standalone]


#31216

FromJason Damisch <jasondamisch@yahoo.com>
Date2014-06-05 13:43 -0700
Message-ID<518c1261-a599-4d62-bf40-b65bd73b052c@googlegroups.com>
In reply to#31211
On Thursday, June 5, 2014 11:21:21 AM UTC-7, Anton Ertl wrote:

> If you want a word that works inside a colon definition, I don't think
> that's possible in standard Forth.  

That answers my question

[toc] | [prev] | [next] | [standalone]


#31235

FromGerry Jackson <gerry@jackson9000.fsnet.co.uk>
Date2014-06-06 09:16 +0100
Message-ID<lmrtcn$epv$1@dont-email.me>
In reply to#31211
On 05/06/2014 19:21, Anton Ertl wrote:
> Jason Damisch <jasondamisch@yahoo.com> writes:
>> Hi Guys ( and Gals ),
>>
>> I want to ask a question.  I want to have a word which compiles a string but instead of returning the address to the string, instead returns an XT which when executed returns the address of the string.
> ....
>> My question, is there a more portable way of doing this?
>
[...]

>
> If you want a word that works inside a colon definition, I don't think
> that's possible in standard Forth.  It may be possible with quotations
> (but the following does not work currently with Gforth):
>
> : string-xt1 ( c-addr u -- xt )
>    2>r postpone [: 2r> postpone sliteral  postpone ;] ;
>
> : [test]" '"' parse string-xt1 postpone literal ; immediate
>
> : foo [test]" bla bla" ;
> foo execute type
>

It doesn't work on my system either but removing POSTPONE LITERAL from 
[test]" does work because ;] already compiles the xt as a literal in its 
enclosing definition which is FOO in this case i.e.

: [test]" '"' parse string-xt1 ; immediate

So you might as well define

: string-xt1" ( -- )
    [char] " parse 2>r postpone [: 2r> postpone sliteral  postpone ;]
; immediate

: foo string-xt1" bla bla" ;
foo execute type

-- 
Gerry

[toc] | [prev] | [next] | [standalone]


#31213

From"Rod Pemberton" <dont_use_email@xnothavet.cqm>
Date2014-06-05 15:36 -0400
Message-ID<op.xgzx2xky6zenlw@localhost>
In reply to#31208
On Thu, 05 Jun 2014 14:00:14 -0400, Jason Damisch <jasondamisch@yahoo.com>  
wrote:

> I want to ask a question.  I want to have a word which compiles a string
> but instead of returning the address to the string, instead returns an
> XT which when executed returns the address of the string.
>
> I have SwiftForth.  So I used the handy dandy world LOCATE to rummage
> around in SwiftForth to see what it provides for me.
>
> [code]
>
> Which sure seems to do the trick.
>
> My question, is there a more portable way of doing this?
>

I'm not quite sure what your code does ...

Did Anton answer your question?

If not, are you asking for something closer to:

: STRINGIFY" :NONAME POSTPONE ." POSTPONE ; ;


Rod Pemberton

[toc] | [prev] | [next] | [standalone]


#31217

FromJason Damisch <jasondamisch@yahoo.com>
Date2014-06-05 13:48 -0700
Message-ID<00ef6f4f-1814-4d62-acfe-e971e714622f@googlegroups.com>
In reply to#31213
On Thursday, June 5, 2014 12:36:23 PM UTC-7, Rod Pemberton wrote:

> If not, are you asking for something closer to:
> : STRINGIFY" :NONAME POSTPONE ." POSTPONE ; ;

This returns an XT which when executed displays the string.  I wanted an XT which when executed places the cstr on the stack.  Then I can COUNT TYPE it or whatever.

Jason

[toc] | [prev] | [next] | [standalone]


#31223

From"Rod Pemberton" <dont_use_email@xnothavet.cqm>
Date2014-06-05 17:30 -0400
Message-ID<op.xgz3duf86zenlw@localhost>
In reply to#31217
On Thu, 05 Jun 2014 16:48:42 -0400, Jason Damisch <jasondamisch@yahoo.com>  
wrote:

> On Thursday, June 5, 2014 12:36:23 PM UTC-7, Rod Pemberton wrote:
>
>> If not, are you asking for something closer to:
>> : STRINGIFY" :NONAME POSTPONE ." POSTPONE ; ;
>
> This returns an XT which when executed displays the string.
> I wanted an XT which when executed places the cstr on the stack.
> Then I can COUNT TYPE it or whatever.

Ok, what about this?  Closer?

: STRINGIFY" :NONAME POSTPONE S" POSTPONE ; ;

No need to COUNT though, just EXECUTE and TYPE .


Rod Pemberton

[toc] | [prev] | [next] | [standalone]


#31227

FromJason Damisch <jasondamisch@yahoo.com>
Date2014-06-05 14:50 -0700
Message-ID<634d5b24-44c0-46b3-b981-eee6b7c1cf8c@googlegroups.com>
In reply to#31223
On Thursday, June 5, 2014 2:30:56 PM UTC-7, Rod Pemberton wrote:

> Ok, what about this?  Closer?
> : STRINGIFY" :NONAME POSTPONE S" POSTPONE ; ;
> No need to COUNT though, just EXECUTE and TYPE .

I could live with that.

Jason

[toc] | [prev] | [next] | [standalone]


#31218

FromJulian Fondren <ayrnieu@gmail.com>
Date2014-06-05 13:50 -0700
Message-ID<4d9528b5-9b93-4c49-9745-97164c37f96a@googlegroups.com>
In reply to#31208
On Thursday, June 5, 2014 1:00:14 PM UTC-5, Jason Damisch wrote:
> Hi Guys ( and Gals ),
> 
> I want to ask a question.  I want to have a word which compiles a string but instead of returning the address to the string, instead returns an XT which when executed returns the address of the string. 
>

This also doesn't involve return the address of the string as you
requested, but instead returns a string ( c-addr u ) pair.  It uses
some conveniences ( s\" [: ;] $TMP ) from gforth, but the underlying
idea of an immediate word that builds a string for EVALUATE can
certainly be duplicated elsewhere.

$TMP ( xt -- c-addr u )  executes the XT given in an environment
where all output is collected into a string, which it returns.

: blah" ( "string" -- xt )
  [: s\" [: s\\\" " type
     \"-parse bounds ?do
       i c@ [char] " = if .\" \\\"" else
       i c@ [char] \ = if .\" \\\\" else
       i c@ emit
     then then loop
     s\" \" ;]" type ;] $TMP evaluate ; immediate

blah" interpretive" execute cr type
: a blah" compiling" ;  a execute cr type
: b blah" with \"quotes\"" execute ;  b cr type

Because it uses EVALUATE , this word's behavior depends on the
meanings of [: s\" ;] in the environment it's run in, every
time you run it.

> 
> My question, is there a more portable way of doing this?
> 
> Jason

Your original blah" is just as portable as my blah" .  So I feel like
what you're asking is for an *already ported* [adj.] way of doing this
-- i.e., a way in ANS Forth, which would require no further porting
work to run on any ANS forth.  OK.  Such a way would involve EVALUATE
and would do away with (and very easily do away with) or would also
implement the convenience words I use above.

But relatively portable code is a much easier target.  You can make
it easier for someone to *port* [v.] without going as far as trying
to find an ANS Forth way of doing everything you do.


-- Julian

[toc] | [prev] | [next] | [standalone]


#31220

FromJason Damisch <jasondamisch@yahoo.com>
Date2014-06-05 14:03 -0700
Message-ID<c07bfea9-9e24-4e41-95ff-61768c11205c@googlegroups.com>
In reply to#31218
On Thursday, June 5, 2014 1:50:46 PM UTC-7, Julian Fondren wrote:

> Such a way would involve EVALUATE
 
> But relatively portable code is a much easier target.  You can make
> it easier for someone to *port* [v.] without going as far as trying
> to find an ANS Forth way of doing everything you do.

This also answers my question.

[toc] | [prev] | [next] | [standalone]


#31234

From"Clyde W. Phillips Jr." <cwpjr02@gmail.com>
Date2014-06-05 19:32 -0700
Message-ID<e2a6867f-b031-4262-bbc0-943b97483ade@googlegroups.com>
In reply to#31208
On Thursday, June 5, 2014 1:00:14 PM UTC-5, Jason Damisch wrote:
> Hi Guys ( and Gals ),
> 
> 
> 
> I want to ask a question.  I want to have a word which compiles a string but instead of returning the address to the string, instead returns an XT which when executed returns the address of the string. 

Sounds like CREATE/BUILDS loop to save/compile the "string" DOES> Return xt pointing to the string 

Thing.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.forth


csiph-web