Skip to content

Commit

Permalink
Merge branch 'main' into pr_type_comment
Browse files Browse the repository at this point in the history
  • Loading branch information
akshanshbhatt committed Jul 12, 2022
2 parents c5aaba3 + 0fdbc50 commit 6c6b6a8
Show file tree
Hide file tree
Showing 141 changed files with 3,507 additions and 877 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ jobs:
- name: Install Windows Conda Packages
if: contains(matrix.os, 'windows')
shell: bash -l {0}
run: conda install m2-bison=3.0.4 ninja
run: conda install m2-bison=3.0.4 ninja rapidjson

- name: Install Linux / macOS Conda Packages
if: contains(matrix.os, 'ubuntu') || contains(matrix.os, 'macos')
shell: bash -l {0}
run: conda install bison=3.4
run: conda install bison=3.4 rapidjson

- name: Conda info
shell: bash -l {0}
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -188,5 +188,9 @@ integration_tests/structs_02
integration_tests/structs_02.c
integration_tests/structs_03
integration_tests/structs_03.c
integration_tests/structs_05
integration_tests/structs_05.c
integration_tests/expr_08
integration_tests/expr_08.c
integration_tests/expr_12
integration_tests/expr_12.c
7 changes: 5 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,12 @@ if (WITH_XEUS)
PATTERN "*.in" EXCLUDE)
endif()

# JSON
# JSON OR LSP (`conda install rapidjson`)
set(WITH_JSON no CACHE BOOL "Build with JSON support")
if (WITH_JSON)
set(WITH_LSP no CACHE BOOL "Build with LSP support")
if (WITH_JSON OR WITH_LSP)
find_package(RapidJSON REQUIRED)
set(HAVE_LFORTRAN_RAPIDJSON yes)
endif()

set(HAVE_LFORTRAN_DEMANGLE yes
Expand Down Expand Up @@ -262,6 +264,7 @@ message("HAVE_LFORTRAN_DEMANGLE: ${HAVE_LFORTRAN_DEMANGLE}")
message("WITH_LLVM: ${WITH_LLVM}")
message("WITH_XEUS: ${WITH_XEUS}")
message("WITH_JSON: ${WITH_JSON}")
message("WITH_LSP: ${WITH_LSP}")
message("WITH_FMT: ${WITH_FMT}")
message("WITH_LFORTRAN_BINARY_MODFILES: ${WITH_LFORTRAN_BINARY_MODFILES}")
message("WITH_RUNTIME_LIBRARY: ${WITH_RUNTIME_LIBRARY}")
Expand Down
1 change: 1 addition & 0 deletions build1.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ cmake \
-DWITH_LLVM=yes \
-DLPYTHON_BUILD_ALL=yes \
-DWITH_STACKTRACE=yes \
-DWITH_LSP=no \
-DWITH_LFORTRAN_BINARY_MODFILES=no \
-DCMAKE_PREFIX_PATH="$CMAKE_PREFIX_PATH_LPYTHON;$CONDA_PREFIX" \
-DCMAKE_INSTALL_PREFIX=`pwd`/inst \
Expand Down
2 changes: 1 addition & 1 deletion ci/build.xsh
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ cd test-bld
# compiled in Release mode and we get link failures if we mix and match build
# modes:
BUILD_TYPE = "Release"
cmake -G $LFORTRAN_CMAKE_GENERATOR -DCMAKE_VERBOSE_MAKEFILE=ON -DWITH_LLVM=yes -DWITH_XEUS=yes -DCMAKE_PREFIX_PATH=$CONDA_PREFIX -DCMAKE_INSTALL_PREFIX=$CONDA_PREFIX -DWITH_LFORTRAN_BINARY_MODFILES=no -DCMAKE_BUILD_TYPE=@(BUILD_TYPE) ..
cmake -G $LFORTRAN_CMAKE_GENERATOR -DCMAKE_VERBOSE_MAKEFILE=ON -DWITH_LLVM=yes -DWITH_LSP=yes -DWITH_XEUS=yes -DCMAKE_PREFIX_PATH=$CONDA_PREFIX -DCMAKE_INSTALL_PREFIX=$CONDA_PREFIX -DWITH_LFORTRAN_BINARY_MODFILES=no -DCMAKE_BUILD_TYPE=@(BUILD_TYPE) ..
cmake --build . --target install
./src/lpython/tests/test_lpython
#./src/bin/lpython < ../src/bin/example_input.txt
Expand Down
100 changes: 93 additions & 7 deletions grammar/asdl_cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -684,9 +684,8 @@ def visitModule(self, mod):
self.emit("public:")
self.emit(" bool success;")
self.emit(" bool allow_procedure_calls;")
self.emit(" bool allow_return_stmts;")
self.emit("")
self.emit(" ExprStmtDuplicator(Allocator& al_) : al(al_), success(false), allow_procedure_calls(true), allow_return_stmts(false) {}")
self.emit(" ExprStmtDuplicator(Allocator& al_) : al(al_), success(false), allow_procedure_calls(true) {}")
self.emit("")
self.duplicate_stmt.append((" ASR::stmt_t* duplicate_stmt(ASR::stmt_t* x) {", 0))
self.duplicate_stmt.append((" if( !x ) {", 1))
Expand Down Expand Up @@ -765,11 +764,6 @@ def make_visitor(self, name, fields):
self.duplicate_stmt.append((" success = false;", 4))
self.duplicate_stmt.append((" return nullptr;", 4))
self.duplicate_stmt.append((" }", 3))
elif name == "Return":
self.duplicate_stmt.append((" if( !allow_return_stmts ) {", 3))
self.duplicate_stmt.append((" success = false;", 4))
self.duplicate_stmt.append((" return nullptr;", 4))
self.duplicate_stmt.append((" }", 3))
self.duplicate_stmt.append((" return down_cast<ASR::stmt_t>(duplicate_%s(down_cast<ASR::%s_t>(x)));" % (name, name), 3))
self.duplicate_stmt.append((" }", 2))
elif self.is_expr:
Expand Down Expand Up @@ -919,6 +913,96 @@ def visitField(self, field):
self.emit("self().replace_%s(x->m_%s);" % (field.type, field.name), level)
self.emit("current_expr = current_expr_copy;", level)

class StmtBaseReplacerVisitor(ASDLVisitor):

def __init__(self, stream, data):
self.replace_stmt = []
self.is_stmt = False
self.is_product = False
super(StmtBaseReplacerVisitor, self).__init__(stream, data)

def visitModule(self, mod):
self.emit("/" + "*"*78 + "/")
self.emit("// Statement Replacer Base class")
self.emit("")
self.emit("template <class Derived>")
self.emit("class BaseStmtReplacer {")
self.emit("public:")
self.emit(" Derived& self() { return static_cast<Derived&>(*this); }")
self.emit("")
self.emit(" ASR::stmt_t** current_stmt;")
self.emit(" ASR::stmt_t** current_stmt_copy;")
self.emit(" bool has_replacement_happened;")
self.emit("")
self.emit(" BaseStmtReplacer() : current_stmt(nullptr), has_replacement_happened(false) {}")
self.emit("")

self.replace_stmt.append((" void replace_stmt(ASR::stmt_t* x) {", 0))
self.replace_stmt.append((" if( !x ) {", 1))
self.replace_stmt.append((" return ;", 2))
self.replace_stmt.append((" }", 1))
self.replace_stmt.append(("", 0))
self.replace_stmt.append((" switch(x->type) {", 1))

super(StmtBaseReplacerVisitor, self).visitModule(mod)

self.replace_stmt.append((" default: {", 2))
self.replace_stmt.append((' LFORTRAN_ASSERT_MSG(false, "Replacement of " + std::to_string(x->type) + " statement is not supported yet.");', 3))
self.replace_stmt.append((" }", 2))
self.replace_stmt.append((" }", 1))
self.replace_stmt.append(("", 0))
self.replace_stmt.append((" }", 0))
for line, level in self.replace_stmt:
self.emit(line, level=level)
self.emit("")
self.emit("};")

def visitType(self, tp):
if not (isinstance(tp.value, asdl.Sum) and
is_simple_sum(tp.value)):
super(StmtBaseReplacerVisitor, self).visitType(tp, tp.name)

def visitSum(self, sum, *args):
self.is_stmt = args[0] == 'stmt'
if self.is_stmt:
for tp in sum.types:
self.visit(tp, *args)

def visitProduct(self, prod, name):
pass

def visitConstructor(self, cons, _):
self.make_visitor(cons.name, cons.fields)

def make_visitor(self, name, fields):
self.emit("")
self.emit("void replace_%s(%s_t* x) {" % (name, name), 1)
self.used = False
for field in fields:
self.visitField(field)
if not self.used:
self.emit("if (x) { }", 2)

if self.is_stmt:
self.replace_stmt.append((" case ASR::stmtType::%s: {" % name, 2))
self.replace_stmt.append((" self().replace_%s(down_cast<ASR::%s_t>(x));" % (name, name), 3))
self.replace_stmt.append((" break;", 3))
self.replace_stmt.append((" }", 2))
self.emit("}", 1)
self.emit("")

def visitField(self, field):
arguments = None
if field.type == "stmt":
level = 2
if field.seq:
self.used = True
self.emit("for (size_t i = 0; i < x->n_%s; i++) {" % field.name, level)
self.emit(" current_stmt_copy = current_stmt;", level)
self.emit(" current_stmt = &(x->m_%s[i]);" % (field.name), level)
self.emit(" self().replace_stmt(x->m_%s[i]);"%(field.name), level)
self.emit(" current_stmt = current_stmt_copy;", level)
self.emit("}", level)

class PickleVisitorVisitor(ASDLVisitor):

Expand Down Expand Up @@ -1973,6 +2057,8 @@ def main(argv):
fp.write("\n\n")
ExprBaseReplacerVisitor(fp, data).visit(mod)
fp.write("\n\n")
StmtBaseReplacerVisitor(fp, data).visit(mod)
fp.write("\n\n")
ExprTypeVisitor(fp, data).visit(mod)
fp.write("\n\n")
ExprValueVisitor(fp, data).visit(mod)
Expand Down
2 changes: 2 additions & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ RUN(NAME expr_08 LABELS llvm c)
RUN(NAME expr_09 LABELS cpython llvm)
RUN(NAME expr_10 LABELS cpython llvm)
RUN(NAME expr_11 LABELS cpython llvm c)
RUN(NAME expr_12 LABELS llvm c)
RUN(NAME test_types_01 LABELS cpython llvm)
RUN(NAME test_str_01 LABELS cpython llvm)
RUN(NAME test_str_02 LABELS cpython llvm)
Expand Down Expand Up @@ -183,6 +184,7 @@ RUN(NAME structs_01 LABELS cpython llvm c)
RUN(NAME structs_02 LABELS llvm c)
RUN(NAME structs_03 LABELS llvm c)
RUN(NAME structs_04 LABELS cpython llvm c)
RUN(NAME structs_05 LABELS llvm c)
RUN(NAME test_str_to_int LABELS cpython llvm)
RUN(NAME test_platform LABELS cpython llvm)
RUN(NAME test_vars_01 LABELS cpython llvm)
Expand Down
19 changes: 19 additions & 0 deletions integration_tests/expr_12.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from ltypes import pointer, i16, Pointer

def g(x: Pointer[i16[:]], y: i16[:]):
y[0] = 1
y[1] = 2
x = pointer(y)
print(x[0], x[1])

def check(ptr: Pointer[i16[:]]):
assert ptr[0] == 1
assert ptr[1] == 2

def f():
yptr1: Pointer[i16[:]]
y: i16[2]
g(yptr1, y)
check(yptr1)

f()
6 changes: 3 additions & 3 deletions integration_tests/func_inline_01.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
def fib(n: i64) -> i64:
if n < 2:
return n
else:
return fib(n - 1) + fib(n - 2)
return fib(n - 1) + fib(n - 2)

def main():
ans: i64
x: i64
x = 8
x = 40
ans = fib(x)
print(ans)
assert ans == 102334155

main()
36 changes: 36 additions & 0 deletions integration_tests/structs_05.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from ltypes import i32, f64, dataclass

@dataclass
class A:
y: f64
x: i32

def verify(s: A[:], x1: i32, y1: f64, x2: i32, y2: f64):
eps: f64 = 1e-12
print(s[0].x, s[0].y)
assert s[0].x == x1
assert abs(s[0].y - y1) < eps
print(s[1].x, s[1].y)
assert s[1].x == x2
assert abs(s[1].y - y2) < eps

def update_1(s: A):
s.x = 2
s.y = 1.2

def update_2(s: A[:]):
s[1].x = 3
s[1].y = 2.3

def g():
# TODO: Replace y: A[2] with y: A[2] = [None, None]
# TODO: And enable cpython in integration_tests.
y: A[2]
y[0] = A(1.1, 1)
y[1] = A(2.2, 2)
verify(y, 1, 1.1, 2, 2.2)
update_1(y[0])
update_2(y)
verify(y, 2, 1.2, 3, 2.3)

g()
3 changes: 3 additions & 0 deletions integration_tests/test_builtin_bool.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ def test_bool():
f2 = -235.6
assert bool(f2)

f2 = 0.0000534
assert bool(f2)

s: str
s = ""
assert not bool(s)
Expand Down
10 changes: 8 additions & 2 deletions integration_tests/test_builtin_float.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ltypes import i32
from ltypes import i32, f64

def test_float():
i: i32
Expand All @@ -12,6 +12,12 @@ def test_float():
assert float(-4235) == -4235.0
assert float(True) == 1.0
assert float(False) == 0.0

b: bool
b = True
f: f64
f = float(b)
assert f == 1.0
b = False
assert b == 0.0

test_float()
13 changes: 13 additions & 0 deletions integration_tests/test_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,25 @@ def test_complex_unary_minus():
assert abs(c2.real - (-3.0)) < 1e-12
assert abs(c2.imag - (-4.0)) < 1e-12

def test_complex_not():
c: c32
c = complex(4, 5)
b: bool
b = not c
assert not b

c2: c64
c2 = complex(0, 0)
b = not c2
assert b

def check():
test_real_imag()
test_complex()
test_complex_abs()
test_complex_binop_32()
test_complex_binop_64()
test_complex_unary_minus()
test_complex_not()

check()
Loading

0 comments on commit 6c6b6a8

Please sign in to comment.