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

NAME

Language::Expr::Compiler::JS

VERSION

version 0.06

DESCRIPTION

Compiles Language::Expr expression to JS code. Some notes:

  • JavaScript version

    This compiler emits JavaScript 1.8.1 code (it uses several Array methods like map() and filter() [supported since JavaScript 1.6], 'let' lexical variables [supported since JavaScript 1.7 / Firefox 2] and native JSON [supported since JavaScript 1.8.1 / Firefox 3.5]).

    Currently to test emitted JavaScript code, we use Spidermonkey 1.9.1+ JavaScript interpreter (typically located in /usr/bin/js) as the JavaScript and JE modules are still not up to par.

  • JavaScript-ness

    The emitted JS code will follow JavaScript's weak typing and coercion rules, e.g. Expr '1+"2"' will simply be translated to JavaScript '1+"2"' and will result in "12".

  • Currently strings are rudimentary escaped.

    Data dumping modules can't be used currently due to segfaults (at least in 5.10.1).

  • Variables by default simply use JavaScript variables.

    E.g. $a becomes a, and so on. Be careful not to make variables which are invalid in JavaScript, e.g. $.. or ${foo/bar}.

    You can subclass and override rule_var() if you want to provide your own variables.

  • Functions by default simply use Javascript functions.

    foo() becomes foo(). Except those mentioned in func_mapping (e.g. rand() becomes Math.rand() if func_mapping->{rand} is 'Math.rand').

    Or you can subclass and override rule_func() for more translation freedom.

ATTRIBUTES

todo => ARRAYREF

Used to remember which subexpression need to be parsed later.

func_mapping => HASHREF

Mapping from Expr function to JavaScript functions/methods. Example:

 { ceil => 'Math.ceil',
   uc   => '.toUpperCase',
 }

METHODS

js($expr) => $js_code

Convert Language::Expr expression into JavaScript code. Dies if there is syntax error in expression.

AUTHOR

  Steven Haryanto <stevenharyanto@gmail.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2010 by Steven Haryanto.

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