Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,57 @@
Lbl BF
r₁→P
r₂→I
L₁→D
Fill(D,768,0)
While {P}
{P}→C
If C='+'
{D}++
ElseIf C='-'
{D}--
ElseIf C='>'
D++
ElseIf C='<'
D--
ElseIf C='.'
Disp {D}▶Char
ElseIf C=','
{I}→{D}
I++
ElseIf C='['?{D}=0
NEXT(P)→P
ElseIf C=']'
PREV(P)→P
End
P++
End
Return
Lbl NEXT
r₁++
1→S
While S
If {r₁}='['
S++
ElseIf {r₁}=']'
S--
End
r₁++
End
r₁
Return
Lbl PREV
r₁--
1→S
While S
If {r₁}=']'
S++
ElseIf {r₁}='['
S--
End
r₁--
End
r₁
Return

View file

@ -0,0 +1,2 @@
"++++++++++++++++++++++++++++++++[>+>+<<-]>>+++++++++++++++++++++++++<<++++++++++[>>.-<.<-]"→Str1
BF(Str1,0)

View file

@ -0,0 +1,32 @@
import os
var
code = if paramCount() > 0: readFile paramStr 1
else: readAll stdin
tape = newSeq[char]()
d = 0
i = 0
proc run(skip = false): bool =
while d >= 0 and i < code.len:
if d >= tape.len: tape.add '\0'
if code[i] == '[':
inc i
let p = i
while run(tape[d] == '\0'): i = p
elif code[i] == ']':
return tape[d] != '\0'
elif not skip:
case code[i]
of '+': inc tape[d]
of '-': dec tape[d]
of '>': inc d
of '<': dec d
of '.': stdout.write tape[d]
of ',': tape[d] = stdin.readChar
else: discard
inc i
discard run()

View file

@ -0,0 +1,52 @@
procedure bfi(string pgm)
sequence jumptable = repeat(0,length(pgm)),
loopstack = {},
data = repeat(0,10) -- size??
integer skip = 0, ch, loopstart, pc, dp
--
-- compile (pack/strip comments and link jumps)
--
for i=1 to length(pgm) do
ch = pgm[i]
switch ch do
case '[': loopstack = append(loopstack,i-skip);
pgm[i-skip] = ch;
case ']': loopstart = loopstack[$];
loopstack = loopstack[1..-2];
jumptable[i-skip] = loopstart;
jumptable[loopstart] = i-skip;
fallthrough
case '+','-','<','>',',','.': pgm[i-skip] = ch;
default: skip += 1
end switch
end for
if length(loopstack) then ?9/0 end if
pgm = pgm[1..-1-skip]
--
-- main execution loop
--
pc = 1
dp = 1
while pc<=length(pgm) do
ch = pgm[pc]
switch ch do
case '>': dp += 1 if dp>length(data) then dp = 1 end if
case '<': dp -= 1 if dp<1 then dp = length(data) end if
case '+': data[dp] += 1
case '-': data[dp] -= 1
case ',': data[dp] = getc(0)
case '.': puts(1,data[dp])
case '[': if data[dp]=0 then pc = jumptable[pc] end if
case ']': if data[dp]!=0 then pc = jumptable[pc] end if
default: ?9/0
end switch
pc += 1
end while
end procedure
constant bf="++++++++[>++++[>++>++++>+++>+<<<<-]>++>->+>>+[<]<-]>>.>>.+.<.>>.<<<++.>---------.>------.<----.++++++++.>>+.>++.+++."
constant fb="++++++++[>++++[>++>++++>+++>+<<<<-]>++>->+>>+[<]<-]>>.>>.+.<.>>.<<<+++.>---.>------.++++++++.<--.>>+.>++.+++.,"
bfi(bf)
bfi(fb)

View file

@ -0,0 +1,2 @@
>++++++++[-<+++++++++>]<.>[][<-]>+>-[+]++>++>+++[>[->+++<<+++>]<<]>-----.
>->+++..+++.>-.<<+[>[+>+]>>]<--------------.>>.+++.------.--------.>+.>+.

View file

@ -0,0 +1,20 @@
# Where `code` is a string.
bf = (code) :
tape = (0)
tape_pos = 0
brackets = ()
i = -1
while (++i < code length) :
if (code(i) == ">"): if (++tape_pos == tape length): tape append(0)..
elsif (code(i) == "<"): tape_pos--.
elsif (code(i) == "+"): tape(tape_pos) = tape(tape_pos) + 1.
elsif (code(i) == "-"): tape(tape_pos) = tape(tape_pos) - 1.
elsif (code(i) == "."): tape(tape_pos) chr print.
elsif (code(i) == ","): tape(tape_pos) = read at(0) ord.
elsif (code(i) == "["): brackets push(i).
elsif (code(i) == "]") :
if (tape(tape_pos) == 0): brackets pop.
else: i = brackets(-1).
.
.
.

View file

@ -0,0 +1,48 @@
define tape_length = 50_000;
define eof_val = -1;
define unbalanced_exit_code = 1;
var cmd = 0;
var cell = 0;
var code = [];
var loops = [];
var tape = tape_length.of(0);
func get_input {
static input_buffer = [];
input_buffer.len || (input_buffer = ((STDIN.readline \\ return eof_val).chomp.chars.map{.ord}));
input_buffer.shift \\ eof_val;
}
func jump {
var depth = 0;
while (depth >= 0) {
++cmd < code.len || Sys.exit(unbalanced_exit_code);
if (code[cmd] == '[') {
++depth;
}
elsif (code[cmd] == ']') {
--depth;
}
}
}
var commands = Hash.new(
'>' => { ++cell },
'<' => { --cell },
'+' => { ++tape[cell] },
'-' => { --tape[cell] },
'.' => { tape[cell].chr.print },
',' => { tape[cell] = get_input() },
'[' => { tape[cell] ? loops.append(cmd) : jump() },
']' => { cmd = (loops.pop - 1) },
);
STDOUT.autoflush(1);
code = ARGF.slurp.chars.grep {|c| commands.exists(c)};
var code_len = code.len;
while (cmd < code_len) {
commands{code[cmd]}.run;
cmd++;
}

View file

@ -0,0 +1,129 @@
import Foundation
let valids = [">", "<", "+", "-", ".", ",", "[", "]"] as Set<Character>
var ip = 0
var dp = 0
var data = [UInt8](count: 30_000, repeatedValue: 0)
let input = Process.arguments
if input.count != 2 {
fatalError("Need one input file")
}
let infile: String!
do {
infile = try String(contentsOfFile: input[1], encoding: NSUTF8StringEncoding) ?? ""
} catch let err {
infile = ""
}
var program = ""
// remove invalid chars
for c in infile.characters {
if valids.contains(c) {
program += String(c)
}
}
let numChars = program.characters.count
if numChars == 0 {
fatalError("Error reading file")
}
func increaseInstructionPointer() {
ip += 1
}
func executeInstruction(ins: Character) {
switch ins {
case ">":
dp += 1
increaseInstructionPointer()
case "<":
dp -= 1
increaseInstructionPointer()
case "+":
data[dp] = data[dp] &+ 1
increaseInstructionPointer()
case "-":
data[dp] = data[dp] &- 1
increaseInstructionPointer()
case ".":
print(Character(UnicodeScalar(data[dp])), terminator: "")
increaseInstructionPointer()
case ",":
handleIn()
increaseInstructionPointer()
case "[":
handleOpenBracket()
case "]":
handleClosedBracket()
default:
fatalError("What")
}
}
func handleIn() {
let input = NSFileHandle.fileHandleWithStandardInput()
let bytes = input.availableData.bytes
let buf = unsafeBitCast(UnsafeBufferPointer(start: bytes, count: 1),
UnsafeBufferPointer<UInt8>.self)
data[dp] = buf[0]
}
func handleOpenBracket() {
if data[dp] == 0 {
var i = 1
while i > 0 {
ip += 1
let ins = program[program.startIndex.advancedBy(ip)]
if ins == "[" {
i += 1
} else if ins == "]" {
i -= 1
}
}
} else {
increaseInstructionPointer()
}
}
func handleClosedBracket() {
if data[dp] != 0 {
var i = 1
while i > 0 {
ip -= 1
let ins = program[program.startIndex.advancedBy(ip)]
if ins == "[" {
i -= 1
} else if ins == "]" {
i += 1
}
}
} else {
increaseInstructionPointer()
}
}
func tick() {
let ins = program[program.startIndex.advancedBy(ip)]
if valids.contains(ins) {
executeInstruction(ins)
} else {
increaseInstructionPointer()
}
}
while ip != numChars {
tick()
}