Context

public class Context

The only responsibility of the InterpreterContext class is to store variables, and keep them during the execution, where multiple expressions might use the same set of variables.

  • The stored variables

    Declaration

    Swift

    public var variables: [String : Any]
  • Debug information for recognised patterns

    Declaration

    Swift

    public var debugInfo: [String : ExpressionInfo]
  • Context can behave as a stack. If push is called, it saves a snapshot of the current state of variables to a stack and lets you modify the content, while the previous values are stored, safely. When pop is called, it restores the last snapshot, destorying all the changes that happened after the last snapshot. Useful for temporal variables!

    Declaration

    Swift

    var stack: [(variables: [String : Any], debugInfo: [String : ExpressionInfo])]
  • Users of the context may optionally provide an initial set of variables

    Declaration

    Swift

    public init(variables: [String : Any] = [:])

    Parameters

    variables

    Variable names and values

  • Context can behave as a stack. If push is called, it saves a snapshot of the current state of variables to a stack and lets you modify the content, while the previous values are stored, safely. When pop is called, it restores the last snapshot, destorying all the changes that happened after the last snapshot. Useful for temporal variables! It should be called before setting the temporal variables

    Declaration

    Swift

    public func push()
  • Context can behave as a stack. If push is called, it saves a snapshot of the current state of variables to a stack and lets you modify the content, while the previous values are stored, safely. When pop is called, it restores the last snapshot, destorying all the changes that happened after the last snapshot. Useful for temporal variables! It should be called when the temporal variables are not needed anymore

    Declaration

    Swift

    public func pop()
  • Creates a new context instance by merging their variable dictionaries. The one in the parameter overrides the duplicated items of the existing one

    Declaration

    Swift

    public func merging(with other: Context?) -> Context

    Parameters

    with

    The other context to merge with

    Return Value

    A new InterpreterContext instance with the current and the parameter variables merged inside

  • Modifies the current context instance by merging its variable dictionary with the parameter. The one in the parameter overrides the duplicated items of the existing one

    Declaration

    Swift

    public func merge(with other: Context?, merge: (_ existing: Any, _ new: Any) throws -> Any)

    Parameters

    with

    The other context to merge with

    existing

    During the merge the parameter on the existing dictionary (same terminolody with Dictionary.merge)

    new

    During the merge the parameter on the merged dictionary (same terminolody with Dictionary.merge)

    Return Value

    The same InterpreterContext instance after merging the variables dictionary with the variables in the context given as parameter