Forums - Open Redstone Engineers
I Dare you to fix this - Printable Version

+- Forums - Open Redstone Engineers (https://forum.openredstone.org)
+-- Forum: Off-Topic (https://forum.openredstone.org/forum-4.html)
+--- Forum: Programming (https://forum.openredstone.org/forum-8.html)
+--- Thread: I Dare you to fix this (/thread-9684.html)



I Dare you to fix this - TheCrimulo - 03-30-2016

https://gist.github.com/anonymous/b9d2fbd692cc1be814a28f0288a22a24
bfsint.py:
Code:
#!/usr/bin/python3
# Python interpreter for Brainfuck# (bf#) by TheCrimulo
# bf# by TheCrimulo
# Open domain, feel free to use
#
# paukkupalika: brainkek
# me: yustin b-evr
import sys

# Get filename, open it and convert it to a plain string.
if len(sys.argv) == 1:
   with open('bfsint.py', 'r+') as f:
       for l in f.readlines():
           print(l)
       quit()
filename = sys.argv[1]
with open(filename, 'r') as f:
   evr = f.read().replace('\n', '')

# Tape and pointers
tape = list(range(256))
for i in tape:
   tape[i] = 0;
pointer_pos = 0
cycle = 0

# Extra vars
assign = []
stack = list(range(1))
for i in stack:
   stack[i] = 0;
depthl = 0
depthlsave = 0
# Iterate through each character and interpret the actions
#for i in range(0, len(evr)):
i = 0
while i < len(evr):
   cval = tape[pointer_pos]
   if evr[i] == ">":
       pointer_pos += 1
   elif evr[i] == "<":
       pointer_pos -= 1
   elif evr[i] == "+":
       tape[pointer_pos] += 1
   elif evr[i] == "-":
       tape[pointer_pos] -= 1
   elif evr[i] == ".":
       print('#i' + str(pointer_pos) + ': ' + str(cval))
   elif evr[i] == "@":
       asciival = str(chr(tape[pointer_pos]))
       print('#a' + str(pointer_pos) + ': ' + asciival)
   elif evr[i] == "$":
       f = i + 1
       while evr[f] != "*":
           assign.append(evr[f])
           f += 1
       assl = ''.join(assign)
       assld = int(assl, 16)
       tape[pointer_pos] = assld
   # $2A*[>++[>++<-]<-]
   elif evr[i] == "[":
       depthl += 1
       depthlsave = depthl
       if cval == 0:
           while (evr[i] != "]") or (depthl != depthlsave):
               i += 1
               if evr[i] == "[":
                   depthl += 1
               if evr[i] == "]":
                   depthl -= 1
           continue
       else:
           stack.append(i)
   elif evr[i] == "]":
       if cval == 0:
           if len(stack) == 1:
               del stack[0]
               continue
           else:
               lenstack = len(stack) - 1
               del stack[lenstack]
               continue
       else:
           i = stack[len(stack) - 1]
i += 1

test.bfs:
Code:
$2*.[>+++[>++<-]<-]

This is a bf-based language.

Bash:
Code:
thecrimulo@ubuntu:~/Escritorio/subatom/bf#$ python3 bfsint.py hworld.bfs
#i0: 2
Traceback (most recent call last):
 File "bfsint.py", line 81, in <module>
   del stack[lenstack]
IndexError: list assignment index out of range

Meny ppl tried to fix it, no one could


RE: I Dare you to fix this - PabloDons - 03-30-2016

I would like to comment on one thing:
Code:
tape = list(range(256))
for i in tape:
  tape[i] = 0;
the level of inefficiency in this is staggering for a few reasons:
  • There are 2 loops
  • Creating an array from an iterator, then resetting all the values in it
  • 3 lines
  • What is this semicolon even doing there? This isn't java
you could achieve the exact same result with 0 loops, about 100x the speed, and 1 line:
Code:
tape = [0] * 256

also about the while loop: I see the value "i" being incremented multiple places, even set to a different value. And at the end of the code, there is a stray "i += 1" outside the while loop that I assume it is supposed to be within. Otherwise it doesn't make any sense.

PROTIP: If there is even a slight chance there might be an error with values, always throw in random "print"'s everywhere. You can never have too many

EDIT: Also, why this?:
Code:
if len(sys.argv) == 1:
   with open('bfsint.py', 'r+') as f:
       for l in f.readlines():
           print(l)
       quit()
It seems to me like you are print out your code if no arguments where passed to the script
btw you can simplify the readlines() loop. The file already contains newlines so all you literally need to do is print(f.read())

EDIT: Okay seriously, to answer your actual call for help (please don't call it a dare, that's just desperate), Your code is trying to delete stack[-1] which obviously doesn't exist. replace the deletion of stack with this code:
Code:
if cval == 0:
            if len(stack) >= 1:
                del stack[len(stack)-1]
                continue

The other error that is raised has to do with the way you save the stack. It doesn't work. You need to figure out a different way of remembering each corresponding square parentheses. Last known index (as far as I can tell from the code) cannot possibly be a reasonable way of determining the correct corresponding one.


RE: I Dare you to fix this - Apuly - 03-30-2016

1: Why are you fixing the length of a list? If you want to use that, use an array.
2: pointer_pos is a terrible name for the index, because 1: it's an index, 2: python doesn't have pointers, and 3: pointer_pos sounds like a pointer pointer to me.
3:

Code:
asciival = str(chr(tape[index]))
Why are you getting the string of a string?
4:
Code:
f = i + 1
      while evr[f] != "*":
          assign.append(evr[f])
          f += 1
      assl = ''.join(assign)
Can be shortened to
Code:
      assign = evr[i+1: evr.find("*", beg=i)]