
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "examples/turtle_dsl.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        Click :ref:`here <sphx_glr_download_examples_turtle_dsl.py>`
        to download the full example code

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_examples_turtle_dsl.py:


Turtle DSL
==========

Implements a LOGO-like toy language for Python’s turtle, with interpreter.

.. GENERATED FROM PYTHON SOURCE LINES 7-91

.. code-block:: default


    try:
        input = raw_input   # For Python2 compatibility
    except NameError:
        pass

    import turtle

    from lark import Lark

    turtle_grammar = """
        start: instruction+

        instruction: MOVEMENT NUMBER            -> movement
                   | "c" COLOR [COLOR]          -> change_color
                   | "fill" code_block          -> fill
                   | "repeat" NUMBER code_block -> repeat

        code_block: "{" instruction+ "}"

        MOVEMENT: "f"|"b"|"l"|"r"
        COLOR: LETTER+

        %import common.LETTER
        %import common.INT -> NUMBER
        %import common.WS
        %ignore WS
    """

    parser = Lark(turtle_grammar)

    def run_instruction(t):
        if t.data == 'change_color':
            turtle.color(*t.children)   # We just pass the color names as-is

        elif t.data == 'movement':
            name, number = t.children
            { 'f': turtle.fd,
              'b': turtle.bk,
              'l': turtle.lt,
              'r': turtle.rt, }[name](int(number))

        elif t.data == 'repeat':
            count, block = t.children
            for i in range(int(count)):
                run_instruction(block)

        elif t.data == 'fill':
            turtle.begin_fill()
            run_instruction(t.children[0])
            turtle.end_fill()

        elif t.data == 'code_block':
            for cmd in t.children:
                run_instruction(cmd)
        else:
            raise SyntaxError('Unknown instruction: %s' % t.data)


    def run_turtle(program):
        parse_tree = parser.parse(program)
        for inst in parse_tree.children:
            run_instruction(inst)

    def main():
        while True:
            code = input('> ')
            try:
                run_turtle(code)
            except Exception as e:
                print(e)

    def test():
        text = """
            c red yellow
            fill { repeat 36 {
                f200 l170
            }}
        """
        run_turtle(text)

    if __name__ == '__main__':
        # test()
        main()


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** ( 0 minutes  0.000 seconds)


.. _sphx_glr_download_examples_turtle_dsl.py:


.. only :: html

 .. container:: sphx-glr-footer
    :class: sphx-glr-footer-example



  .. container:: sphx-glr-download sphx-glr-download-python

     :download:`Download Python source code: turtle_dsl.py <turtle_dsl.py>`



  .. container:: sphx-glr-download sphx-glr-download-jupyter

     :download:`Download Jupyter notebook: turtle_dsl.ipynb <turtle_dsl.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
