-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcampfire.py
executable file
·100 lines (88 loc) · 3.3 KB
/
campfire.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#A very basic first interpreter for campfire language https://github.com/barbuz/campfire/
import sys
import re
class Stack:
#A particular implementation of a stack, with implicit 0s at the bottom and a secondary stack to push popped values to
def __init__(self,other=None):
self.values=list()
self.other=other
@property
def tos(self):
return 0 if len(self.values)==0 else self.values[-1]
def push(self,val):
self.values.append(val)
def pop(self):
val = 0 if len(self.values)==0 else self.values.pop()
self.other.push(val)
return val
def swap(self):
second = 0 if len(self.values)<2 else self.values.pop(-2)
self.values.append(second)
def clear(self):
self.values=list()
def run(code,debug=False):
stack=Stack()
auxiliary=Stack(stack)
stack.other=auxiliary
position=0
direction=1
string_mode=False
while True:
instruction=code[position]
if debug:
print(instruction,stack.tos,position,direction)
if instruction=='"':
string_mode = not string_mode
elif string_mode:
stack.push(ord(instruction))
elif instruction in '0123456789':
stack.push(int(instruction))
elif instruction in '-+*%><':
b=str(stack.pop())
a=str(stack.pop())
stack.push(int(eval(a+instruction+b))) #easy and ugly
elif instruction in '/=':
b=str(stack.pop())
a=str(stack.pop())
stack.push(int(eval(a+instruction*2+b))) #easy and uglier
elif instruction=='!':
stack.push(int(not stack.pop()))
elif instruction=='~':
char=sys.stdin.read(1)
if len(char)==0:
stack.push(0)
else:
stack.push(ord(char))
elif instruction=='&':
stack.push(int(input())) #fails if the input consists of anything different than a single int
elif instruction==',':
print(chr(stack.pop()),end='')
elif instruction=='.':
print(stack.pop(),end='\n')
elif instruction=='_':
stack.pop()
elif instruction=='^':
auxiliary.pop()
elif instruction=='$':
stack.swap()
elif instruction==';':
auxiliary.clear()
else:
pass
occurrences = [match.start() for match in re.finditer(re.escape(instruction),code)] #list of all positions of the current instruction
if len(occurrences)==1:
return
if stack.tos!=0:
direction=-direction
next_occurrence=occurrences[(occurrences.index(position)+direction)%len(occurrences)] #next occurrence depends on direction, and wraps
position=(next_occurrence+direction)%len(code) #the next instruction executed is the one after the next occurrence of the current instruction
def main(args):
with open(args[1],'r') as program:
lines=program.readlines()
code=""
for line in lines: #remove comments and newlines
if line[0]!='#':
code+=line.strip('\n')
run(code,len(args)>2) #any argument is --debug! (support for different arguments may come in the future)
if __name__ == '__main__':
sys.exit(main(sys.argv))