1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-07-01 07:20:20 +02:00
guile/libguile/syntax.c
Andy Wingo 61af4d201a Syntax uses scm_allocate_tagged
* libguile/syntax.c: Define a "struct scm_syntax".  Use it instead of
scm_words and SCM_CELL_OBJECT.
2025-06-23 21:06:31 +02:00

172 lines
4.1 KiB
C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* Copyright 2017-2018,2021,2025
Free Software Foundation, Inc.
This file is part of Guile.
Guile is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Guile is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public
License along with Guile. If not, see
<https://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "alist.h"
#include "eval.h"
#include "gsubr.h"
#include "keywords.h"
#include "modules.h"
#include "pairs.h"
#include "ports.h"
#include "threads.h"
#include "variable.h"
#include "vectors.h"
#include "syntax.h"
struct scm_syntax
{
scm_t_bits tag;
SCM expr;
SCM wrap;
SCM module;
SCM source;
};
static int
scm_is_syntax (SCM x)
{
return SCM_HAS_TYP7 (x, scm_tc7_syntax);
}
static inline struct scm_syntax *
scm_to_syntax (SCM x)
{
if (!scm_is_syntax (x))
abort ();
return (struct scm_syntax *) SCM_UNPACK_POINTER (x);
}
static inline SCM
scm_from_syntax (struct scm_syntax *stx)
{
return SCM_PACK_POINTER (stx);
}
#define SCM_VALIDATE_SYNTAX(pos, scm) \
SCM_I_MAKE_VALIDATE_MSG2 (pos, scm, scm_is_syntax, "syntax object")
SCM_DEFINE (scm_syntax_p, "syntax?", 1, 0, 0,
(SCM obj),
"Return @code{#t} if the argument @var{obj} is a syntax object,\n"
"else @code{#f}.")
#define FUNC_NAME s_scm_syntax_p
{
return scm_from_bool (scm_is_syntax (obj));
}
#undef FUNC_NAME
SCM_DEFINE (scm_make_syntax, "make-syntax", 3, 1, 0,
(SCM exp, SCM wrap, SCM module, SCM source),
"Make a new syntax object.")
#define FUNC_NAME s_scm_make_syntax
{
if (SCM_UNBNDP (source))
source = SCM_BOOL_F;
else if (!scm_is_eq (source, SCM_BOOL_F))
SCM_VALIDATE_VECTOR (1, source);
struct scm_syntax *ret = scm_allocate_tagged (SCM_I_CURRENT_THREAD,
sizeof (*ret));
ret->tag = scm_tc7_syntax;
ret->expr = exp;
ret->wrap = wrap;
ret->module = module;
ret->source = source;
return scm_from_syntax (ret);
}
#undef FUNC_NAME
SCM_DEFINE (scm_syntax_expression, "syntax-expression", 1, 0, 0,
(SCM obj),
"Return the expression contained in the syntax object @var{obj}.")
#define FUNC_NAME s_scm_syntax_expression
{
SCM_VALIDATE_SYNTAX (1, obj);
return scm_to_syntax (obj)->expr;
}
#undef FUNC_NAME
SCM_DEFINE (scm_syntax_wrap, "syntax-wrap", 1, 0, 0,
(SCM obj),
"Return the wrap contained in the syntax object @var{obj}.")
#define FUNC_NAME s_scm_syntax_wrap
{
SCM_VALIDATE_SYNTAX (1, obj);
return scm_to_syntax (obj)->wrap;
}
#undef FUNC_NAME
SCM_DEFINE (scm_syntax_module, "syntax-module", 1, 0, 0,
(SCM obj),
"Return the module info contained in the syntax object @var{obj}.")
#define FUNC_NAME s_scm_syntax_module
{
SCM_VALIDATE_SYNTAX (1, obj);
return scm_to_syntax (obj)->module;
}
#undef FUNC_NAME
SCM_DEFINE (scm_syntax_source, "syntax-source", 1, 0, 0,
(SCM obj),
"Return the source properties for syntax object @var{obj}, as\n"
"an alist possibly containing the keys @code{filename},\n"
"@code{line}, and @code{column}. Return @code{#f} if no\n"
"source properties are available.")
#define FUNC_NAME s_scm_syntax_source
{
SCM_VALIDATE_SYNTAX (1, obj);
return scm_to_syntax (obj)->source;
}
#undef FUNC_NAME
static SCM print_syntax_var;
static void
init_print_syntax_var (void)
{
print_syntax_var =
scm_c_private_variable ("system syntax", "print-syntax");
}
void
scm_i_syntax_print (SCM obj, SCM port, scm_print_state *pstate)
{
static scm_i_pthread_once_t once = SCM_I_PTHREAD_ONCE_INIT;
scm_i_pthread_once (&once, init_print_syntax_var);
scm_call_2 (scm_variable_ref (print_syntax_var), obj, port);
}
void
scm_init_syntax ()
{
#include "syntax.x"
}