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


Groups > comp.lang.forth > #50365

Re: CfV: Quotations

From Bernd Paysan <bernd.paysan@gmx.de>
Newsgroups comp.lang.forth
Subject Re: CfV: Quotations
Date 2016-08-21 22:52 +0000
Organization A noiseless patient Spider
Message-ID <npdbbl$vqt$2@dont-email.me> (permalink)
References (2 earlier) <noqlm8$ag1$3@dont-email.me> <87lgzxuwdr.fsf@jester.gateway.pace.com> <2016Aug16.091357@mips.complang.tuwien.ac.at> <npafji$59r$1@dont-email.me> <2016Aug21.113258@mips.complang.tuwien.ac.at>

Show all headers | View raw


Am Sun, 21 Aug 2016 09:32:58 +0000 schrieb Anton Ertl:

> Ok, so your solution is to have one global variable, and access all the
> other data through that.  I very much doubt that is sufficient, because
> global variables are not sufficient to implement nested Pascal's and GNU
> C's nested functions, either; instead, they do static link chains and
> trampolines.  E.g, I don't think you can do the man-or-boy-test with
> that.

Well, the boy-or-man test isn't really needing better quotations, it's 
needing a bit more OOP.  It wants something applied in a "context", and 
we call that "method".  While you could have many methods, here, one is 
sufficient.  While you could have many different object lifetime schemes, 
stack-allocated here is sufficient.

require mini-oof2.fs

\ define a class for the default values

object class
    field: k
    method b
end-class mob

:noname ( -- n ) k @ ; mob to b
mob new constant m-1  -1 m-1 .k !
mob new constant m0    0 m0 .k !
mob new constant m1    1 m1 .k !

\ define a class for the context

mob class
    field: x1
    field: x2
    field: x3
    field: x4
    field: x5
end-class b-ctx

: a ( k x1 x2 x3 x4 x5 -- result )
    b-ctx new >o
    x5 ! x4 ! x3 ! x2 ! x1 ! k !
    k @ 0<= IF
	x4 @ .b x5 @ .b +
    ELSE
	b
    THEN
    dispose o> ;

:noname ( -- n )
    -1 k +!
    k @ o x1 @ x2 @ x3 @ x4 @ a
; b-ctx to b

: a-n ( n -- n' )
    m1 m-1 m-1 m1 m0 a ;

Test with a bit more return stack space than usual (I just set it to 1G):

27 0 [do] cr [i] dup . a-n . [lOOP]  
0 1 
1 0 
2 -2 
3 0 
4 1 
5 0 
6 1 
7 -1 
8 -10 
9 -30 
10 -67 
11 -138 
12 -291 
13 -642 
14 -1446 
15 -3250 
16 -7244 
17 -16065 
18 -35601 
19 -78985 
20 -175416
21 -389695 
22 -865609 
23 -1922362 
24 -4268854 
25 -9479595 
26 -21051458  ok

Looks good.  If you want OOP, use OOP.  If you want something that is 
slightly less than OOP, just use OOP, too, please.  Unlike nested 
functions, OOP is dead easy to implement.  It really is powerful enough, 
way more powerful than nested functions, as powerful as closures.  As 
quotations are mostly syntactic sugar, they are not needed at all here.

Maybe you want an object on the locals stack to get rid of the relatively 
expensive new/dispose pairs.

-- 
Bernd Paysan
"If you want it done right, you have to do it yourself"
net2o ID: kQusJzA;7*?t=uy@X}1GWr!+0qqp_Cn176t4(dQ*
http://bernd-paysan.de/

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


Thread

CfV: Quotations anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-08-10 16:52 +0000
  Re: CfV: Quotations Matthias Trute <matthias.trute@gmail.com> - 2016-08-10 11:19 -0700
  Re: CfV: Quotations Paul Rubin <no.email@nospam.invalid> - 2016-08-10 11:55 -0700
    Re: CfV: Quotations Bernd Paysan <bernd.paysan@gmx.de> - 2016-08-14 20:52 +0000
      Re: CfV: Quotations hughaguilar96@gmail.com - 2016-08-14 14:42 -0700
        Re: CfV: Quotations Julian Fondren <julian.fondren@gmail.com> - 2016-08-14 16:43 -0700
      Re: CfV: Quotations Paul Rubin <no.email@nospam.invalid> - 2016-08-15 22:16 -0700
        Re: CfV: Quotations anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-08-16 07:13 +0000
          Re: CfV: Quotations Paul Rubin <no.email@nospam.invalid> - 2016-08-18 15:30 -0700
            Re: CfV: Quotations anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-08-19 15:32 +0000
          Re: CfV: Quotations Bernd Paysan <bernd.paysan@gmx.de> - 2016-08-20 20:46 +0000
            Re: CfV: Quotations anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-08-21 09:32 +0000
              Re: CfV: Quotations hughaguilar96@gmail.com - 2016-08-21 11:06 -0700
                Re: CfV: Quotations anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-08-24 17:02 +0000
              Re: CfV: Quotations Bernd Paysan <bernd.paysan@gmx.de> - 2016-08-21 22:52 +0000
                Re: CfV: Quotations anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-08-22 13:54 +0000
                Re: CfV: Quotations Bernd Paysan <bernd.paysan@gmx.de> - 2016-08-22 23:19 +0000
                Re: CfV: Quotations anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-08-24 15:54 +0000
                Re: CfV: Quotations Bernd Paysan <bernd@net2o.de> - 2016-08-28 00:43 +0000
                Re: CfV: Quotations anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-08-28 09:53 +0000
                Re: CfV: Quotations Andrew Haley <andrew29@littlepinkcloud.invalid> - 2016-08-28 08:32 -0500
                Re: CfV: Quotations anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-08-28 14:38 +0000
  Re: CfV: Quotations mhx@iae.nl - 2016-08-10 12:02 -0700
  Re: CfV: Quotations Gerry Jackson <gerry@jackson9000.fsnet.co.uk> - 2016-08-11 17:25 +0100
  Re: CfV: Quotations Julian Fondren <julian.fondren@gmail.com> - 2016-08-11 12:48 -0700
  Re: CfV: Quotations minforth@arcor.de - 2016-08-16 03:31 -0700
  Re: CfV: Quotations Gerry Jackson <gerry@jackson9000.fsnet.co.uk> - 2016-08-31 15:15 +0100
    Re: CfV: Quotations anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-08-31 15:26 +0000
      Re: CfV: Quotations Gerry Jackson <gerry@jackson9000.fsnet.co.uk> - 2016-09-02 22:33 +0100
        Re: CfV: Quotations anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-09-03 06:47 +0000

csiph-web