delta forth

Upload: galkys

Post on 05-Apr-2018

236 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 Delta Forth

    1/22

    Worlds first Forth compiler for the .NET platformValer BOCAN, PhD

  • 7/31/2019 Delta Forth

    2/22

    What is Forth anyway?

    Reverse Polish Notation Stack-based programming

    Delta Forth .NET

    Demonstration

  • 7/31/2019 Delta Forth

    3/22

    Procedural, stack-based language that iscompiled and interpreted at the same time

    Developed by Chuck Moore at the US NationalRadio Astronomy Observatory in the early 1970s

    Procedures are called words that are grouped in

    vocabularies.

    Forth is a meta-language, as new words can formnew specialized Forth-like languages

  • 7/31/2019 Delta Forth

    4/22

    Popular for developing embedded systems.

    Implemented on RISC processors Open Firmware boot ROMs used by Apple, IBM,

    and Sun Microsystems

    First stage boot controller for FreeBSD

    Software that controlled the robotic arm thatrepaired the Hubble Space Telescope

  • 7/31/2019 Delta Forth

    5/22

    50-70% of a typical Forth implementation iswritten in Forth

    Implementation effort is small; a singledeveloper can create a Forth system on a newplatform in months

    Delta Forth .NET was implemented in just 5weeks

  • 7/31/2019 Delta Forth

    6/22

    What is Forth anyway?

    Reverse Polish Notation Stack-based programming

    Delta Forth .NET

    Demonstration

  • 7/31/2019 Delta Forth

    7/22

    Invented by Charles Hamblin in 1950s to enablezero-address memory stores

    In postfix notation, the operators follow theiroperands, i.e. 3 + 5 becomes 3 5 +

    RPN obviates the needs for parentheses

    otherwise required by infix notation

    Reading from left to right, calculations can occuras soon as an operator is read

  • 7/31/2019 Delta Forth

    8/22

    2

    5

    +

    7

    10

    4

    -

    *

    6

    42

    Take infix expression (2 + 5) * (10 4)

    This translates to postfix expression 2 5 + 10 4 - *

    See how the postfix expression is evaluated usinga stack-based algorithm

  • 7/31/2019 Delta Forth

    9/22

    What is Forth anyway?

    Reverse Polish Notation

    Stack-based programming

    Delta Forth .NET

    Demonstration

  • 7/31/2019 Delta Forth

    10/22

    Unusual programming style, close to machine

    Forth uses two stacks:

    Parameter stack, used to transmit parametersbetween words

    Return stack, used to hold the return address of the

    word caller and some counter for loop functions There are some powerful primitives to handle the

    stacks

  • 7/31/2019 Delta Forth

    11/22

    DUP duplicates the topmost element on thestack

    33

    20

    9

    33

    33

    20

    9

  • 7/31/2019 Delta Forth

    12/22

    DROP drops the topmost element on the stack

    33

    20

    9

    20

    9

  • 7/31/2019 Delta Forth

    13/22

    SWAP swaps the two topmost elements on thestack

    33

    20

    9

    20

    33

    9

  • 7/31/2019 Delta Forth

    14/22

    OVER duplicates the second topmost elementon the stack

    33

    20

    9

    20

    33

    20

    9

  • 7/31/2019 Delta Forth

    15/22

    ROT rotates the three topmost elements on thestack

    33

    20

    9

    33

    20

    10

    9

    10

  • 7/31/2019 Delta Forth

    16/22

    What is Forth anyway?

    Reverse Polish Notation

    Stack-based programming

    Delta Forth .NET

    Demonstration

  • 7/31/2019 Delta Forth

    17/22

    Sequel of the Delta Forth for Java project (1997)

    C# command-driven application

    This dialect is not interpreted, the generatedcode is fully managed

    Supports Forth at the .NET consumer level (i.e.no new .NET classes can be derived)

    Supports interoperability with other .NETlibraries

  • 7/31/2019 Delta Forth

    18/22

    Perhaps the most odd looking Hello World outthere

    : main \ Entry point

    ."Hello World"

    ;

  • 7/31/2019 Delta Forth

    19/22

    Finding prime numbers (up to 400 in this case)

    400 constant limit

    : isprime \ Returns 1 if the

    number on top of stack is a prime

    number

    2

    begin

    over over mod 0= 0=

    rot rot dup >r

    over 2 / > 0=

    rot and r> swap

    while

    1+

    repeat

    over 2 / >

    ;

    : main \ Entry point

    ."Prime numbers up to " limit . .":"

    limit 1 do

    i isprime

    if i . space then

    loop

    ;

  • 7/31/2019 Delta Forth

    20/22

    Euclids algorithm4330 constant num1 \ The first number

    8235 constant num2 \ The second number

    ( Word to test if value on top of stack is equal to

    or less than 0 )

    : ZeroLessEqual

    dup 0=

    swap 0