psbots

A PostScript engine developped in TypeScript



Presented by Arnaud Buchholz
Presentation made with Reveal.js

Agenda

    What PDF and PONG have in common ?

    Nothing but...

    πŸ€” They start with P...

    πŸ‘΄ They were both created in the last century.

    Portable Document Format

    In 1992, Adobe created PDF
    on top of concepts introduced by...
    PostScript which was invented in 1976.

    A PDF document is a structured algorithm in which pages are rendered using a program.

    %PDF-1.4
    % ...skipping document attributes ...
    3 0 obj<</Type/Pages/Kids[4 0 R]/Count 1/MediaBox[0 0 595.32 841.92]>>
    endobj
    4 0 obj<</Type/Page/Parent 3 0 R/Resources<</Font<</F0 6 0 R>>>>/Contents 5 0 R>>
    endobj
    5 0 obj<</Length 59>>stream
    BT
    /F0 12 Tf                     % Font selection
    1 0 0 1 100 702.7366667 Tm    % Position
    (Hello World!)Tj              % Write Hello World!
    ET
    endstream
    endobj
    % ...skipping objects (including font descriptor)...
    % ...skipping object reference table...
    						
    Hello World.pdf
    Hello World.pdf (as text)

    PostScript

    PostScript is a dynamically typed, stack-based programming language.
    It is most commonly used in the publishing world, but as a Turing complete programming language, it can be used for many other purposes.

    Why PostScript ?

    A long time ago, I learned PostScript to be able to process* printing streams.

    * reformat, add / remove margins, extract text...

    I have been fascinated by the power and the simplicity of this language.

    PostScript Examples

    PDFI.js

    The PostScript language

    The language relies on simple concepts :

    • Context composed of stacks and dictionaries
      operand stack, call stack, dictionary stack
    • Operators are stored in the system dictionary
    • No variables
      values are either stored in a dictionary or in the operand stack
    • Simple syntax
      only needs a lexical parser, no grammar

    The PostScript language
    Example 1

    To make an addition, push the operands first and then call the operator : 1 2 add.

    Reverse Polish Notation

    The PostScript language
    Example 2

    The following array declaration [ 1 ] consists of :

    • [ : array begin ➜ pushes mark on the operand stack
    • 1 : pushes the integer 1 on the operand stack
    • ] : array close ➜ checks where is the first mark, collects values, clears the operand stack and pushes an array containing collected values

    psbots

    A PostScript engine

    Not complete...

    ...And with deviations

    A PostScript engine implemented in TypeScript

    • No dependencies
    • No graphical instructions
    • Interruptible: execution by cycles
      Some instructions might require several cycles
    • Memory monitoring and leak detection
      πŸ’‘To be improved with reference loop detection
    • Extensions: Host dictionary, finally...


    psbots

    psbots
    Examples

    πŸ§‘β€πŸ’» Online Read-Eval-Print Loop
    • 1 2 add
    • [ 1 ]

    psbots
    Factorial Example

    source
    /factorial
    {
    	%% check stack
    	count 1 lt { stackunderflow } if
    	dup type /integer neq { typecheck } if
    
    	1 exch
    	%% result n
    	{
    		dup 2 lt { pop stop } if
    		dup 3 1 roll mul
    		exch
    		1 sub
    	} loop
    } bind def

    What PDF and PONG have in common !

    A PONG game scripted with PostScript

    Why ?

    I have been invited to a gaming challenge where people could code the PONG's paddle using JavaScript.

    It was fun but...

    • πŸ‘ŽNot real time
    • πŸ‘ŽNo constraint on JavaScript implementation
    • πŸ‘ŽDifficult to secure the algorithm
    • πŸ‘ŽUnfair

    psbots'PONG

    • πŸ‘real time
    • πŸ‘isolated environments
    • πŸ‘Fair time share
      one tick = one cycle per paddle, ball move