namedenums/NamedEnumerations.cc

Mon, 03 Mar 2014 17:02:38 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Mon, 03 Mar 2014 17:02:38 +0200
changeset 117
cc1e14d55047
parent 115
9be16e1c1e44
permissions
-rw-r--r--

- reserved 'constexpr' as a keyword because I know I will need it someday

109
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 /*
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 Copyright 2014 Santeri Piippo
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
3 All rights reserved.
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 Redistribution and use in source and binary forms, with or without
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 modification, are permitted provided that the following conditions
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 are met:
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 1. Redistributions of source code must retain the above copyright
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 notice, this list of conditions and the following disclaimer.
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 2. Redistributions in binary form must reproduce the above copyright
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 notice, this list of conditions and the following disclaimer in the
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 documentation and/or other materials provided with the distribution.
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14 3. The name of the author may not be used to endorse or promote products
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 derived from this software without specific prior written permission.
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27 */
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29 #include <string>
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 #include <deque>
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 #include <algorithm>
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32 #include <cerrno>
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33 #include <cstdio>
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34 #include <cstdlib>
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 #include <cstring>
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36 #include <cstdarg>
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 using std::string;
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39 using std::deque;
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 static int gLineNumber;
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 static std::string gCurrentFile;
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44 // =============================================================================
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 //
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 struct NamedEnumInfo
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47 {
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48 string name;
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49 deque<string> enumerators;
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50 };
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52 // =============================================================================
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 //
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 void SkipWhitespace (char*& cp)
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55 {
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56 while (isspace (*cp))
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 {
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 if (*cp == '\n')
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 gLineNumber++;
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 ++cp;
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62 }
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 if (strncmp (cp, "//", 2) == 0)
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 {
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66 while (*(++cp) != '\n')
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 ;
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69 gLineNumber++;
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70 SkipWhitespace (cp);
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71 }
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72 }
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74 // =============================================================================
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75 //
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76 void Error (const char* fmt, ...)
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77 {
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
78 char buf[1024];
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79 va_list va;
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80 va_start (va, fmt);
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81 vsprintf (buf, fmt, va);
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82 va_end (va);
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83 throw std::string (buf);
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84 }
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86 // =============================================================================
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87 //
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88 int main (int argc, char* argv[])
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89 {
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90 try
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91 {
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 deque<NamedEnumInfo> namedEnumerations;
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93 deque<string> filesToInclude;
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95 if (argc < 3)
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96 {
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97 fprintf (stderr, "usage: %s input [input [input [...]]] output\n", argv[0]);
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
98 return EXIT_FAILURE;
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
99 }
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
100
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
101 for (int i = 1; i < argc - 1; ++ i)
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102 {
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
103 gCurrentFile = argv[i];
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
104 FILE* fp = fopen (argv[i], "r");
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
105 char* buf;
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
106 gLineNumber = 1;
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
107
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
108 if (fp == NULL)
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
109 {
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
110 fprintf (stderr, "could not open %s for writing: %s\n",
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
111 argv[i], strerror (errno));
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
112 exit (EXIT_FAILURE);
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
113 }
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
114
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
115 fseek (fp, 0, SEEK_END);
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
116 long int filesize = ftell (fp);
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
117 rewind (fp);
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
118
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
119 try
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
120 {
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
121 buf = new char[filesize];
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
122 }
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
123 catch (std::bad_alloc)
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
124 {
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
125 Error ("could not allocate %ld bytes for %s\n", filesize, argv[i]);
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
126 }
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
127
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
128 if (static_cast<long> (fread (buf, 1, filesize, fp)) < filesize)
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
129 Error ("could not read %ld bytes from %s\n", filesize, argv[i]);
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
130
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
131 char* const end = &buf[0] + filesize;
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
132
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
133 for (char* cp = &buf[0]; cp < end; ++cp)
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
134 {
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
135 SkipWhitespace (cp);
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
136
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
137 if (strncmp (cp, "#define ", strlen ("#define ")) == 0)
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
138 {
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
139 while (cp < end && *cp != '\n')
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
140 cp++;
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
141
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
142 continue;
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
143 }
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
144
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
145 if ((cp != &buf[0] && isspace (* (cp - 1)) == false) ||
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
146 strncmp (cp, "named_enum ", strlen ("named_enum ")) != 0)
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
147 {
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
148 continue;
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
149 }
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
150
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
151 cp += strlen ("named_enum ");
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
152 SkipWhitespace (cp);
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
153
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
154 NamedEnumInfo nenum;
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
155 auto& enumname = nenum.name;
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
156 auto& enumerators = nenum.enumerators;
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
157
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
158 if (isalpha (*cp) == false && *cp != '_')
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
159 Error ("anonymous named_enum");
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
160
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
161 while (isalnum (*cp) || *cp == '_')
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
162 enumname += *cp++;
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
163
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
164 SkipWhitespace (cp);
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
165
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
166 if (*cp++ != '{')
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
167 Error ("expected '{' after named_enum");
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
168
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
169 for (;;)
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
170 {
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
171 SkipWhitespace (cp);
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
172
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
173 if (*cp == '}')
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
174 {
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
175 cp++;
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
176 break;
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
177 }
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
178
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
179 if (isalpha (*cp) == false && *cp != '_')
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
180 Error ("expected identifier, got '%c'", *cp);
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
181
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
182 std::string enumerator;
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
183
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
184 while (isalnum (*cp) || *cp == '_')
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
185 enumerator += *cp++;
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
186
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
187 SkipWhitespace (cp);
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
188
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
189 if (*cp == ',')
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
190 SkipWhitespace (++cp);
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
191
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
192 if (*cp == '=')
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
193 Error ("named enums must not have defined values");
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
194
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
195 enumerators.push_back (enumerator);
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
196 }
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
197
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
198 SkipWhitespace (cp);
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
199
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
200 if (*cp != ';')
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
201 Error ("expected ';'");
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
202
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
203 if (enumerators.size() > 0)
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
204 {
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
205 namedEnumerations.push_back (nenum);
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
206 filesToInclude.push_back (argv[i]);
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
207 }
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
208 }
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
209 }
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
210
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
211 FILE* fp;
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
212
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
213 if ((fp = fopen (argv[argc - 1], "w")) == NULL)
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
214 Error ("couldn't open %s for writing: %s", argv[argc - 1], strerror (errno));
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
215
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
216 fprintf (fp, "#pragma once\n");
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
217
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
218 std::sort (filesToInclude.begin(), filesToInclude.end());
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
219 auto pos = std::unique (filesToInclude.begin(), filesToInclude.end());
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
220 filesToInclude.resize (std::distance (filesToInclude.begin(), pos));
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
221
115
9be16e1c1e44 - reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents: 110
diff changeset
222 for (const string& a : filesToInclude)
109
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
223 fprintf (fp, "#include \"%s\"\n", basename (a.c_str()));
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
224
115
9be16e1c1e44 - reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents: 110
diff changeset
225 for (NamedEnumInfo& e : namedEnumerations)
109
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
226 {
115
9be16e1c1e44 - reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents: 110
diff changeset
227 fprintf (fp, "\nstatic const char* g_%sNames[] =\n{\n", e.name.c_str());
109
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
228
115
9be16e1c1e44 - reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents: 110
diff changeset
229 for (const string& a : e.enumerators)
109
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
230 fprintf (fp, "\t\"%s\",\n", a.c_str());
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
231
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
232 fprintf (fp, "};\n\n");
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
233
115
9be16e1c1e44 - reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents: 110
diff changeset
234 fprintf (fp, "inline const char* get%sString (%s a)\n"
109
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
235 "{\n"
115
9be16e1c1e44 - reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents: 110
diff changeset
236 "\treturn g_%sNames[a];\n"
109
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
237 "}\n",
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
238 e.name.c_str(), e.name.c_str(), e.name.c_str());
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
239 }
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
240
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
241 printf ("Wrote named enumerations to %s\n", argv[argc - 1]);
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
242 fclose (fp);
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
243 }
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
244 catch (std::string a)
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
245 {
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
246 fprintf (stderr, "%s:%d: error: %s\n", gCurrentFile.c_str(), gLineNumber, a.c_str());
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
247 return 1;
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
248 }
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
249
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
250 return 0;
6572803cd0ca - added named enumerations to botc source, this should help with debugging
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
251 }

mercurial