Skip to content

Commit 26d07f5

Browse files
authored
Update Python implmentation page (microsoft#6646)
* Update Python implmentation page * setup redirect to /python * Add some blather that let's this be a landing page
1 parent e6eacaa commit 26d07f5

5 files changed

Lines changed: 178 additions & 56 deletions

File tree

common-docs/javascript/sts.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
### ~ hint
44

5-
This is a draft reflecting the design of STS - not exactly what is implemented, but quite close.
5+
This is a draft reflecting the design of Static TypeScript (STS) - not exactly what is implemented, but quite close.
66
Comments welcome.
77

88
### ~
99

1010
[TypeScript](http://typescriptlang.org) is a typed superset of JavaScript designed to enable JavaScript developers
1111
to take advantage of code intellisense,
12-
static checking and refactoring made possible by types. TypeScript is gradually typed, meaning that types are optional.
12+
static checking and refactoring made possible by types. TypeScript is gradually typed, meaning that types are optional.
1313
Type inference helps to assign types to untyped code.
1414

1515
In this document, we define a subset of TypeScript called Static TypeScript (STS, for short),

docs/js/python-ref.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"redirect": "/python"
3+
}

docs/js/python.md

Lines changed: 0 additions & 52 deletions
This file was deleted.

docs/language.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ is constructed right after last used variable have been defined.
114114
For functions defined before usage, the closure is constructed at the
115115
point of definition.)
116116
Both of the following examples will yield a compile error.
117+
117118
```typescript
118119
function foo1() {
119120
bar()
@@ -130,7 +131,6 @@ function foo1() {
130131
}
131132
```
132133

133-
134134
For JS-only targets we may implement the following:
135135

136136
* regular expressions
@@ -283,7 +283,6 @@ and dynamic maps.
283283
the order of fields will be static declaration order
284284
* how to validate types of C++ classes (Pin mostly)?
285285

286-
287286
# Python
288287

289288
## Supported language features

docs/python.md

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
# Python in MakeCode
2+
3+
Our Python support is called **MakeCode Python**. As with TypeScript, the Python implementation is a subset of the full Python languange.
4+
5+
The TypeScript implementation in MakeCode is called Static TypeScript (STS) and similarly, MakeCode Python is often referred to as Static Python (SPY).
6+
7+
### ~ hint
8+
9+
#### MakeCode languages
10+
11+
See the [MakeCode Languages](/language) page for general description of programming
12+
language support in MakeCode.
13+
14+
### ~
15+
16+
## Python - Very brief overview
17+
18+
Python is a simple but expressive language that is used widely from programming simple scripting operations to complex website applications. Learning Python with MakeCode let's you start with simple programs while switching between Blocks and even JavaScript if you want. Later, you can advance to more complex concepts such as arrays and classes. As, you develop a proficiency in the Python editor, you can write code that is too complex for Blocks to represent. Then, you're on your way to become a real power coder!
19+
20+
This section is a very brief overview of what Python code looks like.
21+
22+
### Variables and expressions
23+
24+
Python variables are initialized when declared and the type is inferred from the value assigned.
25+
26+
```python
27+
a = 10
28+
b = a * 50
29+
```
30+
31+
Some of the common value types for variables are numeric, boolean, and string.
32+
33+
```python
34+
cards = 52
35+
finished = False
36+
month = "Febraury"
37+
```
38+
39+
Expressions are formed using values of these types along with associated operators:
40+
41+
```python
42+
hours = 24
43+
seconds = hours * 60 * 60
44+
pin1 = False
45+
pin2 = True
46+
command = pin1 and pin2
47+
```
48+
49+
### Control structures
50+
51+
Conditional statements and loops rely on indentation to determine block scope for the control structure. Control blocks don't use any text delimiters other that an indent to specify the block scope. The ``if`` and ``if-else`` statement has the code for the condition indented:
52+
53+
```python
54+
a = True
55+
b = False
56+
if a:
57+
b = False
58+
else:
59+
b = True
60+
```
61+
62+
For a multi-level conditional:
63+
64+
```python
65+
a = True
66+
b = False
67+
if a:
68+
b = False
69+
if a and b:
70+
a = False
71+
else if a or b:
72+
b = True
73+
else:
74+
b = True
75+
```
76+
77+
Loops use indentation to determine what code is repeated:
78+
79+
```python
80+
limit = 0
81+
while limit < 10:
82+
limit = limit + 1
83+
84+
for i in range(10):
85+
limit = limit + i
86+
```
87+
88+
### Functions and function calls
89+
90+
Functions are defined using the `def` directive. The code for a function is indented below the definition statement to show that it is contained in the function. The function is called using the function name with parenthesis to contain any parameters. Functions can return a value or just perform other actions and not return a value.
91+
92+
```python
93+
def square(side):
94+
return side * side
95+
96+
area = square(9)
97+
98+
def showArea(side):
99+
print("The area of the square is: " + square(side))
100+
101+
showArea(32)
102+
```
103+
104+
## MakeCode Python implementation
105+
106+
### Code translation
107+
108+
Python support in MakeCode works by converting SPY source code into Static Typscript (STS) and vice versa.
109+
The translation is mostly 1:1 (that is for every statement of STS you usually get
110+
one statement of SPY and vice versa).
111+
The code generated in both directions is meant to be human readable.
112+
The API surface stays largely the same between STS and SPY, except that camel case
113+
like `onChat` are converted into snake case (e.g., `on_chat`) where
114+
appropriate (that is excluding class and enum names; enum members are converted
115+
to upper snake case).
116+
117+
#### Python
118+
119+
```python
120+
def on_chat():
121+
for i in range(100):
122+
mobs.spawn(CHICKEN, pos(0, 10, 0))
123+
player.on_chat("chicken", on_chat)
124+
```
125+
126+
#### TypeScript
127+
128+
```typescript
129+
player.onChat("chicken", function () {
130+
for (let i = 0; i < 100; i++) {
131+
mobs.spawn(CHICKEN, pos(0, 10, 0))
132+
}
133+
})
134+
```
135+
136+
This approach has the following advantages:
137+
138+
* the blocks to code and code to blocks is supported for both STS and SPY
139+
(in case of SPY there is an intermediate conversion to STS)
140+
* the same high-performance runtime is used across STS and SPY
141+
(it's typically 10-100x faster than embedded interpreters)
142+
* documentation can be converted automatically
143+
* as the SPY - >STS converter infers types, the SPY editor supports
144+
smart autocompletion, contextual doc-comment display, etc.
145+
* features like debugger are easily shared between SPY and STS
146+
147+
The type annotations are technically optional in both STS and SPY -
148+
TypeScript `any` type is supported in the runtime with dynamic member lookup
149+
(though it still uses compact C++-like memory layout for classes).
150+
Some of this have not been fully implemented or exposed to the user yet though.
151+
152+
### Language support
153+
154+
The disadvantage with this technique is that it imposes some limits in compatibility with the full Python language.
155+
156+
The main missing feature (from both STS and SPY) is `eval`,
157+
and this one is very unlikely to ever change.
158+
159+
Currently, SPY auto-imports all STS namespaces (that is, one can say
160+
`pins.D7.digitalWrite(True)` without saying `import pins` first).
161+
This is mostly due to MakeCode libraries using a large number of namespaces
162+
even in simple programs (as they map directly to block categories).
163+
164+
### Not supported
165+
166+
The following Python language features are currently not supported.
167+
168+
* `-*- coding: encoding -*-` (only UTF8 is supported)
169+
* class private names `__*` are not mangled
170+
* complex and imaginary numbers
171+
* big integers
172+
* Formatted string literals (for now)

0 commit comments

Comments
 (0)