Skip to content

Commit

Permalink
Merge branch 'main' into gruntz_demo
Browse files Browse the repository at this point in the history
  • Loading branch information
anutosh491 authored Jul 8, 2024
2 parents f924941 + f414b0f commit 56ea188
Show file tree
Hide file tree
Showing 311 changed files with 3,702 additions and 1,005 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ jobs:
create-args: >-
python=3.10
bison=3.4
symengine=0.11.1
symengine=0.12.0
sympy=1.11.1
- uses: hendrikmuhs/ccache-action@main
Expand Down
14 changes: 14 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,20 @@ if (WITH_LCOMPILERS_FAST_ALLOC)
add_definitions("-DLCOMPILERS_FAST_ALLOC=1")
endif()

# copy runtime files
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/src/runtime/lpython/lpython.py" "${CMAKE_CURRENT_BINARY_DIR}/src/runtime/lpython/lpython.py")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/src/runtime/cmath.py" "${CMAKE_CURRENT_BINARY_DIR}/src/runtime/cmath.py")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/src/runtime/lpython_builtin.py" "${CMAKE_CURRENT_BINARY_DIR}/src/runtime/lpython_builtin.py")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/src/runtime/lpython_intrinsic_numpy.py" "${CMAKE_CURRENT_BINARY_DIR}/src/runtime/lpython_intrinsic_numpy.py")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/src/runtime/lpython_parser.py" "${CMAKE_CURRENT_BINARY_DIR}/src/runtime/lpython_parser.py")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/src/runtime/math.py" "${CMAKE_CURRENT_BINARY_DIR}/src/runtime/math.py")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/src/runtime/os.py" "${CMAKE_CURRENT_BINARY_DIR}/src/runtime/os.py")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/src/runtime/platform.py" "${CMAKE_CURRENT_BINARY_DIR}/src/runtime/platform.py")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/src/runtime/random.py" "${CMAKE_CURRENT_BINARY_DIR}/src/runtime/random.py")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/src/runtime/statistics.py" "${CMAKE_CURRENT_BINARY_DIR}/src/runtime/statistics.py")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/src/runtime/sys.py" "${CMAKE_CURRENT_BINARY_DIR}/src/runtime/sys.py")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/src/runtime/time.py" "${CMAKE_CURRENT_BINARY_DIR}/src/runtime/time.py")

# LLVM
set(WITH_LLVM no CACHE BOOL "Build with LLVM support")
set(WITH_TARGET_AARCH64 no CACHE BOOL "Enable target AARCH64")
Expand Down
25 changes: 13 additions & 12 deletions ci/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@ channels:
- defaults
dependencies:
- llvmdev=11.1.0
- toml
- pytest
- jupyter
- toml=0.10.2
- pytest=7.2.0
- jupyter=1.0.0
- xeus=1.0.1
- xtl
- nlohmann_json
- cppzmq
- jupyter_kernel_test
- xonsh
- re2c
- numpy
- xtl=0.7.4
- nlohmann_json=3.9.1
- cppzmq=4.7.1
- jupyter_kernel_test=0.4.4
- xonsh=0.13.3
- re2c=2.2
- numpy=1.23.4
- zlib
- ninja
- rapidjson
- zstd
- ninja=1.11.0
- rapidjson=1.1.0
# - bison=3.4 [not win]
# - m2-bison=3.4 [win]
5 changes: 4 additions & 1 deletion grammar/Python.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ module LPython
-- need sequences for compare to distinguish between
-- x < 4 < 3 and (x < 4) < 3
| Compare(expr left, cmpop ops, expr* comparators)
| Membership(expr left, membershipop op, expr right)
| Call(expr func, expr* args, keyword* keywords)
| FormattedValue(expr value, int conversion, expr? format_spec)
| JoinedStr(expr* values)
Expand Down Expand Up @@ -110,7 +111,9 @@ module LPython

unaryop = Invert | Not | UAdd | USub

cmpop = Eq | NotEq | Lt | LtE | Gt | GtE | Is | IsNot | In | NotIn
cmpop = Eq | NotEq | Lt | LtE | Gt | GtE | Is | IsNot

membershipop = In | NotIn

comprehension = (expr target, expr iter, expr* ifs, int is_async)

Expand Down
5 changes: 5 additions & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -584,10 +584,13 @@ RUN(NAME test_dict_bool LABELS cpython llvm llvm_jit)
RUN(NAME test_dict_increment LABELS cpython llvm llvm_jit)
RUN(NAME test_dict_keys_values LABELS cpython llvm llvm_jit)
RUN(NAME test_dict_nested1 LABELS cpython llvm llvm_jit)
RUN(NAME test_dict_clear LABELS cpython llvm)
RUN(NAME test_set_len LABELS cpython llvm llvm_jit)
RUN(NAME test_set_add LABELS cpython llvm llvm_jit)
RUN(NAME test_set_remove LABELS cpython llvm llvm_jit)
RUN(NAME test_set_discard LABELS cpython llvm llvm_jit)
RUN(NAME test_set_clear LABELS cpython llvm)
RUN(NAME test_set_pop LABELS cpython llvm)
RUN(NAME test_global_set LABELS cpython llvm llvm_jit)
RUN(NAME test_for_loop LABELS cpython llvm llvm_jit c)
RUN(NAME modules_01 LABELS cpython llvm llvm_jit c wasm wasm_x86 wasm_x64)
Expand All @@ -600,6 +603,7 @@ RUN(NAME test_import_05 LABELS cpython llvm llvm_jit c wasm wasm_x86 wasm_x
RUN(NAME test_import_06 LABELS cpython llvm llvm_jit)
RUN(NAME test_import_07 LABELS cpython llvm llvm_jit c)
RUN(NAME test_math LABELS cpython llvm llvm_jit NOFAST)
RUN(NAME test_membership_01 LABELS cpython llvm)
RUN(NAME test_numpy_01 LABELS cpython llvm llvm_jit c)
RUN(NAME test_numpy_02 LABELS cpython llvm llvm_jit c)
RUN(NAME test_numpy_03 LABELS cpython llvm llvm_jit c)
Expand Down Expand Up @@ -827,6 +831,7 @@ RUN(NAME callback_03 LABELS cpython llvm llvm_jit c)
RUN(NAME lambda_01 LABELS cpython llvm llvm_jit)

RUN(NAME c_mangling LABELS cpython llvm llvm_jit c)
RUN(NAME class_01 LABELS cpython llvm llvm_jit)

# callback_04 is to test emulation. So just run with cpython
RUN(NAME callback_04 IMPORT_PATH .. LABELS cpython)
Expand Down
28 changes: 28 additions & 0 deletions integration_tests/class_01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from lpython import i32,f64
from math import sqrt

class coord:
def __init__(self: "coord"):
self.x: i32 = 3
self.y: i32 = 4

def main():
p1: coord = coord()
sq_dist : i32 = p1.x*p1.x + p1.y*p1.y
dist : f64 = sqrt(f64(sq_dist))
print("Squared Distance from origin = ", sq_dist)
assert sq_dist == 25
print("Distance from origin = ", dist)
assert dist == f64(5)
print("p1.x = 6")
print("p1.y = 8")
p1.x = i32(6)
p1.y = 8
sq_dist = p1.x*p1.x + p1.y*p1.y
dist = sqrt(f64(sq_dist))
print("Squared Distance from origin = ", sq_dist)
assert sq_dist == 100
print("Distance from origin = ", dist)
assert dist == f64(10)

main()
67 changes: 67 additions & 0 deletions integration_tests/loop_12.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@

def test_for_dict_int():
dict_int: dict[i32, i32] = {1:2, 2:3, 3:4}
key: i32
s1: i32 = 0
s2: i32 = 0

for key in dict_int:
print(key)
s1 += key
s2 += dict_int[key]

assert s1 == 6
assert s2 == 9

def test_for_dict_str():
dict_str: dict[str, str] = {"a":"b", "c":"d"}
key: str
s1: str = ""
s2: str = ""

for key in dict_str:
print(key)
s1 += key
s2 += dict_str[key]

assert (s1 == "ac" or s1 == "ca")
assert ((s1 == "ac" and s2 == "bd") or (s1 == "ca" and s2 == "db"))

def test_for_set_int():
set_int: set[i32] = {1, 2, 3}
el: i32
s: i32 = 0

for el in set_int:
print(el)
s += el

assert s == 6

def test_for_set_str():
set_str: set[str] = {'a', 'b'}
el: str
s: str = ""

for el in set_str:
print(el)
s += el

assert (s == "ab" or s == "ba")

def test_nested():
graph: dict[i32, set[i32]] = {1: {2, 3}}
el: i32
s: i32 = 0
for el in graph[1]:
print(el)
s += el

assert s == 5


test_for_dict_int()
test_for_set_int()
test_for_dict_str()
test_for_set_str()
test_nested()
9 changes: 8 additions & 1 deletion integration_tests/symbolics_06.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from sympy import Symbol, sin, cos, exp, log, Abs, pi, diff
from sympy import Symbol, sin, cos, exp, log, Abs, pi, diff, sign
from lpython import S

def test_elementary_functions():
Expand All @@ -25,6 +25,13 @@ def test_elementary_functions():
assert(Abs(S(10)) == S(10))
assert(Abs(S(-1)*x) == Abs(x))

# test sign
assert(sign(S(-10)) == S(-1))
assert(sign(S(0)) == S(0))
assert(sign(S(10)) == S(1))
assert(sign(S(2)* x) == sign(x))
assert(sign(S(-1)* x) == S(-1) * sign(x))

# test composite functions
a: S = exp(x)
b: S = sin(a)
Expand Down
18 changes: 18 additions & 0 deletions integration_tests/test_dict_clear.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
def test_clear():
a: dict[i32, i32] = {1:1, 2:2}

a.clear()
a[3] = 3

assert len(a) == 1
assert 3 in a

b: dict[str, str] = {'a':'a', 'b':'b'}

b.clear()
b['c'] = 'c'

assert len(b) == 1
assert 'c' in b

test_clear()
36 changes: 36 additions & 0 deletions integration_tests/test_membership_01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
def test_int_dict():
a: dict[i32, i32] = {1:2, 2:3, 3:4, 4:5}
i: i32
assert (1 in a)
assert (6 not in a)
i = 4
assert (i in a)

def test_str_dict():
a: dict[str, str] = {'a':'1', 'b':'2', 'c':'3'}
i: str
assert ('a' in a)
assert ('d' not in a)
i = 'c'
assert (i in a)

def test_int_set():
a: set[i32] = {1, 2, 3, 4}
i: i32
assert (1 in a)
assert (6 not in a)
i = 4
assert (i in a)

def test_str_set():
a: set[str] = {'a', 'b', 'c', 'e', 'f'}
i: str
assert ('a' in a)
# assert ('d' not in a)
i = 'c'
assert (i in a)

test_int_dict()
test_str_dict()
test_int_set()
test_str_set()
21 changes: 21 additions & 0 deletions integration_tests/test_set_clear.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def test_clear():
a: set[i32] = {1, 2}

a.clear()
a.add(3)

assert len(a) == 1
a.remove(3)
assert len(a) == 0

b: set[str] = {'a', 'b'}

b.clear()
b.add('c')

assert len(b) == 1
b.remove('c')
assert len(b) == 0


test_clear()
26 changes: 26 additions & 0 deletions integration_tests/test_set_pop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
def set_pop_str():
s: set[str] = {'a', 'b', 'c'}

assert s.pop() in {'a', 'b', 'c'}
assert len(s) == 2
assert s.pop() in {'a', 'b', 'c'}
assert s.pop() in {'a', 'b', 'c'}
assert len(s) == 0

s.add('d')
assert s.pop() == 'd'

def set_pop_int():
s: set[i32] = {1, 2, 3}

assert s.pop() in {1, 2, 3}
assert len(s) == 2
assert s.pop() in {1, 2, 3}
assert s.pop() in {1, 2, 3}
assert len(s) == 0

s.add(4)
assert s.pop() == 4

set_pop_str()
set_pop_int()
Loading

0 comments on commit 56ea188

Please sign in to comment.