]> git.xonotic.org Git - xonotic/gmqcc.git/commitdiff
importing initial ftepp.c; -E option now executes the preprocessor
authorWolfgang (Blub) Bumiller <blub@speed.at>
Fri, 16 Nov 2012 15:57:39 +0000 (16:57 +0100)
committerWolfgang (Blub) Bumiller <blub@speed.at>
Fri, 16 Nov 2012 15:57:59 +0000 (16:57 +0100)
Makefile
ftepp.c [new file with mode: 0644]
gmqcc.h
main.c

index 6b2ef6c6486ed1828487206f91e54e769ae4dcc5..76439cbe02456229ab9a6cb18a700ee42ae439c6 100644 (file)
--- 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 (file)
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 b88df48859d212a04204f5ef82f80c226e31060f..4f92eaf1067d293f8dfd9a9a58fba8f3f1195a75 100644 (file)
--- 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 2c14249945cb52744ee13aed23f33a0a0c602ec8..ba4cba0ca825a27273a13a62fa0d521ccef0df3b 100644 (file)
--- 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<flag>               enable a flag\n"
-           "  -fno-<flag>            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-<flag>            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<warning>            enable a warning\n"
-           "  -Wno-<warning>         disable a warning\n"
-           "  -Wall                  enable all warnings\n"
-           "  -Werror                treat warnings as errors\n");
+            "  -Wno-<warning>         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;
             }
         }