Skip to content

Commit ca9078f

Browse files
Add list.count tests
1 parent 28f8579 commit ca9078f

File tree

68 files changed

+2193
-1870
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+2193
-1870
lines changed

README.md

Lines changed: 125 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,147 +1,192 @@
11
# LPython
22

3-
LPython is a Python compiler. It is in heavy development, currently in
4-
pre-alpha stage. Some of the goals of LPython:
5-
6-
* The best possible performance for numerical, array-oriented code
7-
* Run on all platforms
8-
* Compile a subset of Python yet be fully compatible with Python
9-
* Explore designs so that LPython eventually can compile all Python code
10-
* Fast compilation
11-
* Excellent user-friendly diagnostic messages: error, warnings, hints, notes,
3+
LPython is a Python compiler. It is in heavy development, currently in pre-alpha stage. LPython works on Windows, macOS and Linux. Some of the goals of LPython include:
4+
5+
- The best possible performance for numerical, array-oriented code
6+
- Run on all platforms
7+
- Compile a subset of Python yet be fully compatible with Python
8+
- Explore designs so that LPython eventually can compile all Python code
9+
- Fast compilation
10+
- Excellent user-friendly diagnostic messages: error, warnings, hints, notes,
1211
etc.
13-
* Ahead-of-Time compilation to binaries, plus interactive usage (Jupyter
14-
notebook)
15-
* Transforming Python code to C++, Fortran and other languages
12+
- Ahead-of-Time compilation to binaries, plus interactive usage (Jupyter notebook)
13+
- Transforming Python code to C++, Fortran and other languages
1614

1715
And more.
1816

1917
# Installation
2018

21-
LPython works on Windows, macOS and Linux.
19+
## Step 0: Prerequisites
2220

23-
## Install Conda
21+
Here is the list of requirements needed to build LPython:
22+
23+
- Python (3.10+)
24+
- Conda
25+
26+
For Windows, these are additionally required:
27+
28+
- Miniforge Prompt
29+
- Visual Studio (with "Desktop Development with C++" workload)
30+
31+
Please follow the steps for your desired platform.
32+
33+
## Step 1: Install Conda
34+
35+
This step involves installing Conda using a conda-forge distribution called Miniforge.
2436

2537
Please follow the instructions here to install Conda on your platform:
2638

27-
https://github.com/conda-forge/miniforge/#download
39+
Miniforge download link (for Linux, MacOS and Windows): https://github.com/conda-forge/miniforge/#download
40+
41+
## Step 2: Setting up
42+
43+
This step involves setting up the required configuration to run the programs in LPython.
2844

2945
### Linux
3046

47+
Run the below command to install `binutils-dev` package on Linux.
48+
3149
```bash
3250
sudo apt install binutils-dev
3351
```
3452

3553
### Windows
36-
Install Visual Studio (MSVC), for example the version 2022, you can download the
37-
Community version for free from: https://visualstudio.microsoft.com/downloads/.
3854

39-
Launch the Miniforge prompt from the Desktop.
55+
Please follow the below steps for Windows:
4056

41-
In the shell, initialize the MSVC compiler using:
57+
- Install Visual Studio, for example the version 2022.
4258

43-
```bash
44-
call "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\VsDevCmd" -arch=x64
45-
```
59+
- You can download the
60+
Community version for free from: https://visualstudio.microsoft.com/downloads/.
61+
- After installing Visual Studio and running the Visual Studio Installer, you must install the "Desktop Development with C++" workload which will install Visual C++ Compiler (MSVC).
4662

47-
You can optionally test MSVC via:
63+
- Launch the Miniforge prompt from the Desktop.
4864

49-
```bash
50-
cl /?
51-
link /?
52-
```
65+
- It is recommended to use MiniForge instead of Powershell as the main terminal to build and write code for LPython.
5366

54-
Both commands must print several pages of help text.
67+
- In the MiniForge Prompt, initialize the MSVC compiler using the below command:
5568

56-
## Build LPython
69+
```bash
70+
call "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\VsDevCmd" -arch=x64
71+
```
5772

58-
Clone LPython
73+
- You can optionally test MSVC via:
5974

60-
```bash
61-
git clone https://github.com/lcompilers/lpython.git
62-
cd lpython
63-
```
75+
```bash
76+
cl /?
77+
link /?
78+
```
79+
80+
Both commands must print several pages of help text.
81+
82+
## Step 3: Build LPython
83+
84+
- Clone LPython using the following commands
85+
86+
```bash
87+
git clone https://github.com/lcompilers/lpython.git
88+
cd lpython
89+
```
90+
91+
You may also use GitHub Desktop to do the same.
6492

6593
### Linux and MacOS
6694

6795
- Create a Conda environment using the pre-existing file:
6896

69-
```bash
70-
conda env create -f environment_unix.yml
71-
conda activate lp
72-
```
97+
```bash
98+
conda env create -f environment_unix.yml
99+
conda activate lp
100+
```
73101

74102
- Generate prerequisite files; build in Debug Mode:
75103

76-
```bash
77-
./build0.sh
78-
./build1.sh
79-
```
104+
```bash
105+
./build0.sh
106+
./build1.sh
107+
```
80108

81109
### Windows
82110

83111
- Create a Conda environment using the pre-existing file:
84112

85-
```bash
86-
conda env create -f environment_win.yml
87-
conda activate lp
88-
```
113+
```bash
114+
conda env create -f environment_win.yml
115+
conda activate lp
116+
```
89117

90118
- Generate prerequisite files; build in Release Mode:
91119

92-
```bash
93-
call build0.bat
94-
call build1.bat
95-
```
120+
```bash
121+
call build0.bat
122+
call build1.bat
123+
```
96124

97125
- Tests and examples
98126

99-
```bash
100-
ctest
101-
inst\bin\lpython examples\expr2.py
102-
inst\bin\lpython examples\expr2.py -o a.out
103-
a.out
104-
```
127+
```bash
128+
ctest
129+
inst\bin\lpython examples\expr2.py
130+
inst\bin\lpython examples\expr2.py -o a.out
131+
a.out
132+
```
105133

106-
- After you update a test case file, you also need to update all the reference results associated with that test case:
134+
- Whenever you are updating a test case file, you also need to update all the reference results associated with that test case:
107135

108-
```
109-
python run_tests.py -u --skip-run-with-dbg
110-
```
136+
```
137+
python run_tests.py -u --skip-run-with-dbg
138+
```
111139

112140
- To see all the options associated with LPython test suite, use:
113-
```
114-
python run_tests.py --help
115-
```
116141

117-
## Tests (Linux or MacOs):
142+
```
143+
python run_tests.py --help
144+
```
118145

119-
Run tests:
146+
## Tests:
120147

121-
```bash
122-
ctest
123-
./run_tests.py
124-
```
148+
### Linux or MacOS
125149

126-
Run integration tests:
150+
- Run tests:
127151

128-
```bash
129-
cd integration_tests
130-
./run_tests.py
131-
```
152+
```bash
153+
ctest
154+
./run_tests.py
155+
```
156+
157+
- Run integration tests:
158+
159+
```bash
160+
cd integration_tests
161+
./run_tests.py
162+
```
163+
164+
### Windows
132165

133-
### Speed up Integration Test on Macs
166+
- Run integration tests
167+
168+
```bash
169+
python run_tests.py --skip-run-with-dbg
170+
```
171+
172+
- Update reference tests
173+
174+
```bash
175+
python run_tests.py -u --skip-run-with-dbg
176+
```
177+
178+
## Speed up Integration Tests on MacOS
134179

135180
Integration tests run slowly because Apple checks the hash of each
136-
executable online before running. You can turn off that feature
137-
in the Privacy tab of the Security and Privacy item of System
138-
Preferences, Developer Tools, Terminal.app, "allow the apps below
181+
executable online before running.
182+
183+
You can turn off that feature in the Privacy tab of the Security and Privacy item of System Preferences > Developer Tools > Terminal.app > "allow the apps below
139184
to run software locally that does not meet the system's security
140185
policy."
141186

142-
## Examples (Linux or MacOs)
187+
## Examples (Linux or MacOS)
143188

144-
You can run the following examples by hand in a terminal:
189+
You can run the following examples manually in a terminal:
145190

146191
```bash
147192
./src/bin/lpython examples/expr2.py
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# IntrinsicFunction
2+
3+
An intrinsic function. An **expr** node.
4+
5+
## Declaration
6+
7+
### Syntax
8+
9+
```
10+
IntrinsicFunction(expr* args, int intrinsic_id, int overload_id,
11+
ttype type, expr? value)
12+
```
13+
14+
### Arguments
15+
16+
* `args` represents all arguments passed to the function
17+
* `intrinsic_id` is the unique ID of the generic intrinsic function
18+
* `overload_id` is the ID of the signature within the given generic function
19+
* `type` represents the type of the output
20+
* `value` is an optional compile time value
21+
22+
### Return values
23+
24+
The return value is the expression that the `IntrinsicFunction` represents.
25+
26+
## Description
27+
28+
**IntrinsicFunction** represents an intrinsic function (such as `Abs`,
29+
`Modulo`, `Sin`, `Cos`, `LegendreP`, `FlipSign`, ...) that either the backend
30+
or the middle-end (optimizer) needs to have some special logic for. Typically a
31+
math function, but does not have to be.
32+
33+
IntrinsicFunction is both side-effect-free (no writes to global variables) and
34+
deterministic (no reads from global variables). They are also elemental: can be
35+
vectorized over any argument(s). They can be used inside parallel code and
36+
cached.
37+
38+
The `intrinsic_id` determines the generic function uniquely (`Sin` and `Abs`
39+
have different number, but `IntegerAbs` and `RealAbs` share the number) and
40+
`overload_id` uniquely determines the signature starting from 0 for each
41+
generic function (e.g., `IntegerAbs`, `RealAbs` and `ComplexAbs` can have
42+
`overload_id` equal to 0, 1 and 2, and `RealSin`, `ComplexSin` can be 0, 1).
43+
44+
Backend use cases: Some architectures have special hardware instructions for
45+
operations like Sqrt or Sin and if they are faster than a software
46+
implementation, the backend will use it. This includes the `FlipSign` function
47+
which is our own "special function" that the optimizer emits for certain
48+
conditional floating point operations, and the backend emits an efficient bit
49+
manipulation implementation for architectures that support it.
50+
51+
Middle-end use cases: the middle-end can use the high level semantics to
52+
simplify, such as `sin(e)**2 + cos(e)**2 -> 1`, or it could approximate
53+
expressions like `if (abs(sin(x) - 0.5) < 0.3)` with a lower accuracy version
54+
of `sin`.
55+
56+
We provide ASR -> ASR lowering transformations that substitute the given
57+
intrinsic function with an ASR implementation using more primitive ASR nodes,
58+
typically implemented in the surface language (say a `sin` implementation using
59+
argument reduction and a polynomial fit, or a `sqrt` implementation using a
60+
general power formula `x**(0.5)`, or `LegendreP(2,x)` implementation using a
61+
formula `(3*x**2-1)/2`).
62+
63+
This design also makes it possible to allow selecting using command line
64+
options how certain intrinsic functions should be implemented, for example if
65+
trigonometric functions should be implemented using our own fast
66+
implementation, `libm` accurate implementation, we could also call into other
67+
libraries. These choices should happen at the ASR level, and then the result
68+
further optimized (such as inlined) as needed.
69+
70+
## Types
71+
72+
The argument types in `args` have the types of the corresponding signature as
73+
determined by `intrinsic_id`. For example `IntegerAbs` accepts an integer, but
74+
`RealAbs` accepts a real.
75+
76+
## Examples
77+
78+
The following example code creates `IntrinsicFunction` ASR node:
79+
80+
```fortran
81+
sin(0.5)
82+
```
83+
84+
ASR:
85+
86+
```
87+
(TranslationUnit
88+
(SymbolTable
89+
1
90+
{
91+
})
92+
[(IntrinsicFunction
93+
[(RealConstant
94+
0.500000
95+
(Real 4 [])
96+
)]
97+
0
98+
0
99+
(Real 4 [])
100+
(RealConstant 0.479426 (Real 4 []))
101+
)]
102+
)
103+
```
104+
105+
## See Also
106+
107+
[FunctionCall]()

integration_tests/test_builtin_abs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ def test_abs():
3232

3333
b: bool
3434
b = True
35-
assert abs(b) == 1
35+
assert abs(i32(b)) == 1
3636
b = False
37-
assert abs(b) == 0
37+
assert abs(i32(b)) == 0
3838

3939
test_abs()

0 commit comments

Comments
 (0)