Created
November 16, 2017 19:31
-
-
Save Zwork101/d67e65221646d12eeadbe79077603a1f to your computer and use it in GitHub Desktop.
making a fun language
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import time | |
def run(code): | |
for i, line in enumerate(code.split('\n')): | |
l = Line(line, i) | |
asyncio.ensure_future(l.run()) | |
class Line: | |
def __init__(self, code, line): | |
self.code = code | |
self.line = line | |
self.char_count = {i: v for i, v in enumerate(self.code)} | |
async def execute(self, cmd, start, end): | |
if cmd == 'p': | |
text = self.code[start:end] | |
if '))' in text: | |
text = text.replace('))', '\n') | |
print(text, end='') | |
elif cmd == 't': | |
await asyncio.sleep(float(self.code[start:end])) | |
elif cmd == 'v': | |
name, value = self.code[start:end].split('/') | |
setattr(self, name, value) | |
async def run(self): | |
for index, char in self.char_count.items(): | |
if char == ']': | |
# Search for start of function | |
found_ends = 0 | |
new_index = index | |
while True: | |
new_index -= 1 | |
if self.char_count[new_index] == '[': | |
if found_ends: | |
found_ends -= 1 | |
continue | |
else: | |
await self.execute(self.code[new_index - 1], new_index + 1, index) | |
break | |
elif self.char_count[new_index] == ']': | |
found_ends += 1 | |
if __name__ == '__main__': | |
code = """ | |
t[2]p[Hello] | |
p[World] | |
""" | |
loop = asyncio.get_event_loop() | |
run(code) | |
loop.run_forever() | |
# l = Line("p[Hello World p[Cooctus]]", 0) | |
# l.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment