-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
195 changed files
with
5,648 additions
and
2,729 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from enum import Enum | ||
|
||
from lpython import CPtr, c_p_pointer, p_c_pointer, dataclass, empty_c_void_p, pointer, Pointer, i32, ccallable | ||
|
||
class Value(Enum): | ||
TEN: i32 = 10 | ||
TWO: i32 = 2 | ||
ONE: i32 = 1 | ||
FIVE: i32 = 5 | ||
|
||
@ccallable | ||
@dataclass | ||
class Foo: | ||
value: Value | ||
|
||
@ccallable | ||
@dataclass | ||
class FooC: | ||
value: Value | ||
|
||
def bar(foo_ptr: CPtr) -> None: | ||
foo: Pointer[Foo] = c_p_pointer(foo_ptr, Foo) | ||
foo.value = Value.FIVE | ||
|
||
def barc(foo_ptr: CPtr) -> None: | ||
foo: Pointer[FooC] = c_p_pointer(foo_ptr, FooC) | ||
foo.value = Value.ONE | ||
|
||
def main() -> None: | ||
foo: Foo = Foo(Value.TEN) | ||
fooc: FooC = FooC(Value.TWO) | ||
foo_ptr: CPtr = empty_c_void_p() | ||
|
||
p_c_pointer(pointer(foo), foo_ptr) | ||
bar(foo_ptr) | ||
print(foo.value) | ||
assert foo.value == Value.FIVE.value | ||
|
||
p_c_pointer(pointer(fooc), foo_ptr) | ||
barc(foo_ptr) | ||
print(fooc.value) | ||
assert fooc.value == Value.ONE.value | ||
|
||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from lpython import (i64, i16, CPtr, c_p_pointer, Pointer, sizeof, packed, | ||
dataclass, ccallable, ccall, i32) | ||
|
||
@ccall | ||
def _lfortran_malloc(size: i32) -> CPtr: | ||
pass | ||
|
||
|
||
def alloc(buf_size:i64) -> CPtr: | ||
return _lfortran_malloc(i32(buf_size)) | ||
|
||
|
||
@ccallable | ||
@packed | ||
@dataclass | ||
class S: | ||
a: i16 | ||
b: i64 | ||
|
||
|
||
def main(): | ||
p1: CPtr = alloc(sizeof(S)) | ||
print(p1) | ||
p2: Pointer[S] = c_p_pointer(p1, S) | ||
p2.a = i16(5) | ||
p2.b = i64(4) | ||
print(p2.a, p2.b) | ||
assert p2.a == i16(5) | ||
assert p2.b == i64(4) | ||
|
||
|
||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import numpy, ctypes | ||
from lpython import (i64, i16, CPtr, c_p_pointer, Pointer, sizeof, packed, | ||
dataclass, ccallable, ccall, i32) | ||
|
||
global_arrays = [] | ||
|
||
|
||
def alloc(buf_size:i64) -> CPtr: | ||
xs = numpy.empty(buf_size, dtype=numpy.uint8) | ||
global_arrays.append(xs) | ||
p = ctypes.c_void_p(xs.ctypes.data) | ||
return ctypes.cast(p.value, ctypes.c_void_p) | ||
|
||
|
||
@ccallable | ||
@packed | ||
@dataclass | ||
class S: | ||
a: i16 | ||
b: i64 | ||
|
||
|
||
def main(): | ||
p1: CPtr = alloc(sizeof(S)) | ||
print(p1) | ||
p2: Pointer[S] = c_p_pointer(p1, S) | ||
p2.a = i16(5) | ||
p2.b = i64(4) | ||
print(p2.a, p2.b) | ||
assert p2.a == i16(5) | ||
assert p2.b == i64(4) | ||
|
||
|
||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from lpython import i32, u8, u32, dataclass | ||
from numpy import empty, uint8 | ||
|
||
@dataclass | ||
class LPBHV_small: | ||
dim : i32 = 4 | ||
a : u8[4] = empty(4, dtype=uint8) | ||
|
||
def main0(): | ||
lphv_small : LPBHV_small = LPBHV_small() | ||
i: i32 | ||
for i in range(4): | ||
lphv_small.a[i] = u8(10 + i) | ||
elt: u32 = u32(lphv_small.a[i]) | ||
print(elt) | ||
assert elt == u32(10 + i) | ||
|
||
main0() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
from lpython import u8, u16, u32, u64 | ||
|
||
def test_01(): | ||
x : u32 = u32(10) | ||
print(x) | ||
assert x == u32(10) | ||
|
||
y: u16 = u16(x) | ||
print(y) | ||
assert y == u16(10) | ||
|
||
z: u64 = u64(y) | ||
print(z) | ||
assert z == u64(10) | ||
|
||
w: u8 = u8(z) | ||
print(w) | ||
assert w == u8(10) | ||
|
||
def test_02(): | ||
x : u64 = u64(11) | ||
print(x) | ||
assert x == u64(11) | ||
|
||
y: u8 = u8(x) | ||
print(y) | ||
assert y == u8(11) | ||
|
||
z: u16 = u16(y) | ||
print(z) | ||
assert z == u16(11) | ||
|
||
w: u32 = u32(z) | ||
print(w) | ||
assert w == u32(11) | ||
|
||
def test_03(): | ||
x : u32 = u32(-10) | ||
print(x) | ||
assert x == u32(4294967286) | ||
|
||
y: u16 = u16(x) | ||
print(y) | ||
assert y == u16(65526) | ||
|
||
z: u64 = u64(y) | ||
print(z) | ||
assert z == u64(65526) | ||
|
||
w: u8 = u8(z) | ||
print(w) | ||
assert w == u8(246) | ||
|
||
def test_04(): | ||
x : u64 = u64(-11) | ||
print(x) | ||
# TODO: We are unable to store the following u64 in AST/R | ||
# assert x == u64(18446744073709551605) | ||
|
||
y: u8 = u8(x) | ||
print(y) | ||
assert y == u8(245) | ||
|
||
z: u16 = u16(y) | ||
print(z) | ||
assert z == u16(245) | ||
|
||
w: u32 = u32(z) | ||
print(w) | ||
assert w == u32(245) | ||
|
||
|
||
def main0(): | ||
test_01() | ||
test_02() | ||
test_03() | ||
test_04() | ||
|
||
main0() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from lpython import (f64,) | ||
|
||
result : f64 = f64(14) | ||
divisor : f64 = f64(4) | ||
result /= divisor | ||
|
||
assert abs(result - f64(3.5)) < 1e-12 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from lpython import (i32, f32, dataclass, CPtr, Pointer, c_p_pointer, pointer, | ||
ccallable, empty_c_void_p, f64, ccall, sizeof, i64) | ||
|
||
@ccall | ||
def _lfortran_malloc(size: i32) -> CPtr: | ||
pass | ||
|
||
def alloc(buf_size:i64) -> CPtr: | ||
return _lfortran_malloc(i32(buf_size)) | ||
|
||
@ccallable | ||
@dataclass | ||
class A: | ||
x: i32 | ||
y: f32 | ||
|
||
@ccallable | ||
def f(a: CPtr) -> None: | ||
x: i32 | ||
y: f32 | ||
a1: A = A(3, f32(3.25)) | ||
a2: Pointer[A] | ||
a2 = pointer(a1) | ||
print(a2, pointer(a1)) | ||
# TODO: does not work: | ||
#x = a2.x | ||
#y = a2.y | ||
#assert x == 3 | ||
#assert f64(y) == 3.25 | ||
a2 = c_p_pointer(a, A) | ||
print(a, a2, pointer(a1)) | ||
print(a2.x, a2.y) | ||
assert a2.x == 5 | ||
assert a2.y == f32(6.0) | ||
|
||
def g(): | ||
b: CPtr = alloc(sizeof(A)) | ||
b2: Pointer[A] = c_p_pointer(b, A) | ||
b2.x = 5 | ||
b2.y = f32(6) | ||
f(b) | ||
|
||
g() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.