summaryrefslogtreecommitdiff
path: root/appl/alphabet/typesets/gridtypes.b
blob: 05fc1bcec90075eb95277e6c4f3419f156c29be7 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# warning: autogenerated code; don't bother to change this, change mktypeset.b or grid.b instead
implement Gridtypes;
include "sys.m";
	sys: Sys;
include "alphabet/reports.m";
include "draw.m";
include "sh.m";
include "alphabet.m";
	extvalues: Extvalues;
	Values: import extvalues;
	proxymod: Proxy;
	Typescmd, Modulecmd: import Proxy;
include "alphabet/endpoints.m";
include "grid.m";
	grid: Grid;
	Value: import grid;
include "gridtypes.m";

Pcontext: adt {
	cvt: ref Gridcvt;
	ctxt: ref Context;

	loadtypes: fn(ctxt: self ref Pcontext, name: string): (chan of ref Proxy->Typescmd[ref Value], string);
	type2s: fn(ctxt: self ref Pcontext, tc: int): string;
	alphabet: fn(ctxt: self ref Pcontext): string;
	modules: fn(ctxt: self ref Pcontext, r: chan of string);
	find: fn(ctxt: self ref Pcontext, s: string): (ref Module, string);
	getcvt: fn(ctxt: self ref Pcontext): ref Gridcvt;
};

proxy(): chan of ref Typescmd[ref Alphabet->Value]
{
	return proxy0().t0;
}

proxy0(): (
		chan of ref Typescmd[ref Alphabet->Value],
		chan of (string, chan of ref Typescmd[ref Grid->Value]),
		ref Gridcvt
	)
{
	sys = load Sys Sys->PATH;
	extvalues = checkload(load Extvalues Extvalues->PATH, Extvalues->PATH);
	proxymod = checkload(load Proxy Proxy->PATH, Proxy->PATH);
	grid = checkload(load Grid Grid->PATH, Grid->PATH);
	grid->init();
	cvt := ref Gridcvt(Values[ref Value].new());
	(t, newts) := proxymod->proxy(ref Pcontext(cvt, Context.new()));
	return (t, newts, cvt);
}

include "readdir.m";
Context: adt {
	modules: fn(ctxt: self ref Context, r: chan of string);
	loadtypes: fn(ctxt: self ref Context, name: string)
		: (chan of ref Proxy->Typescmd[ref Value], string);
	find: fn(ctxt: self ref Context, s: string): (ref Module, string);
	new:	fn(): ref Context;
};
Module: adt {
	m: Gridmodule;
	run: fn(m: self ref Module, ctxt: ref Draw->Context, r: ref Reports->Report,
		errorc: chan of string, opts: list of (int, list of ref Value),
		args: list of ref Value): ref Value;
	typesig: fn(m: self ref Module): string;
	quit: fn(m: self ref Module);
};
Context.new(): ref Context
{
	return nil;
}
Context.loadtypes(nil: self ref Context, name: string): (chan of ref Typescmd[ref Value], string)
{
	p := "/dis/alphabet/grid/"+name+"types.dis";
	types := load Gridsubtypes p;
	if(types == nil)
		return (nil, sys->sprint("cannot load %q: %r", p));
	return (types->proxy(), nil);
}
Context.modules(nil: self ref Context, r: chan of string)
{
	if((readdir := load Readdir Readdir->PATH) != nil){
		(a, nil) := readdir->init("/dis/alphabet/grid", Readdir->NAME|Readdir->COMPACT);
		for(i := 0; i < len a; i++){
			m := a[i].name;
			if((a[i].mode & Sys->DMDIR) == 0 && len m > 4 && m[len m - 4:] == ".dis")
				r <-= m[0:len m - 4];
		}
	}
	r <-= nil;
}
Context.find(nil: self ref Context, s: string): (ref Module, string)
{
	p := "/dis/alphabet/grid/"+s+".dis";
	m := load Gridmodule p;
	if(m == nil)
		return (nil, sys->sprint("cannot load %q: %r", p));
	{
		m->init();
	} exception e {
	"fail:*" =>
		return (nil, "init failed: " + e[5:]);
	}
	return (ref Module(m), nil);
}
Module.run(m: self ref Module, nil: ref Draw->Context, r: ref Reports->Report, errorc: chan of string,
		opts: list of (int, list of ref Value), args: list of ref Value): ref Value
{
	return  m.m->run(errorc, r, opts, args);
}
Module.typesig(m: self ref Module): string
{
	return m.m->types();
}
Module.quit(nil: self ref Module)
{
}
Pcontext.type2s(nil: self ref Pcontext, tc: int): string
{
	return Value.type2s(tc);
}

Pcontext.alphabet(nil: self ref Pcontext): string
{
	return "bewcrfs";
}

Pcontext.getcvt(ctxt: self ref Pcontext): ref Gridcvt
{
	return ctxt.cvt;
}

Pcontext.find(ctxt: self ref Pcontext, s: string): (ref Module, string)
{
	return ctxt.ctxt.find(s);
}

Pcontext.modules(ctxt: self ref Pcontext, r: chan of string)
{
	ctxt.ctxt.modules(r);
}

Pcontext.loadtypes(ctxt: self ref Pcontext, name: string): (chan of ref Typescmd[ref Value], string)
{
	return ctxt.ctxt.loadtypes(name);
}

Gridcvt.int2ext(cvt: self ref Gridcvt, gv: ref Value): ref Alphabet->Value
{
	if(gv == nil)
		return nil;
	pick v := gv {
	Vw =>
		return ref (Alphabet->Value).Vw(v.i);
	Vf =>
		return ref (Alphabet->Value).Vf(v.i);
	Vr =>
		return ref (Alphabet->Value).Vr(v.i);
	Vs =>
		return ref (Alphabet->Value).Vs(v.i);
	Vc =>
		return ref (Alphabet->Value).Vc(v.i);
	* =>
		id := cvt.values.add(gv);
		return ref (Alphabet->Value).Vz((gv.typec(), id));
	}
}

Gridcvt.ext2int(cvt: self ref Gridcvt, ev: ref Alphabet->Value): ref Value
{
	if(ev == nil)
		return nil;
	pick v := ev {
	Vd =>
		return nil;		# can't happen
	Vw =>
		return ref Value.Vw(v.i);
	Vf =>
		return ref Value.Vf(v.i);
	Vr =>
		return ref Value.Vr(v.i);
	Vs =>
		return ref Value.Vs(v.i);
	Vc =>
		return ref Value.Vc(v.i);
	Vz =>
		x := cvt.values.v[v.i.id].t1;
		if(x == nil){
			sys->print("gridtypes: bad id %d, type %c\n", v.i.id, v.i.typec);
			return nil;
		}
		return x;
	}
}

Gridcvt.free(cvt: self ref Gridcvt, gv: ref Alphabet->Value, used: int)
{
	pick v := gv {
	Vz =>
		id := v.i.id;
		cvt.values.v[id].t1.free(used);
		cvt.values.del(id);
	}
}

Gridcvt.dup(cvt: self ref Gridcvt, gv: ref Alphabet->Value): ref Alphabet->Value
{
	pick ev := gv {
	Vz =>
		id := ev.i.id;
		v := cvt.values.v[id].t1;
		nv := v.dup();
		if(nv == nil)
			return nil;
		if(nv != v)
			return ref (Alphabet->Value).Vz((ev.i.typec, cvt.values.add(nv)));
		cvt.values.inc(id);
		return ev;
	* =>
		return nil;
	}
}

checkload[T](m: T, path: string): T
{
	if(m != nil)
		return m;
	sys->fprint(sys->fildes(2), "gridtypes: cannot load %s: %r\n", path);
	raise "fail:bad module";
}