Skip to content

Optimisations #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 23 additions & 42 deletions deque.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,24 @@

tracing = False

def push_data(value):
if left is None:
raise RuntimeError(f"Direction is not provided for {word} at {ip}")
if left:
data.appendleft(value)
else:
data.append(value)

def pop_data():
if left is None:
raise RuntimeError(f"Direction is not provided for {word} at {ip}")
if left:
return data.popleft()
else:
return data.pop()

if __name__ == '__main__':

if len(sys.argv) < 2:
print("ERROR: no file path is provided")
exit(1)
Expand All @@ -27,7 +44,6 @@
if word in labels:
raise RuntimeError(f"Label `{word}` already defined")
labels[word] = ip

data = deque()

ip = 0
Expand All @@ -46,122 +62,87 @@
ip += 1
continue

def push_data(value):
if left is None:
raise RuntimeError(f"Direction is not provided for {word} at {ip}")
if left:
data.appendleft(value)
else:
data.append(value)

def pop_data():
if left is None:
raise RuntimeError(f"Direction is not provided for {word} at {ip}")
if left:
return data.popleft()
else:
return data.pop()

if word == 'add':
a = pop_data()
b = pop_data()
push_data(a + b)
ip += 1
elif word == 'sub':
a = pop_data()
b = pop_data()
push_data(b - a)
ip += 1
elif word == 'dup':
a = pop_data()
push_data(a)
push_data(a)
ip += 1
elif word == 'swap':
a = pop_data()
b = pop_data()
push_data(a)
push_data(b)
ip += 1
elif word == 'move':
a = pop_data()
left = not left
push_data(a)
left = not left
ip += 1
elif word == 'over':
a = pop_data()
b = pop_data()
push_data(b)
push_data(a)
push_data(b)
ip += 1
elif word == 'drop':
pop_data()
ip += 1
elif word == 'shr':
a = pop_data()
b = pop_data()
push_data(b >> a)
ip += 1
elif word == 'shl':
a = pop_data()
b = pop_data()
push_data(b << a)
ip += 1
elif word == 'eq':
a = pop_data()
b = pop_data()
push_data(a == b)
ip += 1
elif word == 'or':
a = pop_data()
b = pop_data()
push_data(a | b)
ip += 1
elif word == 'and':
a = pop_data()
b = pop_data()
push_data(a & b)
ip += 1
elif word == '>':
a = pop_data()
b = pop_data()
push_data(a > b)
ip += 1
elif word == '<':
a = pop_data()
b = pop_data()
push_data(a < b)
ip += 1
elif word == '>=':
a = pop_data()
b = pop_data()
push_data(a >= b)
ip += 1
elif word == 'jmpif':
addr = pop_data()
cond = pop_data()
if cond:
ip = addr
else:
ip += 1
ip = addr-1
elif word == 'jmp':
addr = pop_data()
ip = addr
ip = addr-1
elif word == 'print':
print(pop_data())
ip += 1
elif word == 'exit':
exit(0)
elif word == 'trace':
print(''.join(list(map(lambda x: '*' if x == 1 else ' ', data))))
ip += 1
print(''.join(map(lambda x: '*' if x == 1 else ' ', data)))
else:
try:
push_data(int(word))
ip += 1
except ValueError:
push_data(labels[word])
ip += 1
if tracing: print(data)
ip += 1
if tracing:
print(data)