NAME

Language::FormulaEngine::Compiler - Compile a parse tree into perl code

VERSION

version 0.06

DESCRIPTION

ATTRIBUTES

namespace

Namespace to use for looking up functions, converting functions to perl code, and symbolic constants. The namespace will also be bound into the coderefs which get compiled, so any change to the variables (not constants) of the namespace will be visible to compiled formulas.

output_api

Determines the function signature of the coderef that will be generated. Currently supported values are

"function_of_vars"

Compiles as $return = my_formula(%vars) or $return = my_formula(\%vars)

"function_of_namespace"

Compiles as $return = my_formula($namespace) or $return = my_formula(%namespace_attrs) where %namespace_attrs get passed to clone_and_merge of "namespace".

optimize_var_access

By default, when a formula accesses a variable it will call "get_value" in Language::FormulaEngine::Namespace but for higher performance, you can have the formula directly access the variables hashref, bypassing get_value.

If this attribute is not set, the compilation will default to using the optimization if the "namespace" is using the default implementation of get_value (i.e. has not been overridden by a subclass) and if output_api is "function_of_vars".

error

After a failed call to compile, this attribute holds the error message.

code_body

After compilation, this attribute holds the perl source code that was generated prior to being wrapped with the coderef boilerplate.

METHODS

compile( $parse_tree, $subname )

Compile a parse tree, returning a coderef. Any references to functions will be immeditely looked up within the "namespace". Any references to constants in the "namespace" will be inlined into the generated perl. Any other symbol is assumed to be a variable, and will be looked up from the "namespace" at the time the formula is invoked. The generated coderef takes parameters of overrides for the set of variables in the namespace:

  $value= $compiled_sub->(%vars); # vars are optional

Because the generated coderef contains a reference to the namespace, be sure never to store one of the coderefs into that namespace object, else you get a memory leak.

The second argument $subname is optional, but provided to help encourage use of "set_subname" in Sub::Util for generated code.

reset

Clear any temporary results from the last compilation. Returns $self.

generate_coderef_wrapper

  my $coderef= $compiler->generate_coderef_wrapper($perl_code, $subname);

Utility method used by "compile" that wraps a bit of perl code with the relevant boilerplate according to "output_api", and then evals the perl to create the coderef.

On a compile failure, this returns undef and puts the error message into "error".

perlgen( $parse_node )

Generate perl source code for a parse node.

perlgen_var_access

  $compiler->perlgen_var_access($varname);

Generate perl code to access a variable. If "variables_via_namespace" is true, this becomes a call to $namespace->get_value($varname). Else it becomes a reference to the variables hashref $vars->{$varname}.

perlgen_string_literal

Generate a perl string literal. This wraps the string with double-quotes and escapes control characters and ["\\\@\$] using hex-escape notation.

perlgen_literal

If the scalar can be exactly represented by a perl numeric literal, this returns that literal, else it wraps the string with qoutes using "perlgen_string_literal".

AUTHOR

Michael Conrad <mconrad@intellitree.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2021 by Michael Conrad, IntelliTree Solutions llc.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.