forth family[1]

Upload: maraamo

Post on 09-Apr-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 Forth Family[1]

    1/14

    Forth Language FamilyForth Language FamilyComputer Science Department

    Arkansas State University

    Computer Science Department

    Arkansas State University

  • 8/8/2019 Forth Family[1]

    2/14

    Forth

    Chuck Moore, late 1960s

    postfix notation, stack-based

    simple type system, low-level

    threaded execution, VM and native

    compact, interactive, extendableenvironment

    bottom-up programming

  • 8/8/2019 Forth Family[1]

    3/14

    Forth

    Standardized:

    FIG-Forth, Forth-77, Forth-79, Forth-83, ANSI-

    Forth (94)

    Environment:

    compiler, editor, assembler, (disassembler,

    decompiler,) file/disk manager, dictionary

    pforth: http://www.softsynth.com/pforth/

    gforth:

    http://www.complang.tuwien.ac.at/forth/gforth/

  • 8/8/2019 Forth Family[1]

    4/14

    Forth Examples

    : STAR [CHAR] * EMIT ;

    : STARS 0 DO STAR LOOP CR ;

    : SQUARE DUP 0 DO DUP STARS LOOP

    DROP ;

    : TRIANGLE 1 DO I STARS LOOP ;

    : TOWER ( n -- ) DUP TRIANGLE SQUARE ;

  • 8/8/2019 Forth Family[1]

    5/14

    Forth Examples

    : STAR ( -- ) \ Print a single star

    42 EMIT ; \ 42 is the ASCII code for *

    : STARS ( n -- ) \ Print n stars

    0 DO STAR LOOP ; \ Loop n times (0 up to n-1) and execute STAR

    : SQUARE ( n -- ) \ Print an n-line square of stars

    DUP 0 DO \ Loop n times, keeping (DUP-licating) n on the

    stack

    DUP STARS CR \ Each time, print n stars then print CR

    LOOP DROP ; \ After loop is done, drop the n from the stack

    : TRIANGLE ( n -- ) \ Print an n-line triangle

    1 + 1 DO \ Loop n times from 1 to n (instead of 0 to n-1)

    I STARS CR \ This time use the inner loop index I

    LOOP ;

    : TOWER ( n -- ) \ Print a "tower" with an base of size n

  • 8/8/2019 Forth Family[1]

    6/14

    Forth Examples

    CR 7 STARS \ user types this line, output follows

    ******* ok

    CR 3 TRIANGLE \ user types this line, output follows

    *

    **

    ***

    ok

    CR 6 TOWER \ user types this line, output follows

    *

    **

    ***

    ****

    *****

    ******

    ******

    ******

    ******

  • 8/8/2019 Forth Family[1]

    7/14

    Forth Examples \ fib ( x -- , returns first x numbers in Fibonacci sequence )

    : fib { x -- } \ local index variable x

    1 dup . \ initialize sequence & print initial

    values

    1 dup .

    begin

    over over + \ add previous 2 numbers in sequence

    dup . rot drop \ add new number to stack, drop bottomnumber

    x 1- dup -> x 3 < \ decrement x, exit if x < 3

    until

    drop drop \ drop top 2 elements off stack

    ;

  • 8/8/2019 Forth Family[1]

    8/14

    Postscript

    Created by Adobe

    Used as language for vector-based

    graphics programming

    Current version 3.0

    ghostscript:

    http://pages.cs.wisc.edu/~ghost/

  • 8/8/2019 Forth Family[1]

    9/14

    Postscript Example %!PS-Adobe-3.0

    /inch {72 mul} def

    /Times-Roman findfont 50

    scalefont setfont

    2.5 inch 5 inch moveto

    (Hello, World!) show

    showpage

  • 8/8/2019 Forth Family[1]

    10/14

    Joy

    website:

    http://www.latrobe.edu.au/philosophy/phimvt/joy.html

    stack-based

    purely functionalconcatenative

    uses quotation and combinators

  • 8/8/2019 Forth Family[1]

    11/14

    Joy Example

    DEFINE square == dup * ; cube == dup dup * * .

    DEFINE factorial ==[0 =] [pop 1] [dup 1 -

    factorial *] ifte .

  • 8/8/2019 Forth Family[1]

    12/14

    Factor

    website: http://factorcode.org/dynamic typing

    object system

    garbage collection

    reflection

  • 8/8/2019 Forth Family[1]

    13/14

    Factor Examples USE: io

    IN: hello-world

    : hello ( --

    ) "Hello world" print ;

    MAIN: hello

  • 8/8/2019 Forth Family[1]

    14/14

    Factor Examples

    ! Copyright (C) 2008 Slava Pestov.

    ! See http://factorcode.org/license.txt for BSD license.

    USING: accessors calendar calendar.format io io.encodings.ascii

    io.servers kernel threads ;

    IN: time-server

    : handle-time-client ( -- )

    now timestamp>rfc822 print ;

    : ( -- threaded-server )

    ascii

    "time-server" >>name

    1234 >>insecure

    [ handle-time-client ] >>handler ;

    : start-time-server ( -- )

    start-server drop ;