| 1 | package Zuzu; | ||||||||||||
| 2 | |||||||||||||
| 3 | 13 | ▶ | use utf8; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 13 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 13 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 13 | our $VERSION = '0.007002'; | |||||||||||
| 8 | |||||||||||||
| 9 | 13 | ▶ | use Exporter qw( import ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 13 | our @EXPORT_OK = qw( | |||||||||||
| 11 | zuzu_eval | ||||||||||||
| 12 | zuzu_evalfile | ||||||||||||
| 13 | ); | ||||||||||||
| 14 | |||||||||||||
| 15 | 13 | ▶ | use Zuzu::Parser; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | 13 | ▶ | use Zuzu::Runtime; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | |||||||||||||
| 18 | 12 | ▶ | sub zuzu_eval { | ||||||||||
|
Subroutine
zuzu_eval: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 19 | 12 | my ( $script, $runtime_options ) = @_; | |||||||||||
| 20 | 12 | ▶ | $runtime_options ||= {}; | ||||||||||
|
Condition100%2/2
$runtime_options ||= {}
MC/DC100%1/1
$runtime_options ||= {}
$runtime_options
Truth table100%2/2
A
A= $runtime_options
| |||||||||||||
| 21 | |||||||||||||
| 22 | 12 | my $runtime = Zuzu::Runtime->new( %{ $runtime_options } ); | |||||||||||
| 23 | 12 | my $parser = Zuzu::Parser->new( | |||||||||||
| 24 | disabled_visitors => $runtime->disabled_visitors, | ||||||||||||
| 25 | ); | ||||||||||||
| 26 | 12 | my $ast = $parser->parse( $script, '<eval>' ); | |||||||||||
| 27 | 12 | my $result = $runtime->evaluate($ast); | |||||||||||
| 28 | |||||||||||||
| 29 | 8 | return $result; | |||||||||||
| 30 | } | ||||||||||||
| 31 | |||||||||||||
| 32 | 1 | ▶ | sub zuzu_evalfile { | ||||||||||
|
Subroutine
zuzu_evalfile: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 33 | 1 | my ( $filename, $runtime_options ) = @_; | |||||||||||
| 34 | 1 | ▶ | $runtime_options ||= {}; | ||||||||||
|
Condition50%1/2
$runtime_options ||= {}
MC/DC0%0/1
$runtime_options ||= {}
$runtime_options
Truth table50%1/2
A
A= $runtime_options
| |||||||||||||
| 35 | |||||||||||||
| 36 | 1 | ▶ | open my $fh, '<:encoding(UTF-8)', $filename | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 37 | or die "Could not open '$filename': $!\n"; | ||||||||||||
| 38 | 1 | local $/; | |||||||||||
| 39 | 1 | my $source = <$fh>; | |||||||||||
| 40 | 1 | close $fh; | |||||||||||
| 41 | |||||||||||||
| 42 | 1 | my $runtime = Zuzu::Runtime->new( %{ $runtime_options } ); | |||||||||||
| 43 | 1 | my $parser = Zuzu::Parser->new( | |||||||||||
| 44 | disabled_visitors => $runtime->disabled_visitors, | ||||||||||||
| 45 | ); | ||||||||||||
| 46 | 1 | my $ast = $parser->parse( $source, $filename ); | |||||||||||
| 47 | 1 | my $result = $runtime->evaluate($ast); | |||||||||||
| 48 | |||||||||||||
| 49 | 1 | return $result; | |||||||||||
| 50 | } | ||||||||||||
| 51 | |||||||||||||
| 52 | 13 | 1; | |||||||||||
| 53 | |||||||||||||
| 54 | =pod | ||||||||||||
| 55 | |||||||||||||
| 56 | =head1 NAME | ||||||||||||
| 57 | |||||||||||||
| 58 | Zuzu - API helpers for evaluating ZuzuScript | ||||||||||||
| 59 | |||||||||||||
| 60 | =head1 SYNOPSIS | ||||||||||||
| 61 | |||||||||||||
| 62 | use Zuzu qw( zuzu_eval zuzu_evalfile ); | ||||||||||||
| 63 | |||||||||||||
| 64 | my $runtime = zuzu_eval( | ||||||||||||
| 65 | 'function hi() { return "hello"; }', | ||||||||||||
| 66 | { deny_modules => [ 'std/io' ] }, | ||||||||||||
| 67 | ); | ||||||||||||
| 68 | |||||||||||||
| 69 | my $runtime2 = zuzu_evalfile( | ||||||||||||
| 70 | 'example.zzs', | ||||||||||||
| 71 | { lib => [ './modules' ] }, | ||||||||||||
| 72 | ); | ||||||||||||
| 73 | |||||||||||||
| 74 | =head1 DESCRIPTION | ||||||||||||
| 75 | |||||||||||||
| 76 | Provides helper functions for one-shot parsing and evaluation. | ||||||||||||
| 77 | |||||||||||||
| 78 | =head1 FUNCTIONS | ||||||||||||
| 79 | |||||||||||||
| 80 | =head2 zuzu_eval( $script, \%runtime_options ) | ||||||||||||
| 81 | |||||||||||||
| 82 | Parses and evaluates a ZuzuScript source string and returns the | ||||||||||||
| 83 | result of evaluation. | ||||||||||||
| 84 | |||||||||||||
| 85 | =head2 zuzu_evalfile( $filename, \%runtime_options ) | ||||||||||||
| 86 | |||||||||||||
| 87 | Loads a UTF-8 script file, parses and evaluates it, and returns | ||||||||||||
| 88 | the result of evaluation. | ||||||||||||
| 89 | |||||||||||||
| 90 | =head1 SEE ALSO | ||||||||||||
| 91 | |||||||||||||
| 92 | L<https://zuzulang.org/>. | ||||||||||||
| 93 | |||||||||||||
| 94 | =head1 COPYRIGHT AND LICENCE | ||||||||||||
| 95 | |||||||||||||
| 96 | B<< Zuzu >> is copyright Toby Inkster. | ||||||||||||
| 97 | |||||||||||||
| 98 | It is free software; you may redistribute it and/or modify it under | ||||||||||||
| 99 | the terms of either the Artistic License 1.0 or the GNU General Public | ||||||||||||
| 100 | License version 2. | ||||||||||||
| 101 | |||||||||||||
| 102 | =cut | ||||||||||||