6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sun May 05, 2024 10:26 am

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Wed Oct 07, 2015 8:41 am 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10797
Location: England
There's a very interesting thread over at HP Museum, where Thomas Klemm has noted that Python is a convenient high-level language which compiles to bytecode and which includes introspection - so you can automatically convert from python, via bytecode, to a machine language of choice. Python can disassemble its own bytecode so the transformations can be text-based.

The whole thing works well if the machine language of choice has a structural resemblance to python bytecode - in this case it's Focal, which runs on the HP-41C calculator. Perhaps one could convert to a virtual machine like Forth or Sweet 16, or perhaps to assembly language, perhaps in a threaded style.

Thomas put up an example you can run in your browser. His 300 line program converts
Code:
def gcd(a, b):
    while b != 0:
        a, b = b, a % b
    return a
to
Code:
LBL "GCD"
STO 01 ; b
RDN
STO 00 ; a
RDN
LBL 00
RCL 01 ; b
0
X=Y?
GTO 01
RCL 01 ; b
RCL 00 ; a
RCL 01 ; b
MOD
X<>Y
STO 00 ; a
RDN
STO 01 ; b
RDN
GTO 00
LBL 01
LBL 02
RCL 00 ; a
RTN


Top
 Profile  
Reply with quote  
PostPosted: Wed Oct 07, 2015 11:21 am 
Offline
User avatar

Joined: Mon Apr 23, 2012 12:28 am
Posts: 760
Location: Huntsville, AL
Very interesting.

_________________
Michael A.


Top
 Profile  
Reply with quote  
PostPosted: Thu Nov 12, 2015 5:28 pm 
Offline

Joined: Sun Feb 23, 2014 2:43 am
Posts: 78
I contribute to a java bytecode compiler project (java_grinder), which is based on a similar concept. Some random thoughts:

-It's totally worth doing this with Python, especially if you can work out a way to call ML routines. Then its just like mixing BASIC/ML back in the day - but with a much nicer & faster language.

-You can also interpret the bytecode as you see fit. For example, my efforts were limited to 16-bit integers and lack floating-point support.

-Using a separate data stack made life a lot easier, see:
http://wilsonminesco.com/stacks/virtualstacks.html

Working on this sort of stuff has been very rewarding for me, I highly encourage it.


Top
 Profile  
Reply with quote  
PostPosted: Fri Jan 15, 2016 1:56 pm 
Offline

Joined: Thu Jan 14, 2016 5:24 pm
Posts: 27
I've actually used Python to compile my Forth-83 implementation for the VIC-20. The input source code is entirely Forth (based on the 6502 reference model) and the compiler generates a binary image which contains the elemental words (compiled from Forth ASSEMBLER words) and the rest as a list of constituent words.

Using Python meant building and testing the compiler was easy, I have classes for stacks, target code blocks etc and I can dump the contents of the dictionary to help with debugging.


Top
 Profile  
Reply with quote  
PostPosted: Fri Jan 15, 2016 3:28 pm 
Offline
User avatar

Joined: Fri Dec 11, 2009 3:50 pm
Posts: 3351
Location: Ontario, Canada
srowe wrote:
I've actually used Python to compile my Forth-83 implementation for the VIC-20. The input source code is entirely Forth (based on the 6502 reference model)
Welcome, srowe. Nice work defying the "chicken or the egg" problem! As you know, it's usually necessary to use a conventional assembler to create the Forth kernal, which means low-level definitions in the reference code (written in the RPN syntax expected by Forth's own assembler) can't be used.

You may wish to elaborate on what you've done by starting a thread in the Forth section of the forum.

Cheers,
Jeff

_________________
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html


Top
 Profile  
Reply with quote  
PostPosted: Fri Jan 15, 2016 4:37 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10797
Location: England
Yes, welcome! I see there is a thread elsewhere (V-FORTH - Forth-83 for the VIC) with an attachment too.


Top
 Profile  
Reply with quote  
PostPosted: Sun Jan 17, 2016 9:50 am 
Offline

Joined: Thu Jan 14, 2016 5:24 pm
Posts: 27
Thanks. That version is a little out of date (but the git URL is still valid). I'll create a post in the Forth section shortly.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: