The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

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

VERSION

version 0.02

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.

variables_via_namespace

When compiling formulas, one option is to look up all runtime variables (passed to the coderef) through the /namespace object, allowing it to do custom processing to resolve the variables. The other option (the default of false) is to put all the coderef parameters into a hashref and directly access that hashref, which is faster and avoids needing to create temporary namespaces.

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 such as merging the coderef parameters into a temporary namespace, and then evals the perl to create the coderef.

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) 2019 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.