-
Notifications
You must be signed in to change notification settings - Fork 1
/
StringToINTERCAL.pl
83 lines (62 loc) · 1.6 KB
/
StringToINTERCAL.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/perl
#
# StringToINTERCAL.pl
#
# Converts a string to an INTERCAL script that will output said string.
# usage: perl StringToINTERCAL.pl your string here
#
# Author: Maxamilian Demian
#
# https://www.maxodev.org
# https://github.com/Maxoplata/StringToINTERCAL
use strict;
use warnings;
# package definition
{
package StringToINTERCAL;
sub new {
my ($class, $args) = @_;
my $self = bless {
politeCount => 0,
}, $class;
}
sub politeLine {
my ($self, $line) = @_;
if ($self->{politeCount} == 3) {
$self->{politeCount} = 0;
return "PLEASE ${line}\n";
}
$self->{politeCount}++;
return "DO ${line}\n";
}
sub convertToINTERCAL {
my ($self, $string) = @_;
# reset $politeCount
$self->{politeCount} = 0;
my $ret = $self->politeLine(",1 <- #" . length($string));
my $lastCharLoc = 256;
for (my $i = 0; $i < length($string); $i++) {
my $charLoc = oct("0b" . reverse(sprintf("%.8b", ord(substr($string, $i, 1)))));
my $movePosition = 0;
if ($charLoc < $lastCharLoc) {
$movePosition = ($lastCharLoc - $charLoc);
} elsif ($charLoc > $lastCharLoc) {
$movePosition = (256 - $charLoc) + $lastCharLoc;
}
$lastCharLoc -= $movePosition;
if ($lastCharLoc < 1) {
$lastCharLoc = 256 + $lastCharLoc;
}
$ret .= $self->politeLine(",1 SUB #" . ($i + 1) . " <- #${movePosition}");
}
$ret .= $self->politeLine("READ OUT ,1");
$ret .= $self->politeLine("GIVE UP");
return $ret;
}
}
# 1337 codez
if ($#ARGV >= 0) {
my $inputString = join(" ", @ARGV);
my $myINTERCAL = StringToINTERCAL->new;
print $myINTERCAL->convertToINTERCAL($inputString);
}