summaryrefslogtreecommitdiff
path: root/assignment-13/uFPL/Util.icl
diff options
context:
space:
mode:
authorCamil Staps2018-01-03 09:24:21 +0100
committerCamil Staps2018-01-03 09:24:21 +0100
commit33db1946d2a09898761b7d397fe4028725f2215b (patch)
tree1f68cb8b276b7a20f42f325c76e6d4853ea8e68f /assignment-13/uFPL/Util.icl
parentCleanup (diff)
Rename & restructure
Diffstat (limited to 'assignment-13/uFPL/Util.icl')
-rw-r--r--assignment-13/uFPL/Util.icl38
1 files changed, 38 insertions, 0 deletions
diff --git a/assignment-13/uFPL/Util.icl b/assignment-13/uFPL/Util.icl
new file mode 100644
index 0000000..42f7c27
--- /dev/null
+++ b/assignment-13/uFPL/Util.icl
@@ -0,0 +1,38 @@
+implementation module uFPL.Util
+
+import StdClass
+from StdFunc import id, o
+import StdInt
+import StdList
+import StdOverloaded
+import StdString
+
+from Text import class Text(concat), instance Text String
+
+instance zero PrState
+where
+ zero =
+ { indent = 0
+ , output = []
+ }
+
+printToString :: a -> String | print a
+printToString x = concat (print x zero).output
+
+instance print (PrState -> PrState) where print p = p
+instance print String where print s = \st -> {st & output=[s:st.output]}
+instance print Int where print i = \st -> {st & output=[toString i:st.output]}
+
+nl :: PrState -> PrState
+nl st = {st & output=["\n":repeatn st.indent "\t"] ++ st.output}
+
+indent :: PrState -> PrState
+indent st = {st & indent=max 0 (st.indent - 1)}
+
+unindent :: PrState -> PrState
+unindent st = {st & indent=st.indent + 1}
+
+prsperse :: a [b] -> PrState -> PrState | print a & print b
+prsperse _ [] = id
+prsperse _ [x] = print x
+prsperse g [x:xs] = print x o print g o prsperse g xs