PyForth: A Forth interpreter written in Python

What will I talk about?

  • What is Forth?
  • PyForth
  • The Forth interpreter + some PyForth snippets

What is Forth?

  • Stack based programming language
  • ~backwards lisp
  • You can access and manipulate the underlying structures
1 2 +
2 PLUS
: PLUS + ;

PyForth

  • Forth written in Python
  • repl and web version
.S DUP * + - = < > OVER SWAP ROT DROP NIP TUCK 2DUP MOD 
: ; ! @ , > R R> R@ R0 IF ELSE THEN DO LOOP I J
.D

DEMO

  • Dictionary
  • StackĀ 
  • Return stack
  • Input stream

The Forth interpreter

def INTERPRET():
    WORD()
    FIND()
    if abs(POP())
        EXECUTE()
    else:
        NUMBER()

PyForth: Interpret

while input_stream:
        if COMPILE:
            COMPILE()
        else:
            INTERPRET()
1 2 3 DUP
: SQUARED DUP * ;
4 SQUARED
Made with Slides.com