Created
December 10, 2012 15:59
-
-
Save tangentstorm/4251447 to your computer and use it in GitHub Desktop.
PL/0 to retro compiler with recursion! :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# program to demonstrate recursion. | |
# prints a 4-digit binary number | |
const | |
digits = 4; | |
# no parametrs in this language, so we use globals | |
var | |
number, # the number to convert to binary | |
cursor; # the current digit position ( moves high to low ) | |
procedure helper; | |
var bit; | |
begin | |
# we'll use the built in "odd" operator to test the lowest | |
# bit, and then divide by two to discard it. | |
bit := 0; | |
if odd number then bit := 1; | |
number := number / 2; | |
# since we want to print the bits from highest to lowest, | |
# but we're starting with the lowest, we need to put the | |
# the recursive call in the middle. | |
if cursor > 0 then | |
begin | |
cursor := cursor - 1; | |
call helper | |
end; | |
# the above code recursively printed all the bits to our left | |
# so now we can print our bit and return: | |
! bit | |
end; | |
procedure binary; | |
begin | |
cursor := digits; | |
call helper | |
end; | |
begin | |
number := 5; # binary : 0 1 0 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
( this is the retro code, generated with "./pl0_to_retro.py < tests/41_recursion.pl0" ) | |
( compiler is built in python and lives here: https://github.com/tangentstorm/PL0-Language-Tools ) | |
( this generated code was hand-reformatted for clarity but otherwise unchanged ) | |
( -- runtime library ------------ ) | |
: odd? ( n - f ) 2 mod 1 = ; | |
( -- main code ------------------ ) | |
4 variable: digits | |
variables| number cursor | | |
{{ | |
variables| bit | | |
---reveal--- | |
: helper | |
0 bit ! | |
number @ odd? [ 1 bit ! ] ifTrue | |
number @ 2 / number ! | |
cursor @ 0 > | |
[ | |
cursor @ 1 - cursor ! | |
( -- preserve state -- ) | |
bit @ | |
( -- recurse -- ) | |
helper | |
( -- restore state -- ) | |
bit ! | |
] | |
ifTrue | |
bit @ putn cr | |
; | |
}} | |
: binary | |
digits @ cursor ! | |
helper | |
; | |
: run ( - ) | |
digits @ cursor ! | |
5 number ! | |
binary | |
; | |
( ------------------------------- ) | |
3 [ cr ] times | |
run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# here's what it looks like when it runs, using the pascal version of the ngaro vm, | |
# retro.pas available from: https://github.com/sabren/b4 | |
# ( just install freepascal, clone the repo and run "make" ) | |
# as you can see, it correctly shows the 4 bit binary representation of the number 5. :) | |
michal@proton:~/b/gen$ ./retro --with ~/vrx/recurse.rx | |
<<include: "/home/michal/vrx/recurse.rx">> | |
Retro 11.5 | |
ok ( -- runtime library ------------ ) | |
ok : odd? ( n - f ) 2 mod 1 = ; | |
ok ( -- main code ------------------ ) | |
ok 4 | |
ok variable: digits | |
ok variables| number cursor | | |
ok {{ | |
ok variables| bit | | |
ok ---reveal--- | |
ok : helper 0 bit ! number @ odd? [ 1 bit ! ] ifTrue number @ 2 / number ! cursor @ 0 > [ curs | |
or @ 1 - cursor ! ( -- preserve state -- ) bit @ ( -- recurse -- ) helper ( -- restore state -- | |
) bit ! ] ifTrue bit @ putn cr ; | |
ok }} | |
ok : binary digits @ cursor ! helper ; | |
ok : run ( - ) digits @ cursor ! 5 number ! binary ; | |
ok ( ------------------------------- ) | |
ok 3 | |
ok [ cr ] | |
ok times | |
ok run 0 | |
0 | |
1 | |
0 | |
1 | |
ok |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment