See this macro:

MACRO: add ( n -- quot )
    [ \ + ] [ ] replicate-as ;

It's "stack effect" depends on the input.

    IN: scratchpad [ 1 add ] infer .
    ( x x -- x )

    IN: scratchpad [ 2 add ] infer .
    ( x x x -- x )

So you really can't do much more than say this macro produces some
quotation.

Then in calling code, we expand the macro and check the resulting body of
code has the right stack effect.

    : foo1 ( a b -- c ) 1 add ;
    : foo2 ( a b c -- d ) 2 add ;
    : foo3 ( a b c d -- e ) 3 add ;




On Thu, Jan 18, 2018 at 1:04 PM, Doug Coleman <doug.cole...@gmail.com>
wrote:

> Oops.
>
> MACRO: printf ( string -- quot: ( ..a string -- ..b ) )
> MACRO: sprintf ( string -- quot: ( ..a string -- ..b string ) )
>
>
>
> On Thu, Jan 18, 2018 at 3:02 PM John Benediktsson <mrj...@gmail.com>
> wrote:
>
>> MACRO: always produces a quot.
>>
>> But when it's "called" it's whatever the stack effect of that produced
>> quot is when it's expanded into the calling site.
>>
>>
>>
>>
>> On Thu, Jan 18, 2018 at 12:36 PM, Alexander Ilin <ajs...@yandex.ru>
>> wrote:
>>
>>> O-kay... Let me try to ask my question again...
>>>
>>> I've read the blog post that you linked to. In there I found the
>>> following declaration:
>>> `*MACRO:* printf ( format-string -- )`
>>>
>>> This means that the macro takes one argument and removes it from the
>>> stack, doesn't put anything back.
>>>
>>> But in the actual source code of the `formatting` vocab the declaration
>>> is: `MACRO: printf ( format-string -- quot )`, which looks as if the
>>> `printf` word takes one parameter off the stack and puts one item back on
>>> the stack.
>>>
>>> ... which is not true: it doesn't put a quotation on the stack, because
>>> it calls the quotation.
>>>
>>> So, I have two questions, basically:
>>>
>>> 1) why not change the declaration in `formatting` to ``MACRO: printf (
>>> format-string -- )` - with no output onto the stack?
>>> 2) are `MACRO:`s allowed to have invalid stack effect declaration? Are
>>> they somehow exempted from the compiler checks?
>>>
>>> I'm asking this because the declared stack effect shows up in the
>>> documentation, and is confusing. Reading it gives the expression that it
>>> should be used with an extra `call` to the produced quotation:
>>> `11 "he%do, world" printf call`
>>>
>>> 18.01.2018, 19:00, "John Benediktsson" <mrj...@gmail.com>:
>>>
>>> Both of those are macros (basically special words that produce a
>>> quotation), so when "called" they are first macro-expanded into a quotation
>>> and then that quotation is called, with whatever stack effect it has.
>>>
>>> IN: scratchpad 1 2 3 "%s %s %s" printf
>>> 1 2 3
>>>
>>> IN: scratchpad [ "%s %s %s" printf ] infer
>>> ( x x x -- )
>>>
>>> IN: scratchpad [ "%s %s %s" printf ] expand-macros
>>> [
>>>     [ present ] 2 ndip [ " " ] 2 ndip [ present ] 1 ndip
>>>     [ " " ] 1 ndip [ present ] 0 ndip output-stream get
>>>     [ stream-write ] curry 5 napply
>>> ]
>>>
>>> Maybe the article I wrote when developing it would help:
>>>
>>> https://re-factor.blogspot.com/2011/08/printf.html
>>>
>>>
>>> On Thu, Jan 18, 2018 at 4:32 AM, Alexander Ilin <ajs...@yandex.ru>
>>> wrote:
>>>
>>> Hello!
>>>
>>>   There are two similar words: printf and sprintf. They have similar
>>> stack effects, but different behavior.
>>>
>>> MACRO: sprintf ( format-string -- result )
>>> leaves a string on stack
>>>
>>> MACRO: printf ( format-string -- quot )
>>> leaves nothing on stack despite the stack effect specified
>>>
>>>   How come `printf` does'n obey its declared stack effect?
>>>
>>> ---=====---
>>>  Александр
>>>
>>>
>>>
>>> ---=====---
>>> Александр
>>>
>>>
>>> ------------------------------------------------------------
>>> ------------------
>>> Check out the vibrant tech community on one of the world's most
>>> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
>>> _______________________________________________
>>> Factor-talk mailing list
>>> Factor-talk@lists.sourceforge.net
>>> https://lists.sourceforge.net/lists/listinfo/factor-talk
>>>
>>>
>> ------------------------------------------------------------
>> ------------------
>> Check out the vibrant tech community on one of the world's most
>> engaging tech sites, Slashdot.org! http://sdm.link/slashdot______
>> _________________________________________
>> Factor-talk mailing list
>> Factor-talk@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/factor-talk
>>
>
> ------------------------------------------------------------
> ------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> _______________________________________________
> Factor-talk mailing list
> Factor-talk@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/factor-talk
>
>
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to