expression.ts 1.3 KB

12345678910111213141516171819202122232425262728
  1. /*
  2. * Copyright (c) 2018 Mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. type Expression =
  7. | Expression.Literal
  8. | Expression.Symbol
  9. | Expression.Apply
  10. namespace Expression {
  11. export type Literal = string | number | boolean
  12. export type Symbol = { kind: 'symbol', name: string }
  13. export type Arguments = Expression[] | { [name: string]: Expression }
  14. export interface Apply { readonly head: Expression, readonly args?: Arguments }
  15. export function Symbol(name: string): Symbol { return { kind: 'symbol', name }; }
  16. export function Apply(head: Expression, args?: Arguments): Apply { return args ? { head, args } : { head }; }
  17. export function isArgumentsArray(e: Arguments): e is Expression[] { return e instanceof Array; }
  18. export function isArgumentsMap(e: Arguments): e is { [name: string]: Expression } { return !(e instanceof Array); }
  19. export function isLiteral(e: Expression): e is Expression.Literal { return !isApply(e); }
  20. export function isApply(e: Expression): e is Expression.Apply { return !!e && !!(e as Expression.Apply).head && typeof e === 'object'; }
  21. export function isSymbol(e: Expression): e is Expression.Symbol { return !!e && (e as any).kind === 'symbol' }
  22. }
  23. export default Expression