2007-05-20 22:44:44 +02:00
|
|
|
#!/usr/bin/perl -w
|
|
|
|
|
2023-02-21 06:31:52 +01:00
|
|
|
# SPDX-FileCopyrightText: 2002-2023 Pragmatic Software <pragma78@gmail.com>
|
2021-07-11 00:00:22 +02:00
|
|
|
# SPDX-License-Identifier: MIT
|
2017-03-05 22:33:31 +01:00
|
|
|
|
2007-05-20 22:44:44 +02:00
|
|
|
# Quick and dirty by :pragma
|
|
|
|
|
2015-05-08 14:02:35 +02:00
|
|
|
use Math::Units qw(convert);
|
|
|
|
|
|
|
|
my ($arguments, $response, $invalid, @conversion);
|
2007-05-20 22:44:44 +02:00
|
|
|
|
2014-03-24 03:25:37 +01:00
|
|
|
my @valid_keywords = (
|
2020-02-15 23:38:32 +01:00
|
|
|
'sin', 'cos', 'tan', 'atan', 'exp', 'int', 'hex', 'oct', 'log', 'sqrt',
|
|
|
|
'floor', 'ceil', 'asin', 'acos', 'log10', 'sinh', 'cosh', 'tanh', 'abs',
|
|
|
|
'pi', 'deg2rad', 'rad2deg', 'atan2', 'cbrt'
|
2014-03-24 03:25:37 +01:00
|
|
|
);
|
2007-05-20 22:44:44 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
if ($#ARGV < 0) {
|
|
|
|
print "Dumbass.\n";
|
|
|
|
exit 0;
|
2007-05-20 22:44:44 +02:00
|
|
|
}
|
|
|
|
|
2014-03-24 03:25:37 +01:00
|
|
|
$arguments = join(' ', @ARGV);
|
2021-02-07 23:37:12 +01:00
|
|
|
|
|
|
|
my $raw = 0;
|
|
|
|
if ($arguments =~ s/^-raw\s+//) {
|
|
|
|
$raw = 1;
|
|
|
|
}
|
|
|
|
|
2014-08-13 18:55:04 +02:00
|
|
|
my $orig_arguments = $arguments;
|
|
|
|
|
2020-01-18 20:41:49 +01:00
|
|
|
$arguments =~ s/(the )*(ultimate )*answer.*question of life(,? the universe,? and everything)?\s?/42/gi;
|
|
|
|
$arguments =~ s/(the )*(ultimate )*meaning of (life|existence|everything)?/42/gi;
|
2021-02-07 23:37:12 +01:00
|
|
|
$arguments =~ s/baker'?s dozen/13/g;
|
2007-05-20 22:44:44 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
if ($arguments =~ s/(\d+\s?)([^ ]+)\s+to\s+([^ ]+)\s*$/$1/) { @conversion = ($2, $3); }
|
2015-05-08 14:02:35 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
if ($arguments =~ m/([\$`\|{}"'#@=?\[\]])/ or $arguments =~ m/(~~)/) { $invalid = $1; }
|
|
|
|
else {
|
|
|
|
while ($arguments =~ /([a-zA-Z0-9]+)/g) {
|
|
|
|
my $keyword = $1;
|
|
|
|
next if $keyword =~ m/^[0-9]+$/;
|
|
|
|
$invalid = $keyword and last if not grep { /^$keyword$/ } @valid_keywords;
|
|
|
|
}
|
2014-03-24 03:25:37 +01:00
|
|
|
}
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if ($invalid) {
|
2020-02-15 23:38:32 +01:00
|
|
|
print "Illegal symbol '$invalid' in equation\n";
|
|
|
|
exit 1;
|
2014-03-24 03:25:37 +01:00
|
|
|
}
|
|
|
|
|
2014-03-24 04:05:48 +01:00
|
|
|
$response = eval("use POSIX qw/ceil floor/; use Math::Trig; use Math::Complex;" . $arguments);
|
2014-03-24 03:25:37 +01:00
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if ($@) {
|
2020-02-15 23:38:32 +01:00
|
|
|
my $error = $@;
|
|
|
|
$error =~ s/[\n\r]+//g;
|
|
|
|
$error =~ s/ at \(eval \d+\) line \d+.//;
|
|
|
|
$error =~ s/ at EOF$//;
|
|
|
|
$error =~ s/Died at .*//;
|
|
|
|
print $error;
|
|
|
|
exit 1;
|
2007-05-20 22:44:44 +02:00
|
|
|
}
|
|
|
|
|
2015-05-08 14:02:35 +02:00
|
|
|
if (@conversion) {
|
2020-02-15 23:38:32 +01:00
|
|
|
my $result = eval { convert($response, $conversion[0], $conversion[1]); };
|
|
|
|
if ($@) {
|
|
|
|
print "Unknown conversion from $conversion[0] to $conversion[1]. Units are case-sensitive (Hz, not hz).\n";
|
|
|
|
exit 1;
|
|
|
|
}
|
|
|
|
$response = "$result $conversion[1]";
|
2015-05-08 14:02:35 +02:00
|
|
|
}
|
|
|
|
|
2021-02-07 23:37:12 +01:00
|
|
|
if ($raw) {
|
|
|
|
print "$response\n";
|
|
|
|
} else {
|
|
|
|
print "$orig_arguments = $response\n";
|
|
|
|
}
|