Post Reply 
Python to FOCAL Compiler
10-06-2015, 07:55 PM (This post was last modified: 10-06-2015 08:09 PM by Thomas Klemm.)
Post: #7
RE: Python to FOCAL Compiler
(10-06-2015 11:16 AM)Ángel Martin Wrote:  Maybe I should start using Python, this is a good incentive...

You can use Python in your browser if you don't have access to an installation: Python to FOCAL Compiler

Just run the program and the function gcd will be translated. You can create your own functions either in the listing on the left (but then you have to run the program again) or then directly in the REPL in the right window.

You can translate a function to FOCAL:
Code:
>>> translate(gcd.__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

And then you can compare it with the disassembled byte-code:
Code:
>>> dis.dis(gcd)
 27           0 SETUP_LOOP              33 (to 36)
        >>    3 LOAD_FAST                1 (b)
              6 LOAD_CONST               1 (0)
              9 COMPARE_OP               3 (!=)
             12 POP_JUMP_IF_FALSE       35

 28          15 LOAD_FAST                1 (b)
             18 LOAD_FAST                0 (a)
             21 LOAD_FAST                1 (b)
             24 BINARY_MODULO
             25 ROT_TWO
             26 STORE_FAST               0 (a)
             29 STORE_FAST               1 (b)
             32 JUMP_ABSOLUTE            3
        >>   35 POP_BLOCK

 29     >>   36 LOAD_FAST                0 (a)
             39 RETURN_VALUE

Of course you can also just use a function:
Code:
>>> gcd(91, 35)
7
>>> factor(5040)
2
2
2
2
3
3
5
7

Cheers
Thomas
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Python to FOCAL Compiler - Thomas Klemm - 10-05-2015, 10:58 PM
RE: Python to FOCAL Compiler - Sukiari - 10-06-2015, 03:36 AM
RE: Python to FOCAL Compiler - Thomas Klemm - 10-06-2015 07:55 PM
Java to FOCAL Compiler - Thomas Klemm - 10-07-2015, 04:38 PM



User(s) browsing this thread: 1 Guest(s)