Starting Forth Example

Oforth examples. Feel free to post your own code.

Starting Forth Example

Postby Doug » 24 Jul 2018 15:57

Code: Select all

\ Last Revision: 23 Jul 2018  08:03:12  dbh
\ Douglas B. Hoffman

\ This program was written to illustrate one way the Leo Brodie
\ Starting Forth program "No Weighting" can be written in Oforth.
\ For comparison to the original SF program see the listing that
\ comes after. Also, a link to the pertinent SF section is:

\ http://home.iae.nl/users/mhx/sf12/sf12.html  "3. No Weighting"

\ *** BEGIN PROGRAM
import: math  \ to get PI and round

tvar: material-in-use

Object Class new: material ( i.d. density tan  )
 m: initialize \ str density tan
    := tan := density := i.d. ;

\       ht^3*pi*D
\ W =  -----------
\       3*tan^2

 m: weight \ ( ht -- ) input height in feet
    3 ^  PI * @density *
    @tan 2 ^  3 * /
    dup round . "pounds of" . @i.d. . "or" .
    2000 /
    <<wjp(System.Out, 7, JUSTIFY_RIGHT, 6)
    " tons" . ;

 m: use self to material-in-use ;
 
: feet \ ( ht -- ) height in feet
  material-in-use weight ;

\ i.d.        density   tan
"cement"        131    .7    material new const: cement
"loose gravel"   93    .649  material new const: loose-gravel
"packed gravel" 100    .7    material new const: packed-gravel
"dry sand"       90    .754  material new const: dry-sand
"wet sand"      118    .9    material new const: wet-sand
"clay"          120    .727  material new const: clay
\ *** END PROGRAM

cement use
10 feet
\ => 279965 pounds of cement or 139.983 tons ok

10.25 feet
\ => 301492 pounds of cement or 150.746 tons ok

dry-sand use
10 feet
\ => 165779 pounds of dry sand or  82.8893 tons ok


false IFTRUE: [

\ for comparison, a standard Forth version based on the program from Starting Forth

VARIABLE density   VARIABLE theta   VARIABLE i.d.

: STRING ( c) WORD DUP C@ 1+ ALLOT ;
: ,"   [CHAR] " STRING ; ( -- c-addr )
: DU.3 ( du --)   <# # # # [CHAR] . HOLD #S #> TYPE SPACE ;

: MATERIAL ( 'string density theta -- )
   CREATE  , , ,
   DOES>   DUP       @  theta !
      CELL+ DUP @ density !
      CELL+     @ i.d. ! ;

: .SUBSTANCE   i.d. @ COUNT TYPE SPACE ;

: CUBE   2DUP OVER 10 M*/  DROP  10 M*/ ; ( d1 -- d2 )
: /TAN   1000 theta @ M*/ ; ( d1 -- d2 ) 

: FEET  ( d -- )
   CUBE  355 339 M*/  density @ 1 M*/
   /TAN /TAN 5 M+  1 10 M*/
   2DUP ." = " D. ." pounds of "  .SUBSTANCE
   1 2 M*/ ."  or " DU.3 ." tons " ;

\ table of materials
\   string-address     density     theta 
   ," cement"           131       700  MATERIAL cement
   ," loose gravel"      93       649  MATERIAL loose-gravel
   ," packed gravel"    100       700  MATERIAL packed-gravel
   ," dry sand"          90       754  MATERIAL dry-sand
   ," wet sand"         118       900  MATERIAL wet-sand
   ," clay"             120       727  MATERIAL clay
 
 
cement

10.0 feet
\ = 279939 pounds of cement or 139.969 tons ok

cr
10.3 feet
\ = 305658 pounds of cement or 152.829 tons ok

cr
dry-sand
10.3 feet
\ = 165763 pounds of dry sand or 82.881 tons ok

]
Doug
 
Posts: 6
Joined: 20 Jul 2018 14:26

Re: Starting Forth Example

Postby Franck » 25 Jul 2018 07:41

Hello Doug,

Interesting, thank you.

btw, your example is related to a new feature that will be added to Oforth in V1.2.
While reworking the outer interpreter, it will be possible to create new kind of words (inherited from the Word class) that will be handled by the interpreter.

If you don't mind, I will post informations based on your example after the V1.2 is released.

Franck
Franck
 
Posts: 148
Joined: 29 Oct 2014 19:01

Re: Starting Forth Example

Postby Doug » 25 Jul 2018 14:02

Hi Franck,

The new feature sounds useful. Although I found this little program very easy to write in Oforth and prefer it to the Forth solution.

-Doug
Doug
 
Posts: 6
Joined: 20 Jul 2018 14:26


Return to Oforth examples

Who is online

Users browsing this forum: No registered users and 1 guest

cron