From d4e7def24858da3345864050765a87682bea74ec Mon Sep 17 00:00:00 2001 From: Vipul Cariappa Date: Fri, 24 May 2024 09:40:30 +0530 Subject: [PATCH] initial interactive test --- .gitignore | 2 +- src/lpython/python_evaluator.cpp | 16 ++++++++++++++++ src/lpython/python_evaluator.h | 4 +++- src/lpython/tests/test_llvm.cpp | 14 ++++++++++++++ 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 873a23ee9e..9cdcc189be 100644 --- a/.gitignore +++ b/.gitignore @@ -236,4 +236,4 @@ integration_tests/expr_12 integration_tests/expr_12.c # Interactive Shell -/input +input diff --git a/src/lpython/python_evaluator.cpp b/src/lpython/python_evaluator.cpp index 67f2b4df69..9db6d0ec7b 100644 --- a/src/lpython/python_evaluator.cpp +++ b/src/lpython/python_evaluator.cpp @@ -41,6 +41,22 @@ PythonCompiler::PythonCompiler(CompilerOptions compiler_options) PythonCompiler::~PythonCompiler() = default; +Result PythonCompiler::evaluate2(const std::string &code) { + LocationManager lm; + LCompilers::PassManager lpm; + lpm.use_default_passes(); + { + LCompilers::LocationManager::FileLocations fl; + fl.in_filename = "input"; + std::ofstream out("input"); + out << code; + lm.files.push_back(fl); + lm.init_simple(code); + lm.file_ends.push_back(code.size()); + } + diag::Diagnostics diagnostics; + return evaluate(code, false, lm, lpm, diagnostics); +} Result PythonCompiler::evaluate( #ifdef HAVE_LFORTRAN_LLVM diff --git a/src/lpython/python_evaluator.h b/src/lpython/python_evaluator.h index 9cba5267ed..58686c47b9 100644 --- a/src/lpython/python_evaluator.h +++ b/src/lpython/python_evaluator.h @@ -55,7 +55,9 @@ class PythonCompiler Result evaluate( const std::string &code_orig, bool verbose, LocationManager &lm, LCompilers::PassManager& pass_manager, diag::Diagnostics &diagnostics); - + + Result evaluate2(const std::string &code); + Result get_ast2( const std::string &code_orig, diag::Diagnostics &diagnostics); diff --git a/src/lpython/tests/test_llvm.cpp b/src/lpython/tests/test_llvm.cpp index a660dd7509..fce86f1788 100644 --- a/src/lpython/tests/test_llvm.cpp +++ b/src/lpython/tests/test_llvm.cpp @@ -607,3 +607,17 @@ define float @f() float r = e.floatfn("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.generate_object_code = 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::none); // TODO: change to integer4 and check the value once printing top level expressions is implemented +}