| 1 | package Lox; | ||||||||||
| 2 | 260 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 260 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 260 | ▶ | use Lox::AstPrinter; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 260 | ▶ | use Lox::Interpreter; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 260 | ▶ | use Lox::Parser; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 260 | ▶ | use Lox::Resolver; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 260 | ▶ | use Lox::Scanner; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 260 | ▶ | use Lox::TokenType; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 260 | our $VERSION = 0.02; | |||||||||
| 11 | |||||||||||
| 12 | 260 | my $had_error = undef; | |||||||||
| 13 | |||||||||||
| 14 | 259 | ▶ | sub run_file { | ||||||||
|
Subroutine
run_file: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 15 | 259 | my ($path, $debug_mode) = @_; | |||||||||
| 16 | 259 | ▶ | open my $fh, '<', $path or die "Error opening $path: $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 17 | 259 | my $text = do { local $/; <$fh> }; | |||||||||
| 18 | 259 | my $interpreter = Lox::Interpreter->new; | |||||||||
| 19 | 259 | Lox::eval($interpreter, $text, $debug_mode); | |||||||||
| 20 | 193 | ▶ | if ($had_error) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | 59 | exit 65; | |||||||||
| 22 | } | ||||||||||
| 23 | } | ||||||||||
| 24 | |||||||||||
| 25 | 0 | ▶ | sub run_prompt { | ||||||||
|
Subroutine
run_prompt: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 26 | 0 | my ($debug_mode) = @_; | |||||||||
| 27 | 0 | print "Welcome to Perl-Lox version $VERSION\n> "; | |||||||||
| 28 | 0 | my $interpreter = Lox::Interpreter->new; | |||||||||
| 29 | 0 | ▶ | while (my $line = <>) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | 0 | Lox::eval($interpreter, $line, $debug_mode); | |||||||||
| 31 | 0 | undef $had_error; | |||||||||
| 32 | 0 | print "> "; | |||||||||
| 33 | } | ||||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | 261 | ▶ | sub eval { | ||||||||
|
Subroutine
eval: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||
| 37 | 261 | my ($interpreter, $source, $debug_mode) = @_; | |||||||||
| 38 | |||||||||||
| 39 | 261 | my $tokens = scan($source, $debug_mode); | |||||||||
| 40 | 261 | ▶ | return if $had_error; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | |||||||||||
| 42 | 258 | my $stmts = parse($tokens, $debug_mode); | |||||||||
| 43 | 258 | ▶ | return if $had_error; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | |||||||||||
| 45 | 215 | Lox::Resolver->new($interpreter)->run($stmts); | |||||||||
| 46 | 215 | ▶ | return if $had_error; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | |||||||||||
| 48 | 202 | $interpreter->interpret($stmts); | |||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | 261 | ▶ | sub scan { | ||||||||
|
Subroutine
scan: called CC=3 SCAR=11.4
Pod
undocumented
| |||||||||||
| 52 | 261 | my ($source, $debug_mode) = @_; | |||||||||
| 53 | 261 | my $scanner = Lox::Scanner->new({source => $source}); | |||||||||
| 54 | 261 | eval { $scanner->scan_tokens }; | |||||||||
| 55 | 261 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | 0 | die "Unexpected error: $@"; | |||||||||
| 57 | } | ||||||||||
| 58 | 261 | ▶ | $scanner->print if $debug_mode; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 261 | return $scanner->tokens; | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | 258 | ▶ | sub parse { | ||||||||
|
Subroutine
parse: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||
| 63 | 258 | my ($tokens, $debug_mode) = @_; | |||||||||
| 64 | 258 | my $parser = Lox::Parser->new({tokens => $tokens}); | |||||||||
| 65 | 258 | my $stmts = $parser->parse; | |||||||||
| 66 | 258 | ▶ | if ($parser->errors->@*) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | 43 | error(@$_) for ($parser->{errors}->@*); | |||||||||
| 68 | } | ||||||||||
| 69 | 258 | ▶ | print Lox::AstPrinter->new->print_tree($stmts), "\n" if $debug_mode; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 258 | return $stmts; | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 66 | ▶ | sub runtime_error { | ||||||||
|
Subroutine
runtime_error: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 74 | 66 | my ($token, $message) = @_; | |||||||||
| 75 | 66 | report($token->{line}, "at '$token->{lexeme}'", $message); | |||||||||
| 76 | 66 | exit 65; | |||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | 62 | ▶ | sub error { | ||||||||
|
Subroutine
error: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 80 | 62 | my ($token, $message) = @_; | |||||||||
| 81 | 62 | $had_error = 1; | |||||||||
| 82 | 62 | report($token->{line}, "at '$token->{lexeme}'", $message); | |||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | 128 | ▶ | sub report { | ||||||||
|
Subroutine
report: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 86 | 128 | my ($line, $where, $message) = @_; | |||||||||
| 87 | 128 | printf "[Line %s] Error %s: %s.\n", $line, $where, $message; | |||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | 260 | 1; | |||||||||
| 91 | __END__ | ||||||||||