Commits

Even Wiik Thomassen  committed fd15d5f

Added a small script for z-decoding.

  • Participants
  • Parent commits a79121d

Comments (0)

Files changed (3)

File pyhaskell.tex

 parts such as IO, but it pushed the list further down the evaluation stack,
 which could be problematic.
 
-Consider the simple example of printing the string "Hey" to \mycode{stdout}:
+Consider the simple example of printing the string ``Hey'' to \mycode{stdout}:
 \mint{haskell}|main = putStrLn "Hey"|
 In the external core output produced by \gls{ghc}
-(\autoref{lst:putStrLn-extcore}), one might see that it simply involves calling
-\mycode{putStrLn} with the unpacked string "Hey". PyHaskell converts this to
-a set of operations, and in line six of \autoref{lst:putStrLn-evallog} the
-string is printed. The argument to \mycode{putStrLn} can be seen again in line
-seven, and it is given to \mycode{runMainIO} in line eight, until evaluation
-ends.
+(\mylst{lst:putStrLn-extcore}), one might see that the example simply calls
+\mycode{putStrLn} with the unpacked string ``Hey''. PyHaskell converts this to
+a set of operations that it evaluates, and in line six of
+\mylst{lst:putStrLn-evallog} the string is printed. The argument to
+\mycode{putStrLn} can be seen again in line seven, and it is given to
+\mycode{runMainIO} in line eight, where evaluation ends.
 
 \mytodo{actually implement IO (), and return it, then explain what I did and show an example}
 

File tools/example.hcr

+%module main:Main
+  main:Main.main :: (ghczmprim:GHCziTypes.IO
+                     ghczmprim:GHCziTuple.Z0T) =
+    base:SystemziIO.putStrLn
+    (ghczmprim:GHCziCString.unpackCStringzh
+     ("Hey"::ghczmprim:GHCziPrim.Addrzh));
+  main:ZCMain.main :: (ghczmprim:GHCziTypes.IO
+                       ghczmprim:GHCziTuple.Z0T) =
+    base:GHCziTopHandler.runMainIO @ ghczmprim:GHCziTuple.Z0T
+    main:Main.main;

File tools/zdecode.py

+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+"""
+A simple script for z-decoding a file based on the rules in:
+http://hackage.haskell.org/trac/ghc/wiki/Commentary/Compiler/SymbolNames
+
+Example usage: python zdecode.py example.hcr
+"""
+from __future__ import print_function
+import sys
+import string
+
+
+MAPPING = {'ZL': '(', 'ZR': ')', 'ZM': '[',
+            'ZN': ']', 'ZC': ':', 'ZZ': 'Z',
+            'zz': 'z', 'za': '&', 'zb': '|', 'zc': '^',
+            'zd': '$', 'ze': '=', 'zg': '>', 'zh': '#',
+            'zi': '.', 'zl': '<', 'zm': '-', 'zn': '!',
+            'zp': '+', 'zq': '\\', 'zs': '/',
+            'zt': '*', 'zu': '_', 'zv': '%'}
+
+
+def z_decode(text):
+    KEYS = set(MAPPING.keys())
+    decoded = ''
+    lexeme = ''
+
+    for char in text:
+        key = lexeme + char
+
+        if key in KEYS:
+            key = MAPPING[key]
+        elif char in ('Z', 'z'):
+            decoded += lexeme
+            lexeme = char
+            continue
+
+        # Hex encoded char, incomplete
+        elif lexeme.startswith('z'):
+            if char in string.hexdigits:
+                lexeme += char
+                continue
+            elif char == 'U':
+                pass  # TODO: missing support for this
+
+        # Tuples, boxed or unboxed
+        elif lexeme.startswith('Z'):
+            if char in string.digits:
+                lexeme += char
+                continue
+            elif char == 'T':
+                key = '(%s)' % ','.join([''] * int(lexeme[1:]))
+            elif char == 'H':
+                key = '(#%s#)' % ','.join([''] * int(lexeme[1:]))
+
+        decoded += key
+        lexeme = ''
+
+    decoded += lexeme
+    return decoded
+
+
+def main(infile):
+    with open(infile, 'r') as f:
+        text = f.read()
+    print(z_decode(text))
+
+
+if __name__ == '__main__':
+    if len(sys.argv) < 2:
+        print("Error, missing path of input file.")
+        print(__doc__)
+        sys.exit(2)
+    main(sys.argv[1])
+    sys.exit()