From 07ca2e640752ae8fecce307e1c743823f81f37c1 Mon Sep 17 00:00:00 2001 From: "Wolfgang (Blub) Bumiller" Date: Fri, 16 Nov 2012 16:57:39 +0100 Subject: [PATCH] importing initial ftepp.c; -E option now executes the preprocessor --- Makefile | 3 ++- ftepp.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ gmqcc.h | 6 +++++ main.c | 39 +++++++++++++++------------ 4 files changed, 111 insertions(+), 18 deletions(-) create mode 100644 ftepp.c diff --git a/Makefile b/Makefile index 6b2ef6c..76439cb 100644 --- a/Makefile +++ b/Makefile @@ -24,7 +24,8 @@ OBJ = \ code.o \ ast.o \ ir.o \ - con.o + con.o \ + ftepp.o OBJ_C = main.o lexer.o parser.o OBJ_X = exec-standalone.o util.o con.o diff --git a/ftepp.c b/ftepp.c new file mode 100644 index 0000000..b32c7aa --- /dev/null +++ b/ftepp.c @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2012 + * Wolfgang Bumiller + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is furnished to do + * so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "gmqcc.h" +#include "lexer.h" + +typedef struct { + lex_file *lex; +} ftepp_t; + +#define ftepp_tokval(f) ((f)->lex->tok.value) + +ftepp_t* ftepp_init() +{ + ftepp_t *ftepp; + + ftepp = (ftepp_t*)mem_a(sizeof(*ftepp)); + memset(ftepp, 0, sizeof(*ftepp)); + + return ftepp; +} + +static bool ftepp_preprocess(ftepp_t *ftepp) +{ + int token; + + ftepp->lex->flags.preprocessing = true; + + for (token = lex_do(ftepp->lex); token < TOKEN_EOF; token = lex_do(ftepp->lex)) + { + switch (token) { + case TOKEN_EOL: printf("\n"); break; + default: + printf("%s", ftepp_tokval(ftepp)); + break; + } + } + + return (token == TOKEN_EOF); +} + +bool ftepp_preprocess_file(const char *filename) +{ + ftepp_t *ftepp = ftepp_init(); + ftepp->lex = lex_open(filename); + if (!ftepp->lex) { + con_out("failed to open file \"%s\"\n", filename); + return false; + } + return ftepp_preprocess(ftepp); +} + +bool ftepp_preprocess_string(const char *name, const char *str) +{ + ftepp_t *ftepp = ftepp_init(); + ftepp->lex = lex_open_string(str, strlen(str), name); + if (!ftepp->lex) { + con_out("failed to create lexer for string \"%s\"\n", name); + return false; + } + return ftepp_preprocess(ftepp); +} diff --git a/gmqcc.h b/gmqcc.h index b88df48..4f92eaf 100644 --- a/gmqcc.h +++ b/gmqcc.h @@ -752,6 +752,12 @@ bool parser_compile_string(const char *name, const char *str); bool parser_finish (const char *output); void parser_cleanup (); +/*===================================================================*/ +/*====================== ftepp.c commandline ========================*/ +/*===================================================================*/ +bool ftepp_preprocess_file (const char *filename); +bool ftepp_preprocess_string(const char *name, const char *str); + /*===================================================================*/ /*======================= main.c commandline ========================*/ /*===================================================================*/ diff --git a/main.c b/main.c index 2c14249..ba4cba0 100644 --- a/main.c +++ b/main.c @@ -57,28 +57,28 @@ static const char *app_name; static int usage() { con_out("usage: %s [options] [files...]", app_name); con_out("options:\n" - " -h, --help show this help message\n" - " -debug turns on compiler debug messages\n" - " -memchk turns on compiler memory leak check\n"); + " -h, --help show this help message\n" + " -debug turns on compiler debug messages\n" + " -memchk turns on compiler memory leak check\n"); con_out(" -o, --output=file output file, defaults to progs.dat\n" - " -a filename add an asm file to be assembled\n" - " -s filename add a progs.src file to be used\n"); + " -a filename add an asm file to be assembled\n" + " -s filename add a progs.src file to be used\n"); con_out(" -E stop after preprocessing\n"); con_out(" -f enable a flag\n" - " -fno- disable a flag\n" - " -std standard select one of the following standards\n" - " -std=qcc original QuakeC\n" - " -std=fteqcc fteqcc QuakeC\n" - " -std=gmqcc this compiler (default)\n"); + " -fno- disable a flag\n" + " -std standard select one of the following standards\n" + " -std=qcc original QuakeC\n" + " -std=fteqcc fteqcc QuakeC\n" + " -std=gmqcc this compiler (default)\n"); con_out(" -W enable a warning\n" - " -Wno- disable a warning\n" - " -Wall enable all warnings\n" - " -Werror treat warnings as errors\n"); + " -Wno- disable a warning\n" + " -Wall enable all warnings\n" + " -Werror treat warnings as errors\n"); con_out(" -force-crc=num force a specific checksum into the header\n"); con_out("\n"); con_out("flags:\n" - " -fadjust-vector-fields\n" - " when assigning a vector field, its _y and _z fields also get assigned\n" + " -fadjust-vector-fields\n" + " when assigning a vector field, its _y and _z fields also get assigned\n" ); return -1; } @@ -482,10 +482,15 @@ int main(int argc, char **argv) { (items[itr].type == TYPE_SRC ? "progs.src" : ("unknown")))))); - if (!parser_compile_file(items[itr].filename)) - { + if (opts_pp_only) { + if (!ftepp_preprocess_file(items[itr].filename)) { retval = 1; goto cleanup; + } + } + else if (!parser_compile_file(items[itr].filename)) { + retval = 1; + goto cleanup; } } -- 2.39.2