September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,63 @@
// version 1.1.2
// requires 5 chars (10 bytes) of data store
const val hw = """
/++++!/===========?\>++.>+.+++++++..+++\
\+++\ | /+>+++++++>/ /++++++++++<<.++>./
$+++/ | \+++++++++>\ \+++++.>.+++.-----\
\==-<<<<+>+++/ /=.>.+>.--------.-/"""
// input is a multi-line string.
fun snusp(dlen: Int, raw: String) {
val ds = CharArray(dlen) // data store
var dp = 0 // data pointer
var s = raw
// remove leading '\n' from string if present
s = s.trimStart('\n')
// make 2 dimensional instruction store and declare instruction pointers
val cs = s.split('\n')
var ipr = 0
var ipc = 0
// look for starting instruction
findStart@ for ((r, row) in cs.withIndex()) {
for ((i, c) in row.withIndex()) {
if (c == '$') {
ipr = r
ipc = i
break@findStart
}
}
}
var id = 0
val step = fun() {
if (id and 1 == 0)
ipc += 1 - (id and 2)
else
ipr += 1 - (id and 2)
}
// execute
while ((ipr in 0 until cs.size) && (ipc in 0 until cs[ipr].length)) {
when (cs[ipr][ipc]) {
'>' -> dp++
'<' -> dp--
'+' -> ds[dp]++
'-' -> ds[dp]--
'.' -> print(ds[dp])
',' -> ds[dp] = readLine()!![0]
'/' -> id = id.inv()
'\\' -> id = id xor 1
'!' -> step()
'?' -> if (ds[dp] == '\u0000') step()
}
step()
}
}
fun main(args: Array<String>) {
snusp(5, hw)
}

View file

@ -0,0 +1,71 @@
class SNUSP {
has @!inst-pointer;
has @!call-stack;
has @!direction;
has @!memory;
has $!mem-pointer;
method run ($code) {
init();
my @code = pad( |$code.lines );
for @code.kv -> $r, @l {
my $index = @l.grep( /'$'/, :k );
if $index {
@!inst-pointer = $r, $index;
last
}
}
loop {
my $instruction = @code[@!inst-pointer[0]; @!inst-pointer[1]];
given $instruction {
when '>' { $!mem-pointer++ }
when '<' { $!mem-pointer-- }
when '+' { @!memory[$!mem-pointer]++ }
when '-' { @!memory[$!mem-pointer]-- }
when '.' { print @!memory[$!mem-pointer].chr }
when ',' { @!memory[$!mem-pointer] = $*IN.getc.ord }
when '/' { @!direction = @!direction.reverse «*» -1 }
when '\\' { @!direction = @!direction.reverse }
when '!' { nexti() }
when '?' { nexti() unless @!memory[$!mem-pointer] }
when '@' { @!call-stack.push: @!inst-pointer.Array }
when '#' {
last unless +@!call-stack;
@!inst-pointer = |@!call-stack.pop;
nexti();
}
}
nexti();
last if @!inst-pointer[0] > +@code or
@!inst-pointer[1] > +@code[0];
}
sub init () {
@!inst-pointer = 0, 0;
@!direction = 0, 1;
$!mem-pointer = 0;
@!memory = ()
}
sub nexti () { @!inst-pointer Z+= @!direction }
sub pad ( *@lines ) {
my $max = max @lines».chars;
my @pad = @lines.map: $max - *.chars;
map -> $i { flat @lines[$i].comb, ' ' xx @pad[$i] }, ^@lines;
}
}
}
# TESTING
my $hw = q:to/END/;
/++++!/===========?\>++.>+.+++++++..+++\
\+++\ | /+>+++++++>/ /++++++++++<<.++>./
$+++/ | \+++++++++>\ \+++++.>.+++.-----\
\==-<<<<+>+++/ /=.>.+>.--------.-/
END
my $snusp = SNUSP.new;
$snusp.run($hw)

View file

@ -0,0 +1,60 @@
integer id = 0, ipr = 1, ipc = 1
procedure step()
if and_bits(id,1) == 0 then
ipc += 1 - and_bits(id,2)
else
ipr += 1 - and_bits(id,2)
end if
end procedure
procedure snusp(integer dlen, string s)
sequence ds = repeat(0,dlen) -- data store
integer dp = 1 -- data pointer
-- remove leading '\n' from string if present
s = trim_head(s,'\n')
-- make 2 dimensional instruction store and set instruction pointers
sequence cs = split(s,'\n')
ipr = 1
ipc = 1
-- look for starting instruction
for i=1 to length(cs) do
ipc = find('$',cs[i])
if ipc then
ipr = i
exit
end if
end for
id = 0
-- execute
while ipr>=1 and ipr<=length(cs)
and ipc>=1 and ipc<=length(cs[ipr]) do
integer op = cs[ipr][ipc]
switch op do
case '>' : dp += 1
case '<' : dp -= 1
case '+' : ds[dp] += 1
case '-' : ds[dp] -= 1
case '.' : puts(1,ds[dp])
case ',' : ds[dp] = getc(0)
case '/' : id = not_bits(id)
case '\\': id = xor_bits(id,1)
case '!' : step()
case '?' : if ds[dp]=0 then step() end if
end switch
step()
end while
end procedure
constant hw = """
/++++!/===========?\>++.>+.+++++++..+++\
\+++\ | /+>+++++++>/ /++++++++++<<.++>./
$+++/ | \+++++++++>\ \+++++.>.+++.-----\
\==-<<<<+>+++/ /=.>.+>.--------.-/"""
snusp(5, hw)