DOT./Implementations
Jump to navigation
Jump to search
DOT. was planned to be implemented in lots of different languages, both practical and esoteric, but for now there are 10 of them.
A
Ada
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
procedure Main is
Acc : Integer := 0;
Code : String (1 .. 100);
Last : Natural;
begin
Put (">");
Get_Line (Code, Last);
for I in 1 .. Last loop
declare
Cmd : Character := Code (I);
begin
if Cmd = 'D' then
Acc := Acc - 1;
elsif Cmd = 'T' then
Acc := Acc + 1;
elsif Cmd = 'O' then
Put (Acc, Width => 0);
elsif Cmd = '.' then
Put (Code (1 .. Last));
end if;
end;
end loop;
end Main;
D
Delphi
program Main;
{$APPTYPE CONSOLE}
uses
SysUtils;
var
acc: Integer;
code: string;
i: Integer;
cmd: Char;
begin
acc := 0;
Write('>');
ReadLn(code);
for i := 1 to Length(code) do
begin
cmd := code[i];
if cmd = 'D' then
Dec(acc)
else if cmd = 'T' then
Inc(acc)
else if cmd = 'O' then
Write(acc)
else if cmd = '.' then
Write(code);
end;
end.
J
Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int acc = 0;
Scanner scanner = new Scanner(System.in);
System.out.print(">");
String code = scanner.nextLine();
for (int i = 0; i < code.length(); i++) {
char cmd = code.charAt(i);
if (cmd == 'D') {
acc -= 1;
} else if (cmd == 'T') {
acc += 1;
} else if (cmd == 'O') {
System.out.print(acc);
} else if (cmd == '.') {
System.out.print(code);
}
}
}
}
JavaScript
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let acc = 0;
rl.question('>', (code) => {
for (let i = 0; i < code.length; i++) {
const cmd = code[i];
if (cmd === "D") {
acc -= 1;
} else if (cmd === "T") {
acc += 1;
} else if (cmd === "O") {
process.stdout.write(String(acc));
} else if (cmd === ".") {
process.stdout.write(code);
}
}
rl.close();
});
H
Haskell
import System.IO (hFlush, stdout)
main :: IO ()
main = do
putStr ">"
hFlush stdout
code <- getLine
loop code 0
where
loop [] _ = return ()
loop (c:cs) acc
| c == 'D' = loop cs (acc - 1)
| c == 'T' = loop cs (acc + 1)
| c == 'O' = do
putStr (show acc)
loop cs acc
| c == '.' = do
putStr code
loop cs acc
| otherwise = loop cs acc
L
Lisp
(defparameter acc 0)
(format t "> ")
(defparameter code (read-line))
(loop for i from 0 below (length code) do
(let ((cmd (aref code i)))
(cond
((char= cmd #\D)
(decf acc))
((char= cmd #\T)
(incf acc))
((char= cmd #\O)
(write-char (code-char (+ acc 48)))) ; преобразуем число в символ
((char= cmd #\.)
(format t "~a" code)))))
Lua
acc = 0
io.write("> ")
code = io.read()
for i = 1, #code do
local cmd = code:sub(i, i)
if cmd == "D" then
acc = acc - 1
elseif cmd == "T" then
acc = acc + 1
elseif cmd == "O" then
io.write(acc)
elseif cmd == "." then
io.write(code)
end
end
P
Pascal
program Main;
uses
crt;
var
acc: integer;
code: string;
i: integer;
cmd: char;
begin
acc := 0;
write('>');
readln(code);
for i := 1 to length(code) do
begin
cmd := code[i];
if cmd = 'D' then
dec(acc)
else if cmd = 'T' then
inc(acc)
else if cmd = 'O' then
write(acc)
else if cmd = '.' then
write(code);
end;
end.
PowerShell
$acc = 0
$code = Read-Host ">"
foreach ($cmd in $code.ToCharArray()) {
if ($cmd -eq 'D') {
$acc -= 1
}
elseif ($cmd -eq 'T') {
$acc += 1
}
elseif ($cmd -eq 'O') {
Write-Host -NoNewline $acc
}
elseif ($cmd -eq '.') {
Write-Host -NoNewline $code
}
}
python
acc = 0
code = input(">")
for cmd in code:
if cmd == "D":
acc -= 1
elif cmd == "T":
acc += 1
elif cmd == "O":
print(acc, end='')
elif cmd == ".":
print(code, end='')