delta forth .net
DESCRIPTION
World’s first Forth compiler for the .NET platform. Delta Forth .NET. Valer BOCAN , PhD. Agenda. What is Forth anyway? Reverse Polish Notation Stack-based programming Delta Forth .NET Demonstration. What is Forth anyway?. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Delta Forth .NET](https://reader036.vdocuments.us/reader036/viewer/2022081507/56816723550346895ddbae1d/html5/thumbnails/1.jpg)
Delta Forth .NETWorld’s first Forth compiler for the .NET platform Valer BOCAN, PhD
![Page 2: Delta Forth .NET](https://reader036.vdocuments.us/reader036/viewer/2022081507/56816723550346895ddbae1d/html5/thumbnails/2.jpg)
Agenda What is Forth anyway? Reverse Polish Notation Stack-based programming Delta Forth .NET Demonstration
![Page 3: Delta Forth .NET](https://reader036.vdocuments.us/reader036/viewer/2022081507/56816723550346895ddbae1d/html5/thumbnails/3.jpg)
What is Forth anyway? Procedural, stack-based language that
is compiled and interpreted at the same time
Developed by Chuck Moore at the US National Radio Astronomy Observatory in the early 1970s
Procedures are called words that are grouped in vocabularies.
Forth is a meta-language, as new words can form new specialized Forth-like languages
![Page 4: Delta Forth .NET](https://reader036.vdocuments.us/reader036/viewer/2022081507/56816723550346895ddbae1d/html5/thumbnails/4.jpg)
Where is Forth used? 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
that repaired the Hubble Space Telescope
![Page 5: Delta Forth .NET](https://reader036.vdocuments.us/reader036/viewer/2022081507/56816723550346895ddbae1d/html5/thumbnails/5.jpg)
Implementing Forth 50-70% of a typical Forth
implementation is written in… Forth Implementation effort is small; a single
developer can create a Forth system on a new platform in months
Delta Forth .NET was implemented in just 5 weeks
![Page 6: Delta Forth .NET](https://reader036.vdocuments.us/reader036/viewer/2022081507/56816723550346895ddbae1d/html5/thumbnails/6.jpg)
Agenda What is Forth anyway? Reverse Polish Notation Stack-based programming Delta Forth .NET Demonstration
![Page 7: Delta Forth .NET](https://reader036.vdocuments.us/reader036/viewer/2022081507/56816723550346895ddbae1d/html5/thumbnails/7.jpg)
Reverse Polish Notation (RPN) Invented by Charles Hamblin in 1950’s
to enable zero-address memory stores In postfix notation, the operators follow
their operands, 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 occur as soon as an operator is read
![Page 8: Delta Forth .NET](https://reader036.vdocuments.us/reader036/viewer/2022081507/56816723550346895ddbae1d/html5/thumbnails/8.jpg)
RPN Sample
25+
7104-
*
642
Take infix expression (2 + 5) * (10 – 4) This translates to postfix expression 2 5
+ 10 4 - * See how the postfix expression is
evaluated using a stack-based algorithm
![Page 9: Delta Forth .NET](https://reader036.vdocuments.us/reader036/viewer/2022081507/56816723550346895ddbae1d/html5/thumbnails/9.jpg)
Agenda What is Forth anyway? Reverse Polish Notation Stack-based programming Delta Forth .NET Demonstration
![Page 10: Delta Forth .NET](https://reader036.vdocuments.us/reader036/viewer/2022081507/56816723550346895ddbae1d/html5/thumbnails/10.jpg)
Stack-based programming Unusual programming style, close to machine Forth uses two stacks:
Parameter stack, used to transmit parameters between 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
![Page 11: Delta Forth .NET](https://reader036.vdocuments.us/reader036/viewer/2022081507/56816723550346895ddbae1d/html5/thumbnails/11.jpg)
Forth primitives (1/5) DUP – duplicates the topmost element
on the stack
33209
3333209
DUP
![Page 12: Delta Forth .NET](https://reader036.vdocuments.us/reader036/viewer/2022081507/56816723550346895ddbae1d/html5/thumbnails/12.jpg)
Forth primitives (2/5) DROP – drops the topmost element on
the stack
33209
209
DROP
![Page 13: Delta Forth .NET](https://reader036.vdocuments.us/reader036/viewer/2022081507/56816723550346895ddbae1d/html5/thumbnails/13.jpg)
Forth primitives (3/5) SWAP – swaps the two topmost
elements on the stack
33209
20339
SWAP
![Page 14: Delta Forth .NET](https://reader036.vdocuments.us/reader036/viewer/2022081507/56816723550346895ddbae1d/html5/thumbnails/14.jpg)
Forth primitives (4/5) OVER – duplicates the second topmost
element on the stack
33209
2033209
OVER
![Page 15: Delta Forth .NET](https://reader036.vdocuments.us/reader036/viewer/2022081507/56816723550346895ddbae1d/html5/thumbnails/15.jpg)
Forth primitives (5/5) ROT – rotates the three topmost
elements on the stack
33209
3320109
ROT10
![Page 16: Delta Forth .NET](https://reader036.vdocuments.us/reader036/viewer/2022081507/56816723550346895ddbae1d/html5/thumbnails/16.jpg)
Agenda What is Forth anyway? Reverse Polish Notation Stack-based programming Delta Forth .NET Demonstration
![Page 17: Delta Forth .NET](https://reader036.vdocuments.us/reader036/viewer/2022081507/56816723550346895ddbae1d/html5/thumbnails/17.jpg)
Delta Forth .NET Sequel of the Delta Forth for Java project
(1997) C# command-driven application This dialect is not interpreted, the generated
code is fully managed Supports Forth at the .NET consumer level
(i.e. no new .NET classes can be derived) Supports interoperability with other .NET
libraries
![Page 18: Delta Forth .NET](https://reader036.vdocuments.us/reader036/viewer/2022081507/56816723550346895ddbae1d/html5/thumbnails/18.jpg)
Hello World in Forth Perhaps the most odd looking Hello
World out there: main \ Entry point
."Hello World"
;
![Page 19: Delta Forth .NET](https://reader036.vdocuments.us/reader036/viewer/2022081507/56816723550346895ddbae1d/html5/thumbnails/19.jpg)
Forth Sample (1/2) 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;
![Page 20: Delta Forth .NET](https://reader036.vdocuments.us/reader036/viewer/2022081507/56816723550346895ddbae1d/html5/thumbnails/20.jpg)
Forth Sample (2/2) Euclid’s algorithm
4330 constant num1 \ The first number8235 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< or;
: gcd (num1 num2 - - -) over ZeroLessEqual if (num1 is <= 0) drop drop else dup ZeroLessEqual if (num2 is <= 0) drop drop else begin over over = if (We've got the result) . else over over > if
swap then over - 0 then until then then;
: main \ Entry point ."GCD of " num1 . ." and " num2 . ." is " num1 num2 gcd cr;
![Page 21: Delta Forth .NET](https://reader036.vdocuments.us/reader036/viewer/2022081507/56816723550346895ddbae1d/html5/thumbnails/21.jpg)
Agenda What is Forth anyway? Reverse Polish Notation Stack-based programming Delta Forth .NET Demonstration