Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/isaacg1/pyth
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacg1 committed May 31, 2017
2 parents 671566d + 5b775cc commit ac22179
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions docs/details.rst
Original file line number Diff line number Diff line change
@@ -1,77 +1,77 @@
2. More Details About Pyth
**************************

Since you now have the programming environment set up to program in, let's learn some more about the language. Let's try adding some numbers now.
Now that you have the programming environment set up, let's learn some more about the language.

2.1. Pyth Uses Prefix Notation
==============================

Ok, ``2+2`` seems easy enough. Check and see what the interpreter makes of that. It'll be 4 right?::
Try entering ``2+2``. It'll be 4 right?::

2
Traceback (most recent call last):
File "safe_pyth.py", line 300, in <module>
File "<string>", line 5, in <module>
TypeError: plus() missing 1 required positional argument: 'b'

Oh noes! An error! What went wrong? Well, Pyth doesn't use Infix notation like most languages do (the operator goes between the operands), but instead uses `Prefix (aka Polish) <http://en.wikipedia.org/wiki/Polish_notation>`_ notation, which means the operator goes *before* the operands. This has the benefit of not requiring parenthesis, making programs shorter and freeing up operators for other use. So let's try that math problem again::
Oh noes! An error! What went wrong? Well, Pyth doesn't use Infix notation (the operator goes between the operands) like most languages do. Instead, Pyth uses `Prefix (aka Polish) <http://en.wikipedia.org/wiki/Polish_notation>`_ notation, which means the operator goes *before* the operands. This has the benefit of not requiring parenthesis, making programs shorter and freeing up operators for other uses. Let's try that math problem again::

+2 2
Output:

4

There, now its working like we expected it to. Notice the space between the ``2``'s so the parser doesn't interpret them as a ``22``.
Now it is working as expected! Notice the space between the ``2``'s so the parser doesn't interpret them as a ``22``.

2.2. Pyth Has Many, Many Operators
==================================

The addition operator we just saw doesn't even begin to scratch the surface of Pyth's rich variety of operators. Besides addition Pyth has all the customary arithmetic operators - ``-`` for subtraction, ``*`` for multiplication, ``/`` for division, ``%`` for modulo, and ``^`` for exponentiation. Integers are defined as you would expect and Floats with the decimal point. Ex::
The addition operator we just saw doesn't even begin to scratch the surface of Pyth's rich variety of operators. As well as addition, Pyth has all the customary arithmetic operators - ``-`` for subtraction, ``*`` for multiplication, ``/`` for division, ``%`` for modulo, and ``^`` for exponentiation.

Integers are defined as you would expect and Floats with the decimal point. Ex::

0
1
18374
2.5

However, negative numbers do not use a unary version of the subtraction symbol since all operators have a fixed arity (more on that later) but use the unary reversal operator: ``_``::
However, negative numbers do not use a unary version of the subtraction symbol since all operators have a fixed arity (more on arity later). Instead, negative numbers use the unary reversal operator: ``_``::

Invalid: -25

Valid: _25

Pyth also has predefined variables that are set to useful values before program execution starts. Examples are::
Pyth also has many predefined variables::

Z=0
T=10
k=""
d=" "
b="\n"

All operators, variables, and control flow keywords, are always one character long to reduce the size of the program. This does make Pyth programs very hard to read.

As I said before, Pyth has much more than arithmetic, but you'll learn about them in due time.
All operators, variables, and control flow keywords, are always one character long to reduce the size of the program.

2.3. All Operators in Pyth Have a Fixed Arity
=============================================

A very important concept in Pyth is that of arity. The `arity <http://en.wikipedia.org/wiki/Arity>`_ of an operator or function (according to Wikipedia) is *the number of arguments or operands the function or operation accepts.* Most programming languages allow functions to accept different amounts of arguments, but this causes them to require parenthesis. In the want for smaller programs, Pyth has a fixed number of operands per operator, allowing it to do away with parenthesis or any other delimiter.
The `arity <http://en.wikipedia.org/wiki/Arity>`_ of an operator or function (according to Wikipedia) is *the number of arguments or operands the function or operation accepts.* Most programming languages allow functions to accept different numbers of arguments, but doing so requires parenthesis. Pyth instead has a fixed number of operands per operator, allowing it to do away with parenthesis or any other delimiter.

2.4. Operators Mean Different Things in Different Contexts
==========================================================

In an effort to further increase the number of function available with only one character operators, operators do different things in Pyth depending on the values passed to it. For example, the ``+`` operator adds numbers, which you already saw, but if it gets two sequences (e.g. strings, lists) as operands, it concatenates them::
To further increase the number of functions available with only one character operators, operators operate differently depending on the values passed to it. For example, we saw the ``+`` operator adds numbers, but if ``+`` gets two sequences (e.g. strings, lists) as operands, it concatenates them::

+2 T -> 12 (remember that T=10)

+"Hello ""World!" -> Hello World!

The ``+`` sign meaning both concatenation and addition is pretty common, but Pyth takes this to another level, with most operators having 2 or more meanings.
Letting ``+`` mean both concatenation and addition is pretty common, but Pyth takes this to another level, with most operators having 2 or more meanings.

2.5. The Pyth Code is Compiled to Python Code Then Run
======================================================

Pyth is technically a `JIT <http://en.wikipedia.org/wiki/Just-in-time_compilation>`_ compiled language since the Pyth code is converted to Python through a series of rules defined in the file ``data.py`` and then run with the variables and functions defined in ``macros.py``. To see this in action, you can select the debug button in the online interpreter or pass the ``-d`` flag to the local one to see the compiled Python code. Here is what comes out as debug from ``^T6`` which evaluates to a million::
Pyth is technically a `JIT <http://en.wikipedia.org/wiki/Just-in-time_compilation>`_ compiled language since the Pyth code is converted to Python through a series of rules defined in the file ``data.py`` and then run with the variables and functions defined in ``macros.py``. You can select the debug button in the online interpreter or pass the ``-d`` flag to the local one to see the compiled Python code. Here is what comes out as debug from ``^T6`` which evaluates to a million::

==================================================
^T6
Expand All @@ -81,5 +81,3 @@ Pyth is technically a `JIT <http://en.wikipedia.org/wiki/Just-in-time_compilatio
1000000

As you can see, the exponentiation call is translated to a call to the function ``Ppow`` and is implicitly printed through a custom print function called ``Pprint``. The debug option can be very helpful with seeing what at first glance looks like a bunch of random characters does.

Next we'll write some example programs to learn more about Pyth.

0 comments on commit ac22179

Please sign in to comment.