xref: /inferno-os/appl/alphabet/typesets/fstypes.b (revision 37da2899f40661e3e9631e497da8dc59b971cbd0)
1# warning: autogenerated code; don't bother to change this, change mktypeset.b or fs.b instead
2implement Fstypes;
3include "sys.m";
4	sys: Sys;
5include "alphabet/reports.m";
6include "draw.m";
7include "sh.m";
8include "alphabet.m";
9	extvalues: Extvalues;
10	Values: import extvalues;
11	proxymod: Proxy;
12	Typescmd, Modulecmd: import Proxy;
13include "fs.m";
14	fs: Fs;
15	Value: import fs;
16include "fstypes.m";
17
18Pcontext: adt {
19	cvt: ref Fscvt;
20	ctxt: ref Context;
21
22	loadtypes: fn(ctxt: self ref Pcontext, name: string): (chan of ref Proxy->Typescmd[ref Value], string);
23	type2s: fn(ctxt: self ref Pcontext, tc: int): string;
24	alphabet: fn(ctxt: self ref Pcontext): string;
25	modules: fn(ctxt: self ref Pcontext, r: chan of string);
26	find: fn(ctxt: self ref Pcontext, s: string): (ref Module, string);
27	getcvt: fn(ctxt: self ref Pcontext): ref Fscvt;
28};
29
30proxy(): chan of ref Typescmd[ref Alphabet->Value]
31{
32	return proxy0().t0;
33}
34
35proxy0(): (
36		chan of ref Typescmd[ref Alphabet->Value],
37		chan of (string, chan of ref Typescmd[ref Fs->Value]),
38		ref Fscvt
39	)
40{
41	sys = load Sys Sys->PATH;
42	extvalues = checkload(load Extvalues Extvalues->PATH, Extvalues->PATH);
43	proxymod = checkload(load Proxy Proxy->PATH, Proxy->PATH);
44	fs = checkload(load Fs Fs->PATH, Fs->PATH);
45	fs->init();
46	cvt := ref Fscvt(Values[ref Value].new());
47	(t, newts) := proxymod->proxy(ref Pcontext(cvt, Context.new()));
48	return (t, newts, cvt);
49}
50
51include "readdir.m";
52Context: adt {
53	modules: fn(ctxt: self ref Context, r: chan of string);
54	loadtypes: fn(ctxt: self ref Context, name: string)
55		: (chan of ref Proxy->Typescmd[ref Value], string);
56	find: fn(ctxt: self ref Context, s: string): (ref Module, string);
57	new:	fn(): ref Context;
58};
59Module: adt {
60	m: Fsmodule;
61	run: fn(m: self ref Module, ctxt: ref Draw->Context, r: ref Reports->Report,
62		errorc: chan of string, opts: list of (int, list of ref Value),
63		args: list of ref Value): ref Value;
64	typesig: fn(m: self ref Module): string;
65	quit: fn(m: self ref Module);
66};
67Context.new(): ref Context
68{
69	return nil;
70}
71Context.loadtypes(nil: self ref Context, name: string): (chan of ref Typescmd[ref Value], string)
72{
73	p := "/dis/alphabet/fs/"+name+"types.dis";
74	types := load Fssubtypes p;
75	if(types == nil)
76		return (nil, sys->sprint("cannot load %q: %r", p));
77	return (types->proxy(), nil);
78}
79Context.modules(nil: self ref Context, r: chan of string)
80{
81	if((readdir := load Readdir Readdir->PATH) != nil){
82		(a, nil) := readdir->init("/dis/alphabet/fs", Readdir->NAME|Readdir->COMPACT);
83		for(i := 0; i < len a; i++){
84			m := a[i].name;
85			if((a[i].mode & Sys->DMDIR) == 0 && len m > 4 && m[len m - 4:] == ".dis")
86				r <-= m[0:len m - 4];
87		}
88	}
89	r <-= nil;
90}
91Context.find(nil: self ref Context, s: string): (ref Module, string)
92{
93	p := "/dis/alphabet/fs/"+s+".dis";
94	m := load Fsmodule p;
95	if(m == nil)
96		return (nil, sys->sprint("cannot load %q: %r", p));
97	{
98		m->init();
99	} exception e {
100	"fail:*" =>
101		return (nil, "init failed: " + e[5:]);
102	}
103	return (ref Module(m), nil);
104}
105Module.run(m: self ref Module, ctxt: ref Draw->Context, r: ref Reports->Report, nil: chan of string,
106		opts: list of (int, list of ref Value), args: list of ref Value): ref Value
107{
108	# add errorc
109	return m.m->run(ctxt, r, opts, args);
110}
111Module.typesig(m: self ref Module): string
112{
113	return m.m->types();
114}
115Module.quit(nil: self ref Module)
116{
117}
118Pcontext.type2s(nil: self ref Pcontext, tc: int): string
119{
120	return Value.type2s(tc);
121}
122
123Pcontext.alphabet(nil: self ref Pcontext): string
124{
125	return "rdcfsmptx";
126}
127
128Pcontext.getcvt(ctxt: self ref Pcontext): ref Fscvt
129{
130	return ctxt.cvt;
131}
132
133Pcontext.find(ctxt: self ref Pcontext, s: string): (ref Module, string)
134{
135	return ctxt.ctxt.find(s);
136}
137
138Pcontext.modules(ctxt: self ref Pcontext, r: chan of string)
139{
140	ctxt.ctxt.modules(r);
141}
142
143Pcontext.loadtypes(ctxt: self ref Pcontext, name: string): (chan of ref Typescmd[ref Value], string)
144{
145	return ctxt.ctxt.loadtypes(name);
146}
147
148Fscvt.int2ext(cvt: self ref Fscvt, gv: ref Value): ref Alphabet->Value
149{
150	if(gv == nil)
151		return nil;
152	pick v := gv {
153	Vd =>
154		return ref (Alphabet->Value).Vd(v.i);
155	Vf =>
156		return ref (Alphabet->Value).Vf(v.i);
157	Vr =>
158		return ref (Alphabet->Value).Vr(v.i);
159	Vs =>
160		return ref (Alphabet->Value).Vs(v.i);
161	Vc =>
162		return ref (Alphabet->Value).Vc(v.i);
163	* =>
164		id := cvt.values.add(gv);
165		return ref (Alphabet->Value).Vz((gv.typec(), id));
166	}
167}
168
169Fscvt.ext2int(cvt: self ref Fscvt, ev: ref Alphabet->Value): ref Value
170{
171	if(ev == nil)
172		return nil;
173	pick v := ev {
174	Vd =>
175		return ref Value.Vd(v.i);
176	Vw =>
177		return nil;		# can't happen
178	Vf =>
179		return ref Value.Vf(v.i);
180	Vr =>
181		return ref Value.Vr(v.i);
182	Vs =>
183		return ref Value.Vs(v.i);
184	Vc =>
185		return ref Value.Vc(v.i);
186	Vz =>
187		x := cvt.values.v[v.i.id].t1;
188		if(x == nil){
189			sys->print("fstypes: bad id %d, type %c\n", v.i.id, v.i.typec);
190			return nil;
191		}
192		return x;
193	}
194}
195
196Fscvt.free(cvt: self ref Fscvt, gv: ref Alphabet->Value, used: int)
197{
198	pick v := gv {
199	Vz =>
200		id := v.i.id;
201		cvt.values.v[id].t1.free(used);
202		cvt.values.del(id);
203	}
204}
205
206Fscvt.dup(cvt: self ref Fscvt, gv: ref Alphabet->Value): ref Alphabet->Value
207{
208	pick ev := gv {
209	Vz =>
210		id := ev.i.id;
211		v := cvt.values.v[id].t1;
212		nv := v.dup();
213		if(nv == nil)
214			return nil;
215		if(nv != v)
216			return ref (Alphabet->Value).Vz((ev.i.typec, cvt.values.add(nv)));
217		cvt.values.inc(id);
218		return ev;
219	* =>
220		return nil;
221	}
222}
223
224checkload[T](m: T, path: string): T
225{
226	if(m != nil)
227		return m;
228	sys->fprint(sys->fildes(2), "fstypes: cannot load %s: %r\n", path);
229	raise "fail:bad module";
230}
231