fork download
  1. # FALSE Interpreter.
  2. # esolangs.org/wiki/FALSE
  3. # Author: mbomb007
  4.  
  5. import sys
  6. from operator import*
  7.  
  8. stack = []
  9. var = dict()
  10.  
  11. def I(stack,i):stack+=[i]
  12. def C(stack,c):stack+=[ord(c)]
  13.  
  14. def dup(stack):stack+=[stack[-1]]
  15. def drop(stack):stack.pop()
  16. def swap(stack):stack[-2:]=stack[-1:-3:-1]
  17. def rot(stack):stack[-3:]=stack[-2:]+[stack[-3]]
  18. def pick(stack):stack+=[stack[~stack.pop()]]
  19.  
  20. # +-*/&|>=
  21. def arith(stack,n):stack[-2:]=[(add,sub,mul,floordiv,and_,or_,gt,eq)[n](*stack[-2:])]
  22. def negate(stack):stack[-1]=-stack[-1]
  23. def tilde(stack):stack[-1]=~stack[-1]
  24.  
  25. def byref(stack,v):C(stack,v)
  26. def store(stack):global var;a=stack.pop();var[chr(a)]=stack.pop()
  27. def fetch(stack):global var;stack+=[var[chr(stack.pop())]]
  28.  
  29. def read(stack):
  30. try:C(stack,sys.stdin.read(1))
  31. except:I(stack,-1)
  32. def write(stack):print(chr(stack.pop()),end='')
  33. def num(stack):print(stack.pop(),end='')
  34. def flush():sys.stdout.flush()
  35.  
  36. def main(code):
  37. global stack,var
  38. code=list(code)
  39. while code:
  40. _=''
  41. while code[0]in"0123456789":_+=code.pop(0)
  42. if _:stack+=[int(_)]
  43. _=code.pop(0);o=ord(_)
  44. if 96<o<123:C(stack,_)
  45. A="+-*/&|>="
  46. if _ in A:arith(stack,A.index(_))
  47. if o==39:C(stack,code.pop(0))
  48. if o==91:
  49. q=0;code2=''
  50. while']'!=code[0]or q:
  51. if code[0]=='[':q+=1
  52. if code[0]==']':q-=1
  53. code2+=code.pop(0)
  54. stack+=[lambda:main(code2)]
  55. if o==34:q=code.index('"');print(*code[:q],sep='',end='');code=code[q+1:]
  56. if o==123:code=code[code.index('}')+1:]
  57. if o==33:stack.pop()()
  58. if o==63:
  59. f=stack.pop()
  60. if stack.pop():f()
  61. if o==35:
  62. f=stack.pop();c=stack.pop()
  63. while c():f()
  64. if _=='ß':flush()
  65. if _ in"$%\\@ø_~:;^,.":[dup,drop,swap,rot,pick,negate,tilde,store,fetch,read,write,num][r"$%\@ø_~:;^,.".index(_)](stack)
  66.  
  67. #print(stack)
  68.  
  69. code=r'''
  70. 33 49a:," "a;,
  71. '''
  72. main(code)
  73. print()
  74.  
  75. code=r'''
  76. { factorial program in false! }
  77.  
  78. [$1=~[$1-f;!*]?]f: { fac() in false }
  79.  
  80. "calculate the factorial of [1..8]: "
  81. ß^ß'0-$$0>~\8>|$
  82. "result: "
  83. ~[\f;!.]?
  84. ["illegal input!"]?"
  85. "
  86. '''
  87. main(code)
  88. print()
  89.  
Success #stdin #stdout 0s 9992KB
stdin
Standard input is empty
stdout
! 1
calculate the factorial of [1..8]: result: illegal input!