To understand what's going on with quotations you have to understand the
difference between run-time, compile-time, how the stack checker works, and
a few other details.

Run-time is after your code has been compiled and you have started to
execute it--it's your program.

Compile-time is when the compiler is invoked, and this is done when loading
vocabularies or when invoking the compiler explicitly at run-time by
calling ``compile``. (Self-modifying code, tools like the listener that
accept code, compile it, and run it--these usages of the compiler at
runtime are slow and discouraged unless you need to do it that way. We tend
not to invoke the compiler at run-time.) Parsing words and macro expansion
happen as part of compile-time, since compile-time is really parse-time,
macro-expansion-time, stack-checker-time, and optimizing-compiler-time
combined.

Now for the stack-checker. Control flow words, called combinators, e.g.
``bi, tri, bi@, if``, have to be inlined and also written in such a way
that they allow stack effects to be inferred at compile-time but look like
they do something at run-time. In reality, they become part of the control
flow graph and are no-ops at run-time. We do this through the use of
compose/prepose, curry, and the stack-checker algorithm. While quotations
are sequences, in order for the stack-checker to infer the proper effect
for combinators, you can't call append on two quotations, you must call
compose instead. Likewise with suffix/prefix, you should call curry.

To put it all together, look at a parse-time example, SYNTAX: definition:

IN: syntax
SYNTAX: SYNTAX: scan-new-word parse-definition define-syntax ;

: parse-definition ( -- quot ) \ ; parse-until >quotation ;

: define-syntax ( word quot -- )
    [ drop ] [ define ] 2bi t "parsing" set-word-prop ;

What's happening here is that, at parse-time, you create a new word, parse
its definition until the semi-colon, and then convert that into a
quotation. So all your suffix! and append! are really creating an array of
code at parse-time and then, since converting between sequence types makes
sense, you convert that array to a quotation. Since this happens at
parse-time, it's safe to do this--you do not need compose/prepose and
curry. Finally, setting the "parsing" property tells the parser that your
new SYNTAX: is a parsing word.

I hope this helps, and please ask any followup questions!

Doug

On Tue, Dec 16, 2014 at 4:55 PM, John Benediktsson <mrj...@gmail.com> wrote:
>
> You can see what code is generated for both approaches:
>
> SYNTAX: FOO: scan-token '[ _ foo set ] append! ;
>
>     IN: scratchpad [ FOO: hello ] .
>     [ "hello" foo set ]
>
> SYNTAX: FOO1: scan-token '[ _ foo set ] suffix! \ call suffix! ;
>
>     IN: scratchpad [ FOO1: hello ] .
>     [ [ "hello" foo set ] call ]
>
> The first is simpler, although the results should be the same as your
> version.
>
> And quotations are just sequences, see this:
>
>     IN: scratchpad [ 1 2 3 ] first .
>     1
>
>     IN: scratchpad [ 1 2 ] { 3 } append .
>     [ 1 2 3 ]
>
>     IN: scratchpad { 1 2 } [ 3 ] append .
>     { 1 2 3 }
>
> Knowing that, you could also do this, which looks a little weird but works:
>
> SYNTAX: FOO2: scan-token suffix! { foo set } append! ;
>
>     IN: scratchpad [ FOO2: hello ] .
>     [ "hello" foo set ]
>
>
>
> On Tue, Dec 16, 2014 at 4:17 PM, tgkuo <tgk...@gmail.com> wrote:
>>
>>  Hi,
>>
>> About the parsing using append! as below
>>
>> > SYNTAX: FOO: scan-token '[ _ foo set ] append! ;
>>
>> seemed to have the same effect as this one, which is more understandable
>>
>> SYNTAX: FOO1: scan-token '[ _ foo set ] suffix! \ call suffix! ;
>>
>> As I knew, append! has the stack effect of ( seq seq -- seq ) but the
>> quotation is not a kind of seq, how this works?
>>
>>
>> BR
>>
>> kuo
>>
>>
>>
>>
>>
>> 於 2014/12/17 上午1:00, John Benediktsson 提到:
>>
>> If you want your code to work the way you intend, using a local
>> namespace, then how about this:
>>
>>  SYNTAX: FOO: scan-token '[ _ foo set ] append! ;
>>
>>  That will set in the namespace that you want.
>>
>> On Tue, Dec 16, 2014 at 8:59 AM, John Benediktsson <mrj...@gmail.com>
>> wrote:
>>>
>>> Also, I'm not sure why you need a parsing word for that, you can just:
>>>
>>>  IN: foo
>>>
>>>  SYMBOL: foo
>>>
>>>  IN: bar
>>>
>>>  foo [ "hello" ] initialize
>>>
>>>  ... or
>>>
>>>  "hello" foo set-global
>>>
>>>  the first will initialize if not ``f``, the second will always set.
>>>
>>>
>>>
>>> On Tue, Dec 16, 2014 at 8:55 AM, Andrea Ferretti <
>>> ferrettiand...@gmail.com> wrote:
>>>
>>>> Hi, I am trying to define some syntax words that should have effect on
>>>> a global (or dynamic) variable. What I am trying to achieve is
>>>> something similar to how the LIBRARY: word affects later occurrences
>>>> of FUNCTION:
>>>>
>>>> The problem is, it does not seem to work. The simplest example I can
>>>> make is up the following.
>>>>
>>>> In package foo
>>>>
>>>>     USING: kernel lexer namespaces ;
>>>>     IN: foo
>>>>
>>>>     SYMBOL: foo
>>>>
>>>>     SYNTAX: FOO: scan-token foo set ;
>>>>
>>>> Then in package bar
>>>>
>>>>     USING: namespaces foo prettyprint ;
>>>>     IN: bar
>>>>
>>>>     FOO: hello
>>>>
>>>>     foo get .
>>>>
>>>>
>>>> This executes the top level form `foo get .` but prints f instead of
>>>> "hello". The same happens if I use run-file directly. If, instead, I
>>>> make the same declaration
>>>>
>>>>     FOO: hello
>>>>
>>>> in the listener, I can check that foo is modified.
>>>>
>>>> Is there a way to make this work? I could also avoi the variable, but
>>>> while parsing I do not have access to the stack, and so I do not know
>>>> how to pass any information among parsing words.
>>>>
>>>>
>>>> ------------------------------------------------------------------------------
>>>> Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
>>>> from Actuate! Instantly Supercharge Your Business Reports and Dashboards
>>>> with Interactivity, Sharing, Native Excel Exports, App Integration &
>>>> more
>>>> Get technology previously reserved for billion-dollar corporations, FREE
>>>>
>>>> http://pubads.g.doubleclick.net/gampad/clk?id=164703151&iu=/4140/ostg.clktrk
>>>> _______________________________________________
>>>> Factor-talk mailing list
>>>> Factor-talk@lists.sourceforge.net
>>>> https://lists.sourceforge.net/lists/listinfo/factor-talk
>>>>
>>>
>>
>> ------------------------------------------------------------------------------
>> Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
>> from Actuate! Instantly Supercharge Your Business Reports and Dashboards
>> with Interactivity, Sharing, Native Excel Exports, App Integration & more
>> Get technology previously reserved for billion-dollar corporations, 
>> FREEhttp://pubads.g.doubleclick.net/gampad/clk?id=164703151&iu=/4140/ostg.clktrk
>>
>>
>>
>> _______________________________________________
>> Factor-talk mailing 
>> listFactor-talk@lists.sourceforge.nethttps://lists.sourceforge.net/lists/listinfo/factor-talk
>>
>>
>>
>>
>> ------------------------------------------------------------------------------
>> Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
>> from Actuate! Instantly Supercharge Your Business Reports and Dashboards
>> with Interactivity, Sharing, Native Excel Exports, App Integration & more
>> Get technology previously reserved for billion-dollar corporations, FREE
>>
>> http://pubads.g.doubleclick.net/gampad/clk?id=164703151&iu=/4140/ostg.clktrk
>> _______________________________________________
>> Factor-talk mailing list
>> Factor-talk@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/factor-talk
>>
>>
>
> ------------------------------------------------------------------------------
> Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
> from Actuate! Instantly Supercharge Your Business Reports and Dashboards
> with Interactivity, Sharing, Native Excel Exports, App Integration & more
> Get technology previously reserved for billion-dollar corporations, FREE
>
> http://pubads.g.doubleclick.net/gampad/clk?id=164703151&iu=/4140/ostg.clktrk
> _______________________________________________
> Factor-talk mailing list
> Factor-talk@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/factor-talk
>
>
------------------------------------------------------------------------------
Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
from Actuate! Instantly Supercharge Your Business Reports and Dashboards
with Interactivity, Sharing, Native Excel Exports, App Integration & more
Get technology previously reserved for billion-dollar corporations, FREE
http://pubads.g.doubleclick.net/gampad/clk?id=164703151&iu=/4140/ostg.clktrk
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to