Advertisement
none-None1

Fish Code interpreter

Mar 12th, 2024
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.31 KB | None | 0 0
  1. """
  2. Fish Code language interpreter
  3. Language specification at https://esolangs.org/wiki/Fish_Code
  4. Use like this:
  5. code
  6. !
  7. input
  8. """
  9. import re,sys
  10. INC,DEC,INPC,OUTC,INP,OUT,PUSH,POP,SWAP,JZ,JNZ,JMP,SET,SUB=0,1,2,3,4,5,6,7,8,9,10,11,12,13
  11. stacks=[[],[]]
  12. curstack=0
  13. reg=0
  14. def parse(c):
  15.     """Parse a line of code"""
  16.     res=[]
  17.     while c:
  18.         if c[0] in '+-,.:;?!~':
  19.             res.append(('+-,.:;?!~'.index(c[0]),0))
  20.             c=c[1:]
  21.         elif re.match('\\(\\(\\(\\d*?\\)\\)\\)',c):
  22.             matched=re.match('\\(\\(\\(\\d*?\\)\\)\\)',c)
  23.             m=int(c[:matched.end()][3:-3])
  24.             c=c[matched.end():]
  25.             res.append((SUB,m))
  26.         elif re.match('\\(\\(\\d*?\\)\\)',c):
  27.             matched=re.match('\\(\\(\\d*?\\)\\)',c)
  28.             m=int(c[:matched.end()][2:-2])
  29.             c=c[matched.end():]
  30.             res.append((SET,m))
  31.         elif re.match('\\(\\d*?\\)',c):
  32.             matched=re.match('\\(\\d*?\\)',c)
  33.             m=int(c[:matched.end()][1:-1])
  34.             c=c[matched.end():]
  35.             res.append((JZ,m))
  36.         elif re.match('\\[\\d*?\\]',c):
  37.             matched=re.match('\\[\\d*?\\]',c)
  38.             m=int(c[:matched.end()][1:-1])
  39.             c=c[matched.end():]
  40.             res.append((JNZ,m))
  41.         elif re.match('\\{\\d*?\\}', c):
  42.             matched = re.match('\\{\\d*?\\}', c)
  43.             m = int(c[:matched.end()][1:-1])
  44.             c = c[matched.end():]
  45.             res.append((JMP, m))
  46.         else:
  47.             raise SyntaxError('Invalid command')
  48.     return res
  49. def parse_whole(c):
  50.     """Parse a whole program"""
  51.     x=c.split('\n')
  52.     res=[]
  53.     for i in x:
  54.         if not i:
  55.             continue
  56.         i=i.strip()
  57.         if not re.match('\<.*?\>\<',i):
  58.             raise SyntaxError('Invalid code')
  59.         else:
  60.             res.append(parse(i[1:-2]))
  61.     return res
  62. def getchar():
  63.     """Read a character"""
  64.     c=sys.stdin.read(1)
  65.     if not c:
  66.         return 0
  67.     return ord(c)&255
  68. def getint():
  69.     """Read an integer"""
  70.     try:
  71.         x=int(input())
  72.     except:
  73.         return 0
  74.     else:
  75.         return x&255
  76. def interpret_line(c):
  77.     """Interpret a line of code and return IP"""
  78.     global stacks,curstack,reg
  79.     for op,arg in c:
  80.         if op==INC:
  81.             reg+=1
  82.             reg&=255
  83.         elif op==DEC:
  84.             reg-=1
  85.             reg&=255
  86.         elif op==INPC:
  87.             reg=getchar()
  88.         elif op==OUTC:
  89.             sys.stdout.write(chr(reg))
  90.         elif op==INP:
  91.             reg=getint()
  92.         elif op==OUT:
  93.             print(reg)
  94.         elif op==PUSH:
  95.             stacks[curstack].append(reg)
  96.         elif op==POP:
  97.             reg=stacks[curstack].pop()
  98.         elif op==SWAP:
  99.             curstack=1-curstack
  100.         elif op==JZ:
  101.             if not reg:
  102.                 return arg
  103.         elif op==JNZ:
  104.             if reg:
  105.                 return arg
  106.         elif op==JMP:
  107.             return arg
  108.         elif op==SET:
  109.             reg=arg
  110.         elif op==SUB:
  111.             reg=(reg-arg)&255
  112.     sys.exit()
  113. def interpret(c):
  114.     """Interpret a whole program"""
  115.     c=parse_whole(c)
  116.     ip=0
  117.     while 1:
  118.         ip=interpret_line(c[ip])
  119. code=[]
  120. while 1:
  121.     x=input()
  122.     if x.strip()=='!':
  123.         break
  124.     code.append(x.strip())
  125. interpret('\n'.join(code))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement