[OT] Eternal programming
Roman Suzi
rnd at onego.ru
Tue Jul 10 11:45:18 EDT 2001
On Tue, 10 Jul 2001, Mikael Olofsson wrote:
> INTERCAL or Brainf***, anyone?
The following script was written in half an hour
(after I found this page:
http://chemlab.pc.maricopa.edu/pocket/pfmanual.html
(Chemists are going to use Forth in nanotech programming. ;-)
#!/usr/bin/python
# -*- mode: python -*-
# future core Eternal/Python implementation ;-)
# This code is in Public Domain
# $Id:$
global defining, stack, dictionary, executionstack
defining = 0
def _DOT():
global stack
try:
v = stack.pop()
print v
return 1
except:
return None
def _ADD():
global stack
try:
v2 = stack.pop()
v1 = stack.pop()
stack.append(v1+v2)
return 1
except:
return None
stack = []
dictionary = {
"." : {'immediate': 0, 'body': _DOT},
"+" : {'immediate': 0, 'body': _ADD},
}
def isnumber(x):
try:
int(x)
except:
return None
return x
def builtin(word):
return type(word['body']) == type(builtin)
def SEARCH(token):
global dictionary
try:
return dictionary[token]
except:
return None
def EXECUTE(word):
if builtin(word):
return word['body']()
else:
return INTERPRETE(word['body'])
def EMPTYSTACK():
raise "Empty stack"
def WHAT_IS_IT():
raise "???"
def COMPILE(token):
pass
def LITERAL(token):
pass
def INTERPRET(tokens):
global defining, stack
for token in tokens:
found = SEARCH(token)
if found:
if not defining or found['immediate']:
if not EXECUTE(found):
EMPTYSTACK()
else:
COMPILE(token)
else:
if isnumber(token):
if not defining:
stack.append(int(token))
else:
LITERAL(token)
else:
WHAT_IS_IT(token)
return 1
INTERPRET(["1", "2", "+", "."])
# End of interpreter.py
TODO:
- executionstack instead of Python-based recursion
- compiling part of the story
- what is MCOMPILE and is it useful enough to complicate things?
- token reader
- where globals should live? (multithreading-safely)
- error handling
It needs courage to replace Forth's machine-code orientedness into
very-high Python structures ;-) I just can't decide if I want to emulate
memory arrays or do it in free fashion ;-)
Sincerely yours, Roman A.Suzi
--
- Petrozavodsk - Karelia - Russia - mailto:rnd at onego.ru -
More information about the Python-list
mailing list