Interface Coder


  • public interface Coder
    TypeScript code generator: emits TypeScript type declarations from Hydra modules
    • Method Detail

      • collectForallParams

        static java.util.List<Name> collectForallParams​(Type t)
        Collect the bound parameter names from a chain of nested foralls, in outer-to-inner order; stops at the first non-forall type
      • collectImports

        static java.util.Set<Name> collectImports​(ModuleName currentNs,
                                                  Type t)
        Collect the names of every type referenced in a type tree that belongs to a different namespace than the current module, for computing the imports needed at the top of the emitted .ts file
      • collectInnerTypeImports

        static java.util.Set<Name> collectInnerTypeImports​(ModuleName currentNs,
                                                           Term term)
        Collect type imports referenced inside a term tree (lambda bodies, type applications, let-binding schemes), supplementing the top-level typeScheme walk
      • collectTermImports

        static java.util.Set<Name> collectTermImports​(ModuleName currentNs,
                                                      Term t)
        Like collectImports but walks a Term, gathering free term-level variables that resolve to a different module
      • eagerFreeVariablesInTerm

        static java.util.Set<Name> eagerFreeVariablesInTerm​(Term term)
        Find the free variables of a term that are referenced eagerly, i.e. outside of any nested lambda body; used to break false dependency cycles caused by DSL-level thunks such as hydra.parsers.lazy
      • eagerFreeVariablesInTerm_dfltVars

        static <T0> java.util.Set<Name> eagerFreeVariablesInTerm_dfltVars​(java.util.function.Function<Term,​java.util.List<Term>> hydra_rewriting_subterms,
                                                                          java.util.function.Function<Term,​java.util.Set<Name>> hydra_typeScript_coder_eagerFreeVariablesInTerm,
                                                                          Term term,
                                                                          T0 ignored)
      • encodeBindingAsStatement

        static Statement encodeBindingAsStatement​(InferenceContext cx,
                                                  Graph g,
                                                  ModuleName currentNs,
                                                  Binding b)
        Encode a let-binding as a TS statement inside an enclosing function body, hoisting lambda-valued bindings as nested function declarations
      • encodeLazyCall

        static Expression encodeLazyCall​(InferenceContext cx,
                                         Graph g,
                                         ModuleName currentNs,
                                         Term headTerm,
                                         java.util.List<Term> args,
                                         java.util.List<java.lang.Boolean> lazyFlags)
        Emit a fully-applied primitive call with selected arguments wrapped in thunks, per a parallel list of laziness flags
      • encodeLiteral

        static Expression encodeLiteral​(Literal lit)
        Render a Hydra literal as a TypeScript expression
      • encodeLiteralType

        static TypeExpression encodeLiteralType​(LiteralType lt)
        Map a Hydra literal type to a TypeScript type expression
      • encodeParam

        static <T0,​T1> Pattern encodeParam​(T0 cx,
                                                 T1 g,
                                                 ModuleName currentNs,
                                                 Name pname,
                                                 Type dom)
        Build a TS function parameter as a pattern, typed unless the domain is the analyze pass's untyped-variable sentinel
      • encodeType

        static <T0,​T1,​T2> Either<T2,​TypeExpression> encodeType​(T0 cx,
                                                                                 T1 g,
                                                                                 ModuleName currentNs,
                                                                                 Type t)
        Map a Hydra type to a TypeScript type expression
      • encodeTypeOrAny

        static <T0,​T1> TypeExpression encodeTypeOrAny​(T0 cx,
                                                            T1 g,
                                                            ModuleName currentNs,
                                                            Type typ)
        Try to encode a Hydra type as a TS type expression, falling back to any if encodeType fails
      • filterNonLocalNames

        static java.util.Set<Name> filterNonLocalNames​(ModuleName currentNs,
                                                       java.util.Set<Name> names)
        Keep only names whose module differs from the current module's
      • flattenApplication

        static Pair<Term,​java.util.List<Term>> flattenApplication​(Term t)
        Walk an application spine, returning the innermost head term and its arguments in application order
      • forceLazyRefs

        static Term forceLazyRefs​(java.util.Set<Name> targets0,
                                  Term term0)
        Rewrite free occurrences of the given target names from x to x(undefined), forcing a thunked binding created by thunkLazyLet
      • functionDeclarationFromTerm

        static FunctionDeclaration functionDeclarationFromTerm​(InferenceContext cx,
                                                               Graph g,
                                                               ModuleName currentNs,
                                                               java.lang.String lname,
                                                               Term term,
                                                               Optional<Type> _mScheme)
        Build a TS function declaration from a Hydra term by peeling lambdas and let bindings into explicit parameters, statements, and a body
      • importsToText

        static java.lang.String importsToText​(java.util.Set<java.lang.String> overlaySubs,
                                              java.lang.String kind,
                                              ModuleName currentNs,
                                              java.util.Set<Name> names)
        Render a set of qualified names as TypeScript import statements grouped by source module
      • lazyFlagsForPrimitive

        static java.util.List<java.lang.Boolean> lazyFlagsForPrimitive​(Graph g,
                                                                       Name name)
        Look up a primitive by name and return its per-parameter laziness flags in parameter order
      • letBindingIsThunkCandidate

        static java.lang.Boolean letBindingIsThunkCandidate​(Binding b)
        True when a let-binding's value should be thunked to preserve Haskell's lazy evaluation semantics
      • mkDocComment

        static Optional<DocumentationComment> mkDocComment​(Optional<java.lang.String> mdesc)
        Build a documentation comment from an optional description string, or nothing if the description is missing or empty
      • moduleToTypeScript

        static Either<Error_,​java.util.Map<java.lang.String,​java.lang.String>> moduleToTypeScript​(java.util.Set<java.lang.String> overlaySubs,
                                                                                                              Module mod,
                                                                                                              java.util.List<Definition> defs,
                                                                                                              InferenceContext cx,
                                                                                                              Graph g)
        Convert a Hydra module to a map from .ts file path to TypeScript source
      • printInterfaceDeclaration

        static java.lang.String printInterfaceDeclaration​(InterfaceDeclaration decl)
        Render an interface declaration
      • printLiteral

        static java.lang.String printLiteral​(Literal lit)
        Render a TypeScript literal as source text
      • printModuleItem

        static java.lang.String printModuleItem​(ModuleItem mi)
        Render a top-level module item
      • printPropertySignature

        static java.lang.String printPropertySignature​(PropertySignature ps)
        Render a property signature, with an optional JSDoc comment prepended
      • printTypeAliasDeclaration

        static java.lang.String printTypeAliasDeclaration​(TypeAliasDeclaration decl)
        Render a type alias declaration
      • printTypeExpression

        static java.lang.String printTypeExpression​(TypeExpression t)
        Render a TypeScript type expression as source text
      • printTypeParameter

        static java.lang.String printTypeParameter​(TypeParameter tp)
        Render one generic type parameter, with an optional extends constraint
      • printTypeParameterList

        static java.lang.String printTypeParameterList​(java.util.List<TypeParameter> tps)
        Render a generic parameter list, or the empty string when there are no parameters
      • sanitizeParamName

        static java.lang.String sanitizeParamName​(Name n)
        Sanitize a Hydra parameter name into a valid, non-reserved TS identifier
      • sortBindingsTopologically

        static java.util.List<Binding> sortBindingsTopologically​(java.util.List<Binding> bindings)
        Reorder let-bindings so each appears after the sibling bindings it depends on, avoiding JS temporal-dead-zone errors
      • sortTermDefsTopologically

        static <T0> java.util.List<TermDefinition> sortTermDefsTopologically​(T0 currentNs,
                                                                             java.util.List<TermDefinition> tdefs)
        Reorder term definitions so each appears after the intra-module definitions it depends on
      • stripForalls

        static Type stripForalls​(Type t)
        Strip leading forall quantifiers from a type, returning the innermost body
      • termHeadVariable

        static Optional<Name> termHeadVariable​(Term t)
        If a term reduces, through annotations and type applications, to a variable reference, return its name
      • thunkLazyBindings

        static Pair<java.util.List<Binding>,​Term> thunkLazyBindings​(java.util.List<Binding> bindings,
                                                                          Term body)
        The bindings-and-body core of thunkLazyLet, shared by the Term_let encoder and the lifted-binding path in functionDeclarationFromTerm
      • thunkLazyLet

        static Let thunkLazyLet​(Let lt)
        Thunk every let-binding for which letBindingIsThunkCandidate holds, and rewrite forced references to it accordingly
      • tsArray

        static Expression tsArray​(java.util.List<Expression> elems)
        An array expression: [e1, e2, ...]
      • tsArrow

        static Expression tsArrow​(java.util.List<java.lang.String> params,
                                  Expression body)
        An untyped arrow function with an expression body
      • tsArrowTyped

        static Expression tsArrowTyped​(java.util.List<Pattern> patterns,
                                       Expression body)
        A typed arrow function with typed parameter patterns
      • tsAsAny

        static Expression tsAsAny​(Expression e)
        Wrap an expression in a TypeScript 'as any' cast
      • tsCall

        static Expression tsCall​(Expression callee,
                                 java.util.List<Expression> args)
        A call expression, parenthesizing the callee when it's an arrow function or other ambiguous call target
      • tsDocEntityRef

        static java.lang.String tsDocEntityRef​(EntityReference v1)
        Render a EntityReference as TSDoc link syntax
      • tsEnvGetGraph

        static <T0> T0 tsEnvGetGraph​(T0 g)
        Identity getter for analyzeFunctionTerm when the environment is a Graph
      • tsEnvSetGraph

        static <T0,​T1> T0 tsEnvSetGraph​(T0 newG,
                                              T1 _old)
        Setter for analyzeFunctionTerm when the environment is a Graph
      • tsEscapeString

        static java.lang.String tsEscapeString​(java.lang.String s)
        Escape a Hydra string for embedding as a double-quoted TypeScript string literal
      • tsExprIdent

        static Expression tsExprIdent​(java.lang.String s)
        A bare identifier expression
      • tsExprStr

        static Expression tsExprStr​(java.lang.String s)
        A string-literal expression
      • tsIdent

        static Identifier tsIdent​(java.lang.String s)
        Wrap a string into a TypeScript identifier
      • tsMember

        static Expression tsMember​(Expression obj,
                                   java.lang.String prop)
        A member-access expression: obj.prop
      • tsNamedType

        static TypeExpression tsNamedType​(java.lang.String n)
        A bare named type reference
      • tsObject

        static Expression tsObject​(java.util.List<Pair<java.lang.String,​Expression>> props)
        An object literal with identifier-style keys
      • tsParam

        static TypeParameter tsParam​(java.lang.String n)
        A generic type parameter with no constraint
      • tsParen

        static Expression tsParen​(Expression e)
        Wrap an expression in parentheses when its serialized form needs grouping
      • tsPropSig

        static PropertySignature tsPropSig​(java.lang.String name,
                                           java.lang.Boolean optional,
                                           TypeExpression typ)
        A readonly property signature, with the name sanitized for TS reserved words
      • tsTypedIdent

        static Pattern tsTypedIdent​(java.lang.String name,
                                    TypeExpression typ)
        A typed-identifier pattern for a function parameter with a known domain type
      • tsUndefined

        static Expression tsUndefined()
        The undefined value as an expression