|
| 1 | +import { |
| 2 | + ObjectType, |
| 3 | + ValueType, |
| 4 | + array, |
| 5 | +} from '../types'; |
| 6 | + |
| 7 | +import type {Expression} from '../expression'; |
| 8 | +import type {ParsingContext} from '../parsing_context'; |
| 9 | +import type {EvaluationContext} from '../evaluation_context'; |
| 10 | +import type {Type} from '../types'; |
| 11 | +import {typeOf, Value, isValue} from '../values'; |
| 12 | +import {Literal} from './literal'; |
| 13 | +import exp from 'constants'; |
| 14 | + |
| 15 | +export abstract class Semiliteral implements Expression { |
| 16 | + type: Type; |
| 17 | + |
| 18 | + constructor(type: Type) { |
| 19 | + this.type = type; |
| 20 | + } |
| 21 | + |
| 22 | + static parse(args: ReadonlyArray<unknown>, context: ParsingContext): Expression { |
| 23 | + if (args.length !== 2) |
| 24 | + return context.error(`'semiliteral' expression requires exactly one argument, but found ${args.length - 1} instead.`) as null; |
| 25 | + |
| 26 | + if (!isValue(args[1])) |
| 27 | + return context.error('invalid value') as null; |
| 28 | + |
| 29 | + const value = args[1] as Value; |
| 30 | + const type = typeOf(value); |
| 31 | + |
| 32 | + if (type.kind === 'array') { |
| 33 | + const arr = value as Array<unknown>; |
| 34 | + const parsed = arr.map(item => context.parse(item, null, ValueType)); |
| 35 | + return new ArraySemiliteral(parsed); |
| 36 | + } else if (type.kind === 'object') { |
| 37 | + const obj = value as Record<string, unknown>; |
| 38 | + const parsed = Object.keys(obj).reduce((acc, key) => { |
| 39 | + acc[key] = context.parse(obj[key], null, ValueType); |
| 40 | + return acc; |
| 41 | + }, {} as Record<string, Expression>); |
| 42 | + return new ObjectSemiliteral(parsed); |
| 43 | + } else { |
| 44 | + return new Literal(type, value); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + abstract evaluate(ctx: EvaluationContext): Value; |
| 49 | + |
| 50 | + abstract eachChild(fn: (_: Expression) => void): void; |
| 51 | + |
| 52 | + abstract outputDefined(): boolean; |
| 53 | +} |
| 54 | + |
| 55 | +class ArraySemiliteral extends Semiliteral { |
| 56 | + arr: Array<Expression>; |
| 57 | + |
| 58 | + constructor(arr: Array<Expression>) { |
| 59 | + let elementType: Type | null = null; |
| 60 | + for (const expr of arr) { |
| 61 | + if (!elementType) { |
| 62 | + elementType = expr.type; |
| 63 | + } else if (elementType === expr.type) { |
| 64 | + continue; |
| 65 | + } else { |
| 66 | + elementType = ValueType; |
| 67 | + break; |
| 68 | + } |
| 69 | + } |
| 70 | + super(array(elementType ?? ValueType, arr.length)); |
| 71 | + this.arr = arr; |
| 72 | + } |
| 73 | + |
| 74 | + evaluate(ctx: EvaluationContext): Array<Value> { |
| 75 | + return this.arr.map(arg => arg.evaluate(ctx)); |
| 76 | + } |
| 77 | + |
| 78 | + eachChild(fn: (_: Expression) => void) { |
| 79 | + this.arr.forEach(fn); |
| 80 | + } |
| 81 | + |
| 82 | + outputDefined() { |
| 83 | + return this.arr.every(arg => arg.outputDefined()); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +class ObjectSemiliteral extends Semiliteral { |
| 88 | + obj: Record<string, Expression>; |
| 89 | + |
| 90 | + constructor(obj: Record<string, Expression>) { |
| 91 | + super(ObjectType); |
| 92 | + this.obj = obj; |
| 93 | + } |
| 94 | + |
| 95 | + evaluate(ctx: EvaluationContext): Record<string, Value> { |
| 96 | + return Object.keys(this.obj).reduce((acc, key) => { |
| 97 | + acc[key] = this.obj[key].evaluate(ctx); |
| 98 | + return acc; |
| 99 | + }, {} as Record<string, Value>); |
| 100 | + } |
| 101 | + |
| 102 | + eachChild(fn: (_: Expression) => void) { |
| 103 | + Object.values(this.obj).forEach(fn); |
| 104 | + } |
| 105 | + |
| 106 | + outputDefined() { |
| 107 | + return Object.values(this.obj).every(arg => arg.outputDefined()); |
| 108 | + } |
| 109 | +} |
0 commit comments