Skip to content

Push different types to the deque #6

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 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- deque_formated.py
47 changes: 40 additions & 7 deletions deque.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
data = deque()

ip = 0

buffer = ''
bufferleft = None

while ip < len(program):
word = program[ip]
if tracing:
Expand Down Expand Up @@ -62,12 +66,12 @@ def pop_data():
else:
return data.pop()

if word == 'add':
if word == '+':
a = pop_data()
b = pop_data()
push_data(a + b)
ip += 1
elif word == 'sub':
elif word == '-':
a = pop_data()
b = pop_data()
push_data(b - a)
Expand Down Expand Up @@ -109,17 +113,17 @@ def pop_data():
b = pop_data()
push_data(b << a)
ip += 1
elif word == 'eq':
elif word == '==':
a = pop_data()
b = pop_data()
push_data(a == b)
ip += 1
elif word == 'or':
elif word == '||':
a = pop_data()
b = pop_data()
push_data(a | b)
ip += 1
elif word == 'and':
elif word == '&&':
a = pop_data()
b = pop_data()
push_data(a & b)
Expand Down Expand Up @@ -152,15 +156,44 @@ def pop_data():
elif word == 'print':
print(pop_data())
ip += 1
elif word == 'printType':

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be more versatile to have an instruction that pushes the type as a string to the stack, which would also let you print it with the print instruction.

print(type(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
elif word.startswith("'") and word.endswith("'"):
word = word[1:]
word = word[:-1]
Comment on lines +168 to +169

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

word[1:-1]

push_data(word)
ip += 1
elif word.startswith("'"):
bufferleft = left
word = word[1:]
buffer += word + ' '
ip += 1
elif word.endswith("'"):
left = bufferleft
word = word[:-1]
buffer += word
push_data(buffer)
buffer = ''
bufferleft = None
left = None
ip += 1
elif "." in word:
push_data(float(word))
ip += 1
else:
try:
push_data(int(word))
ip += 1
if buffer != '':
buffer += word + ' '
ip += 1
else:
push_data(int(word))
ip += 1
except ValueError:
push_data(labels[word])
ip += 1
Expand Down
3 changes: 3 additions & 0 deletions pushType.deque
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
0! '0'!
!'I have a crush on you' !printType
0.1! 0.2! +! print!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whatever code editor you're using messed up the formatting—this file should have a trailing newline.

8 changes: 4 additions & 4 deletions rule110.deque
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
0! 0! 0! 0! 0! 0! 0! 0! 0! 0! 0! 1! 0!
40!

outer: 1! sub! dup! 2! eq! outer_end! jmpif!
outer: 1! -! dup! 2! ==! outer_end! jmpif!
trace

!1 !shl !7 !and !or
!1 !shl !7 !&& !||

0!
loop: !over !2 !< !end !jmpif
!1 !shl !7 !and !or
!1 !shl !7 !&& !||

!dup !1 !swap !shl
!110 !swap !and
!110 !swap !&&
!over !shr
!move
!loop !jmp
Expand Down