Subleq: Difference between revisions

Content added Content deleted
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 246: Line 246:
Hello, world!
Hello, world!
</pre>
</pre>

=={{header|AWK}}==
=={{header|AWK}}==
<lang AWK>
<lang AWK>
Line 301: Line 302:
</pre>
</pre>


=={{Header|BBC BASIC}}==
=={{header|BBC BASIC}}==
The BBC BASIC implementation reads the machine code program as a string from standard input and stores it in an array of signed 32-bit integers. The default size of the array is 256, but other values could easily be substituted. No attempt is made to handle errors arising from invalid Subleq programs.
The BBC BASIC implementation reads the machine code program as a string from standard input and stores it in an array of signed 32-bit integers. The default size of the array is 256, but other values could easily be substituted. No attempt is made to handle errors arising from invalid Subleq programs.
<lang bbcbasic>REM >subleq
<lang bbcbasic>REM >subleq
Line 421: Line 422:
Hello, world!
Hello, world!
</pre>
</pre>

=={{header|C sharp|C#}}==
{{trans|Java}}
<lang csharp>using System;

namespace Subleq {
class Program {
static void Main(string[] args) {
int[] mem = {
15, 17, -1, 17, -1, -1, 16, 1, -1, 16,
3, -1, 15, 15, 0, 0, -1, 72, 101, 108,
108, 111, 44, 32, 119, 111, 114, 108, 100, 33,
10, 0,
};

int instructionPointer = 0;

do {
int a = mem[instructionPointer];
int b = mem[instructionPointer + 1];

if (a == -1) {
mem[b] = Console.Read();
}
else if (b == -1) {
Console.Write((char)mem[a]);
}
else {
mem[b] -= mem[a];
if (mem[b] < 1) {
instructionPointer = mem[instructionPointer + 2];
continue;
}
}

instructionPointer += 3;
} while (instructionPointer >= 0);
}
}
}</lang>
{{out}}
<pre>Hello, world!</pre>


=={{header|C++}}==
=={{header|C++}}==
Line 477: Line 520:
Hello, world!
Hello, world!
</pre>
</pre>

=={{header|C#|C sharp}}==
{{trans|Java}}
<lang csharp>using System;

namespace Subleq {
class Program {
static void Main(string[] args) {
int[] mem = {
15, 17, -1, 17, -1, -1, 16, 1, -1, 16,
3, -1, 15, 15, 0, 0, -1, 72, 101, 108,
108, 111, 44, 32, 119, 111, 114, 108, 100, 33,
10, 0,
};

int instructionPointer = 0;

do {
int a = mem[instructionPointer];
int b = mem[instructionPointer + 1];

if (a == -1) {
mem[b] = Console.Read();
}
else if (b == -1) {
Console.Write((char)mem[a]);
}
else {
mem[b] -= mem[a];
if (mem[b] < 1) {
instructionPointer = mem[instructionPointer + 2];
continue;
}
}

instructionPointer += 3;
} while (instructionPointer >= 0);
}
}
}</lang>
{{out}}
<pre>Hello, world!</pre>


=={{header|COBOL}}==
=={{header|COBOL}}==
Line 1,239: Line 1,240:
Hello, world!
Hello, world!
</pre>
</pre>



=={{header|Oforth}}==
=={{header|Oforth}}==
Line 1,445: Line 1,445:
}</lang>
}</lang>
{{Output}}<pre>Hello, world!</pre>
{{Output}}<pre>Hello, world!</pre>

=={{header|Perl 6}}==
{{trans|Perl}}
<lang perl6>my @hello-world = <15 17 -1 17 -1 -1 16 1 -1 16 3 -1 15 15 0 0 -1 72 101 108 108 111 44 32 119 111 114 108 100 33 10 0>;

my @memory = @hello-world;
my $ip = 0;
while $ip >= 0 && $ip < @memory {
my ($a, $b, $c) = @memory[$ip, $ip+1, $ip+2];
$ip += 3;
if $a < 0 {
@memory[$b] = getc.ord;
} elsif $b < 0 {
print @memory[$a].chr;
} else {
if (@memory[$b] -= @memory[$a]) <= 0 {
$ip = $c;
}
}
}</lang>

{{out}}
<pre>Hello, world!</pre>


=={{header|Phix}}==
=={{header|Phix}}==
Line 1,652: Line 1,629:


(subleq Hello)</lang>
(subleq Hello)</lang>
{{out}}
<pre>Hello, world!</pre>

=={{header|Raku}}==
(formerly Perl 6)
{{trans|Perl}}
<lang perl6>my @hello-world = <15 17 -1 17 -1 -1 16 1 -1 16 3 -1 15 15 0 0 -1 72 101 108 108 111 44 32 119 111 114 108 100 33 10 0>;

my @memory = @hello-world;
my $ip = 0;
while $ip >= 0 && $ip < @memory {
my ($a, $b, $c) = @memory[$ip, $ip+1, $ip+2];
$ip += 3;
if $a < 0 {
@memory[$b] = getc.ord;
} elsif $b < 0 {
print @memory[$a].chr;
} else {
if (@memory[$b] -= @memory[$a]) <= 0 {
$ip = $c;
}
}
}</lang>

{{out}}
{{out}}
<pre>Hello, world!</pre>
<pre>Hello, world!</pre>
Line 1,760: Line 1,761:
}</lang>
}</lang>
{{Out}}See it running in your browser by [https://scastie.scala-lang.org/f4MszRqZR5qtxI6YwarJhw Scastie (JVM)].
{{Out}}See it running in your browser by [https://scastie.scala-lang.org/f4MszRqZR5qtxI6YwarJhw Scastie (JVM)].

=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Perl 6}}
{{trans|Perl 6}}