elliptic.Kernel.Context Module

class elliptic.Kernel.Context.Context[source]

Bases: object

Defines a context for code generation.

A Context is basically a dictionary of stacks. In other words, it defines a stack_name -> stack mapping. Each stack has is semantically defined by its stack_name.

Example

>>> context = Context()
>>> context.put_value('current_value', '100')
>>> context.put_value('current_value', '200')
>>> context.get_value('current_value')  # 200
>>> context.pop_value('current_value')
>>> context.get_value('current_value')  # 100
clear_values(name)[source]

Clears the stack named name.

Parameters

name (str) – Stack name.

Return type

None

get_value(name)[source]

Gets the front value of the stack named name.

Parameters

name (str) – Stack name.

Return type

str

pop_value(name)[source]

Pops the front value of the stack named name.

Parameters

name (str) – Stack name.

Return type

None

put_value(name, value)[source]

Pushes the value value to a stack named name.

Parameters
Return type

None

class elliptic.Kernel.Context.ContextDelegate(context, unique_id)[source]

Bases: abc.ABC

Delegate class for getting the generated code template file and its kwargs for a given expression.

Also defines the context state changes when the corresponding expression node is visited and exited.

context

Context instance.

unique_id

A unique id that can be used to identify values that were created from this context delegate.

Parameters
  • context (Context) – Context instance.

  • unique_id (int) – A unique id.

abstract context_enter()[source]

Modifies the context state. Called when the expression node is visited.

Use this method to prepare the context for expressions that will be visited afterwards. It is preferable to keep most context.put_value calls in this method.

Return type

None

abstract context_exit()[source]

Modifies the context state. Called when the expression node is exited.

Use this method to clear values from the context and prepare it for the expression nodes that were visited before. It is preferable to keep most context.pop_value calls in this method.

Return type

None

abstract get_template_file()[source]

Returns the template file containing the generated code for the expression.

Return type

str

abstract template_kwargs()[source]

Returns the arguments (a dictionary) that will be passed to the template.

Return type

Dict[str, Any]

exception elliptic.Kernel.Context.ContextException[source]

Bases: Exception

Exception raised when an error related to a Context operation occurs.