Created
November 16, 2017 20:23
-
-
Save Zwork101/d469674aa754dba5d27f840fffe6e3ac to your computer and use it in GitHub Desktop.
CooctusLang
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 re | |
lines = [] | |
async def run(code): | |
tasks = [] | |
for i, line in enumerate(code.split('\n')): | |
l = Line(line, i) | |
tasks.append(asyncio.ensure_future(l.run())) | |
await asyncio.gather(*tasks) | |
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)} | |
lines.append(self) | |
@staticmethod | |
def check_and_replace(text): | |
for match in re.finditer(r'\|(?P<k>\w+)\/(?P<v>\d+)\|', text): | |
var = getattr([l for l in lines if l.line == int(match['v'])][0], match['k']) | |
text = text[:match.start()] + var + text[match.end():] | |
return text | |
async def execute(self, cmd, start, end): | |
if cmd == 'p': | |
text = self.code[start:end] | |
#repr(text) | |
if '))' in text: | |
text = text.replace('))', '\n') | |
print(Line.check_and_replace(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 |name/2|] | |
v[name/Nathan] | |
""" | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(run(code)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment