![]() |
The interpreter and the tmscm layer | ![]() |
TeXmacs currently compiles exactly one Scheme back-end, Guile. Both build systems only include the directories
Scheme/Scheme
and
Scheme/Guile
: CMake through the TeXmacs_Scheme_SRCS glob in the top level
CMakeLists.txt
(in the directory above
src/src/
), the traditional build through scheme_src in
makefile.in
. The header
Scheme/Scheme/object.hpp
includes
Scheme/Guile/guile_tm.hpp
unconditionally.
The CMake option SCHEME_IMPL selects either the embedded Guile 1.8 (the default embedded18, which expects a tm-guile188 directory next to the top level
CMakeLists.txt
; it is not part of this source tree and has to be provided separately) or a system Guile found with pkg-config (guile-1.8, guile-3.0, guile-2.2 or guile-2.0). The traditional build detects Guile with
misc/m4/guile.m4
.
The directory
Scheme/Tiny/
contains an experimental back-end based on TinyScheme (
tinyscheme_tm.cpp
,
tinyscheme_tm.hpp
and the interpreter itself). It is not compiled by either build system, and the commented out include in
object.hpp
refers to a file name (tinytmscm_tm.hpp) which does not exist. It is useful as an illustration of what a back-end must provide, but should not be expected to build.
No other back-end (such as S7) is part of this source tree.
The Guile API changed several times. The configuration defines exactly one of the macros GUILE_A, GUILE_B, GUILE_C or GUILE_D (
System/config.h.cmake
), and
guile_tm.hpp
maps a common set of names (scm_is_list, scm_str2scm, scm_new_procedure, scm_lookup_string, …) onto the corresponding calls of that generation:
Old versions which still use the gh_ interface (guile/gh.h).
Guile 1.8 (also the embedded version), with libguile.h or, if GUILE_HEADER_18 is defined, libguile18.h.
Newer versions (CMake selects it for a system Guile 2.0 or later). The main difference with GUILE_C is the cast of procedure pointers in scm_new_procedure.
The glue routine scheme-dialect returns "guile-a" to "guile-d". For instance,
init-texmacs.scm
uses it to decide how to wrap primitive-load: for the newer versions it binds the current-reader fluid explicitly, so that files loaded through the module system are read with the right reader options.
Code outside
Scheme/Guile/
should not use the Guile API directly, but the abstraction of
guile_tm.hpp
:
A tmscm is a Guile SCM.
tmscm_null, tmscm_true, tmscm_false, tmscm_cons, tmscm_car, tmscm_cdr and the usual compositions up to tmscm_cadddr, tmscm_set_car, tmscm_set_cdr.
tmscm_is_null, tmscm_is_pair, tmscm_is_list, tmscm_is_bool, tmscm_is_int, tmscm_is_double, tmscm_is_string, tmscm_is_symbol, tmscm_is_equal (equal?) and tmscm_is_blackbox.
bool_to_tmscm, int_to_tmscm, long_to_tmscm, double_to_tmscm, string_to_tmscm, symbol_to_tmscm and, in the other direction, tmscm_to_bool, tmscm_to_int, tmscm_to_uint, tmscm_to_double, tmscm_to_string, tmscm_to_symbol. The conversions of all other types are in
Scheme/Scheme/glue.cpp
(see the glue).
eval_scheme (string), eval_scheme_file (string) and call_scheme (fun, …) with up to four arguments or an array<tmscm>.
tmscm_install_procedure (name, func, args, opt, rest) defines a Scheme procedure implemented by a C++ function taking tmscm arguments (scm_c_define_gsubr for recent versions).
TMSCM_ASSERT (cond, arg, pos, name) raises a Scheme wrong-type-arg error; TMSCM_ARG1 to TMSCM_ARG10 give the argument position, and TMSCM_UNSPECIFIED is the value returned by procedures without result.
Starting the interpreter takes two steps, which are described from the point of view of the main program in the main program and crash handling:
Called by texmacs_entrypoint. For GUILE_C and GUILE_D it calls scm_boot_guile, which runs call_back (that is, TeXmacs_main) inside the Guile runtime and never returns; for the older versions it uses gh_enter.
Called by the constructor of the server. It evaluates a small bootstrap program which sets the reader options (keywords written :key, source positions, debugging), defines display-to-string, object->string, texmacs-version and the list object-stack (see Scheme objects in C++), registers the black box smob type (initialize_smobs) and installs all glue routines (initialize_glue). Only then does the server load
init-texmacs.scm
.
Unless DEBUG_ON is defined, every evaluation started from C++ (eval_scheme, eval_scheme_file, call_scheme) is wrapped in two Guile catches:
an inner lazy catch (TeXmacs_lazy_catcher), which prints the error message and backtrace on the error port while the stack is still intact and then rethrows;
an outer catch (TeXmacs_catcher), which stops the error and returns the pair (key . args) as the result of the evaluation.
So a Scheme error never unwinds into the C++ caller: the caller simply receives a pair instead of the expected value. Since the conversions of object return neutral values for unexpected types, the error is in practice only visible in the console. With DEBUG_ON, the evaluations are not protected at all.
All C++ values which have no natural Scheme representation are passed as black boxes. A blackbox (
Kernel/Abstractions/blackbox.hpp
) is a reference counted pointer to a whitebox_rep<T>, which holds a copy of a value of type T together with the type identifier type_helper<T>::id; close_box and open_box wrap and unwrap values, and open_box asserts that the type matches.
On the Scheme side there is a single smob type, created by initialize_smobs in
guile_tm.cpp
. The smob holds a heap allocated blackbox; its free function deletes it (which decrements the reference count of the boxed value), its print function shows <tree …>, <url …>, <widget> and so on depending on the type, and its equality function makes equal? compare the boxed C++ values with their operator ==. Trees, URLs, observers, widgets, widget promises, commands, modifications and patches are all represented this way; the predicates tree?, observer?, url?, modification?, patch? and blackbox? test the type tag. (URLs are special: the glue also accepts a plain string wherever a URL is expected.)