#!/bin/pliant
module "/pliant/v1.p"


# this is the clean implementation like in rot13.c

function rot13 src -> dest
  arg Char src dest
  var Int byte := src number
  var Int cap := byte .and. 32
  byte := byte .and. .not. cap
  byte := (shunt byte>="A":number and byte<="Z":number (byte-"A":number+13)%26+"A":number byte) .or. cap
  dest := character byte


# Now we get the table filled at compile time, using the clean implementation:
# this is where the Pliant power will obviously appear

(var Array:Char table) size := 256
for (var Int i) 0 255
  table i := rot13 character:i


# So we can overwrite the implementation with the new faster one
# it's now used below, but is intended to demonstate Pliant programming

function rot13 src -> dest
  arg Char src dest
  strong_definition
  dest := table src:number


# lastly, this is the exact translation of the main program that make the
# module behave as a Unix filter such as in the various C examples

module "/pliant/pointers.p"
module "/pliant/os.p"

while (os_read 0 addressof:(var uInt8 c) 1)=1
  os_write 1 (addressof table:c) 1
