interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / train / aien-train / native / omegese45 / translate / en2goal.om

446 lines · 8721 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note en2goal: deterministic rule-based English request -> R45 goal line G.
3note No model. The grammar is the rule table otrules below, documented
4note verbatim in docs/omegese45/TRANSLATOR.md. Needs otio.om before it.
5note
6note Rule table format: one rule per line, `<class> <pattern word> ...`.
7note class m = one mm job, p = a pair, x = has more than one reading (23).
8note A pattern word is literal alternatives joined by `/`, optional when it
9note starts with `?`, or a number slot `#m #k #j #n #s #t` (m, k of the left
10note matrix, j, n of the right matrix, seed, second seed). A rule matches when
11note the whole normalized token sequence matches the whole pattern.
12text otrules "m ?please multiply a/an #m by #k matrix by/with/times a/an #j by #n matrix with seed #s\nm ?please ?run/do/compute/submit ?a/an/one matmul/matmul_u32 ?job ?of ?size #m x #k x #n ?with seed #s\nm goal for review one matmul_u32 job multiplying a/an #m by #k matrix by a/an #j by #n matrix with seed #s\np ?please ?run/do/compute/submit two matmuls/matmul/matmul_u32s/matmul_u32 ?jobs ?of ?size #m x #k x #n ?with seeds #s and #t\np matmuls #m x #k x #n ?with seeds #s ?and #t\np ?please multiply a/an #m by #k matrix by/with/times a/an #j by #n matrix twice with seeds #s and #t\np goal for review two matmul_u32 jobs each multiplying a/an #m by #k matrix by a/an #j by #n matrix with seeds #s and #t in that order\nx ?please ?run/do/compute/submit ?a/an/one matmul/matmul_u32 ?job ?of ?size #m x #k x #n ?with seeds #s ?and #t\nx ?please ?run/do/compute/submit two matmuls/matmul/matmul_u32s/matmul_u32 ?jobs ?of ?size #m x #k x #n ?with seed #s\nx ?please multiply a/an #m by #k matrix by/with/times a/an #j by #n matrix with seeds #s and #t\nx ?please ?run/do/compute/submit ?a/an/one matmul/matmul_u32 ?job ?of ?size #m x #k x #n ?with seed #s and/or ?seed #t\nx ?please ?run/do/compute/submit ?a/an/one matmul/matmul_u32 ?job ?of ?size #m x #k x #n ?with seed #s seed #t\n"
13text otfr "goal |mm |pair |ushort 0x|ulong 0x|end\n|EN_UNSUPPORTED\n|EN_RANGE\n|EN_SHAPE\n|EN_AMBIG\n|usage: en2goal < request\n"
14
15note Normalized request: lowercase words joined by single spaces.
16global otn uchar 2048
17global otnl ulong 1
18global ottb ulong 1024
19global ottl ulong 1024
20global otnt ulong 1
21note Slot values and set flags for the rule being matched (m k j n s t).
22global otsv ulong 6
23global otss ulong 6
24note Accepted reading: kind (1 mm, 2 pair), code, slots m k n s t.
25global otrd ulong 8
26
27const OTBAD ulong 1099511627776
28
29fn otisdg c uchar to bool
30ret and ge c 48 le c 57
31end
32
33note Raw input byte i of otin[0..n) is a digit; false outside the range.
34fn otdgat i ulong n ulong to bool
35if ge i n
36ret false
37end
38ret otisdg get otin i
39end
40
41fn otnemit c uchar to void
42o = get otnl 0
43put otn o c
44put otnl 0 add o 1
45end
46
47note Emit a separator: one space, never leading, never doubled.
48fn otnsep to void
49o = get otnl 0
50if eq o 0
51ret
52end
53if eq get otn sub o 1 32
54ret
55end
56otnemit 32
57end
58
59note Normalize otin[0..n) into otn. Returns false for a byte outside the
60note request alphabet (TAB and 0x20..0x7e). Rules: A-Z lowercased; space,
61note TAB and , ; : ? ! ( ) " separate words; `.` separates words unless a
62note digit is on both sides; `x` next to a digit is a word of its own.
63fn otnorm n ulong to bool
64put otnl 0 0
65for i ulong n
66c = get otin i
67if or lt c 32 gt c 126
68if ne c 9
69ret false
70end
71end
72if and ge c 65 le c 90
73c = add c 32
74end
75sep = false
76if or eq c 9 eq c 32
77sep = true
78end
79if or or eq c 44 eq c 59 or eq c 58 eq c 63
80sep = true
81end
82if or or eq c 33 eq c 40 or eq c 41 eq c 34
83sep = true
84end
85if eq c 46
86pd = otdgat sub i 1 n
87nd = otdgat add i 1 n
88if not and pd nd
89sep = true
90end
91end
92if eq c 120
93pd = otdgat sub i 1 n
94nd = otdgat add i 1 n
95if or pd nd
96otnsep
97otnemit c
98otnsep
99continue
100end
101end
102if sep
103otnsep
104else
105otnemit c
106end
107end
108o = get otnl 0
109if gt o 0
110if eq get otn sub o 1 32
111put otnl 0 sub o 1
112end
113end
114ret true
115end
116
117note Split otn into tokens (start, length).
118fn ottoken to void
119n = get otnl 0
120t = ulong 0
121b = ulong 0
122for i ulong n
123if eq get otn i 32
124put ottb t b
125put ottl t sub i b
126t = add t 1
127b = add i 1
128end
129end
130if gt n 0
131put ottb t b
132put ottl t sub n b
133t = add t 1
134end
135put otnt 0 t
136end
137
138note Token ti is a number word: starts with a digit, or + or - then a digit.
139fn otisnum ti ulong to bool
140b = get ottb ti
141l = get ottl ti
142c = get otn b
143if otisdg c
144ret true
145end
146if and gt l 1 or eq c 43 eq c 45
147ret otisdg get otn add b 1
148end
149ret false
150end
151
152note Decimal value of number token ti; OTBAD when malformed (a sign, a
153note non-digit, or more than 9 digits). OTBAD is outside every range.
154fn otnumv ti ulong to ulong
155b = get ottb ti
156l = get ottl ti
157if gt l 9
158ret OTBAD
159end
160v = ulong 0
161for i ulong l
162c = get otn add b i
163if not otisdg c
164ret OTBAD
165end
166v = add mul v 10 ulong sub c 48
167end
168ret v
169end
170
171note Token ti equals rule text bytes [ps, pe).
172fn oteqw ti ulong ps ulong pe ulong to bool
173b = get ottb ti
174l = get ottl ti
175if ne l sub pe ps
176ret false
177end
178for i ulong l
179if ne get otn add b i get otrules add ps i
180ret false
181end
182end
183ret true
184end
185
186note Token ti equals one of the `/`-separated alternatives in [ps, pe).
187fn otalt ti ulong ps ulong pe ulong to bool
188a = ps
189p = ps
190while le p pe bound 4096
191e = eq p pe
192if not e
193e = eq get otrules p 47
194end
195if e
196if oteqw ti a p
197ret true
198end
199a = add p 1
200end
201p = add p 1
202end
203ret false
204end
205
206fn otslix c uchar to ulong
207if eq c 109
208ret 0
209end
210if eq c 107
211ret 1
212end
213if eq c 106
214ret 2
215end
216if eq c 110
217ret 3
218end
219if eq c 115
220ret 4
221end
222ret 5
223end
224
225note Match the rule pattern from byte pi against tokens from ti to the end.
226fn otmat pi ulong ti ulong to bool
227if eq get otrules pi 10
228ret eq ti get otnt 0
229end
230pe = pi
231while true bound 4096
232c = get otrules pe
233if or eq c 32 eq c 10
234break
235end
236pe = add pe 1
237end
238np = pe
239if eq get otrules pe 32
240np = add pe 1
241end
242ps = pi
243if eq get otrules pi 63
244ps = add pi 1
245if otmat np ti
246ret true
247end
248end
249if ge ti get otnt 0
250ret false
251end
252if eq get otrules ps 35
253if not otisnum ti
254ret false
255end
256si = otslix get otrules add ps 1
257put otsv si otnumv ti
258put otss si 1
259ret otmat np add ti 1
260end
261if not otalt ti ps pe
262ret false
263end
264ret otmat np add ti 1
265end
266
267note A dim is representable in D (0x000..0xfff). Dim 0 and 257..4095 reach
268note the core, which refuses them with `bound` (interface v1.2 section 4).
269fn otdimok v ulong to bool
270ret le v 4095
271end
272
273note Decide one matched m/p rule: 0 ok, 21 range, 22 shape.
274fn otdecide pair bool to ulong
275bad = false
276if not otdimok get otsv 0
277bad = true
278end
279if not otdimok get otsv 1
280bad = true
281end
282if not otdimok get otsv 3
283bad = true
284end
285if gt get otsv 4 65535
286bad = true
287end
288if pair
289if gt get otsv 5 65535
290bad = true
291end
292end
293if eq get otss 2 1
294if not otdimok get otsv 2
295bad = true
296end
297end
298if bad
299ret 21
300end
301if eq get otss 2 1
302if ne get otsv 2 get otsv 1
303ret 22
304end
305end
306ret 0
307end
308
309note Parse the normalized tokens. Returns the exit code; on 0 the reading
310note is in otrd (kind, m, k, n, s, t).
311fn otparse to ulong
312nr = ulong 0
313amb = false
314r = ulong 0
315while lt r len otrules bound 64
316cl = get otrules r
317for i ulong 6
318put otsv i 0
319put otss i 0
320end
321hit = otmat add r 2 0
322if hit
323if eq cl 120
324amb = true
325else
326pair = eq cl 112
327code = otdecide pair
328kind = ulong 1
329if pair
330kind = 2
331end
332if eq nr 0
333put otrd 0 kind
334put otrd 1 code
335put otrd 2 get otsv 0
336put otrd 3 get otsv 1
337put otrd 4 get otsv 3
338put otrd 5 get otsv 4
339put otrd 6 get otsv 5
340else
341if not otsame kind code
342amb = true
343end
344end
345nr = add nr 1
346end
347end
348while ne get otrules r 10 bound 4096
349r = add r 1
350end
351r = add r 1
352end
353if amb
354ret 23
355end
356if eq nr 0
357ret 20
358end
359ret get otrd 1
360end
361
362note The current match gives the same reading as the stored one.
363fn otsame kind ulong code ulong to bool
364if ne kind get otrd 0
365ret false
366end
367if ne code get otrd 1
368ret false
369end
370if ne get otsv 0 get otrd 2
371ret false
372end
373if ne get otsv 1 get otrd 3
374ret false
375end
376if ne get otsv 3 get otrd 4
377ret false
378end
379if ne get otsv 4 get otrd 5
380ret false
381end
382if eq kind 2
383if ne get otsv 5 get otrd 6
384ret false
385end
386end
387ret true
388end
389
390fn otemd v ulong to void
391otpfr 3
392otphex v 3
393otpb 32
394end
395
396fn otems v ulong to void
397otpfr 4
398otphex v 4
399otpb 32
400end
401
402note Append the G line for the reading in otrd.
403fn otemitg to void
404mv = get otrd 2
405kv = get otrd 3
406nv = get otrd 4
407otpfr 0
408if eq get otrd 0 2
409otpfr 2
410else
411otpfr 1
412end
413otemd mv
414otemd kv
415otemd nv
416otems get otrd 5
417if eq get otrd 0 2
418otems get otrd 6
419end
420otpfr 5
421end
422
423note en2goal on otin[0..n): fills otob with the G line or the typed refusal
424note line and returns the exit code (0, 20, 21, 22, 23).
425fn otengoal n ulong to int
426if gt n 0
427if eq get otin sub n 1 10
428n = sub n 1
429end
430end
431code = ulong 20
432if le n 512
433if otnorm n
434ottoken
435if gt get otnt 0 0
436code = otparse
437end
438end
439end
440if eq code 0
441otemitg
442ret 0
443end
444otpfr sub code 14
445ret int code
446end