Last active
December 16, 2022 04:54
-
-
Save ytaki0801/7b307be1e86f2e30427a1f9b651a5999 to your computer and use it in GitHub Desktop.
Mini LOGO interpreter in Python with Turtle Graphics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import turtle, time | |
comm = {'fd': lambda n: turtle.fd(n), | |
'rt': lambda n: turtle.rt(n), | |
'lt': lambda n: turtle.lt(n)} | |
def logo(s, p, pe): | |
while p < pe: | |
if s[p] == 'loop': | |
n = int(s[p + 1]) | |
p += 3 | |
c, ps = 1, p | |
while c != 0: | |
if s[p] == '[': c += 1 | |
if s[p] == ']': c -= 1 | |
p += 1 | |
for _ in range(n): logo(s, ps, p - 1) | |
else: | |
comm[s[p]](int(s[p + 1])) | |
p += 2 | |
s = input().rstrip().split() | |
turtle.clear() | |
logo(s, 0, len(s)) | |
time.sleep(5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment