#include #include #include #include #include #include #include #include #include using LCompilers::TRY; using LCompilers::PythonCompiler; using LCompilers::CompilerOptions; TEST_CASE("llvm 1") { //std::cout << "LLVM Version:" << std::endl; //LCompilers::LLVMEvaluator::print_version_message(); LCompilers::LLVMEvaluator e; e.add_module(R"""( define i64 @f1() { ret i64 4 } )"""); CHECK(e.execfn("f1") == 4); e.add_module(""); //CHECK(e.execfn("f1") == 4); e.add_module(R"""( define i64 @f2() { ret i64 5 } )"""); CHECK(e.execfn("f2") == 5); //e.add_module(""); //CHECK(e.execfn("f2") == 5); } TEST_CASE("llvm 1 fail") { LCompilers::LLVMEvaluator e; CHECK_THROWS_AS(e.add_module(R"""( define i64 @f1() { ; FAIL: "=x" is incorrect syntax %1 =x alloca i64 } )"""), LCompilers::LCompilersException); CHECK_THROWS_WITH(e.add_module(R"""( define i64 @f1() { ; FAIL: "=x" is incorrect syntax %1 =x alloca i64 } )"""), "parse_module(): Invalid LLVM IR"); } TEST_CASE("llvm 2") { LCompilers::LLVMEvaluator e; e.add_module(R"""( @count = global i64 0 define i64 @f1() { store i64 4, i64* @count %1 = load i64, i64* @count ret i64 %1 } )"""); CHECK(e.execfn("f1") == 4); e.add_module(R"""( @count = external global i64 define i64 @f2() { %1 = load i64, i64* @count ret i64 %1 } )"""); CHECK(e.execfn("f2") == 4); CHECK_THROWS_AS(e.add_module(R"""( define i64 @f3() { ; FAIL: @count is not defined %1 = load i64, i64* @count ret i64 %1 } )"""), LCompilers::LCompilersException); } TEST_CASE("llvm 3") { LCompilers::LLVMEvaluator e; e.add_module(R"""( @count = global i64 5 )"""); e.add_module(R"""( @count = external global i64 define i64 @f1() { %1 = load i64, i64* @count ret i64 %1 } define void @inc() { %1 = load i64, i64* @count %2 = add i64 %1, 1 store i64 %2, i64* @count ret void } )"""); CHECK(e.execfn("f1") == 5); /* e.execfn("inc"); CHECK(e.execfn("f1") == 6); e.execfn("inc"); CHECK(e.execfn("f1") == 7); */ /* e.add_module(R"""( @count = external global i64 define void @inc2() { %1 = load i64, i64* @count %2 = add i64 %1, 2 store i64 %2, i64* @count ret void } )"""); CHECK(e.execfn("f1") == 7); e.execfn("inc2"); CHECK(e.execfn("f1") == 9); e.execfn("inc"); CHECK(e.execfn("f1") == 10); e.execfn("inc2"); CHECK(e.execfn("f1") == 12); */ // Test that we can have another independent LLVMEvaluator and use both at // the same time: /* LCompilers::LLVMEvaluator e2; e2.add_module(R"""( @count = global i64 5 define i64 @f1() { %1 = load i64, i64* @count ret i64 %1 } define void @inc() { %1 = load i64, i64* @count %2 = add i64 %1, 1 store i64 %2, i64* @count ret void } )"""); CHECK(e2.execfn("f1") == 5); e2.execfn("inc"); CHECK(e2.execfn("f1") == 6); e2.execfn("inc"); CHECK(e2.execfn("f1") == 7); CHECK(e.execfn("f1") == 12); e2.execfn("inc"); CHECK(e2.execfn("f1") == 8); CHECK(e.execfn("f1") == 12); e.execfn("inc"); CHECK(e2.execfn("f1") == 8); CHECK(e.execfn("f1") == 13); */ } TEST_CASE("llvm 4") { LCompilers::LLVMEvaluator e; e.add_module(R"""( @count = global i64 5 define i64 @f1() { %1 = load i64, i64* @count ret i64 %1 } define void @inc() { %1 = load i64, i64* @count %2 = add i64 %1, 1 store i64 %2, i64* @count ret void } )"""); CHECK(e.execfn("f1") == 5); /* e.execfn("inc"); CHECK(e.execfn("f1") == 6); e.execfn("inc"); CHECK(e.execfn("f1") == 7); e.add_module(R"""( declare void @inc() define void @inc2() { call void @inc() call void @inc() ret void } )"""); CHECK(e.execfn("f1") == 7); e.execfn("inc2"); CHECK(e.execfn("f1") == 9); e.execfn("inc"); CHECK(e.execfn("f1") == 10); e.execfn("inc2"); CHECK(e.execfn("f1") == 12); CHECK_THROWS_AS(e.add_module(R"""( define void @inc2() { ; FAIL: @inc is not defined call void @inc() call void @inc() ret void } )"""), LCompilers::LCompilersException); */ } TEST_CASE("llvm array 1") { LCompilers::LLVMEvaluator e; e.add_module(R"""( ; Sum the three elements in %a define i64 @sum3(i64* %a) { %a1addr = getelementptr i64, i64* %a, i64 0 %a1 = load i64, i64* %a1addr %a2addr = getelementptr i64, i64* %a, i64 1 %a2 = load i64, i64* %a2addr %a3addr = getelementptr i64, i64* %a, i64 2 %a3 = load i64, i64* %a3addr %tmp = add i64 %a2, %a1 %r = add i64 %a3, %tmp ret i64 %r } define i64 @f() { %a = alloca [3 x i64] %a1 = getelementptr [3 x i64], [3 x i64]* %a, i64 0, i64 0 store i64 1, i64* %a1 %a2 = getelementptr [3 x i64], [3 x i64]* %a, i64 0, i64 1 store i64 2, i64* %a2 %a3 = getelementptr [3 x i64], [3 x i64]* %a, i64 0, i64 2 store i64 3, i64* %a3 %r = call i64 @sum3(i64* %a1) ret i64 %r } )"""); CHECK(e.execfn("f") == 6); } TEST_CASE("llvm array 2") { LCompilers::LLVMEvaluator e; e.add_module(R"""( %array = type {i64, [3 x i64]} ; Sum the three elements in %a define i64 @sum3(%array* %a) { %a1addr = getelementptr %array, %array* %a, i64 0, i32 1, i64 0 %a1 = load i64, i64* %a1addr %a2addr = getelementptr %array, %array* %a, i64 0, i32 1, i64 1 %a2 = load i64, i64* %a2addr %a3addr = getelementptr %array, %array* %a, i64 0, i32 1, i64 2 %a3 = load i64, i64* %a3addr %tmp = add i64 %a2, %a1 %r = add i64 %a3, %tmp ret i64 %r } define i64 @f() { %a = alloca %array %idx0 = getelementptr %array, %array* %a, i64 0, i32 0 store i64 3, i64* %idx0 %idx1 = getelementptr %array, %array* %a, i64 0, i32 1, i64 0 store i64 1, i64* %idx1 %idx2 = getelementptr %array, %array* %a, i64 0, i32 1, i64 1 store i64 2, i64* %idx2 %idx3 = getelementptr %array, %array* %a, i64 0, i32 1, i64 2 store i64 3, i64* %idx3 %r = call i64 @sum3(%array* %a) ret i64 %r } )"""); //CHECK(e.execfn("f") == 6); } int f(int a, int b) { return a+b; } TEST_CASE("llvm callback 0") { LCompilers::LLVMEvaluator e; std::string addr = std::to_string((int64_t)f); e.add_module(R"""( define i64 @addrcaller(i64 %a, i64 %b) { %f = inttoptr i64 )""" + addr + R"""( to i64 (i64, i64)* %r = call i64 %f(i64 %a, i64 %b) ret i64 %r } define i64 @f1() { %r = call i64 @addrcaller(i64 2, i64 3) ret i64 %r } )"""); CHECK(e.execfn("f1") == 5); } // Tests passing the complex struct by reference TEST_CASE("llvm complex type") { LCompilers::LLVMEvaluator e; e.add_module(R"""( %complex = type { float, float } define float @sum2(%complex* %a) { %a1addr = getelementptr %complex, %complex* %a, i32 0, i32 0 %a1 = load float, float* %a1addr %a2addr = getelementptr %complex, %complex* %a, i32 0, i32 1 %a2 = load float, float* %a2addr %r = fadd float %a2, %a1 ret float %r } define float @f() { %a = alloca %complex %a1 = getelementptr %complex, %complex* %a, i32 0, i32 0 store float 3.5, float* %a1 %a2 = getelementptr %complex, %complex* %a, i32 0, i32 1 store float 4.5, float* %a2 %r = call float @sum2(%complex* %a) ret float %r } )"""); CHECK(std::abs(e.execfn("f") - 8) < 1e-6); } // Tests passing the complex struct by value TEST_CASE("llvm complex type value") { LCompilers::LLVMEvaluator e; e.add_module(R"""( %complex = type { float, float } define float @sum2(%complex %a_value) { %a = alloca %complex store %complex %a_value, %complex* %a %a1addr = getelementptr %complex, %complex* %a, i32 0, i32 0 %a1 = load float, float* %a1addr %a2addr = getelementptr %complex, %complex* %a, i32 0, i32 1 %a2 = load float, float* %a2addr %r = fadd float %a2, %a1 ret float %r } define float @f() { %a = alloca %complex %a1 = getelementptr %complex, %complex* %a, i32 0, i32 0 store float 3.5, float* %a1 %a2 = getelementptr %complex, %complex* %a, i32 0, i32 1 store float 4.5, float* %a2 %ap = load %complex, %complex* %a %r = call float @sum2(%complex %ap) ret float %r } )"""); //CHECK(std::abs(e.execfn("f") - 8) < 1e-6); } // Tests passing boolean by reference TEST_CASE("llvm boolean type") { LCompilers::LLVMEvaluator e; e.add_module(R"""( define i1 @and_func(i1* %p, i1* %q) { %pval = load i1, i1* %p %qval = load i1, i1* %q %r = and i1 %pval, %qval ret i1 %r } define i1 @b() { %p = alloca i1 %q = alloca i1 store i1 1, i1* %p store i1 0, i1* %q %r = call i1 @and_func(i1* %p, i1* %q) ret i1 %r } )"""); CHECK(e.execfn("b") == false); } // Tests passing boolean by value TEST_CASE("llvm boolean type") { LCompilers::LLVMEvaluator e; e.add_module(R"""( define i1 @and_func(i1 %p, i1 %q) { %r = and i1 %p, %q ret i1 %r } define i1 @b() { %p = alloca i1 %q = alloca i1 store i1 1, i1* %p store i1 0, i1* %q %pval = load i1, i1* %p %qval = load i1, i1* %q %r = call i1 @and_func(i1 %pval, i1 %qval) ret i1 %r } )"""); CHECK(e.execfn("b") == false); } // Tests pointers TEST_CASE("llvm pointers 1") { LCompilers::LLVMEvaluator e; e.add_module(R"""( @r = global i64 0 define i64 @f() { store i64 8, i64* @r ; Dereferences the pointer %r ;%rval = load i64, i64* @r %raddr = ptrtoint i64* @r to i64 ret i64 %raddr } )"""); int64_t r = e.execfn("f"); CHECK(r != 8); int64_t *p = (int64_t*)r; CHECK(*p == 8); } TEST_CASE("llvm pointers 2") { LCompilers::LLVMEvaluator e; e.add_module(R"""( @r = global float 0.0 define i64 @f() { store float 8.0, float* @r %raddr = ptrtoint float* @r to i64 ret i64 %raddr } )"""); int64_t r = e.execfn("f"); float *p = (float *)r; CHECK(std::abs(*p - 8) < 1e-6); } TEST_CASE("llvm pointers 3") { LCompilers::LLVMEvaluator e; e.add_module(R"""( ; Takes a variable and returns a pointer to it define i64 @pointer_reference(float* %var) { %ret = ptrtoint float* %var to i64 ret i64 %ret } ; Takes a pointer and returns the value of what it points to define float @pointer_dereference(i64 %p) { %p2 = inttoptr i64 %p to float* %ret = load float, float* %p2 ret float %ret } define float @f() { %var = alloca float store float 8.0, float* %var %pointer_val = call i64 @pointer_reference(float* %var) ; Save the pointer to a variable %pointer_var = alloca i64 store i64 %pointer_val, i64* %pointer_var ; Extract value out of the pointer %pointer_var %pointer_val2 = load i64, i64* %pointer_var %ret = call float @pointer_dereference(i64 %pointer_val2) ret float %ret } )"""); float r = e.execfn("f"); CHECK(std::abs(r - 8) < 1e-6); } TEST_CASE("llvm pointers 4") { LCompilers::LLVMEvaluator e; e.add_module(R"""( ; Takes a variable and returns a pointer to it define float* @pointer_reference(float* %var) { ret float* %var } define float @pointer_dereference(float* %var) { %ret = load float, float* %var ret float %ret } define float @f() { %var = alloca float store float 8.0, float* %var %pointer_val = call float* @pointer_reference(float* %var) ; Save the pointer to a variable %pointer_var = alloca float* store float* %pointer_val, float** %pointer_var ; Extract value out of the pointer %pointer_var %pointer_val2 = load float*, float** %pointer_var %ret = call float @pointer_dereference(float* %pointer_val2) ret float %ret } )"""); float r = e.execfn("f"); CHECK(std::abs(r - 8) < 1e-6); } TEST_CASE("PythonCompiler 1") { CompilerOptions cu; cu.po.disable_main = true; cu.emit_debug_line_column = false; cu.separate_compilation = false; cu.interactive = true; cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir(); PythonCompiler e(cu); LCompilers::Result r = e.evaluate2("1"); CHECK(r.ok); CHECK(r.result.type == PythonCompiler::EvalResult::integer4); CHECK(r.result.i32 == 1); } // TEST_CASE("PythonCompiler 2") { // CompilerOptions cu; // cu.po.disable_main = true; // cu.emit_debug_line_column = false; // cu.separate_compilation = false; // cu.interactive = true; // cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir(); // PythonCompiler e(cu); // LCompilers::Result // r = e.evaluate2("i: i32 = 3 % 2"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::none); // r = e.evaluate2("i"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::integer4); // CHECK(r.result.i32 == 1); // } TEST_CASE("PythonCompiler i32 expressions") { CompilerOptions cu; cu.po.disable_main = true; cu.emit_debug_line_column = false; cu.separate_compilation = false; cu.interactive = true; cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir(); PythonCompiler e(cu); LCompilers::Result r = e.evaluate2("1"); CHECK(r.ok); CHECK(r.result.type == PythonCompiler::EvalResult::integer4); CHECK(r.result.i32 == 1); r = e.evaluate2("1 + 2"); CHECK(r.ok); CHECK(r.result.type == PythonCompiler::EvalResult::integer4); CHECK(r.result.i32 == 3); r = e.evaluate2("1 - 2"); CHECK(r.ok); CHECK(r.result.type == PythonCompiler::EvalResult::integer4); CHECK(r.result.i32 == -1); r = e.evaluate2("1 * 2"); CHECK(r.ok); CHECK(r.result.type == PythonCompiler::EvalResult::integer4); CHECK(r.result.i32 == 2); r = e.evaluate2("3 ** 3"); CHECK(r.ok); CHECK(r.result.type == PythonCompiler::EvalResult::integer4); CHECK(r.result.i32 == 27); r = e.evaluate2("4 // 2"); CHECK(r.ok); CHECK(r.result.type == PythonCompiler::EvalResult::integer4); CHECK(r.result.i32 == 2); r = e.evaluate2("4 / 2"); CHECK(r.ok); CHECK(r.result.type == PythonCompiler::EvalResult::real8); CHECK(r.result.f64 == 2); } // TEST_CASE("PythonCompiler i32 declaration") { // CompilerOptions cu; // cu.po.disable_main = true; // cu.emit_debug_line_column = false; // cu.separate_compilation = false; // cu.interactive = true; // cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir(); // PythonCompiler e(cu); // LCompilers::Result // r = e.evaluate2("i: i32"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::none); // r = e.evaluate2("i = 5"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::statement); // r = e.evaluate2("i"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::integer4); // CHECK(r.result.i32 == 5); // r = e.evaluate2("j: i32 = 9"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::none); // r = e.evaluate2("j"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::integer4); // CHECK(r.result.i32 == 9); // r = e.evaluate2("i + j"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::integer4); // CHECK(r.result.i32 == 14); // } TEST_CASE("PythonCompiler i64 expressions") { CompilerOptions cu; cu.po.disable_main = true; cu.emit_debug_line_column = false; cu.separate_compilation = false; cu.interactive = true; cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir(); PythonCompiler e(cu); LCompilers::Result r = e.evaluate2("i64(1)"); CHECK(r.ok); CHECK(r.result.type == PythonCompiler::EvalResult::integer8); CHECK(r.result.i64 == 1); r = e.evaluate2("i64(1) + i64(2)"); CHECK(r.ok); CHECK(r.result.type == PythonCompiler::EvalResult::integer8); CHECK(r.result.i64 == 3); r = e.evaluate2("i64(1) - i64(2)"); CHECK(r.ok); CHECK(r.result.type == PythonCompiler::EvalResult::integer8); CHECK(r.result.i64 == -1); r = e.evaluate2("i64(1) * i64(2)"); CHECK(r.ok); CHECK(r.result.type == PythonCompiler::EvalResult::integer8); CHECK(r.result.i64 == 2); r = e.evaluate2("i64(3) ** i64(3)"); CHECK(r.ok); CHECK(r.result.type == PythonCompiler::EvalResult::integer8); CHECK(r.result.i64 == 27); r = e.evaluate2("i64(4) // i64(2)"); CHECK(r.ok); CHECK(r.result.type == PythonCompiler::EvalResult::integer8); CHECK(r.result.i64 == 2); r = e.evaluate2("i64(4) / i64(2)"); CHECK(r.ok); CHECK(r.result.type == PythonCompiler::EvalResult::real8); CHECK(r.result.f64 == 2); } // TEST_CASE("PythonCompiler i64 declaration") { // CompilerOptions cu; // cu.po.disable_main = true; // cu.emit_debug_line_column = false; // cu.separate_compilation = false; // cu.interactive = true; // cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir(); // PythonCompiler e(cu); // LCompilers::Result // r = e.evaluate2("i: i64"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::none); // r = e.evaluate2("i = i64(5)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::statement); // r = e.evaluate2("i"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::integer8); // CHECK(r.result.i64 == 5); // r = e.evaluate2("j: i64 = i64(9)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::none); // r = e.evaluate2("j"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::integer8); // CHECK(r.result.i64 == 9); // r = e.evaluate2("i + j"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::integer8); // CHECK(r.result.i64 == 14); // } // TEST_CASE("PythonCompiler u32 expressions") { // CompilerOptions cu; // cu.po.disable_main = true; // cu.emit_debug_line_column = false; // cu.separate_compilation = false; // cu.interactive = true; // cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir(); // PythonCompiler e(cu); // LCompilers::Result // r = e.evaluate2("u32(1)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::unsignedInteger4); // CHECK(r.result.u32 == 1); // r = e.evaluate2("u32(1) + u32(2)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::unsignedInteger4); // CHECK(r.result.u32 == 3); // r = e.evaluate2("u32(20) - u32(10)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::unsignedInteger4); // CHECK(r.result.u32 == 10); // r = e.evaluate2("u32(1) * u32(2)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::unsignedInteger4); // CHECK(r.result.u32 == 2); // r = e.evaluate2("u32(3) ** u32(3)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::unsignedInteger4); // CHECK(r.result.u32 == 27); // r = e.evaluate2("u32(4) // u32(2)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::unsignedInteger4); // CHECK(r.result.u32 == 2); // r = e.evaluate2("u32(4) / u32(2)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::real8); // CHECK(r.result.f64 == 2); // } // TEST_CASE("PythonCompiler u32 declaration") { // CompilerOptions cu; // cu.po.disable_main = true; // cu.emit_debug_line_column = false; // cu.separate_compilation = false; // cu.interactive = true; // cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir(); // PythonCompiler e(cu); // LCompilers::Result // r = e.evaluate2("i: u32"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::none); // r = e.evaluate2("i = u32(5)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::statement); // r = e.evaluate2("i"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::unsignedInteger4); // CHECK(r.result.u32 == 5); // r = e.evaluate2("j: u32 = u32(9)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::none); // r = e.evaluate2("j"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::unsignedInteger4); // CHECK(r.result.u32 == 9); // r = e.evaluate2("i * j"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::unsignedInteger4); // CHECK(r.result.u32 == 45); // } // TEST_CASE("PythonCompiler u64 expressions") { // CompilerOptions cu; // cu.po.disable_main = true; // cu.emit_debug_line_column = false; // cu.separate_compilation = false; // cu.interactive = true; // cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir(); // PythonCompiler e(cu); // LCompilers::Result // r = e.evaluate2("u64(1)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::unsignedInteger8); // CHECK(r.result.u64 == 1); // r = e.evaluate2("u64(1) + u64(2)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::unsignedInteger8); // CHECK(r.result.u64 == 3); // r = e.evaluate2("u64(20) - u64(10)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::unsignedInteger8); // CHECK(r.result.u64 == 10); // r = e.evaluate2("u64(1) * u64(2)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::unsignedInteger8); // CHECK(r.result.u64 == 2); // r = e.evaluate2("u64(3) ** u64(3)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::unsignedInteger8); // CHECK(r.result.u64 == 27); // r = e.evaluate2("u64(4) // u64(2)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::unsignedInteger8); // CHECK(r.result.u64 == 2); // r = e.evaluate2("u64(4) / u64(2)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::real8); // CHECK(r.result.f64 == 2); // } // TEST_CASE("PythonCompiler u64 declaration") { // CompilerOptions cu; // cu.po.disable_main = true; // cu.emit_debug_line_column = false; // cu.separate_compilation = false; // cu.interactive = true; // cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir(); // PythonCompiler e(cu); // LCompilers::Result // r = e.evaluate2("i: u64"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::none); // r = e.evaluate2("i = u64(5)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::statement); // r = e.evaluate2("i"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::unsignedInteger8); // CHECK(r.result.u64 == 5); // r = e.evaluate2("j: u64 = u64(9)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::none); // r = e.evaluate2("j"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::unsignedInteger8); // CHECK(r.result.u64 == 9); // r = e.evaluate2("i * j"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::unsignedInteger8); // CHECK(r.result.u64 == 45); // } // TEST_CASE("PythonCompiler i8 expressions") { // CompilerOptions cu; // cu.po.disable_main = true; // cu.emit_debug_line_column = false; // cu.separate_compilation = false; // cu.interactive = true; // cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir(); // PythonCompiler e(cu); // LCompilers::Result // r = e.evaluate2("i8(1)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::integer1); // CHECK(r.result.i32 == 1); // r = e.evaluate2("i8(1) + i8(2)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::integer1); // CHECK(r.result.i32 == 3); // r = e.evaluate2("i8(1) - i8(2)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::integer1); // CHECK(r.result.i32 == -1); // r = e.evaluate2("i8(1) * i8(2)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::integer1); // CHECK(r.result.i32 == 2); // r = e.evaluate2("i8(3) ** i8(3)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::integer1); // CHECK(r.result.i32 == 27); // r = e.evaluate2("i8(4) // i8(2)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::integer1); // CHECK(r.result.i32 == 2); // r = e.evaluate2("i8(4) / i8(2)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::real8); // CHECK(r.result.f64 == 2); // } // TEST_CASE("PythonCompiler i8 declaration") { // CompilerOptions cu; // cu.po.disable_main = true; // cu.emit_debug_line_column = false; // cu.separate_compilation = false; // cu.interactive = true; // cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir(); // PythonCompiler e(cu); // LCompilers::Result // r = e.evaluate2("i: i8"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::none); // r = e.evaluate2("i = i8(5)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::statement); // r = e.evaluate2("i"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::integer1); // CHECK(r.result.i32 == 5); // r = e.evaluate2("j: i8 = i8(9)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::none); // r = e.evaluate2("j"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::integer1); // CHECK(r.result.i32 == 9); // r = e.evaluate2("i + j"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::integer1); // CHECK(r.result.i32 == 14); // } // TEST_CASE("PythonCompiler u8 expressions") { // CompilerOptions cu; // cu.po.disable_main = true; // cu.emit_debug_line_column = false; // cu.separate_compilation = false; // cu.interactive = true; // cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir(); // PythonCompiler e(cu); // LCompilers::Result // r = e.evaluate2("u8(1)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::unsignedInteger1); // CHECK(r.result.u32 == 1); // r = e.evaluate2("u8(1) + u8(2)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::unsignedInteger1); // CHECK(r.result.u32 == 3); // r = e.evaluate2("u8(20) - u8(10)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::unsignedInteger1); // CHECK(r.result.u32 == 10); // r = e.evaluate2("u8(1) * u8(2)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::unsignedInteger1); // CHECK(r.result.u32 == 2); // r = e.evaluate2("u8(3) ** u8(3)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::unsignedInteger1); // CHECK(r.result.u32 == 27); // r = e.evaluate2("u8(4) // u8(2)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::unsignedInteger1); // CHECK(r.result.u32 == 2); // r = e.evaluate2("u8(4) / u8(2)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::real8); // CHECK(r.result.f64 == 2); // } // TEST_CASE("PythonCompiler u8 declaration") { // CompilerOptions cu; // cu.po.disable_main = true; // cu.emit_debug_line_column = false; // cu.separate_compilation = false; // cu.interactive = true; // cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir(); // PythonCompiler e(cu); // LCompilers::Result // r = e.evaluate2("i: u8"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::none); // r = e.evaluate2("i = u8(5)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::statement); // r = e.evaluate2("i"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::unsignedInteger1); // CHECK(r.result.u32 == 5); // r = e.evaluate2("j: u8 = u8(9)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::none); // r = e.evaluate2("j"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::unsignedInteger1); // CHECK(r.result.u32 == 9); // r = e.evaluate2("i * j"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::unsignedInteger1); // CHECK(r.result.u32 == 45); // } // TEST_CASE("PythonCompiler i16 expressions") { // CompilerOptions cu; // cu.po.disable_main = true; // cu.emit_debug_line_column = false; // cu.separate_compilation = false; // cu.interactive = true; // cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir(); // PythonCompiler e(cu); // LCompilers::Result // r = e.evaluate2("i16(1)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::integer2); // CHECK(r.result.i32 == 1); // r = e.evaluate2("i16(1) + i16(2)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::integer2); // CHECK(r.result.i32 == 3); // r = e.evaluate2("i16(1) - i16(2)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::integer2); // CHECK(r.result.i32 == -1); // r = e.evaluate2("i16(1) * i16(2)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::integer2); // CHECK(r.result.i32 == 2); // r = e.evaluate2("i16(3) ** i16(3)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::integer2); // CHECK(r.result.i32 == 27); // r = e.evaluate2("i16(4) // i16(2)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::integer2); // CHECK(r.result.i32 == 2); // r = e.evaluate2("i16(4) / i16(2)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::real8); // CHECK(r.result.f64 == 2); // } // TEST_CASE("PythonCompiler i16 declaration") { // CompilerOptions cu; // cu.po.disable_main = true; // cu.emit_debug_line_column = false; // cu.separate_compilation = false; // cu.interactive = true; // cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir(); // PythonCompiler e(cu); // LCompilers::Result // r = e.evaluate2("i: i16"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::none); // r = e.evaluate2("i = i16(5)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::statement); // r = e.evaluate2("i"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::integer2); // CHECK(r.result.i32 == 5); // r = e.evaluate2("j: i16 = i16(9)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::none); // r = e.evaluate2("j"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::integer2); // CHECK(r.result.i32 == 9); // r = e.evaluate2("i + j"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::integer2); // CHECK(r.result.i32 == 14); // } // TEST_CASE("PythonCompiler u16 expressions") { // CompilerOptions cu; // cu.po.disable_main = true; // cu.emit_debug_line_column = false; // cu.separate_compilation = false; // cu.interactive = true; // cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir(); // PythonCompiler e(cu); // LCompilers::Result // r = e.evaluate2("u16(1)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::unsignedInteger2); // CHECK(r.result.u32 == 1); // r = e.evaluate2("u16(1) + u16(2)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::unsignedInteger2); // CHECK(r.result.u32 == 3); // r = e.evaluate2("u16(20) - u16(10)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::unsignedInteger2); // CHECK(r.result.u32 == 10); // r = e.evaluate2("u16(1) * u16(2)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::unsignedInteger2); // CHECK(r.result.u32 == 2); // r = e.evaluate2("u16(3) ** u16(3)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::unsignedInteger2); // CHECK(r.result.u32 == 27); // r = e.evaluate2("u16(4) // u16(2)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::unsignedInteger2); // CHECK(r.result.u32 == 2); // r = e.evaluate2("u16(4) / u16(2)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::real8); // CHECK(r.result.f64 == 2); // } // TEST_CASE("PythonCompiler u16 declaration") { // CompilerOptions cu; // cu.po.disable_main = true; // cu.emit_debug_line_column = false; // cu.separate_compilation = false; // cu.interactive = true; // cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir(); // PythonCompiler e(cu); // LCompilers::Result // r = e.evaluate2("i: u16"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::none); // r = e.evaluate2("i = u16(5)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::statement); // r = e.evaluate2("i"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::unsignedInteger2); // CHECK(r.result.u32 == 5); // r = e.evaluate2("j: u16 = u16(9)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::none); // r = e.evaluate2("j"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::unsignedInteger2); // CHECK(r.result.u32 == 9); // r = e.evaluate2("i * j"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::unsignedInteger2); // CHECK(r.result.u32 == 45); // } // TEST_CASE("PythonCompiler boolean expressions") { // CompilerOptions cu; // cu.po.disable_main = true; // cu.emit_debug_line_column = false; // cu.separate_compilation = false; // cu.interactive = true; // cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir(); // PythonCompiler e(cu); // LCompilers::Result // r = e.evaluate2("True"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::boolean); // CHECK(r.result.b); // r = e.evaluate2("False"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::boolean); // CHECK(!r.result.b); // r = e.evaluate2("False or True"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::boolean); // CHECK(r.result.b); // r = e.evaluate2("False and True"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::boolean); // CHECK(!r.result.b); // } // TEST_CASE("PythonCompiler boolean declaration") { // CompilerOptions cu; // cu.po.disable_main = true; // cu.emit_debug_line_column = false; // cu.separate_compilation = false; // cu.interactive = true; // cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir(); // PythonCompiler e(cu); // LCompilers::Result // r = e.evaluate2("t: bool"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::none); // r = e.evaluate2("t = True"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::statement); // r = e.evaluate2("t"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::boolean); // CHECK(r.result.b); // r = e.evaluate2("f: bool = False"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::none); // r = e.evaluate2("f"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::boolean); // CHECK(!r.result.b); // r = e.evaluate2("t or f"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::boolean); // CHECK(r.result.b); // r = e.evaluate2("t and f"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::boolean); // CHECK(!r.result.b); // } // TEST_CASE("PythonCompiler string 1") { // CompilerOptions cu; // cu.po.disable_main = true; // cu.emit_debug_line_column = false; // cu.separate_compilation = false; // cu.interactive = true; // cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir(); // PythonCompiler e(cu); // LCompilers::Result // r = e.evaluate2("\"My String\""); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::string); // CHECK(std::strcmp(r.result.str, "My String") == 0); // r = e.evaluate2("\"s1\" + \" \" + \"s2\""); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::string); // CHECK(std::strcmp(r.result.str, "s1 s2") == 0); // } // TEST_CASE("PythonCompiler string 2") { // CompilerOptions cu; // cu.po.disable_main = true; // cu.emit_debug_line_column = false; // cu.separate_compilation = false; // cu.interactive = true; // cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir(); // PythonCompiler e(cu); // LCompilers::Result // r = e.evaluate2("s: str"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::none); // r = e.evaluate2("s"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::string); // CHECK(r.result.str == nullptr); // r = e.evaluate2(R"( // s = "" // i: i32 = 0 // for i in range(10): // s += str(i) // )"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::statement); // r = e.evaluate2("s"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::string); // CHECK(std::strcmp(r.result.str, "0123456789") == 0); // } // TEST_CASE("PythonCompiler string 3") { // CompilerOptions cu; // cu.po.disable_main = true; // cu.emit_debug_line_column = false; // cu.separate_compilation = false; // cu.interactive = true; // cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir(); // PythonCompiler e(cu); // LCompilers::Result // r = e.evaluate2(R"( // def my_concat(x: str, y: str) -> str: // return x + " " + y // )"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::none); // r = e.evaluate2("s: str = \"0123456789\""); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::none); // r = e.evaluate2("my_concat(s, \"NUM\")"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::string); // CHECK(std::strcmp(r.result.str, "0123456789 NUM") == 0); // r = e.evaluate2("my_concat(\"Python\", \"REPL\")"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::string); // CHECK(std::strcmp(r.result.str, "Python REPL") == 0); // } // TEST_CASE("PythonCompiler Array 1") { // CompilerOptions cu; // cu.po.disable_main = true; // cu.emit_debug_line_column = false; // cu.separate_compilation = false; // cu.interactive = true; // cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir(); // PythonCompiler e(cu); // LCompilers::Result // r = e.evaluate2("i: i32[10] = empty(10, dtype=int32)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::statement); // r = e.evaluate2("print(i)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::statement); // } // TEST_CASE("PythonCompiler lists") { // CompilerOptions cu; // cu.po.disable_main = true; // cu.emit_debug_line_column = false; // cu.separate_compilation = false; // cu.interactive = true; // cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir(); // PythonCompiler e(cu); // LCompilers::Result // r = e.evaluate2("[1, 2, 3]"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::struct_type); // CHECK(LCompilers::ASRUtils::get_type_code(r.result.structure.ttype) == "list[i32]"); // CHECK(e.aggregate_type_to_string(r.result) == "[1, 2, 3]"); // r = e.evaluate2("[u8(1), u8(2), u8(3)] + [u8(1), u8(2), u8(3)]"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::struct_type); // CHECK(LCompilers::ASRUtils::get_type_code(r.result.structure.ttype) == "list[u8]"); // CHECK(e.aggregate_type_to_string(r.result) == "[1, 2, 3, 1, 2, 3]"); // r = e.evaluate2("x: list[f64] = [1.5, 2.5, 3.5]"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::statement); // r = e.evaluate2("x + [4.5]"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::struct_type); // CHECK(LCompilers::ASRUtils::get_type_code(r.result.structure.ttype) == "list[r64]"); // CHECK(e.aggregate_type_to_string(r.result) == "[1.500000, 2.500000, 3.500000, 4.500000]"); // r = e.evaluate2("[\"lfortran\", \"lpython\", \"lc\"]"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::struct_type); // CHECK(LCompilers::ASRUtils::get_type_code(r.result.structure.ttype) == "list[str]"); // CHECK(e.aggregate_type_to_string(r.result) == "[\"lfortran\", \"lpython\", \"lc\"]"); // } // TEST_CASE("PythonCompiler tuples") { // CompilerOptions cu; // cu.po.disable_main = true; // cu.emit_debug_line_column = false; // cu.separate_compilation = false; // cu.interactive = true; // cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir(); // PythonCompiler e(cu); // LCompilers::Result // r = e.evaluate2("(1, 2)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::struct_type); // CHECK(LCompilers::ASRUtils::get_type_code(r.result.structure.ttype) == "tuple[i32, i32]"); // CHECK(e.aggregate_type_to_string(r.result) == "(1, 2)"); // r = e.evaluate2("(1, 2, 2.5)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::struct_type); // CHECK(LCompilers::ASRUtils::get_type_code(r.result.structure.ttype) == "tuple[i32, i32, r64]"); // CHECK(e.aggregate_type_to_string(r.result) == "(1, 2, 2.500000)"); // r = e.evaluate2("(1, 2, 2.5, \"LPython\")"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::struct_type); // CHECK(LCompilers::ASRUtils::get_type_code(r.result.structure.ttype) == "tuple[i32, i32, r64, str]"); // CHECK(e.aggregate_type_to_string(r.result) == "(1, 2, 2.500000, \"LPython\")"); // r = e.evaluate2("(1, 2, 2.5, \"LPython\", True)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::struct_type); // CHECK(LCompilers::ASRUtils::get_type_code(r.result.structure.ttype) == "tuple[i32, i32, r64, str, i1]"); // CHECK(e.aggregate_type_to_string(r.result) == "(1, 2, 2.500000, \"LPython\", True)"); // r = e.evaluate2("(i8(1), i16(1), i64(1))"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::struct_type); // CHECK(LCompilers::ASRUtils::get_type_code(r.result.structure.ttype) == "tuple[i8, i16, i64]"); // CHECK(e.aggregate_type_to_string(r.result) == "(1, 1, 1)"); // r = e.evaluate2("(f32(1.0),)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::struct_type); // CHECK(LCompilers::ASRUtils::get_type_code(r.result.structure.ttype) == "tuple[r32]"); // CHECK(e.aggregate_type_to_string(r.result) == "(1.000000)"); // } // TEST_CASE("PythonCompiler classes") { // CompilerOptions cu; // cu.po.disable_main = true; // cu.emit_debug_line_column = false; // cu.separate_compilation = false; // cu.interactive = true; // cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir(); // PythonCompiler e(cu); // LCompilers::Result // r = e.evaluate2(R"( // @dataclass // class MyClass1: // x: i32 // )"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::none); // r = e.evaluate2("c1: MyClass1 = MyClass1(12)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::statement); // r = e.evaluate2("c1"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::struct_type); // CHECK(e.aggregate_type_to_string(r.result) == "MyClass1(x=12)"); // r = e.evaluate2(R"( // @dataclass // class MyClass2: // i: i32 // f: f64 // )"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::none); // r = e.evaluate2("c2: MyClass2 = MyClass2(12, 2.5)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::statement); // r = e.evaluate2("c2"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::struct_type); // CHECK(e.aggregate_type_to_string(r.result) == "MyClass2(i=12, f=2.500000)"); // r = e.evaluate2(R"( // @dataclass // class MyClass3: // i: i32 // f: f64 // s: str // )"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::none); // r = e.evaluate2("c3: MyClass3 = MyClass3(12, 2.5, \"LPython\")"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::statement); // r = e.evaluate2("c3"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::struct_type); // CHECK(e.aggregate_type_to_string(r.result) == "MyClass3(i=12, f=2.500000, s=\"LPython\")"); // r = e.evaluate2(R"( // @dataclass // class MyClass4: // i_1: bool // i_8: i8 // i_16: i16 // i_32: i32 // i_64: i64 // )"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::none); // r = e.evaluate2("c4: MyClass4 = MyClass4(True, i8(2), i16(3), i32(4), i64(5))"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::statement); // r = e.evaluate2("c4"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::struct_type); // // CHECK(e.aggregate_type_to_string(r.result) == "MyClass4(i_1=True, i_8=2, i_16=3, i_32=4, i_64=5)"); // FIXME: look at issue #2793 // r = e.evaluate2(R"( // @dataclass // class MyClass5: // u_1: bool // u_8: u8 // u_16: u16 // u_32: u32 // u_64: u64 // )"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::none); // r = e.evaluate2("c5: MyClass5 = MyClass5(False, u8(2), u16(3), u32(4), u64(5))"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::statement); // r = e.evaluate2("c5"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::struct_type); // CHECK(e.aggregate_type_to_string(r.result) == "MyClass5(u_1=False, u_8=2, u_16=3, u_32=4, u_64=5)"); // } // TEST_CASE("PythonCompiler underscore 1") { // CompilerOptions cu; // cu.po.disable_main = true; // cu.emit_debug_line_column = false; // cu.separate_compilation = false; // cu.interactive = true; // cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir(); // PythonCompiler e(cu); // LCompilers::Result // r = e.evaluate2("2"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::integer4); // CHECK(r.result.i32 == 2); // r = e.evaluate2("_"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::integer4); // CHECK(r.result.i32 == 2); // r = e.evaluate2("_ + 4"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::integer4); // CHECK(r.result.i32 == 6); // r = e.evaluate2("_ * 2"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::integer4); // CHECK(r.result.i32 == 12); // } // TEST_CASE("PythonCompiler underscore 2") { // CompilerOptions cu; // cu.po.disable_main = true; // cu.emit_debug_line_column = false; // cu.separate_compilation = false; // cu.interactive = true; // cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir(); // PythonCompiler e(cu); // LCompilers::Result // r = e.evaluate2("2"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::integer4); // CHECK(r.result.i32 == 2); // r = e.evaluate2("_"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::integer4); // CHECK(r.result.i32 == 2); // r = e.evaluate2("2.5"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::real8); // CHECK(r.result.f64 == 2.5); // r = e.evaluate2("_"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::real8); // CHECK(r.result.f64 == 2.5); // r = e.evaluate2("\"lpython\""); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::string); // CHECK(std::strcmp(r.result.str, "lpython") == 0); // r = e.evaluate2("_"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::string); // CHECK(std::strcmp(r.result.str, "lpython") == 0); // r = e.evaluate2("[1, 2, 3]"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::struct_type); // CHECK(LCompilers::ASRUtils::get_type_code(r.result.structure.ttype) == "list[i32]"); // CHECK(e.aggregate_type_to_string(r.result) == "[1, 2, 3]"); // r = e.evaluate2("_"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::struct_type); // CHECK(LCompilers::ASRUtils::get_type_code(r.result.structure.ttype) == "list[i32]"); // CHECK(e.aggregate_type_to_string(r.result) == "[1, 2, 3]"); // } // TEST_CASE("PythonCompiler underscore 3") { // CompilerOptions cu; // cu.po.disable_main = true; // cu.emit_debug_line_column = false; // cu.separate_compilation = false; // cu.interactive = true; // cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir(); // PythonCompiler e(cu); // LCompilers::Result // r = e.evaluate2("[1, 2, 3]"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::struct_type); // CHECK(LCompilers::ASRUtils::get_type_code(r.result.structure.ttype) == "list[i32]"); // CHECK(e.aggregate_type_to_string(r.result) == "[1, 2, 3]"); // r = e.evaluate2("_ + [1, 2, 3]"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::struct_type); // CHECK(LCompilers::ASRUtils::get_type_code(r.result.structure.ttype) == "list[i32]"); // CHECK(e.aggregate_type_to_string(r.result) == "[1, 2, 3, 1, 2, 3]"); // r = e.evaluate2(R"( // _.append(5) // x: list[i32] = _ // x // )"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::struct_type); // CHECK(LCompilers::ASRUtils::get_type_code(r.result.structure.ttype) == "list[i32]"); // CHECK(e.aggregate_type_to_string(r.result) == "[1, 2, 3, 1, 2, 3, 5]"); // } // TEST_CASE("PythonCompiler underscore 4") { // CompilerOptions cu; // cu.po.disable_main = true; // cu.emit_debug_line_column = false; // cu.separate_compilation = false; // cu.interactive = true; // cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir(); // PythonCompiler e(cu); // LCompilers::Result // r = e.evaluate2("[1, 2, 3]"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::struct_type); // CHECK(LCompilers::ASRUtils::get_type_code(r.result.structure.ttype) == "list[i32]"); // CHECK(e.aggregate_type_to_string(r.result) == "[1, 2, 3]"); // r = e.evaluate2("_"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::struct_type); // CHECK(LCompilers::ASRUtils::get_type_code(r.result.structure.ttype) == "list[i32]"); // CHECK(e.aggregate_type_to_string(r.result) == "[1, 2, 3]"); // r = e.evaluate2("f: bool = False"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::none); // r = e.evaluate2("_"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::struct_type); // CHECK(LCompilers::ASRUtils::get_type_code(r.result.structure.ttype) == "list[i32]"); // CHECK(e.aggregate_type_to_string(r.result) == "[1, 2, 3]"); // } // TEST_CASE("PythonCompiler asr verify 1") { // CompilerOptions cu; // cu.po.disable_main = true; // cu.emit_debug_line_column = false; // cu.separate_compilation = false; // cu.interactive = true; // cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir(); // PythonCompiler e(cu); // LCompilers::Result // r = e.evaluate2("i: i32 = 3 % 2"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::none); // r = e.evaluate2("i"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::integer4); // CHECK(r.result.i32 == 1); // } // TEST_CASE("PythonCompiler asr verify 2") { // CompilerOptions cu; // cu.po.disable_main = true; // cu.emit_debug_line_column = false; // cu.separate_compilation = false; // cu.interactive = true; // cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir(); // PythonCompiler e(cu); // LCompilers::Result // r = e.evaluate2(R"( // def is_even(x: i32) -> i32: // if x % 2 == 0: // return 1 // return 0 // )"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::none); // r = e.evaluate2("is_even(4)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::integer4); // CHECK(r.result.i32 == 1); // r = e.evaluate2("is_even(3)"); // CHECK(r.ok); // CHECK(r.result.type == PythonCompiler::EvalResult::integer4); // CHECK(r.result.i32 == 0); // } TEST_CASE("PythonCompiler asr verify 3") { CompilerOptions cu; cu.po.disable_main = true; cu.emit_debug_line_column = false; cu.separate_compilation = false; cu.interactive = true; cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir(); PythonCompiler e(cu); LCompilers::Result r = e.evaluate2(R"( def addi(x: i32, y: i32) -> i32: return x + y )"); CHECK(r.ok); CHECK(r.result.type == PythonCompiler::EvalResult::none); r = e.evaluate2(R"( def subi(x: i32, y: i32) -> i32: return addi(x, -y) )"); CHECK(r.ok); CHECK(r.result.type == PythonCompiler::EvalResult::none); r = e.evaluate2("addi(2, 3)"); CHECK(r.ok); CHECK(r.result.type == PythonCompiler::EvalResult::integer4); CHECK(r.result.i32 == 5); r = e.evaluate2("subi(2, 3)"); CHECK(r.ok); CHECK(r.result.type == PythonCompiler::EvalResult::integer4); CHECK(r.result.i32 == -1); } TEST_CASE("PythonCompiler asr verify 4") { CompilerOptions cu; cu.po.disable_main = true; cu.emit_debug_line_column = false; cu.separate_compilation = false; cu.interactive = true; cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir(); PythonCompiler e(cu); LCompilers::Result r = e.evaluate2(R"( def addr(x: f64, y: f64) -> f64: return x + y )"); CHECK(r.ok); CHECK(r.result.type == PythonCompiler::EvalResult::none); r = e.evaluate2(R"( def subr(x: f64, y: f64) -> f64: return addr(x, -y) )"); CHECK(r.ok); CHECK(r.result.type == PythonCompiler::EvalResult::none); r = e.evaluate2("addr(2.5, 3.5)"); CHECK(r.ok); CHECK(r.result.type == PythonCompiler::EvalResult::real8); CHECK(r.result.f64 == 6); r = e.evaluate2("subr(2.5, 3.5)"); CHECK(r.ok); CHECK(r.result.type == PythonCompiler::EvalResult::real8); CHECK(r.result.f64 == -1); }