PL1CompInPy is a starter Python project for a PL/1 compiler.
- Features
- Generated API Reference
- Build And Delivery
- Sponsorship
- PL/I Bootstrap Sources
- Code of Conduct
- MIT License
The initial implementation provides a small compiler pipeline:
- lexical analysis for a useful subset of PL/1-like syntax
- parsing of assignments, declarations, calls, labels,
GOTO/GO TO, control blocks, I/O statements, and preprocessor commands - optional preservation of PL/I
/* ... */comment sections for source-to-source and compiler-in-PL/I passes - contextual keyword recognition for PL/1 statement, attribute, condition, I/O, and preprocessor words
- a visitor-enabled AST intermediate representation
- readable Python-like/Python-source output plus starter native, JVM, and .NET backends
Classic PL/I does not reserve keywords globally. A word such as IF, CALL, or DECLARE
can still be used as an identifier when the surrounding syntax makes that role clear. For
that reason, PL1CompInPy keeps words as identifier tokens and attaches optional keyword
metadata from pl1compinpy.keywords.KEYWORD_CATALOG.
The current catalog covers the main language-reference families:
- structural statements:
PROCEDURE,PROC,ENTRY,BEGIN,DO,END - declaration statements:
DECLARE,DCL,DEFAULT,DFT,FORMAT - flow-control statements:
CALL,IF,THEN,ELSE,SELECT,GO,GOTO,RETURN,STOP - condition statements and conditions:
ON,SIGNAL,REVERT,ERROR,FINISH,ENDFILE,ZERODIVIDE, and related conditions - storage statements and attributes:
ALLOCATE,ALLOC,FREE,AUTOMATIC,STATIC,BASED,CONTROLLED - data attributes:
FIXED,FLOAT,BINARY,DECIMAL,REAL,COMPLEX,CHARACTER,BIT,POINTER,PICTURE, and related aliases - I/O statements and options:
OPEN,CLOSE,GET,PUT,READ,WRITE,REWRITE,LOCATE,DELETE,LIST,SKIP,KEY - preprocessor/listing words:
INCLUDE,XINCLUDE,ACTIVATE,DEACTIVATE,REPLACE,PAGE,PRINT,PUSH,POP
python -m venv .venv
source .venv/bin/activate
python -m pip install -e .
python -m pl1compinpy --helpCompile a file:
python -m pl1compinpy examples/hello.pl1Compile several source modules with PL/I include members:
python -m pl1compinpy examples/language/multi_source_main.pl1 examples/language/module_helper.pl1
python -m pl1compinpy examples/language/include_main.pl1 -I examples/languageEmit assembly instead of the default Python-like output:
python -m pl1compinpy examples/hello.pl1 --target python-source
python -m pl1compinpy examples/hello.pl1 --target jvm-bytecode
python -m pl1compinpy examples/hello.pl1 --target dotnet-il
python -m pl1compinpy examples/hello.pl1 --target x586-windows
python -m pl1compinpy examples/hello.pl1 --target x86_64-windows
python -m pl1compinpy examples/hello.pl1 --target x586-macos
python -m pl1compinpy examples/hello.pl1 --target arm64-macos
python -m pl1compinpy examples/hello.pl1 --target arm64-windowsInclude packaged PL/I builtin source before compiling:
python -m pl1compinpy examples/hello.pl1 --builtin SUBSTRCreate a binary executable/container artifact:
python -m pl1compinpy examples/hello.pl1 --emit binary --binary-format pe32-x586-windows -o hello.exe
python -m pl1compinpy examples/hello.pl1 --emit binary --binary-format pe64-x86_64-windows -o hello-x64.exe
python -m pl1compinpy examples/hello.pl1 --emit binary --binary-format elf64-x86_64 -o hello-x86_64.elf
python -m pl1compinpy examples/hello.pl1 --emit binary --binary-format elf64-aarch64 -o hello-aarch64.elf
python -m pl1compinpy examples/hello.pl1 --emit binary --binary-format macho64-x86_64-macos -o hello-intel-macos
python -m pl1compinpy examples/hello.pl1 --emit binary --binary-format macho64-arm64-macos -o hello-m2-macosCreate a static or dynamic-library style artifact:
python -m pl1compinpy examples/language/multi_source_main.pl1 examples/language/module_helper.pl1 --emit library --library-format static-ar -o libmulti.a
python -m pl1compinpy examples/language/multi_source_main.pl1 examples/language/module_helper.pl1 --emit library --library-format shared-elf64 -o libmulti.so
python -m pl1compinpy examples/language/multi_source_main.pl1 examples/language/module_helper.pl1 --emit library --library-format shared-pe64 -o multi.dllThe companion script examples/backend/library_artifacts.py shows the same static archive and shared-library artifact flow from the Python API.
Create a JVM .class file using Java 17 classfile version 61:
python -m pl1compinpy examples/backend/jvm_bytecode.pl1 --emit class -o PL1Program.classCreate a .NET executable using Microsoft ILAsm:
python -m pl1compinpy examples/backend/dotnet_il.pl1 --emit dotnet-exe -o PL1Program.exeRun tests:
python -m unittest discover -s tests
make test
make test-binaryThe test suite covers include expansion, strict include errors, recursive include protection, multi-source compilation, CLI library output, library manifests, dynamic-load runtime dispatch, binary container emission with runtime-result verification, and generated API documentation for the public compiler surface.
Build and package a local delivery:
make deliverThe delivery target runs syntax checks, regenerates API documentation, runs unit and binary pipeline tests, verifies examples, emits sample text/binary/library artifacts, and packages dist/PL1CompInPy-delivery.tar.gz.
The project includes backend emitters for:
python-source: Python source codejvm-bytecode: JVM bytecode-style textual output--emit class: binary JVM.classoutput targeting JDK 17 classfile version 61dotnet-il: .NET Common Intermediate Language / ILAsm textual output--emit dotnet-exe: invokes ILAsm to turn generated .NET IL into a PE.exex586-windows: 32-bit x86-style assembly for Windows toolchains using Cprintfx86_64-windows: 64-bit x86 assembly for Windows x64 toolchains using Cprintfx586-macos: 32-bit x86-style assembly with macOS symbol namingarm64-macos: Apple Silicon/M2-style ARM64 assembly using macOS symbol namingarm64-windows: ARM64-style assembly with Windows symbol naming
Currently supported compiler features:
- integer variable declarations and storage
- integer assignment with
+,-,*, and/ - level-numbered structures/records with nested scalar fields and dotted field references such as
CUSTOMER.ADDRESS.ZIP - pointer-qualified based structure references such as
P->REC.ID, plus defaultBASED(P)access throughREC.ID - example coverage for default based access, explicit pointer-qualified access, and multiple pointer views over one based structure
- PL/I-style expression precedence for
**, unary operators,*//,+/-,||, comparisons,&/AND, and|/OR, driven by a central precedence table IF/THEN/ELSEcomparisons with=,^=,<>,<,<=,>, and>=DO WHILEpre-test loops and post-testDO ... UNTILloopsSELECT/WHEN/OTHERWISEconditional groups- labels and
GOTO/GO TObranch statements - parsed
%preprocessor commands such as%DECLARE,%IF,%DO,%INCLUDE,%REPLACE,%GOTO,%PROCEDURE,%RETURN, and listing controls - IBM-style preprocessing before lexing for
%DECLARE, compile-time assignment,%ACTIVATE,%DEACTIVATE,%REPLACE,%NOTE, simple%IF/%ELSE/%ENDselection, and compile-time builtins such asPARMSET,SYSPARM,LENGTH,SUBSTR,UPPERCASE, andLOWERCASE %INCLUDE,%XINCLUDE,%INSCAN, and%XINSCANsource expansion before lexing, with include directories and recursive include protection- multi-source compilation where the module containing
PROC OPTIONS(MAIN)is treated as the main module - labels and procedure bodies
PROC OPTIONS(MAIN)as a program entry point, including unnamedPROC OPTIONS(MAIN)treated as entry nameMAINPROC(PARM) OPTIONS(MAIN)command-line binding, where the first parameter receives the command tailPROC RECURSIVEmetadata, with recursive calls lowered as ordinary calls that continue at the next statement after returnPROC RETURNS(...)metadata for function return type- console output through
CALL DISPLAY(...),CALL PRINT(...), and basicPUT LIST(...)
The emitters generate readable assembler text. They are intentionally small and direct so the runtime calling conventions and target-specific prologues can be refined as the compiler grows.
The binary writer now follows the compiler pipeline:
PL/1 source -> lexer -> parser -> AST -> executable mnemonics -> machine code -> binary container
For example, a PL/1 assignment such as TOTAL = 40 + 2; is lowered into mnemonic operations
such as MOV_EAX_IMM, PUSH_EAX, POP_EBX, ADD_EAX_EBX, and STORE_EAX_VAR, then encoded
as machine-code bytes before being placed in the executable file.
The binary writer currently creates minimal executable/container files with correct platform signatures and source-derived starter code:
pe32-x586-windows: Windows PE32.exeformat for 32-bit x86/x586pe64-x86_64-windows: Windows PE32+.exeformat for x86_64/AMD64elf64-x86_64: ELF64 executable container for Intel/AMD 64-bit Unix-style systemself64-aarch64: ELF64 executable container for ARM64/AArch64 Unix-style systemsmacho64-x86_64-macos: Mach-O 64-bit executable container for Intel macOSmacho64-arm64-macos: Mach-O 64-bit executable container for Apple Silicon/M2 macOS
macOS uses Mach-O, not ELF. ELF is provided for Unix-style targets; Apple Intel and M2 targets use Mach-O containers.
The binary layer exposes explicit PE, ELF, and Mach-O linker classes in pl1compinpy.codegen.linkers.
The pe32-x586-windows and pe64-x86_64-windows paths include source-driven instruction encoding
for starter arithmetic and exit code. The ELF and Mach-O paths use the same mnemonic pipeline and
have starter encoders for Intel and ARM64 machine code, ready to be expanded with full runtime I/O
and platform linker details.
The executable pipeline includes a first runtime calling convention:
- procedure-local variables are allocated in the procedure stack frame
- procedure parameters are passed on the stack from right to left
- default procedure calls use call by reference
- by-reference arguments pass the address of the caller variable, similar to C pointer-style calls
CALL P(B,A) BY NAME;is normalized by matching argument names toP's parameter names, sorting them into parameter order, and lowering the result as a by-reference call- user procedures are registered in a dynamic function table as they are detected
- runtime services are registered in a static function table with PL/I-like names, pointer targets, parameter descriptions, return types, and call modes
- command-line runtime services expose
COMMAND,ARGC, andARGV, and direct runtime execution binds main procedure parameters from hostargv - PL/I builtins are also registered in the static table, but a source program must declare them with
DCL name BUILTIN;before calls are accepted - declared static builtins now include
SUBSTR,LENGTH,INDEX,POINTER,COMPLEX,REAL,IMAG,CONJG,ABS,SIGN,MIN,MAX,MOD,ROUND,TRUNC,CEIL,FLOOR, complex-capable math helpers, and fixed-decimal conversion helpers CALLvalidation uses the merged runtime and dynamic tables before lowering- procedure definitions are emitted before main code and the entry path jumps over them, so procedures run only when called
- native targets use a C-style runtime link model: generated objects reference
pl1rt_init/pl1rt_shutdown, then the final executable is resolved against a PL/I runtime object/archive or shared/import library plus the platform C runtime - library emission supports static archive-style artifacts and starter shared-library containers for ELF
.so, Mach-O dynamic libraries, and Windows DLL/COFF-linkage workflows - JVM output links the runtime through the
pl1compinpy/runtime/PL1Runtimeclass on the classpath - .NET IL links the runtime through the
PL1CompInPy.Runtimemanaged assembly - dynamic-load runtime services include
DYNLOAD/DYNSYMfor native libraries plus Java class-load and .NET assembly-load descriptors - AST nodes support a visitor pattern through
accept, and runtime execution has aRuntimeExecutionVisitorfor future compiler passes and runtime checks; seeexamples/runtime/runtime_visitor.py - semantic analysis includes canonical
PliTypeparsing, backend type mappings, and a debugger-orientedSymbolTablewith scope, storage, dimensions, based-pointer, procedure, parameter, and field records - arithmetic semantics include
COMPLEX/CPLXas a first-class arithmetic mode, runtimeComplexValuestorage, complex promotion in the numeric tower, and equality-only comparison semantics for complex values - Python source output passes
" ".join(sys.argv[1:])to the firstOPTIONS(MAIN)procedure parameter; native/JVM/.NET backends now identify the same main entry and reserve argument slots for parameterized mains - backend control-flow lowering treats simple
DOas a block,DO WHILEas a pre-test loop,DO ... UNTILas a post-test loop,IF/THEN/ELSEas decisions, andSELECT/WHEN/OTHERWISEas branches - GOTO statements are parsed as AST nodes and lowered to unconditional branches for the native mnemonic/assembly backends, while Python, JVM text, and .NET IL outputs preserve the branch intent in their target form
- PL/I preprocessor statements are parsed into
PreprocessorStatementAST nodes when they are not consumed by the compile-time preprocessor, so future macro expansion can still use the visitor pipeline; current backends preserve remaining commands as target comments - PL/I comments use
/* ... */; the default lexer treats them as whitespace, whileLexer(..., preserve_comments=True)records them asCommentSectionmetadata onProgram.comments
The examples include an imported PL/I quicksort library at examples/language/quicksort_imported.pli; it is kept unchanged as a larger reference source while the current compiler subset grows toward it. Semantic notes for operator precedence, PL/I type modeling, and debugger symbol tables live in docs/SEMANTICS.md.
The bootstrap/ directory starts the self-hosting path: it contains PL/I source modules for a bootstrap lexer, parser, expanded runtime table, Apple M3/macOS ARM64 backend, and compiler driver written in the current PL1CompInPy dialect. These sources are parsed by the current Python frontend and are intended to move compiler logic into PL/I incrementally.
The runtime also includes starter storage and I/O services:
- PL/I-style array declarations such as
DCL A(10) FIXED BIN(31); - PL/I-style level-numbered structure/record declarations such as
DCL 1 CUSTOMER, 2 ID FIXED BIN(31), 2 NAME CHAR(20); - runtime structure values with nested field get/set, simple field-size layout, and flattened field storage for native/JVM/.NET backends
- heap allocation helpers used by dynamic array storage
FLOATdeclarations initialized as floating-point values in Python outputPICTURE/PICdecimal display patterns using digit positions such as9, zero-suppressedZ, stored decimal., and implied decimalV- conversion helpers between fixed decimal, float, picture-formatted storage, packed decimal bytes, and zoned decimal text
- calculation engine with fixed binary, scaled fixed decimal, float, bit, and character values plus explicit casts and numeric promotion
- exported
FixedDecimal,PackedDecimalCodec,ZonedDecimalCodec,DecimalRuntime, andCalculationBuiltinRuntimeAPIs for runtime callers POINTERlocator variables andBASED(pointer)record storage bound to heap blocks through pointer values- declared
POINTERbuiltin support for normalizing null pointers, existing pointer values, and numeric handle/offset pairs into runtimePointerValueobjects - declared internal runtime builtins
PL1RT_ALLOC,PL1RT_FREE,PL1RT_REALLOC,PL1RT_SIZE,PL1RT_PEEK,PL1RT_POKE, andPL1RT_FILLexpose controlled access to runtime heap storage without using normal PL/IALLOCATE/FREEstatements - pointer-qualified based structure member access with explicit
P->REC.FIELDreferences and default-pointerREC.FIELDreferences - based structure examples cover default pointer access, explicit pointer-qualified access, and multiple pointer locators sharing one structure definition
- string storage as two bytes of big-endian length followed by sequential payload bytes, with runtime
LENGTH,SUBSTR, andINDEXhelpers over the stored payload - a first packaged PL/I builtin source file for
SUBSTR(string, start[, length]) - static builtin call checking for
DCL SUBSTR BUILTIN; - examples for declared numeric, string, and pointer builtins in
examples/builtins/numeric_string_builtins.pl1andexamples/builtins/pointer.pl1 - PL/I-style file declarations such as
DCL F FILE RECORD OUTPUT ENVIRONMENT(RECFM(V), LRECL(80), PATH('out.dat')) BINARY; - PL/I-style file statements:
OPEN FILE(F);,READ FILE(F) INTO(BUF);,WRITE FILE(F) FROM(BUF);,REWRITE FILE(F) FROM(BUF);,LOCATE FILE(F) SET(POS);,DELETE FILE(F);, andCLOSE FILE(F); - normal Unix-style stream files
- binary stream chunk I/O through
READ FILE(F) SIZE(n) INTO(BUF);, positioned I/O throughOFFSET(...),POSITION(...), andRBA(...), text line reads throughLINE, and append/update modes for ordinary files - fixed-record files using
RECFM(F)andLRECL(n) - variable-record files using
RECFM(V), represented here with a two-byte big-endian length prefix followed by record data - binary and text record payloads
- primitive TCP socket I/O runtime with client/server sockets, SSL client sockets, and TLS client sockets
- file-like socket stream layer for easier text/binary payload
READ/WRITE, with Unix-style, fixed, and variable record framing - static runtime function descriptors for
ALLOC,FREE, file I/O, VSAM I/O, TCP/IP, SSL, and TLS helpers - runtime link manifests embedded in generated native executable payloads, recording the target runtime libraries and used runtime calls
- binary pipeline tests compile PL/I sources to PE, ELF, and Mach-O containers and validate the same source program through the runtime visitor result
- generic function dispatch by argument type, using runtime lambda implementations
- local VSAM-style datasets with a catalog file plus binary data file for KSDS, ESDS, RRDS, and LDS organizations
- VSAM runtime descriptors and I/O dispatch for
OPEN,WRITE FROM, KSDS keyedREAD, ESDS RBAREAD, RRDS RRNREAD/WRITE, LDS RBA/LENGTHREAD, andCLOSE
PL1CompInPy/
pyproject.toml
docs/
API.md
scripts/
generate_api_docs.py
src/pl1compinpy/
builtins/
loader.py
pl1/
substr.pl1
cli.py
codegen/
backends.py
binary_formats.py
dotnet_executable.py
dotnet_il.py
executable_pipeline.py
jvm_bytecode.py
libraries.py
python_source.py
compiler.py
core/
ast.py
compiler.py
frontend/
include.py
keywords.py
lexer.py
parser.py
runtime/
arrays.py
based.py
calculation.py
calling.py
command_line.py
decimal.py
dynload.py
function_table.py
heap.py
io.py
picture.py
socket_io.py
strings.py
vsam/
catalog.py
io.py
tests/