-
Notifications
You must be signed in to change notification settings - Fork 5
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
- deque_formated.py | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,10 @@ | |
data = deque() | ||
|
||
ip = 0 | ||
|
||
buffer = '' | ||
bufferleft = None | ||
|
||
while ip < len(program): | ||
word = program[ip] | ||
if tracing: | ||
|
@@ -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) | ||
|
@@ -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) | ||
|
@@ -152,15 +156,44 @@ def pop_data(): | |
elif word == 'print': | ||
print(pop_data()) | ||
ip += 1 | ||
elif word == 'printType': | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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 | ||
|
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! | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
Uh oh!
There was an error while loading. Please reload this page.