def __init__(self, ast): self.ast = ast self.globals = data.Map(data.Symbol, data.Type) self.globals.set(data.Symbol('import'), data.Method(self._import)) self.globals.set(data.Symbol('if'), data.Method(self._if)) self.globals.set(data.Symbol('while'), data.LazyMethod(self._while)) self.globals.set(data.Symbol('return'), data.Method(lambda val: val)) self.globals.set(data.Symbol('py'), data.Method(self.py)) self.globals.set(data.Symbol('inf'), data.Number(float('inf'))) self.globals.set(data.Symbol('infinity'), data.Number(float('inf'))) self.globals.set(data.Symbol('number'), type_method(data.Number)) self.globals.set(data.Symbol('string'), type_method(data.String)) self.globals.set(data.Symbol('func'), type_method(data.Func, self.globals)) self.globals.set(data.Symbol('class'), type_method(data.Class)) self.globals.set(data.Symbol('list'), type_method(data.List)) self.globals.set(data.Symbol('range'), type_method(data.Range)) self.globals.set(data.Symbol('type'), type_method(data.Type)) self.globals.set(data.Symbol('symbol'), type_method(data.Symbol)) self.globals.set(data.Symbol('map'), type_method(data.Map)) self.globals.set(data.Symbol('bool'), type_method(data.Bool)) self.globals.set(data.Symbol('void'), data.Method(lambda: type(None))) self.globals.set(data.Symbol('_pytype'), type_method(data.PyType))
import data as data import errors as errors import random as r def randint(*args): """Return random number""" if len(args) < 2: errors.error(f'Random needs 2 arguments, but only got {len(args)}') a = float(args[0].val) b = float(args[1].val) if round(a) != a or round(b) != b: return data.Number(r.uniform(a, b)) else: return data.Number(r.randint(a, b)) def choice(**kwargs): if len(kwargs) != 1: errors.error(f'Choice needs 1 arguement, but got {len(kwargs)}') else: return data.Type(r.choice(kwargs)) attrs = { 'choice': data.Method(choice), 'randint': data.Method(randint), }
errors.error(f'rot13 needs exactly 1 argument, got {len(args)}') val = str(args[0].val) out = '' for char in val: if char.isalpha(): add = 65 if char.isupper() else 97 new = ord(char) - add out += chr(((new + 13) % 26) + add) else: out += char return data.String(out) def xor(*args): if len(args) != 2: errors.error(f'xor needs exactly 2 arguments, got {len(args)}') val = str(args[0].val) key = str(args[1].val) out = '' for i in range(len(val)): out += chr(ord(val[i]) ^ ord(key[i % len(key)])) return data.String(out) attrs = { 'sha256':data.Method(sha256), 'sha512':data.Method(sha512), 'sha128':data.Method(sha128), 'md5':data.Method(md5), 'rot13':data.Method(rot13), 'xor':data.Method(xor), }
def decode(carrier): img = carrier.val image = Image.open(img, 'r') out = '' imgdata = iter(image.getdata()) while (True): pixels = [value for value in imgdata.__next__()[:3] + imgdata.__next__()[:3] + imgdata.__next__()[:3]] binstr = '' for i in pixels[:8]: if (i % 2 == 0): binstr += '0' else: binstr += '1' out += chr(int(binstr, 2)) if (pixels[-1] % 2 != 0): return data.String(out) attrs = { 'encode': data.Method(encode), 'decode': data.Method(decode), 'encode_wav': data.Method(encode_wav), 'decode_wav': data.Method(decode_wav), }
import data import errors import parse import os import os.path PRIVATE = 2 PROTECTED = 1 PUBLIC = 0 rw = lambda name: len(name) - len(name.lstrip('_')) type_method = lambda type, *other: data.Method( lambda *args: type if not args or len(args) < len(other) else type(*other, *args)) def expr(val, scope): if val.type == 'string': return data.String(val.val[0]) elif val.type == 'number': return data.Number(val.val[0]) elif val.type == 'decl': t = data.call(data.get_name(scope, val.val[0])) if t == data.Number: default = data.Number(0) elif t == data.String: default = data.String('') elif t == data.Func: default = data.Func(scope, data.Block(Program(parse.Node('program'))))