TypedInterpreter

public class TypedInterpreter : Interpreter, Printer

A type of interpreter implementation that is capable of evaluating arbitrary string expressions to strongly typed variables

  • The result is a strongly typed value or nil (if it cannot be properly processed)

    Declaration

    Swift

    public typealias EvaluatedType = Any?
  • The global context used for every evaluation with this instance

    Declaration

    Swift

    public let context: Context
  • The interpreter used for evaluating variable values. In case of the TypedInterpreter, it’s itself

    Declaration

    Swift

    public lazy var interpreterForEvaluatingVariables: TypedInterpreter { get set }
  • The data types that the expression is capable of recognise

    Declaration

    Swift

    public let dataTypes: [DataTypeProtocol]
  • The list of functions that are available during the evaluation to process the recognised data types

    Declaration

    Swift

    public let functions: [FunctionProtocol]
  • A cache of functions where expressions have matched before. This improves the performance a lot, when computing already established functions

    Declaration

    Swift

    var functionCache: [String : FunctionProtocol]
  • A cache of data types where expressions have matched before. This improves the performance a lot, when computing already established data types

    Declaration

    Swift

    var dataTypeCache: [String : DataTypeProtocol]
  • Each item of the input list (data types, functions and the context) is optional, but strongly recommended to provide them. It’s usual that for every data type, there are a few functions provided, so the list can occasionally be pretty long.

    • context: Global context that is going to be used with every expression evaluated with the current instance. Defaults to empty context

    Declaration

    Swift

    public init(dataTypes: [DataTypeProtocol] = [],
                functions: [FunctionProtocol] = [],
                context: Context = Context())

    Parameters

    dataTypes

    The types the interpreter should recognise the work with

    functions

    The functions that can operate on the dataTypes

  • The evaluation method, that produces the strongly typed results. In this case, only the globally available context can be used

    Declaration

    Swift

    public func evaluate(_ expression: String) -> Any?

    Parameters

    expression

    The input

    Return Value

    The output of the evaluation

  • The evaluation method, that produces the strongly typed results. In this case, only the context is a result of merging the global context and the one provided in the parameter

    Declaration

    Swift

    public func evaluate(_ expression: String, context: Context) -> Any?

    Parameters

    expression

    The input

    context

    Local context that is going to be used with this expression only

    Return Value

    The output of the evaluation

  • If the expression belongs to a cached function, it uses the function converter to evaluate it

    Declaration

    Swift

    func functionFromCache(for expression: String, using context: Context, connectedRanges: [ClosedRange<String.Index>]) -> Any?

    Parameters

    expression

    The expression to evaluate

    context

    The context to be using when the evaluation happens

    connectedRanges

    Ranges of string indices that are connected with opening-closing tag pairs, respectively

    Return Value

    The value - if the expression is interpreted. nil otherwise

  • If the expression belongs to a cached data type, it uses the data type converter to evaluate it

    Declaration

    Swift

    func dataTypeFromCache(for expression: String) -> Any?

    Parameters

    expression

    The expression to evaluate

    Return Value

    The value - if the expression is interpreted. nil otherwise

  • If the expression is recognised as a function, it uses that function to evaluate the value

    Declaration

    Swift

    func function(for expression: String, using context: Context, connectedRanges: [ClosedRange<String.Index>]) -> Any?

    Parameters

    expression

    The expression to evaluate

    context

    The context to be using when the evaluation happens

    connectedRanges

    Ranges of string indices that are connected with opening-closing tag pairs, respectively

    Return Value

    The value - if the expression is interpreted. nil otherwise

  • If the expression is recognised as a data type, it uses that data type to convert its value

    Declaration

    Swift

    func dataType(for expression: String) -> Any?

    Parameters

    expression

    The expression to evaluate

    context

    The context to be using when the evaluation happens

    Return Value

    The value - if the expression is interpreted. nil otherwise

  • If the expression is recognised as a variable, it uses that variable to replace its value

    Declaration

    Swift

    func variable(for expression: String, using context: Context) -> Any?

    Parameters

    expression

    The expression to evaluate

    context

    The context where the variables are stored

    Return Value

    The value - if the expression is interpreted. nil otherwise

  • A helper to be able to effectively print any result, coming out of the evaluation. The print method recognises the used data type and uses its string conversion block

    Declaration

    Swift

    public func print(_ input: Any) -> String

    Parameters

    input

    Any value that is a valid DataType or a CustomStringConvertible instance

    Return Value

    The string representation of the value or empty string if it cannot be processed