Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion integration_tests/array_01_decl.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ltypes import i32, i64, f32, f64, c32, c64
from ltypes import i32, i64, f32, f64, c32, c64, int, float
from numpy import empty

def accept_i32_array(xi32: i32[:]) -> i32:
Expand All @@ -9,6 +9,10 @@ def accept_i64_array(xi64: i64[:]) -> i64:
xi64[0] = 64
return xi64[0]

def accept_int_array(xint: int[:]) -> i64:
xint[0] = 64
return xint[0]

Comment on lines +12 to +15
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works with LPython but not with CPython. CPython throws the following error,

Traceback (most recent call last):
  File "/Users/czgdp1807/lpython_project/lpython/integration_tests/array_01_decl.py", line 12, in <module>
    def accept_int_array(xint: int[:]) -> i64:
TypeError: 'type' object is not subscriptable

So I pushed 8336f96. Using py_float, py_int seems a bit hackish but then the alternative is to not support x: float[:], x: int[:], which makes reduces the significance of x: float use case. Like if users can only declare normal variables and not arrays using floating point annotations then its not worth it to support float annotation. What do you say @certik ?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any other way to achieve this (i.e., make the float[:] work with CPython)?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not something like Array[int]? int[:] might not be in the spirit of PEP 483.

I general, we should never have something that works in LPython and not in CPython. That's an extension of Python and is a road to perdition! The reverse is ok IMO.

Copy link
Copy Markdown
Collaborator

@czgdp1807 czgdp1807 Sep 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Array[int] makes sense. For multi-dimensional arrays, should it look like, Array[Array[int]]? Plus then we should also support Array[Array[f32]] (similarly for all the ltypes).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a more complex issue, so I created #1107 where we can discuss it more. In short: Array[int, :, :] can probably be done, but it's quite verbose, so we should first decide if we want to go this route, in addition (or even instead!) of the short i64[:,:] syntax. Also note that int in CPython means arbitrary precision integer, so we should not use it interchangeably with i64. However, float in CPython means f64, so that could be done, for scalars. For arrays of f64, the same issue happens. See #1107 for more discussion.

def accept_f32_array(xf32: f32[:]) -> f32:
xf32[0] = 32.0
return xf32[0]
Expand All @@ -17,16 +21,28 @@ def accept_f64_array(xf64: f64[:]) -> f64:
xf64[0] = 64.0
return xf64[0]

def accept_float_array(xfloat: float[:]) -> f64:
xfloat[0] = 64.0
return xfloat[0]

def declare_arrays():
ai32: i32[3] = empty(3)
ai64: i64[10] = empty(10)
aint: int[10] = empty(10)
af32: f32[3] = empty(3)
af64: f64[10] = empty(10)
afloat: float[10] = empty(10)
ac32: c32[3] = empty(3)
ac64: c64[10] = empty(10)
print(accept_i32_array(ai32))
print(accept_i64_array(ai64))
print(accept_int_array(aint))
print(accept_i64_array(aint))
print(accept_int_array(ai64))
print(accept_f32_array(af32))
print(accept_f64_array(af64))
print(accept_float_array(afloat))
print(accept_f64_array(afloat))
print(accept_float_array(af64))

declare_arrays()
8 changes: 8 additions & 0 deletions integration_tests/array_02_decl.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ def accept_multidim_i32_array(xi32: i32[:, :]) -> i32:
def accept_multidim_i64_array(xi64: i64[:, :, :]) -> i64:
return xi64[9, 9, 9]

def accept_multidim_i64_array_return_int(xi64: i64[:, :, :]) -> int:
return xi64[9, 9, 9]

def accept_multidim_f32_array(xf32: f32[:]) -> f32:
return xf32[0]

def accept_multidim_f64_array(xf64: f64[:, :]) -> f64:
return xf64[0, 1]

def accept_multidim_f64_array_return_float(xf64: f64[:, :]) -> float:
return xf64[0, 1]

def declare_arrays():
ai32: i32[3, 3] = empty([3, 3])
ai64: i64[10, 10, 10] = empty([10, 10, 10])
Expand All @@ -22,8 +28,10 @@ def declare_arrays():
ac64: c64[10, 13, 11, 16] = empty([10, 13, 11, 16])
print(accept_multidim_i32_array(ai32))
print(accept_multidim_i64_array(ai64))
print(accept_multidim_i64_array_return_int(ai64))
print(accept_multidim_f32_array(af32))
print(accept_multidim_f64_array(af64))
print(accept_multidim_f64_array_return_float(af64))


declare_arrays()
2 changes: 1 addition & 1 deletion integration_tests/array_expr_01.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def array_expr_01():
i: i32
shape1d: i32[1] = empty(1, dtype=int32)
shape3d: i32[3] = empty(3, dtype=int32)
eps: f64
eps: float
eps = 1e-12

dim1 = 10
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/array_expr_02.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ltypes import i32, f32, TypeVar
from ltypes import i32, f32, TypeVar, float
from numpy import empty, sqrt, float32

n: i32
Expand Down
4 changes: 2 additions & 2 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -762,13 +762,13 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
} else if (var_annotation == "i32") {
type = ASRUtils::TYPE(ASR::make_Integer_t(al, loc,
4, dims.p, dims.size()));
} else if (var_annotation == "i64") {
} else if (var_annotation == "i64" || var_annotation == "int") {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This we should not do, since int means arbitrary precision integer, which we will eventually support in LPython as well, but that is not the same as f64.

type = ASRUtils::TYPE(ASR::make_Integer_t(al, loc,
8, dims.p, dims.size()));
} else if (var_annotation == "f32") {
type = ASRUtils::TYPE(ASR::make_Real_t(al, loc,
4, dims.p, dims.size()));
} else if (var_annotation == "f64") {
} else if (var_annotation == "f64" || var_annotation == "float") {
type = ASRUtils::TYPE(ASR::make_Real_t(al, loc,
8, dims.p, dims.size()));
} else if (var_annotation == "c32") {
Expand Down
19 changes: 16 additions & 3 deletions src/runtime/ltypes/ltypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
# TODO: this does not seem to restrict other imports
__slots__ = ["i8", "i16", "i32", "i64", "f32", "f64", "c32", "c64", "CPtr",
"overload", "ccall", "TypeVar", "pointer", "c_p_pointer", "Pointer",
"p_c_pointer", "vectorize", "inline"]
"p_c_pointer", "vectorize", "inline", "float", "int"]

py_float = type(0.1)
py_int = type(1)

# data-types

Expand All @@ -19,6 +22,14 @@ def __init__(self, name):
def __getitem__(self, params):
return Array(self, params)

def __call__(self, arg):
if self._name == "float":
return py_float(arg)
elif self._name == "int":
return py_int(arg)
raise Exception("Type: %s cannot be called" % str(self._name))


class Pointer:
def __getitem__(self, type):
return type
Expand All @@ -37,6 +48,8 @@ def __init__(self, type, dims):
c32 = Type("c32")
c64 = Type("c64")
CPtr = Type("c_ptr")
int = Type("int")
float = Type("float")

# Restrictions

Expand All @@ -63,9 +76,9 @@ def ltype(x):
"""
Converts CPython types to LPython types
"""
if type(x) == int:
if type(x) == py_int:
return i32, i64
elif type(x) == float:
elif type(x) == py_float:
return f32, f64
elif type(x) == complex:
return c32, c64
Expand Down
4 changes: 4 additions & 0 deletions tests/dict_expr_01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def test_dict():
mydictionarty3: dict[int, float] = {}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is currently not supported in LPython, as we do not allow arbitrary precision integers.


test_dict()
4 changes: 4 additions & 0 deletions tests/errors/test_dict13.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def test_dict():
mydictionarty3: dict[float, int] = {}

test_dict()
4 changes: 2 additions & 2 deletions tests/reference/asr-array_01_decl-39cf894.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"basename": "asr-array_01_decl-39cf894",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/../integration_tests/array_01_decl.py",
"infile_hash": "e5f2b6082b67a8a7847e7051d7b4d3c954d1ea88d865326810543dca",
"infile_hash": "aff2044be01be58d46846039e73055f75af986a6b927c110b46028c8",
"outfile": null,
"outfile_hash": null,
"stdout": "asr-array_01_decl-39cf894.stdout",
"stdout_hash": "c28cc5ab9eeaa418b37fb8a3d48be3afe9ee53f1a798c002c1872410",
"stdout_hash": "cb1e8ec9995c8e1ba7f77241620a3f2b297925721128ee961fb3a67f",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-array_01_decl-39cf894.stdout

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions tests/reference/asr-array_02_decl-e8f6874.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"basename": "asr-array_02_decl-e8f6874",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/../integration_tests/array_02_decl.py",
"infile_hash": "49bfd0530662b64164eb87c7e1bc53d518a7ccc75469dfb7ced80e5f",
"infile_hash": "a3cd5bb595c7768034df0496eadc70bdf484cd56eeb28fbe4b465b09",
"outfile": null,
"outfile_hash": null,
"stdout": "asr-array_02_decl-e8f6874.stdout",
"stdout_hash": "f4d6d5213a91087f4c3542434b6b73f4d10c40f873b3b469bf298967",
"stdout_hash": "cecd7a33f29f74049dd90b9bc13f8005ad8f0db3cf2bd9ba26a0f3e5",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-array_02_decl-e8f6874.stdout

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions tests/reference/asr-dict_expr_01-9c1f5d8.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "asr-dict_expr_01-9c1f5d8",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/dict_expr_01.py",
"infile_hash": "e31da5228b0722933947559f021553ddf900cedbfeecc93fa71d5761",
"outfile": null,
"outfile_hash": null,
"stdout": "asr-dict_expr_01-9c1f5d8.stdout",
"stdout_hash": "60af4235ccd42a13ac0b6ce36d8814e168286fb51a1ef2660d3b8339",
"stderr": null,
"stderr_hash": null,
"returncode": 0
}
1 change: 1 addition & 0 deletions tests/reference/asr-dict_expr_01-9c1f5d8.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(TranslationUnit (SymbolTable 1 {_lpython_main_program: (Function (SymbolTable 4 {}) _lpython_main_program [] [] [(SubroutineCall 1 test_dict () [] ())] () Source Public Implementation () .false. .false. .false. .false.), main_program: (Program (SymbolTable 3 {}) main_program [] [(SubroutineCall 1 _lpython_main_program () [] ())]), test_dict: (Function (SymbolTable 2 {mydictionarty3: (Variable 2 mydictionarty3 Local () () Default (Dict (Integer 8 []) (Real 8 [])) Source Public Required .false.)}) test_dict [] [] [(= (Var 2 mydictionarty3) (DictConstant [] [] (Dict (Integer 8 []) (Real 8 []))) ())] () Source Public Implementation () .false. .false. .false. .false.)}) [])
9 changes: 9 additions & 0 deletions tests/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ ast = true
asr = true
llvm = true

[[test]]
filename = "dict_expr_01.py"
asr = true

[[test]]
filename = "../integration_tests/array_01_decl.py"
asr = true
Expand Down Expand Up @@ -707,6 +711,11 @@ asr = true
filename = "errors/test_dict1.py"
asr = true

# Enable once gh-1080 is merged
# [[test]]
# filename = "errors/test_dict13.py"
# asr = true

[[test]]
filename = "errors/test_zero_division.py"
asr = true
Expand Down