summaryrefslogtreecommitdiff
path: root/appl/cmd/mash/dump.b
blob: ed4b63095fef70e2a7b5ab570f3429a3f67c4a07 (plain)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
140
141
142
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#
#	Output routines.
#

#
#	Echo list of strings.
#
echo(e: ref Env, s: list of string)
{
	out := e.outfile();
	if (out == nil)
		return;
	out.putc('+');
	for (t := s; t != nil; t = tl t) {
		out.putc(' ');
		out.puts(hd t);
	}
	out.putc('\n');
	out.close();
}

#
#	Return text representation of Word/Item/Cmd.
#

Word.word(w: self ref Word, d: string): string
{
	if (w == nil)
		return nil;
	if (d != nil)
		return d + w.text;
	if (w.flags & Wquoted)
		return enquote(w.text);
	return w.text;
}

Item.text(i: self ref Item): string
{
	if (i == nil)
		return nil;
	case i.op {
	Icaret =>
		return i.left.text() + " ^ " + i.right.text();
	Iicaret =>
		return i.left.text() + i.right.text();
	Idollarq =>
		return i.word.word("$\"");
	Idollar or Imatch =>
		return i.word.word("$");
	Iword =>
		return i.word.word(nil);
	Iexpr =>
		return "(" + i.cmd.text() + ")";
	Ibackq =>
		return "`" + group(i.cmd);
	Iquote =>
		return "\"" + group(i.cmd);
	Iinpipe =>
		return "<" + group(i.cmd);
	Ioutpipe =>
		return ">" + group(i.cmd);
	* =>
		return "?" + string i.op;
	}
}

words(l: list of ref Item): string
{
	s: string;
	while (l != nil) {
		if (s == nil)
			s = (hd l).text();
		else
			s = s + " " + (hd l).text();
		l = tl l;
	}
	return s;
}

redir(s: string, c: ref Cmd): string
{
	if (c == nil)
		return s;
	for (l := c.redirs; l != nil; l = tl l) {
		r := hd l;
		s = s + " " + rdsymbs[r.op] + " " + r.word.text();
	}
	return s;
}

cmd2in(c: ref Cmd, s: string): string
{
	return c.left.text() + " " + s + " " + c.right.text();
}

group(c: ref Cmd): string
{
	if (c == nil)
		return "{ }";
	return redir("{ " + c.text() + " }", c);
}

sequence(c: ref Cmd): string
{
	s: string;
	do {
		r := c.right;
		t := ";";
		if (r.op == Casync) {
			r = r.left;
			t = "&";
		}
		if (s == nil)
			s = r.text() + t;
		else
			s = r.text() + t + " " + s;
		c = c.left;
	} while (c != nil);
	return s;
}

Cmd.text(c: self ref Cmd): string
{
	if (c == nil)
		return nil;
	case c.op {
	Csimple =>
		return redir(words(c.words), c);
	Cseq =>
		return sequence(c);
	Cfor =>
		return "for (" + c.item.text() + " in " + words(c.words) + ") " + c.left.text();
	Cif =>
		return "if (" + c.left.text() +") " + c.right.text();
	Celse =>
		return c.left.text() +" else " + c.right.text();
	Cwhile =>
		return "while (" + c.left.text() +") " + c.right.text();
	Ccase =>
		return redir("case " + c.left.text() + " { " + c.right.text() + "}", c);
	Ccases =>
		s := c.left.text();
		if (s[len s - 1] != '&')
			return s + "; " + c.right.text();
		return s + " " + c.right.text();
	Cmatched =>
		return cmd2in(c, "=>");
	Cdefeq =>
		return c.item.text() + " := " + words(c.words);
	Ceq =>
		return c.item.text() + " = " + words(c.words);
	Cfn =>
		return "fn " + c.item.text() + " " + group(c.left);
	Crescue =>
		return "rescue " + c.item.text() + " " + group(c.left);
	Casync =>
		return c.left.text() + "&";
	Cgroup =>
		return group(c.left);
	Clistgroup =>
		return ":" + group(c.left);
	Csubgroup =>
		return "@" + group(c.left);
	Cnop =>
		return nil;
	Cword =>
		return c.item.text();
	Ccaret =>
		return cmd2in(c, "^");
	Chd =>
		return "hd " + c.left.text();
	Clen =>
		return "len " + c.left.text();
	Cnot =>
		return "!" + c.left.text();
	Ctl =>
		return "tl " + c.left.text();
	Ccons =>
		return cmd2in(c, "::");
	Ceqeq =>
		return cmd2in(c, "==");
	Cnoteq =>
		return cmd2in(c, "!=");
	Cmatch =>
		return cmd2in(c, "~");
	Cpipe =>
		return cmd2in(c, "|");
	Cdepend =>
		return words(c.words) + " : " + words(c.left.words) + " " + c.left.text();
	Crule =>
		return c.item.text() + " :~ " + c.left.item.text() + " " + c.left.text();
	* =>
		if (c.op >= Cprivate)
			return "Priv+" + string (c.op - Cprivate);
		else
			return "?" + string c.op;
	}
	return nil;
}