HP-42S Compiler for Niklaus Wirth's PL/0 Language
|
01-14-2024, 04:01 PM
Post: #1
|
|||
|
|||
HP-42S Compiler for Niklaus Wirth's PL/0 Language
Introduction
Recently I stumbled upon the book Compilerbau by Niklaus Wirth. A small language PL/0 is used to illustrate how to write an interpreter and a compiler. For the compiler a stack-based machine is assumed. Thus I wondered if an HP-calculator could be used as target. There's a PL/0 Language Tools project but it appears to be abandoned. The last commit on the GitHub repository was made 7 years ago. It is still based on Python 2 which is now dead. The Language PL/0 It is a simplified variant of Pascal with the following EBNF: Code: program = block "." . Installation and Setup Download Project Download the ZIP-file and unzip it. unzip pl0-compiler.zip It should contain the following files: Code: Archive: pl0-compiler.zip Setup the Virtual Environment python3 -m venv venv source venv/bin/activate pip install --upgrade pip setuptools wheel pip install ply Programs Some example programs can be found in the examples directory. Fibonacci Code: # This program calculates the first K values of the fibonacci sequence. Compile to HP-42S The program pl0_hp42s.py is based on pl0_compiler.py and creates commands for the HP-42S: python pl0_hp42s.py examples/fibonacci.pl0 Code: GTO 00 # main Caveat Input Output For input and output use ? and !: ?n; ?k; CALL perm; !p Syntax The syntax for comparisons is slightly changed. Instead of = for equal use ==. Instead of # for not equal use !=. Integer Division It is assumed that the machine uses only integers. Therefore division results in integers. However I find it more useful to use the ordinary division on an HP-42S. It is easy to include the IP command if that is needed. Or then replace ÷ by BASE÷ in the code: Code: elif term[0] == "DIVIDE": Recursion Due to the limited size of the return stack of the HP-42S I assume that recursion doesn't really work. Scope of Variables The same variable name can be used local to a procedure but these are still mapped to global registers. Cf. examples/scope.pl0 pl0-compiler.zip (Size: 14.87 KB / Downloads: 11) |
|||
« Next Oldest | Next Newest »
|
Messages In This Thread |
HP-42S Compiler for Niklaus Wirth's PL/0 Language - Thomas Klemm - 01-14-2024 04:01 PM
RE: HP-42S Compiler for Niklaus Wirth's PL/0 Language - Thomas Klemm - 02-17-2024, 10:41 AM
RE: HP-42S Compiler for Niklaus Wirth's PL/0 Language - robve - 02-17-2024, 05:02 PM
RE: HP-42S Compiler for Niklaus Wirth's PL/0 Language - Thomas Klemm - 02-18-2024, 09:32 AM
RE: HP-42S Compiler for Niklaus Wirth's PL/0 Language - floppy - 02-18-2024, 11:42 AM
RE: HP-42S Compiler for Niklaus Wirth's PL/0 Language - robve - 02-18-2024, 02:59 PM
RE: HP-42S Compiler for Niklaus Wirth's PL/0 Language - brouhaha - 02-21-2024, 12:35 PM
RE: HP-42S Compiler for Niklaus Wirth's PL/0 Language - BruceH - 02-18-2024, 12:45 PM
|
User(s) browsing this thread: 1 Guest(s)