From: Ben Pfaff Date: Sun, 18 Apr 2021 20:48:53 +0000 (-0700) Subject: Break macro structure definition into new file. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=237ba2110e3ceef00deffa01bd87189a269ca387;p=pspp Break macro structure definition into new file. --- diff --git a/src/language/control/define.c b/src/language/control/define.c index b718ae4420..958ae5035c 100644 --- a/src/language/control/define.c +++ b/src/language/control/define.c @@ -20,6 +20,7 @@ #include "language/command.h" #include "language/lexer/lexer.h" +#include "language/lexer/macro.h" #include "language/lexer/scan.h" #include "language/lexer/token.h" @@ -49,47 +50,6 @@ match_macro_id (struct lexer *lexer, const char *id) return false; } -struct tokens - { - struct token *tokens; - size_t n; - }; - -struct macro_param - { - char *name; /* NULL for a positional parameter. */ - struct tokens def; /* Default expansion. */ - bool expand_arg; /* Macro-expand the argument? */ - - enum - { - ARG_N_TOKENS, - ARG_CHAREND, - ARG_ENCLOSE, - ARG_CMDEND - } - arg_type; - union - { - int n_tokens; - struct token charend; - struct token enclose[2]; - }; - }; - -struct macro - { - char *name; - - struct macro_param *params; - size_t n_params; - - char **body; - size_t n_body; - }; - -static void macro_destroy (struct macro *); - static bool parse_quoted_token (struct lexer *lexer, struct token *token) { @@ -242,51 +202,3 @@ error: macro_destroy (m); return CMD_FAILURE; } - -static void -tokens_uninit (struct tokens *tokens) -{ - for (size_t i = 0; i < tokens->n; i++) - token_destroy (&tokens->tokens[i]); - free (tokens->tokens); -} - -static void -macro_destroy (struct macro *m) -{ - if (!m) - return; - - free (m->name); - for (size_t i = 0; i < m->n_params; i++) - { - struct macro_param *p = &m->params[i]; - free (p->name); - - tokens_uninit (&p->def); - - switch (p->arg_type) - { - case ARG_N_TOKENS: - break; - - case ARG_CHAREND: - token_destroy (&p->charend); - break; - - case ARG_ENCLOSE: - token_destroy (&p->enclose[0]); - token_destroy (&p->enclose[1]); - break; - - case ARG_CMDEND: - break; - } - } - free (m->params); - for (size_t i = 0; i < m->n_body; i++) - free (m->body[i]); - free (m->body); - free (m); -} - diff --git a/src/language/lexer/automake.mk b/src/language/lexer/automake.mk index 4387c3dd22..01b3df49c6 100644 --- a/src/language/lexer/automake.mk +++ b/src/language/lexer/automake.mk @@ -24,6 +24,8 @@ language_lexer_sources = \ src/language/lexer/include-path.h \ src/language/lexer/lexer.c \ src/language/lexer/lexer.h \ + src/language/lexer/macro.c \ + src/language/lexer/macro.h \ src/language/lexer/format-parser.c \ src/language/lexer/format-parser.h \ src/language/lexer/scan.c \ diff --git a/src/language/lexer/macro.c b/src/language/lexer/macro.c new file mode 100644 index 0000000000..ca086fc4df --- /dev/null +++ b/src/language/lexer/macro.c @@ -0,0 +1,68 @@ +/* PSPP - a program for statistical analysis. + Copyright (C) 2021 Free Software Foundation, Inc. + + 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 Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include + +#include "language/lexer/macro.h" + +#include + +void +macro_tokens_uninit (struct macro_tokens *tokens) +{ + for (size_t i = 0; i < tokens->n; i++) + token_destroy (&tokens->tokens[i]); + free (tokens->tokens); +} + +void +macro_destroy (struct macro *m) +{ + if (!m) + return; + + free (m->name); + for (size_t i = 0; i < m->n_params; i++) + { + struct macro_param *p = &m->params[i]; + free (p->name); + + macro_tokens_uninit (&p->def); + + switch (p->arg_type) + { + case ARG_N_TOKENS: + break; + + case ARG_CHAREND: + token_destroy (&p->charend); + break; + + case ARG_ENCLOSE: + token_destroy (&p->enclose[0]); + token_destroy (&p->enclose[1]); + break; + + case ARG_CMDEND: + break; + } + } + free (m->params); + for (size_t i = 0; i < m->n_body; i++) + free (m->body[i]); + free (m->body); + free (m); +} diff --git a/src/language/lexer/macro.h b/src/language/lexer/macro.h new file mode 100644 index 0000000000..2264ad628b --- /dev/null +++ b/src/language/lexer/macro.h @@ -0,0 +1,68 @@ +/* PSPP - a program for statistical analysis. + Copyright (C) 2021 Free Software Foundation, Inc. + + 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 Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#ifndef MACRO_H +#define MACRO_H 1 + +#include +#include + +#include "language/lexer/token.h" + +struct macro_tokens + { + struct token *tokens; + size_t n; + }; + +void macro_tokens_uninit (struct macro_tokens *); + +struct macro_param + { + char *name; /* NULL for a positional parameter. */ + struct macro_tokens def; /* Default expansion. */ + bool expand_arg; /* Macro-expand the argument? */ + + enum + { + ARG_N_TOKENS, + ARG_CHAREND, + ARG_ENCLOSE, + ARG_CMDEND + } + arg_type; + union + { + int n_tokens; + struct token charend; + struct token enclose[2]; + }; + }; + +struct macro + { + char *name; + + struct macro_param *params; + size_t n_params; + + char **body; + size_t n_body; + }; + +void macro_destroy (struct macro *); + +#endif /* macro.h */