brainfuck.py

Created by nicolas-patrois

Created on April 27, 2018

1.15 KB

Le langage minimaliste BrainFuck, version légèrement étendue.

usage : brainfuck(“;>;[<+>-]<:”,”10 12”)

commandes=”,.+-><[];:#!” < et > déplacent la tête de lecture + et - incrémentent et décrémentent la cellule . et , affichent et entrent un code ASCII [ et ] est la seule boucle si la cellule est nulle : et ; affichent et entrent un nombre ! affiche l’état de la bande # indique que la suite est ignorée


def brainfuck(q,entree=""):
 P=print
 sep="\n\t "
 e=[]
 for l in entree:
  e+=list(l)
 p=0
 dp=0
 c=0
 m=[0]

 while p<len(q):
  c+=1
  if c>=10**6:
   P("Limite max. Votre code semble tourner en rond.")
   break
  elif q[p]==">":
   dp+=1
   if dp==len(m):
    m.append(0)
   p+=1
  elif q[p]=="<":
   dp-=1
   if dp==-1:
    m=[0]+m
    dp=0
   p+=1
  elif q[p]=="+":
   m[dp]+=1
   p+=1
  elif q[p]=="-":
   m[dp]-=1
   p+=1
  elif q[p]==".":
   P(chr(m[dp]),end="")
   p+=1
  elif q[p]==",":
   m[dp]=ord(e.pop(0))
   p+=1
  elif q[p]==":":
   print(m[dp],end=" ")
   p+=1
  elif q[p]==";":
   while e[0] in sep:
    e.pop(0)
   while e and e[0] not in sep:
    m[dp]*=10
    m[dp]+=int(e.pop(0))
   p+=1
  elif q[p]=="[":
   if m[dp]!=0:
    p+=1
    continue
   nbo=1
   while nbo!=0:
    p+=1
    if q[p]=="[":
     nbo+=1
    if q[p]=="]":
     nbo-=1
   p+=1
  elif q[p]=="]":
   if m[dp]==0:
    p+=1
    continue
   nbf=1
   while nbf!=0:
    p-=1
    if q[p]=="]":
     nbf+=1
    if q[p]=="[":
     nbf-=1
  elif q[p]=="!":
   for i in range(len(m)):
    if i==dp:
     P("["+str(m[i])+"]",end=" ")
    else:
     P(m[i],end=" ")
   P()
   p+=1

 P()

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.