none-None1

ObjLang interpreter (sorry I forgot to sign in in the last post, causing a duplicate post)

Feb 1st, 2024
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.76 KB | Source Code | 0 0
  1. """
  2. ObjLang is an esolang where all programs are marshalled Python objects.
  3.  
  4. See Esolang Wiki: https://esolangs.org/wiki/ObjLang for details.
  5. """
  6. import sys,marshal,base64
  7. if len(sys.argv)<2:
  8.     print('Input Base64 of program')
  9.     code=marshal.loads(base64.b64decode(input()))
  10. else:
  11.     with open(sys.argv[1],'rb') as f:
  12.         code=marshal.loads(f.read())
  13. class Interpreter:
  14.     def __init__(self):
  15.         self.loop_var = []
  16.         self.rules = {}
  17.         self.arglist=[]
  18.  
  19.     def add_rule(self,rule_name,rule_func):
  20.         self.rules[rule_name]=rule_func
  21.  
  22.     def rule(self,rule_name):
  23.         def inner_func(func):
  24.             self.add_rule(rule_name,func)
  25.         return inner_func
  26.  
  27.     def eval_code(self,c):
  28.         if not isinstance(c,dict) and not isinstance(c,list) and not isinstance(c,int) and not isinstance(c,str):
  29.             raise SyntaxError('Must be a dictionary or an integer or a string or a list')
  30.         if not isinstance(c,dict):
  31.             return c
  32.         if len(c)!=1:
  33.             raise SyntaxError('Must have exactly one key')
  34.         k,v=next(iter(c.items()))
  35.         if k not in self.rules:
  36.             raise SyntaxError('Rule "%s" not found'%k)
  37.         return self.rules[k](self,*v)
  38. inter=Interpreter()
  39. @inter.rule('print')
  40. def func(inp,x):
  41.     ev_x=inp.eval_code(x)
  42.     print(ev_x)
  43.     return ev_x
  44.  
  45. @inter.rule('input')
  46. def func(inp):
  47.     return input()
  48.  
  49. @inter.rule('intinput')
  50. def func(inp):
  51.     return int(input())
  52.  
  53. @inter.rule('add')
  54. def func(inp,x,y):
  55.     return inp.eval_code(x)+inp.eval_code(y)
  56.  
  57. @inter.rule('sub')
  58. def func(inp,x,y):
  59.     return inp.eval_code(x)-inp.eval_code(y)
  60.  
  61. @inter.rule('mul')
  62. def func(inp,x,y):
  63.     return inp.eval_code(x)*inp.eval_code(y)
  64.  
  65. @inter.rule('div')
  66. def func(inp,x,y):
  67.     return inp.eval_code(x)//inp.eval_code(y)
  68.  
  69. @inter.rule('mod')
  70. def func(inp,x,y):
  71.     return inp.eval_code(x)%inp.eval_code(y)
  72.  
  73. @inter.rule('and')
  74. def func(inp,x,y):
  75.     return inp.eval_code(x)&inp.eval_code(y)
  76.  
  77. @inter.rule('or')
  78. def func(inp,x,y):
  79.     return inp.eval_code(x)|inp.eval_code(y)
  80.  
  81. @inter.rule('not')
  82. def func(inp,x):
  83.     return ~inp.eval_code(x)
  84.  
  85. @inter.rule('xor')
  86. def func(inp,x,y):
  87.     return inp.eval_code(x)^inp.eval_code(y)
  88.  
  89. @inter.rule('lsh')
  90. def func(inp,x,y):
  91.     return inp.eval_code(x)<<inp.eval_code(y)
  92.  
  93. @inter.rule('rsh')
  94. def func(inp,x,y):
  95.     return inp.eval_code(x)>>inp.eval_code(y)
  96.  
  97. @inter.rule('neg')
  98. def func(inp,x):
  99.     return -inp.eval_code(x)
  100.  
  101. @inter.rule('pow')
  102. def func(inp,x,y):
  103.     return inp.eval_code(x)**inp.eval_code(y)
  104.  
  105. @inter.rule('eq')
  106. def func(inp,x,y):
  107.     return int(inp.eval_code(x)==inp.eval_code(y))
  108.  
  109. @inter.rule('neq')
  110. def func(inp,x,y):
  111.     return int(inp.eval_code(x)!=inp.eval_code(y))
  112.  
  113. @inter.rule('geq')
  114. def func(inp,x,y):
  115.     return int(inp.eval_code(x)>=inp.eval_code(y))
  116.  
  117. @inter.rule('leq')
  118. def func(inp,x,y):
  119.     return int(inp.eval_code(x)<=inp.eval_code(y))
  120.  
  121. @inter.rule('lt')
  122. def func(inp,x,y):
  123.     return int(inp.eval_code(x)<inp.eval_code(y))
  124.  
  125. @inter.rule('gt')
  126. def func(inp,x,y):
  127.     return int(inp.eval_code(x)>inp.eval_code(y))
  128.  
  129. @inter.rule('lnt')
  130. def func(inp,x):
  131.     return int(not bool(inp.eval_code(x)))
  132.  
  133. @inter.rule('lnd')
  134. def func(inp,x,y):
  135.     return int(bool(inp.eval_code(x)) and bool(inp.eval_code(y)))
  136.  
  137. @inter.rule('lor')
  138. def func(inp,x,y):
  139.     return int(bool(inp.eval_code(x)) or bool(inp.eval_code(y)))
  140.  
  141. @inter.rule('if')
  142. def func(inp,x,y,z):
  143.     if inp.eval_code(x):
  144.         return inp.eval_code(y)
  145.     return inp.eval_code(z)
  146.  
  147. @inter.rule('for')
  148. def func(inp,x,y,z):
  149.     ev_y,ev_z=inp.eval_code(y),inp.eval_code(z)
  150.     res=[]
  151.     for i in range(y,z+1):
  152.         inp.loop_var.append(i)
  153.         res.append(inp.eval_code(x))
  154.         inp.loop_var.pop()
  155.     return res
  156.  
  157. @inter.rule('while')
  158. def func(inp,x,y):
  159.     while inp.eval_code(x):
  160.         inp.eval_code(y)
  161.     return 0
  162.  
  163. @inter.rule('loop')
  164. def func(inp):
  165.     return inp.loop_var[-1]
  166.  
  167. @inter.rule('str')
  168. def func(inp,x):
  169.     return chr(inp.eval_code(x))
  170.  
  171. @inter.rule('num')
  172. def func(inp,x):
  173.     return ord(inp.eval_code(x))
  174.  
  175. @inter.rule('index')
  176. def func(inp,x,y):
  177.     return inp.eval_code(x)[inp.eval_code(y)]
  178.  
  179. @inter.rule('comma')
  180. def func(inp,*args):
  181.     return list(map(inp.eval_code,args))
  182.  
  183. @inter.rule('func')
  184. def func(inp,code,command_name,*args):
  185.     def inner_func(inpp,*argss):
  186.         al={}
  187.         for i,j in enumerate(argss):
  188.             al[args[i]]=inpp.eval_code(j)
  189.         inpp.arglist.append(al)
  190.         res=inpp.eval_code(code)
  191.         inpp.arglist.pop()
  192.         return res
  193.     inp.add_rule(command_name,inner_func)
  194.  
  195. @inter.rule('arg')
  196. def func(inp,arg):
  197.     return inp.arglist[-1][arg]
  198. print('Return value: %r'%inter.eval_code(code))
Add Comment
Please, Sign In to add comment
OSZAR »