Template and rewrite-building routines in rewrite.ts
This commit is contained in:
parent
f5501ccb64
commit
036c342264
|
@ -19,6 +19,8 @@ import {
|
||||||
PEmbedded,
|
PEmbedded,
|
||||||
Pattern,
|
Pattern,
|
||||||
Rewrite,
|
Rewrite,
|
||||||
|
TAttenuate,
|
||||||
|
TCompound,
|
||||||
TRef,
|
TRef,
|
||||||
Template,
|
Template,
|
||||||
_embedded,
|
_embedded,
|
||||||
|
@ -191,6 +193,11 @@ export function rfilter(... patterns: Pattern[]): Caveat {
|
||||||
return ps.length === 1 ? Caveat.Rewrite(ps[0]) : Caveat.Alts(Alts(ps));
|
return ps.length === 1 ? Caveat.Rewrite(ps[0]) : Caveat.Alts(Alts(ps));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function rmap(... rewrites: [Pattern, Template][]): Caveat {
|
||||||
|
const rs = rewrites.map(([pattern, template]) => Rewrite({ pattern, template }));
|
||||||
|
return rs.length === 1 ? Caveat.Rewrite(rs[0]) : Caveat.Alts(Alts(rs));
|
||||||
|
}
|
||||||
|
|
||||||
export function attenuate(ref: Ref, ... a: Caveat[]): Ref {
|
export function attenuate(ref: Ref, ... a: Caveat[]): Ref {
|
||||||
if (a.length === 0) return ref;
|
if (a.length === 0) return ref;
|
||||||
return { ... ref, attenuation: [... (ref.attenuation ?? []), ... a] };
|
return { ... ref, attenuation: [... (ref.attenuation ?? []), ... a] };
|
||||||
|
@ -286,3 +293,27 @@ export function pBoolean(): Pattern {
|
||||||
export function pDiscard(): Pattern {
|
export function pDiscard(): Pattern {
|
||||||
return Pattern.PDiscard(PDiscard());
|
return Pattern.PDiscard(PDiscard());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function tRec(label: SturdyValue, ... fields: Array<Template>): Template {
|
||||||
|
return Template.TCompound(TCompound.rec({ label, fields }));
|
||||||
|
}
|
||||||
|
|
||||||
|
export function tArr(... items: Array<Template>): Template {
|
||||||
|
return Template.TCompound(TCompound.arr(items));
|
||||||
|
}
|
||||||
|
|
||||||
|
export function tDict(... entries: [SturdyValue, Template][]): Template {
|
||||||
|
return Template.TCompound(TCompound.dict(new Dictionary<_embedded, Template>(entries)));
|
||||||
|
}
|
||||||
|
|
||||||
|
export function tLit(value: SturdyValue): Template {
|
||||||
|
return Template.Lit(Lit(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
export function tRef(binding: number): Template {
|
||||||
|
return Template.TRef(TRef(binding));
|
||||||
|
}
|
||||||
|
|
||||||
|
export function tAttenuate(template: Template, ... attenuation: Caveat[]): Template {
|
||||||
|
return Template.TAttenuate(TAttenuate({ template, attenuation }));
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue