\ a few forth examples. \ to load into bashforth, do: \ include demo.f \ these require bashforth 0.37a, because of the \ recent addition of ?do, used in some examples \ -------------- example 1 -------------- \ standard beginner example: : star 42 emit ; : stars 0 ?do star loop ; 10 stars \ -------------- example 2 -------------- \ catching errors: : new ( -- ) cr ." starting a new interpreter" prompt ['] quit catch ?dup if cr ." interpreter aborted with error:" throw then ; \ execute "new" to start new interpreter, \ then cause an error, for example, by trying \ to execute a non-existing word \ -------------- example 3 -------------- \ beginners example again, using create> does \ a word, creating words which emit characters : emits ( c -- ) create , does> @ emit ; \ a word, creating words which can execute a word n times : plural ( a -- ) create , does> @ swap 0 ?do dup >r execute r> loop drop ; \ -- using the above two words -- 46 emits dot ' dot plural dots 5 dots \ -------------- example 4 -------------- \ -- using create does> to build new datatype: array -- : array ( n -- ) create allot does> + ; \ -- putting arrays to use -- : inc ( a -- ) dup @ 1+ swap ! ; variable #contacts : contact ( -- ) #contacts @ constant #contacts inc ; contact alf contact ben contact carl contact dee #contacts @ array age \ define array with name age, #contacts elements 25 carl age ! 28 alf age ! 22 dee age ! 32 ben ! .( alf's age: ) alf age @ .