lib/Lingy/Main.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« lib/Lingy/Lang/ScalarClass.pm ↑ Summary script/_lingy.pl »
1 2 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Lingy::Main;
4  
5 2 ▶use Lingy::Lang::RT;
Subroutine BEGIN: called CC=1 SCAR=2.6
6 use Lingy::Common;
7  
8 use Getopt::Long;
9  
10 use constant default => '--repl';
11 use constant options => +{
12 'clj|C' => 'bool',
13 'dev|D' => 'bool',
14 'eval|e' => 'str',
15 ppp => 'bool',
16 repl => 'bool',
17 run => 'arg',
18 version => 'bool',
19 xxx => 'bool',
20 };
21  
22 sub new {
23 my $class = shift;
24  
25 bless {
26 map( ($_, ''), keys %{$class->options} ),
27 @_,
28 }, $class;
29 }
30  
31 sub run {
32 my ($self, @args) = @_;
33  
34 $self->getopt(@args);
35  
36 my ($repl, $run, $eval, $version, $clj, $dev, $args) =
37 @{$self}{qw<repl run eval version clj dev args>};
38 local @ARGV = @$args;
39  
40 RT->init;
41  
42 RT->rep(qq<(clojure-repl-on)>) if $clj;
43 RT->rep(qq<(use 'lingy.devel)>) if $dev;
44  
45 $version ? $self->do_version() :
46 $eval ? $self->do_eval() :
47 $repl ? $self->do_repl() :
48 $run ? $self->do_run() :
49 $self->do_repl();
50 }
51  
52 sub do_version {
53 RT->rep(
54 '(println (str "Lingy [" *HOST* "] version " (lingy-version)))',
55 );
56 }
57  
58 sub do_eval {
59 my ($self) = @_;
60 my ($repl, $eval, $ppp, $xxx) =
61 @{$self}{qw<repl eval ppp xxx>};
62  
63 if ($repl) {
64 RT->rep(qq<(do $eval\n)>);
65 RT->repl;
66 } else {
67 if ($ppp) {
68 RT->rep(qq<(use 'lingy.devel) (PPP (quote $eval\n))>);
69 } elsif ($xxx) {
70 RT->rep(qq<(use 'lingy.devel) (XXX (quote $eval\n))>);
71 } else {
72 unshift @ARGV, '-';
73 map print("$_\n"),
74 grep $_ ne 'nil',
75 RT->rep($eval);
76 }
77 }
78 }
79  
80 sub do_repl {
81 RT->repl;
82 }
83  
84 sub do_run {
85 my ($self) = @_;
86 my $run = $self->{run};
87 if ($run ne '/dev/stdin') {
88 -f $run or err "No such file '$run'";
89 }
90 RT->rep(qq<(load-file "$run")>);
91 }
92  
93 sub from_stdin {
94 not -t STDIN or exists $ENV{LINGY_TEST_STDIN};
95 }
96  
97 sub getopt {
98 my ($self, @args) = @_;
99  
100 my $default = $self->default;
101  
102 if ($default and not(@args or $self->from_stdin)) {
103 @args = ($default);
104 }
105  
106 local @ARGV = @args;
107  
108 my $spec = {};
109 my $opts = $self->options;
110 for my $key (keys %$opts) {
111 (my $name = $key) =~ s/\|.*//;
112 my $type = $opts->{$key};
113 if ($type eq 'bool') {
114 $spec->{$key} = \$self->{$name};
115 }
116 elsif ($type eq 'str') {
117 $spec->{"$key=s"} = \$self->{$name};
118 }
119 elsif ($type eq 'arg') {
120 }
121 else {
122 err "Option type '$type' not supported";
123 }
124 }
125  
126 $spec->{help} = sub {
127 print $ENV{LINGY_USAGE};
128 exit 0;
129 };
130  
131 Getopt::Long::Configure(qw(
132 gnu_getopt
133 no_auto_abbrev
134 no_ignore_case
135 ));
136 eval {
137 GetOptions (%$spec) or
138 err "Error in command line arguments";
139 };
140 die "$@$ENV{LINGY_USAGE}" if $@;
141  
142 if (@ARGV) {
143 if ($self->{repl}) {
144 unshift @ARGV, 'NO_SOURCE_PATH';
145 } else {
146 $self->{run} = $ARGV[0];
147 $self->{run} = '/dev/stdin'
148 if $self->{run} eq '-';
149 }
150  
151 } else {
152 if ($self->from_stdin) {
153 $self->{run} = '/dev/stdin';
154 unshift @ARGV, '<stdin>';
155 } else {
156 unshift @ARGV, 'NO_SOURCE_PATH';
157 }
158 }
159  
160 $self->{args} = [@ARGV];
161 }
162  
163 1;
« lib/Lingy/Lang/ScalarClass.pm ↑ Summary script/_lingy.pl »