diff options
| author | Dirk-Jan C. Binnema <djcb@djcbsoftware.nl> | 2026-03-07 12:20:35 +0200 |
|---|---|---|
| committer | Dirk-Jan C. Binnema <djcb@djcbsoftware.nl> | 2026-03-07 12:20:35 +0200 |
| commit | 69c6031b0b1cf58ea1d430d3f4579ee4563ab642 (patch) | |
| tree | e43e523081613259805de8489a36f2e3b8ef4ded /scm | |
| parent | 44cad5c7c93f18d6f3cce3dc06727e290fa67d47 (diff) | |
scm: support --eval
With the ~--eval~ option you can evaluate an expression in mu/scm
environment. For example:
$ mu scm --eval \
'(format #t "found ~d match(es)\n" (length (mfind "hello")))'
found 7173 match(es)
Add command-line parameter and implement.
Diffstat (limited to 'scm')
| -rw-r--r-- | scm/mu-scm.cc | 19 | ||||
| -rw-r--r-- | scm/mu-scm.hh | 19 |
2 files changed, 36 insertions, 2 deletions
diff --git a/scm/mu-scm.cc b/scm/mu-scm.cc index ab162fb..068c2c8 100644 --- a/scm/mu-scm.cc +++ b/scm/mu-scm.cc @@ -1,5 +1,5 @@ /* -** Copyright (C) 2025 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl> +** Copyright (C) 2025-2026 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl> ** ** This program is free software; you can redistribute it and/or modify it ** under the terms of the GNU General Public License as published by the @@ -272,6 +272,23 @@ Mu::Scm::run_script(const Mu::Store& store, const Mu::Options& opts, return Ok(); } +Result<void> +Mu::Scm::run_eval(const Mu::Store& store, const Mu::Options& opts, const std::string& expr) +{ + if (const auto res = prepare_run(opts, scm_args); !res) + return Err(res.error()); + + scm_args.emplace_back("-c"); + // a little hacky... + scm_args.emplace_back("(use-modules ((mu))) " + expr); + + run_scm(store, opts); + + return Ok(); +} + + + #ifdef BUILD_TESTS /* diff --git a/scm/mu-scm.hh b/scm/mu-scm.hh index e213331..96cd72c 100644 --- a/scm/mu-scm.hh +++ b/scm/mu-scm.hh @@ -63,7 +63,8 @@ namespace Mu::Scm { * based on the configuration. * * @param store a Store object - * @param opts options; opts.scm.script_path must set + * @param opts options + * @param a path to the script * * @return Ok() or some error */ @@ -71,6 +72,22 @@ namespace Mu::Scm { const std::string& script_path); + + /** + * Evaluate a Guile/SCM expression + * + * Initialize the Scm sub-system, then start evaluate some expression + * based on the configuration. + * + * @param store a Store object + * @param opts options + * @param expr some expression to evaluate + * + * @return Ok() or some error + */ + Result<void> run_eval(const Mu::Store& store, const Mu::Options& opts, + const std::string& expr); + /** * Helpers * |
