Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
68
Task/Subleq/Ada/subleq.adb
Normal file
68
Task/Subleq/Ada/subleq.adb
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
with Ada.Text_IO;
|
||||
|
||||
procedure Subleq is
|
||||
|
||||
Storage_Size: constant Positive := 2**8; -- increase or decrease memory
|
||||
Steps: Natural := 999; -- "emergency exit" to stop endless loops
|
||||
|
||||
subtype Address is Integer range -1 .. (Storage_Size-1);
|
||||
subtype Memory_Location is Address range 0 .. Address'Last;
|
||||
|
||||
type Storage is array(Memory_Location) of Integer;
|
||||
|
||||
package TIO renames Ada.Text_IO;
|
||||
package IIO is new TIO.Integer_IO(Integer);
|
||||
|
||||
procedure Read_Program(Mem: out Storage) is
|
||||
Idx: Memory_Location := 0;
|
||||
begin
|
||||
while not TIO.End_Of_Line loop
|
||||
IIO.Get(Mem(Idx));
|
||||
Idx := Idx + 1;
|
||||
end loop;
|
||||
exception
|
||||
when others => TIO.Put_Line("Reading program: Something went wrong!");
|
||||
end Read_Program;
|
||||
|
||||
procedure Execute_Program(Mem: in out Storage) is
|
||||
PC: Integer := 0; -- program counter
|
||||
function Source return Integer is (Mem(PC));
|
||||
function Dest return Integer is (Mem(PC+1));
|
||||
function Branch return Integer is (Mem(PC+2));
|
||||
function Next return Integer is (PC+3);
|
||||
begin
|
||||
while PC >= 0 and Steps >= 0 loop
|
||||
Steps := Steps -1;
|
||||
if Source = -1 then -- read input
|
||||
declare
|
||||
Char: Character;
|
||||
begin
|
||||
TIO.Get (Char);
|
||||
Mem(Dest) := Character'Pos (Char);
|
||||
end;
|
||||
PC := Next;
|
||||
elsif Dest = -1 then -- write output
|
||||
TIO.Put(Character'Val(Mem(Source)));
|
||||
PC := Next;
|
||||
else -- subtract and branch if less or equal
|
||||
Mem(Dest) := Mem(Dest) - Mem(Source);
|
||||
if Mem(Dest) <= 0 then
|
||||
PC := Branch;
|
||||
else
|
||||
PC := Next;
|
||||
end if;
|
||||
end if;
|
||||
end loop;
|
||||
TIO.Put_Line(if PC >= 0 then "Emergency exit: program stopped!" else "");
|
||||
exception
|
||||
when others => TIO.Put_Line("Failure when executing Program");
|
||||
end Execute_Program;
|
||||
|
||||
Memory: Storage := (others => 0); -- no initial "junk" in memory!
|
||||
|
||||
begin
|
||||
|
||||
Read_Program(Memory);
|
||||
Execute_Program(Memory);
|
||||
|
||||
end Subleq;
|
||||
81
Task/Subleq/COBOL/subleq.cob
Normal file
81
Task/Subleq/COBOL/subleq.cob
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
identification division.
|
||||
program-id. subleq-program.
|
||||
data division.
|
||||
working-storage section.
|
||||
01 subleq-source-code.
|
||||
05 source-string pic x(2000).
|
||||
01 subleq-virtual-machine.
|
||||
05 memory-table.
|
||||
10 memory pic s9999
|
||||
occurs 500 times.
|
||||
05 a pic s9999.
|
||||
05 b pic s9999.
|
||||
05 c pic s9999.
|
||||
05 instruction-pointer pic s9999.
|
||||
05 input-output-character pic x.
|
||||
01 working-variables.
|
||||
05 loop-counter pic 9999.
|
||||
05 instruction-counter pic 9999.
|
||||
05 string-pointer pic 9999.
|
||||
05 adjusted-index-a pic 9999.
|
||||
05 adjusted-index-b pic 9999.
|
||||
05 output-character-code pic 9999.
|
||||
procedure division.
|
||||
read-source-paragraph.
|
||||
accept source-string from console.
|
||||
display 'READING SUBLEQ PROGRAM... ' with no advancing.
|
||||
move 1 to string-pointer.
|
||||
move 0 to instruction-counter.
|
||||
perform split-source-paragraph varying loop-counter from 1 by 1
|
||||
until loop-counter is greater than 500
|
||||
or string-pointer is greater than 2000.
|
||||
display instruction-counter with no advancing.
|
||||
display ' WORDS READ.'.
|
||||
execute-paragraph.
|
||||
move 1 to instruction-pointer.
|
||||
move 0 to instruction-counter.
|
||||
display 'BEGINNING RUN... '.
|
||||
display ''.
|
||||
perform execute-instruction-paragraph
|
||||
until instruction-pointer is negative.
|
||||
display ''.
|
||||
display 'HALTED AFTER ' instruction-counter ' INSTRUCTIONS.'.
|
||||
stop run.
|
||||
execute-instruction-paragraph.
|
||||
add 1 to instruction-counter.
|
||||
move memory(instruction-pointer) to a.
|
||||
add 1 to instruction-pointer.
|
||||
move memory(instruction-pointer) to b.
|
||||
add 1 to instruction-pointer.
|
||||
move memory(instruction-pointer) to c.
|
||||
add 1 to instruction-pointer.
|
||||
if a is equal to -1 then perform input-paragraph.
|
||||
if b is equal to -1 then perform output-paragraph.
|
||||
if a is not equal to -1 and b is not equal to -1
|
||||
then perform subtraction-paragraph.
|
||||
split-source-paragraph.
|
||||
unstring source-string delimited by all spaces
|
||||
into memory(loop-counter)
|
||||
with pointer string-pointer.
|
||||
add 1 to instruction-counter.
|
||||
input-paragraph.
|
||||
display '> ' with no advancing.
|
||||
accept input-output-character from console.
|
||||
add 1 to b giving adjusted-index-b.
|
||||
move function ord(input-output-character)
|
||||
to memory(adjusted-index-b).
|
||||
subtract 1 from memory(adjusted-index-b).
|
||||
output-paragraph.
|
||||
add 1 to a giving adjusted-index-a.
|
||||
add 1 to memory(adjusted-index-a) giving output-character-code.
|
||||
move function char(output-character-code)
|
||||
to input-output-character.
|
||||
display input-output-character with no advancing.
|
||||
subtraction-paragraph.
|
||||
add 1 to c.
|
||||
add 1 to a giving adjusted-index-a.
|
||||
add 1 to b giving adjusted-index-b.
|
||||
subtract memory(adjusted-index-a) from memory(adjusted-index-b).
|
||||
if memory(adjusted-index-b) is equal to zero
|
||||
or memory(adjusted-index-b) is negative
|
||||
then move c to instruction-pointer.
|
||||
|
|
@ -15,17 +15,17 @@ func inp .
|
|||
.
|
||||
proc subleq &mem[] .
|
||||
repeat
|
||||
a = mem[p]
|
||||
b = mem[p + 1]
|
||||
c = mem[p + 2]
|
||||
a = mem[p + 1]
|
||||
b = mem[p + 2]
|
||||
c = mem[p + 3]
|
||||
p += 3
|
||||
if a = -1
|
||||
mem[b] = inp
|
||||
mem[b + 1] = inp
|
||||
elif b = -1
|
||||
write strchar mem[a]
|
||||
write strchar mem[a + 1]
|
||||
else
|
||||
mem[b] -= mem[a]
|
||||
if mem[b] <= 0
|
||||
mem[b + 1] -= mem[a + 1]
|
||||
if mem[b + 1] <= 0
|
||||
p = c
|
||||
.
|
||||
.
|
||||
|
|
@ -33,8 +33,6 @@ proc subleq &mem[] .
|
|||
.
|
||||
.
|
||||
prog[] = [ 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 ]
|
||||
arrbase prog[] 0
|
||||
#
|
||||
subleq prog[]
|
||||
#
|
||||
input_data
|
||||
|
|
|
|||
43
Task/Subleq/JavaScript/subleq.js
Normal file
43
Task/Subleq/JavaScript/subleq.js
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
class Subleq {
|
||||
static main() {
|
||||
const 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
|
||||
];
|
||||
const readline = require('readline').createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout
|
||||
});
|
||||
|
||||
let instructionPointer = 0;
|
||||
const loop = () => {
|
||||
const a = mem[instructionPointer];
|
||||
const b = mem[instructionPointer + 1];
|
||||
|
||||
if (a === -1) {
|
||||
readline.question('', (input) => {
|
||||
mem[b] = parseInt(input, 10);
|
||||
instructionPointer += 3;
|
||||
loop();
|
||||
});
|
||||
} else if (b === -1) {
|
||||
process.stdout.write(String.fromCharCode(mem[a]));
|
||||
instructionPointer += 3;
|
||||
loop();
|
||||
} else {
|
||||
mem[b] -= mem[a];
|
||||
if (mem[b] < 1) {
|
||||
instructionPointer = mem[instructionPointer + 2];
|
||||
} else {
|
||||
instructionPointer += 3;
|
||||
}
|
||||
loop();
|
||||
}
|
||||
};
|
||||
loop();
|
||||
}
|
||||
}
|
||||
|
||||
// Run the main function
|
||||
Subleq.main();
|
||||
26
Task/Subleq/Pluto/subleq.pluto
Normal file
26
Task/Subleq/Pluto/subleq.pluto
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
require "io2"
|
||||
|
||||
local function subleq(program)
|
||||
local words = program:split(" "):map(|w| -> tonumber(w))
|
||||
local sb = ""
|
||||
local ip = 0
|
||||
while true do
|
||||
local a = words[ip + 1]
|
||||
local b = words[ip + 2]
|
||||
local c = words[ip + 3]
|
||||
ip += 3
|
||||
if a < 0 then
|
||||
words[b + 1] = tonumber(io.readStr("Enter a character : ", 1, 1))
|
||||
elseif b < 0 then
|
||||
sb ..= string.char(words[a + 1])
|
||||
else
|
||||
words[b + 1] -= words[a + 1]
|
||||
if words[b + 1] <= 0 then ip = c end
|
||||
if ip < 0 then break end
|
||||
end
|
||||
end
|
||||
io.write(sb)
|
||||
end
|
||||
|
||||
local program = "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"
|
||||
subleq(program)
|
||||
37
Task/Subleq/PowerShell/subleq-1.ps1
Normal file
37
Task/Subleq/PowerShell/subleq-1.ps1
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
function Invoke-Subleq ([int[]]$Program)
|
||||
{
|
||||
[int]$ip, [string]$output = $null
|
||||
|
||||
try
|
||||
{
|
||||
while ($ip -ge 0)
|
||||
{
|
||||
if ($Program[$ip] -eq -1)
|
||||
{
|
||||
$Program[$Program[$ip + 1]] = [int](Read-Host -Prompt SUBLEQ)[0]
|
||||
}
|
||||
elseif ($Program[$ip + 1] -eq -1)
|
||||
{
|
||||
$output += "$([char]$Program[$Program[$ip]])"
|
||||
}
|
||||
else
|
||||
{
|
||||
$Program[$Program[$ip + 1]] -= $Program[$Program[$ip]]
|
||||
|
||||
if ($Program[$Program[$ip + 1]] -le 0)
|
||||
{
|
||||
$ip = $Program[$ip + 2]
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
$ip += 3
|
||||
}
|
||||
|
||||
return $output
|
||||
}
|
||||
catch [IndexOutOfRangeException],[Exception]
|
||||
{
|
||||
Write-Host "$($Error[0].Exception.Message)" -ForegroundColor Red
|
||||
}
|
||||
}
|
||||
1
Task/Subleq/PowerShell/subleq-2.ps1
Normal file
1
Task/Subleq/PowerShell/subleq-2.ps1
Normal file
|
|
@ -0,0 +1 @@
|
|||
Invoke-Subleq -Program 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue