⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lessons.txt

📁 eForth is a small portable Forth design for a wide range of microprocessors. This is the first imple
💻 TXT
📖 第 1 页 / 共 2 页
字号:
( Example 1.      The Universal Greeting )

DECIMAL

: HELLO CR ." Hello, world!" ;

( Example 2.      The Big F )

: bar   CR ." *****" ;
: post  CR ." *    " ;
: F     bar post bar post post post ;

( Type 'F' and a return on your keyboard, and you will see a large
F character displayed on the screen )

( Example 3.      FIG, Forth Interest Group )

: center CR ."   *  " ;
: sides  CR ." *   *" ;
: triad1 CR ." * * *" ;
: triad2 CR ." **  *" ;
: triad3 CR ." *  **" ;
: triad4 CR ."  *** " ;
: quart  CR ." ** **" ;
: right  CR ." * ***" ;
: bigT  bar center center center center center center ;
: bigI  center center center center center center center ;
: bigN  sides triad2 triad2 triad1 triad3 triad2 sides ;
: bigG  triad4 sides post right triad1 sides triad4 ;
: FIG  F bigI bigG ;


( Example 4.      Repeated Patterns 

FOR     [ index -- ]            Set up loop given the index.
NEXT    [ -- ]                  Decrement index by 1.  If index<0, exit.                          If index=limit, exit loop; otherwise
                                Otherwise repeat after FOR.
R@      [ -- index ]            Return the current loop index. )


VARIABLE WIDTH                  ( number of asterisks to print )

: ASTERISKS ( -- , print n asterisks on the screen, n=width )
        WIDTH @                 ( limit=width, initial index=0 )
        FOR ." *"               ( print one asterisk at a time )
        NEXT                    ( repeat n times )
        ;

: RECTANGLE ( height width -- , print a rectangle of asterisks )
        WIDTH !                 ( initialize width to be printed )
        FOR     CR
                ASTERISKS       ( print a line of asterisks )
        NEXT
        ;

: PARALLELOGRAM ( height width -- )
        WIDTH !
        FOR     CR R@ SPACES    ( shift the lines to the right )
                ASTERISKS       ( print one line )
        NEXT
        ;

: TRIANGLE ( width -- , print a triangle area with asterisks )
        FOR     CR
                R@ WIDTH !      ( increase width every line )
                ASTERISKS       ( print one line )
        NEXT
        ;

( Try the following instructions:

        3 10 RECTANGLE
        5 18 PARALLELOGRAM
        12 TRIANGLE  )


( Example 5.      The Theory That Jack Built )
( This example shows you how to build a hiararchical structure in Forth)

DECIMAL

: the           ." the " ;
: that          CR ." That " ;
: this          CR ." This is " the ;
: jack          ." Jack Builds" ;
: summary       ." Summary" ;
: flaw          ." Flaw" ;
: mummery       ." Mummery" ;
: k             ." Constant K" ;
: haze          ." Krudite Verbal Haze" ;
: phrase        ." Turn of a Plausible Phrase" ;
: bluff         ." Chaotic Confusion and Bluff" ;
: stuff         ." Cybernatics and Stuff" ;
: theory        ." Theory " jack ;
: button        ." Button to Start the Machine" ;
: child         ." Space Child with Brow Serene" ;
: cybernatics   ." Cybernatics and Stuff" ;

: hiding        CR ." Hiding " the flaw ;
: lay           that ." Lay in " the theory ;
: based         CR ." Based on " the mummery ;
: saved         that ." Saved " the summary ;
: cloak         CR ." Cloaking " k ;
: thick         IF that ELSE CR ." And " THEN
                ." Thickened " the haze ;
: hung          that ." Hung on " the phrase ;
: cover         IF that ." Covered "
                ELSE CR ." To Cover "
                THEN bluff ;
: make          CR ." To Make with " the cybernatics ;
: pushed        CR ." Who Pushed " button ;
: without       CR ." Without Confusion, Exposing the Bluff" ;

: rest                                  ( pause for user interaction )
        ." . "                          ( print a period )
        10 SPACES                       ( followed by 10 spaces )
        KEY                             ( wait the user to press a key )
        DROP CR CR CR ;

(
KEY     [ -- char ]             Wait for a keystroke, and return the
                                ASCII code of the key pressed.
DROP    [ n -- ]                Discard the number.
SPACE   [ -- ]                  Display a blank.
SPACES  [ n -- ]                Display n blanks.
IF      [ f -- ]                If the flag is 0, skip the following
                                instructions up to ELSE or THEN.  If
                                flag is not 0, execute the following
                                instructions up to ELSE and skip to
                                THEN.
ELSE    [ -- ]                  Skip the following instructions
                                up to THEN.
THEN    [ -- ]                  Terminate an IF-ELSE-THEN structure
                                or an IF-THEN structure.
)

: cloaked cloak saved based hiding lay rest ;

: THEORY
        CR this theory rest
        this flaw lay rest
        this mummery hiding lay rest
        this summary based hiding lay rest
        this k saved based hiding lay rest
        this haze cloaked
        this bluff hung 1 thick cloaked
        this stuff 1 cover hung 0 thick cloaked
        this button make 0 cover hung 0 thick cloaked
        this child pushed
                CR ." That Made with " cybernatics without hung
                CR ." And, Shredding " the haze cloak
                CR ." Wrecked " the summary based hiding
                CR ." And Demolished " the theory rest
        ;

( Type THEORY to start)



( Example 6.      Help )
( How to use Forth interpreter to carry on a dialog )

: question
        CR CR ." Any more problems you want to solve?"
        CR ." What kind ( sex, job, money, health ) ?"
        CR
        ;

: help  CR
        CR ." Hello!  My name is Creating Computer."
        CR ." Hi there!"
        CR ." Are you enjoying yourself here?"
        KEY 32 OR 89 =
        CR
        IF      CR ." I am glad to hear that."
        ELSE    CR ." I am sorry about that."
                CR ." maybe we can brighten your visit a bit."
        THEN
        CR ." Say!"
        CR ." I can solved all kinds of problems except those dealing"
        CR ." with Greece. "
        question
        ;

: sex   CR CR ." Is your problem TOO MUCH or TOO LITTLE?"
        CR
        ;

: too  ;                                ( noop for syntax smoothness )

: much  CR CR ." You call that a problem?!!  I SHOULD have that problem."
        CR ." If it reall y bothers you, take a cold shower."
        question
        ;

: little
        CR CR ." Why are you here!"
        CR ." You should be in Tokyo or New York of Amsterdam or"
        CR ." some place with some action."
        question
        ;

: health
        CR CR ." My advise to you is:"
        CR ."      1. Take two tablets of aspirin."
        CR ."      2. Drink plenty of fluids."
        CR ."      3. Go to bed (along) ."
        question
        ;

: job   CR CR ." I can sympathize with you."
        CR ." I have to work very long every day with no pay."
        CR ." My advise to you, is to open a rental computer store."
        question
        ;

: money
        CR CR ." Sorry!  I am broke too."
        CR ." Why don't you sell encyclopedias of marry"
        CR ." someone rich or stop eating, so you won't "
        CR ." need so much money?"
        question
        ;

: HELP help ;
: H help ;
: h help ;

( Type 'help' to start )


( Example 7.  Money Exchange 

The first example we will use to demonstrate how numbers are
used in Forth is a money exchange program, which converts money
represented in different currencies.  Let's start with the
following currency exchange table:

        33.55 NT        1 Dollar
        7.73 HK         1 Dollar
        9.47 RMB        1 Dollar
        1 Ounce Gold    285 Dollars
        1 Ounce Silver  4.95 Dollars )

DECIMAL

: NT    ( nNT -- $ )    100 3355 */  ;
: $NT   ( $ -- nNT )    3355 100 */  ;
: RMB   ( nRMB -- $ )   100 947 */  ;
: $RMB  ( $ -- nJmp )   947 100 */  ;
: HK    ( nHK -- $ )    100 773 */  ;
: $HK   ( $ -- $ )      773 100 */  ;
: GOLD  ( nOunce -- $ ) 285 *  ;
: $GOLD ( $ -- nOunce ) 285 /  ;
: SILVER ( nOunce -- $ ) 495 100 */  ;
: $SILVER ( $ -- nOunce ) 100 495 */  ;
: OUNCE ( n -- n, a word to improve syntax )  ;
: DOLLARS ( n -- )      . ;

( With this set of money exchange words, we can do some tests:

        5 ounce gold .
        10 ounce silver .
        100 $NT .
        20 $RMB .

If you have many different currency bills in your wallet, you
can add then all up in dollars:

        1000 NT 500 HK + .S
        320 RMB + .S
        DOLLARS ( print out total worth in dollars )


( Example 8. Temperature Conversion

Converting temperature readings between Celcius and Farenheit
is also an interesting problem.  The difference between temperature
conversion and money exchange is that the two temperature scales
have an offset besides the scaling factor. )

: F>C ( nFarenheit -- nCelcius )
        32 -
        10 18 */
        ;

: C>F ( nCelcius -- nFarenheit )
        18 10 */
        32 +
        ;

( Try these commands

90 F>C .        shows the temperature in a hot summer day and
0 C>F .         shows the temperature in a cold winter night. 

In the above examples, we use the following Forth arithmatic
operators:

+       [ n1 n2 -- n1+n2 ]      Add n1 and n2 and leave sum on stack.
-       [ n1 n2 -- n1-n2 ]      Subtract n2 from n1 and leave differrence
                                on stack.
*       [ n1 n2 -- n1*n2 ]      Multiply n1 and n2 and leave product
                                on stack.
/       [ n1 n2 -- n1/n2 ]      Divide n1 by n2 and leave quotient on
                                stack.
*/      [ n1 n2 n3 -- n1*n2/n3] Multiply n1 and n2, divide the product
                                by n3 and leave quotient on the stack.
.S      [ ... -- ... ]          Show the topmost 4 numbers on stack.
)


( Example 9.  Weather Reporting. )

: WEATHER ( nFarenheit -- )
        DUP     55 <
        IF      ." Too cold!" DROP
        ELSE    85 <
                IF      ." About right."
                ELSE    ." Too hot!"
                THEN
        THEN
        ;


( You can type the following instructions and get some responses from the
computer:

        90 WEATHER Too hot!
        70 WEATHER About right.
        32 WEATHER Too cold.
)

 

( Example 10.  Print the multiplication table )

: ONEROW ( nRow -- )
        CR
        DUP 3 .R 3 SPACES
        1 11
        FOR     2DUP *
                4 .R
                1 +
        NEXT
        DROP ;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -