-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommata.py
executable file
·78 lines (60 loc) · 1.82 KB
/
commata.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
,,, (Commata)
A language that probably hopefully does something.
Sumant Bhaskaruni
v0.2.0 (basically, don't use it)
"""
import functions
import tokenizer
class Stack:
def __init__(self, items = None):
if items == None:
self.items = []
else:
self.items = items
def push(self, item, index = None):
if index == None:
self.items.append(item)
else:
self.items.insert(index, item)
def pop(self, index = None):
if index == None:
return self.items.pop()
else:
return self.items.pop(index)
def peek(self, index = None):
if index == None:
return self.items[-1]
else:
return self.items[index]
def reverse(self):
self.items = self.items[::-1]
def __len__(self):
return len(self.items)
def __contains__(self, item):
return item in self.items
def __iter__(self):
return (self.pop(0) for i in range(len(self)))
def run(code, args):
tokens = tokenizer.tokenize(code)
stacks = [Stack()]
stk_no = 0
for arg in args:
stacks[stk_no].push(functions.lit_eval(arg))
for token in tokens:
if token[0] == 'number':
stacks[stk_no].push(functions.lit_eval(token[1]))
elif token[0] == 'string':
stacks[stk_no].push(functions.lit_eval(token[1][1:-1]))
elif token[0] == 'unclosed_string':
stacks[stk_no].push(functions.lit_eval(token[1][1:]))
elif token[0] == 'char':
stacks[stk_no].push(functions.lit_eval(token[1][1:]))
else:
functions.commands[token[1]](stacks, stk_no, stacks[stk_no])
try:
print(stacks[stk_no].pop())
except IndexError:
print()