Debug.icl 4.67 KB
Newer Older
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
implementation module Debug

import StdArray, StdEnum
from StdFile	import <<<, ferror, stderr
from StdMisc	import abort
from StdTuple	import fst
from StdList	import ++
from StdBool	import && 
from StdString	import %

import	Wrap, ShowWrapped

print :: ![{#Char}] .b -> .b
print debugStrings value
	| fst (ferror (stderr <<< debugStrings))
		=	abort "Debug, print: couldn't write to stderr"
	// otherwise
		=	value

debugBefore :: !.a !(DebugShowFunction .a) .b -> .b
debugBefore debugValue show value
	=	print (show debugValue) value

debugAfter :: !.a !(DebugShowFunction .a) !.b -> .b
debugAfter debugValue show value
	=	print (show debugValue) value

debugValue :: !(DebugShowFunction .a) !.a -> .a
debugValue show value
	=	print (show copy1) copy2
	where
		(copy1, copy2)
			=	copyUniqueValue value

		copyUniqueValue :: !.a -> (!.a, !.a)
		copyUniqueValue value
			=	code {
				.o 1 0
					push_a	0				
				.d 2 0
				}

:: DebugShowFunction a :== !a -> [{#Char}]

debugShow :: DebugShowFunction .a
debugShow
47
	=	\debugValue -> showWrapped (wrapNode debugValue) ++ ["\n"]
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65


:: DebugOptionRecord
	=	{maxDepth :: !Int, maxBreadth :: !Int, maxChars :: !Int, terminator :: !{#Char}}
DebugDefaultOptions
	:== {maxDepth = MaxInt, maxBreadth = MaxInt, maxChars = MaxInt, terminator = "\n"}

MaxInt
	:== (1<<31)-1

:: DebugShowOption 
	=	DebugMaxDepth !Int			// default MaxInt
	|	DebugMaxBreadth !Int		// default MaxInt
	|	DebugMaxChars !Int			// default MaxInt
	|	DebugTerminator !{#Char}	// default "\n"

debugShowWithOptions :: [DebugShowOption] -> DebugShowFunction .a
debugShowWithOptions debugOptions 
66
	=	\debug -> chop maxChars (showWrapped (pruneWrappedNode maxDepth maxBreadth (wrapNode debug))) ++ [terminator]
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
	where
		{maxDepth, maxBreadth, maxChars, terminator}
			=	set debugOptions DebugDefaultOptions
			where
				set [] options
					=	options
				set [DebugMaxDepth maxDepth:t] options
					=	set t {options & maxDepth=maxDepth}
				set [DebugMaxBreadth maxBreadth:t] options
					=	set t {options & maxBreadth=maxBreadth}
				set [DebugMaxChars maxChars:t] options
					=	set t {options & maxChars=maxChars}
				set [DebugTerminator terminator:t] options
					=	set t {options & terminator=terminator}

:: Indicators
	=	...
	|	.+.

MaxCharsString
	:== ".."
MaxBreadthString
	:== "..."
MaxBreadthIndicator
91
	:==	wrapNode ...
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
92
MaxDepthIndicator
93
	:==	wrapNode .+.
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139

pruneWrappedNode :: !Int !Int !WrappedNode -> !WrappedNode
pruneWrappedNode maxDepth maxBreadth value
	=	prune 0 value
	where
		prune :: !Int WrappedNode -> WrappedNode
		prune depth value
			| depth == maxDepth
				=	MaxDepthIndicator
		prune depth (WrappedIntArray a)
			=	pruneBasicArray depth a
		prune depth (WrappedBoolArray a)
			=	pruneBasicArray depth a
		prune depth (WrappedRealArray a)
			=	pruneBasicArray depth a
		prune depth (WrappedFileArray a)
			=	pruneBasicArray depth a
		prune depth (WrappedString a)
			| size a > maxBreadth
				=	WrappedString ((a % (0, maxBreadth-1)) +++ MaxBreadthString)
		prune depth (WrappedArray a)
			=	WrappedArray (pruneArray depth a)
		prune depth (WrappedRecord descriptor args)
			=	WrappedRecord descriptor (pruneArray depth args)
		prune depth (WrappedOther WrappedDescriptorCons args)
			| size args == 2
				=	WrappedOther WrappedDescriptorCons
						{prune (depth+1) args.[0], prune depth args.[1]}
		prune depth (WrappedOther WrappedDescriptorTuple args)
			=	WrappedOther WrappedDescriptorTuple (pruneArray depth args)
		prune depth (WrappedOther descriptor args)
			=	WrappedOther descriptor (pruneArray depth args)
		prune _ a
			=	a

		pruneArray :: !Int !{WrappedNode} -> {WrappedNode}
		pruneArray depth a
			| size a > maxBreadth
				=	{{prune (depth+1) e \\ e <-: a & i <- [0 .. maxBreadth]}
						& [maxBreadth] = MaxBreadthIndicator}
			// otherwise
				=	{prune (depth+1) e \\ e <-: a}

		pruneBasicArray :: !Int !(a b) -> WrappedNode | Array .a & ArrayElem b
		pruneBasicArray depth a
			| size a > maxBreadth
140
				=	WrappedArray (pruneArray depth {wrapNode e \\ e <-: a & i <- [0 .. maxBreadth]})
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
141
			// otherwise
142
				=	WrappedArray {wrapNode e \\ e <-: a}
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173

chop :: !Int ![{#Char}] -> [{#Char}]
chop _ []
	=	[]
chop maxSize list=:[string:strings]
	| maxSize < stringSize + sizeMaxCharsString
		| fits maxSize list
			=	list
		| stringSize > sizeMaxCharsString
			=	[string % (0, maxSize-sizeMaxCharsString-1), MaxCharsString]
		// otherwise
			=	[MaxCharsString]
	// otherwise
		=	[string : chop (maxSize - stringSize) strings]
	where
		fits _ []
			=	True
		fits maxSize [h : t]
			=	maxSize >= size h && fits (maxSize - size h) t

		stringSize
			=	size string
		sizeMaxCharsString
			=	size MaxCharsString

instance <<< [a] | <<< a where
	(<<<) :: *File [a] -> *File | <<< a
	(<<<) file []
		=	file
	(<<<) file [h:t]
		=	file <<< h <<< t