Repair an error and a couple of infelicities in quasivalue.ts

This commit is contained in:
Tony Garnock-Jones 2021-12-13 12:20:51 +01:00
parent 5cc9fa29ed
commit 4d42968cd6
1 changed files with 21 additions and 8 deletions

View File

@ -4,7 +4,7 @@
import { AnyValue, Ref } from './actor.js';
import { Pattern } from '../gen/dataspacePatterns.js';
import * as P from './pattern.js';
import { Value, RecordConstructorInfo, is } from '@preserves/core';
import { Value, RecordConstructorInfo, is, Record } from '@preserves/core';
import { Meta, Type, GenType } from '@preserves/schema';
export type DefinitionOrVariantInfo = {
@ -33,7 +33,7 @@ export function bind(p?: QuasiValue): QuasiValue {
return { type: 'bind', inner: p ?? _ };
}
bind.quasiValue = (inner: QuasiValue) => rec(Symbol.for('bind'), inner);
bind.quasiValue = (inner?: QuasiValue) => rec(Symbol.for('bind'), inner ?? _);
export function discard(): QuasiValue {
return { type: 'discard' };
@ -50,11 +50,21 @@ export function lit(value: AnyValue): QuasiValue {
lit.quasiValue = (q: QuasiValue) => rec(Symbol.for('lit'), q);
export function rec(label: AnyValue, ... items: QuasiValue[]): QuasiValue {
return { type: 'rec', label, items };
const literals = items.flatMap(i => i.type === 'lit' ? [i.value] : []);
if (literals.length === items.length) {
return lit(Record(label, literals));
} else {
return { type: 'rec', label, items };
}
}
export function arr(... items: QuasiValue[]): QuasiValue {
return { type: 'arr', items };
const literals = items.flatMap(i => i.type === 'lit' ? [i.value] : []);
if (literals.length === items.length) {
return lit(literals);
} else {
return { type: 'arr', items };
}
}
export function dict(... entries: [AnyValue, QuasiValue][]): QuasiValue {
@ -194,10 +204,13 @@ export function ctor(info: QuasiValueConstructorInfo, ... items: QuasiValue[]):
}
function qArr(q: QuasiValue): QuasiValue[] {
if (q.type !== 'arr') {
if (q.type === 'arr') {
return q.items;
} else if (q.type === 'lit' && Array.isArray(q.value)) {
return q.value.map(lit);
} else {
throw new Error("Array of quasivalues needed");
}
return q.items;
}
switch (def._variant) {
@ -226,9 +239,9 @@ export function ctor(info: QuasiValueConstructorInfo, ... items: QuasiValue[]):
export function finish(q: QuasiValue): Pattern {
// console.log('--------------------------');
// console.log(require('util').inspect(q, {depth: null}));
// console.log(q);
const p = walk(q);
// console.log(stringify(fromPattern(p)))
// console.log(p);
return p;
}