diff --git a/cmake/deps.cmake b/cmake/deps.cmake index 696a8ad8..dfa45958 100644 --- a/cmake/deps.cmake +++ b/cmake/deps.cmake @@ -134,10 +134,7 @@ if(CODON_JUPYTER) NAME cppzmq URL https://github.com/zeromq/cppzmq/archive/refs/tags/v4.8.1.tar.gz VERSION 4.8.1 - OPTION "CPPZMQ_BUILD_TESTS OFF") - if(cppzmq_ADDED) - set_target_properties(unit_tests PROPERTIES EXCLUDE_FROM_ALL ON) - endif() + OPTIONS "CPPZMQ_BUILD_TESTS OFF") CPMAddPackage( NAME xtl GITHUB_REPOSITORY "xtensor-stack/xtl" diff --git a/codon/compiler/jit.cpp b/codon/compiler/jit.cpp index 9f325544..e959be3c 100644 --- a/codon/compiler/jit.cpp +++ b/codon/compiler/jit.cpp @@ -84,8 +84,8 @@ public: }; } // namespace -JIT::JIT(const std::string &argv0) - : compiler(std::make_unique(argv0, /*debug=*/true)) { +JIT::JIT(const std::string &argv0, const std::string &mode) + : compiler(std::make_unique(argv0, /*debug=*/true)), mode(mode) { if (auto e = Engine::create()) { engine = std::move(e.get()); } else { @@ -176,6 +176,17 @@ llvm::Expected JIT::exec(const std::string &code) { auto sctx = cache->imports[MAIN_IMPORT].ctx; auto preamble = std::make_shared(); try { + auto *e = node->getSuite() + ? const_cast(node->getSuite())->lastInBlock() + : &node; + if (e) + if (auto ex = (*e)->getExpr()) { + *e = std::make_shared( + std::vector{std::make_shared( + std::make_shared("_jit_display"), ex->expr, + std::make_shared(mode))}, + false); + } auto s = ast::SimplifyVisitor(sctx, preamble).transform(node); auto simplified = std::make_shared(); for (auto &s : preamble->globals) diff --git a/codon/compiler/jit.h b/codon/compiler/jit.h index 0299c1b7..b781299d 100644 --- a/codon/compiler/jit.h +++ b/codon/compiler/jit.h @@ -19,9 +19,10 @@ class JIT { private: std::unique_ptr compiler; std::unique_ptr engine; + std::string mode; public: - explicit JIT(const std::string &argv0); + explicit JIT(const std::string &argv0, const std::string &mode = ""); Compiler *getCompiler() const { return compiler.get(); } Engine *getEngine() const { return engine.get(); } diff --git a/codon/parser/ast/stmt.cpp b/codon/parser/ast/stmt.cpp index 04fff18d..119825aa 100644 --- a/codon/parser/ast/stmt.cpp +++ b/codon/parser/ast/stmt.cpp @@ -52,6 +52,16 @@ void SuiteStmt::flatten(StmtPtr s, std::vector &stmts) { stmts.push_back(ss); } } +StmtPtr *SuiteStmt::lastInBlock() { + if (stmts.empty()) + return nullptr; + if (auto s = const_cast(stmts.back()->getSuite())) { + auto l = s->lastInBlock(); + if (l) + return l; + } + return &(stmts.back()); +} std::string BreakStmt::toString(int) const { return "(break)"; } ACCEPT_IMPL(BreakStmt, ASTVisitor); diff --git a/codon/parser/ast/stmt.h b/codon/parser/ast/stmt.h index b0d7d7ac..23fde86b 100644 --- a/codon/parser/ast/stmt.h +++ b/codon/parser/ast/stmt.h @@ -92,6 +92,7 @@ struct SuiteStmt : public Stmt { const Stmt *firstInBlock() const override { return stmts.empty() ? nullptr : stmts[0]->firstInBlock(); } + StmtPtr *lastInBlock(); /// Flatten all nested SuiteStmt objects that do not own a block in the statement /// vector. This is shallow flattening. diff --git a/extra/jupyter/src/codon.cpp b/extra/jupyter/src/codon.cpp index a54f7f5a..fa960ca0 100644 --- a/extra/jupyter/src/codon.cpp +++ b/extra/jupyter/src/codon.cpp @@ -61,7 +61,7 @@ nl::json CodonJupyter::execute_request_impl(int execution_counter, const string } void CodonJupyter::configure_impl() { - jit = std::make_unique(argv0); + jit = std::make_unique(argv0, "jupyter"); llvm::cantFail(jit->init()); } diff --git a/stdlib/internal/__init__.codon b/stdlib/internal/__init__.codon index 511d2984..be82cae3 100644 --- a/stdlib/internal/__init__.codon +++ b/stdlib/internal/__init__.codon @@ -25,6 +25,7 @@ from internal.types.collections.dict import * import internal.c_stubs as _C from internal.builtin import * +from internal.builtin import _jit_display from internal.box import Box from internal.str import * diff --git a/stdlib/internal/builtin.codon b/stdlib/internal/builtin.codon index e4d4122b..0822b797 100644 --- a/stdlib/internal/builtin.codon +++ b/stdlib/internal/builtin.codon @@ -329,3 +329,14 @@ class int: raise ValueError("invalid literal for int() with base " + str(base) + ": " + s) return result + + +def _jit_display(x, s: Static[str]): + if hasattr(x, "__repr_pretty__") and s == "jupyter": + return x.__repr_pretty__() + elif hasattr(x, "__repr__"): + return x.__repr__() + elif hasattr(x, "__str__"): + return x.__str__() + else: + return '' \ No newline at end of file