From abe30ac2b292e142fb5c3fe28240c7f1b0861665 Mon Sep 17 00:00:00 2001 From: MadMaurice Date: Sun, 4 Apr 2021 01:17:18 +0200 Subject: [PATCH] Add compile_file function --- lib/Minilisp.pm | 10 ++++++++++ tool/run.pl | 17 +++++++++++++++++ tool/test-runner.pl | 9 +-------- 3 files changed, 28 insertions(+), 8 deletions(-) create mode 100644 tool/run.pl diff --git a/lib/Minilisp.pm b/lib/Minilisp.pm index f776389..795518b 100644 --- a/lib/Minilisp.pm +++ b/lib/Minilisp.pm @@ -613,4 +613,14 @@ sub compile { return $parsed; } +sub compile_file { + my $file = shift; + + open(my $fh, "<", $file) or die "Could not open $file: $!"; + my $script = do { local $/; <$fh> }; + close($fh); + + return compile($script); +} + 1; diff --git a/tool/run.pl b/tool/run.pl new file mode 100644 index 0000000..a7602a7 --- /dev/null +++ b/tool/run.pl @@ -0,0 +1,17 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use FindBin; +use lib "$FindBin::Bin/../lib"; +use Minilisp; + +my $scriptfile = shift; + +die "No script file provided." unless defined $scriptfile && -f $scriptfile; + +my $parsed = Minilisp::compile_file($scriptfile); + +$parsed->($ctx); + +exit 0; diff --git a/tool/test-runner.pl b/tool/test-runner.pl index c23dcae..0facdc2 100755 --- a/tool/test-runner.pl +++ b/tool/test-runner.pl @@ -13,14 +13,7 @@ my $scriptfile = shift; die "No script file provided." unless defined $scriptfile && -f $scriptfile; -open(my $fh, "<", $scriptfile) or die "Could not open $scriptfile: $!"; -my $script = do { local $/; <$fh> }; -close($fh); - -# Remove shebang -$script =~ s/^#!.*\n//; - -my $parsed = Minilisp::compile($script); +my $parsed = Minilisp::compile_file($scriptfile); my $plan = 0;