Mon, 13 Aug 2012 19:04:29 +0300
Added switch support... fixed more problems with marks in the process
0 | 1 | /* |
2 | * botc source code | |
3 | * Copyright (C) 2012 Santeri `Dusk` Piippo | |
4 | * All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright notice, | |
10 | * this list of conditions and the following disclaimer. | |
11 | * 2. Redistributions in binary form must reproduce the above copyright notice, | |
12 | * this list of conditions and the following disclaimer in the documentation | |
13 | * and/or other materials provided with the distribution. | |
3 | 14 | * 3. Neither the name of the developer nor the names of its contributors may |
15 | * be used to endorse or promote products derived from this software without | |
16 | * specific prior written permission. | |
0 | 17 | * 4. Redistributions in any form must be accompanied by information on how to |
18 | * obtain complete source code for the software and any accompanying | |
19 | * software that uses the software. The source code must either be included | |
20 | * in the distribution or be available for no more than the cost of | |
21 | * distribution plus a nominal fee, and must be freely redistributable | |
22 | * under reasonable conditions. For an executable file, complete source | |
23 | * code means the source code for all modules it contains. It does not | |
24 | * include source code for modules or files that typically accompany the | |
25 | * major components of the operating system on which the executable file | |
26 | * runs. | |
27 | * | |
28 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
29 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
30 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
31 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
32 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
33 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
34 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
35 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
36 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
37 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
38 | * POSSIBILITY OF SUCH DAMAGE. | |
39 | */ | |
40 | ||
36
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
41 | #define __MAIN_CXX__ |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
42 | |
0 | 43 | #include <stdio.h> |
44 | #include <stdlib.h> | |
45 | #include <string.h> | |
46 | #include "common.h" | |
47 | ||
48 | #include "str.h" | |
49 | #include "scriptreader.h" | |
50 | #include "objwriter.h" | |
2
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
51 | #include "events.h" |
6
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
52 | #include "commands.h" |
20
d7b13805d1e0
Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
53 | #include "stringtable.h" |
22
b48e10ca8832
Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
21
diff
changeset
|
54 | #include "variables.h" |
0 | 55 | |
56 | #include "bots.h" | |
57 | #include "botcommands.h" | |
58 | ||
42
5cd91fd1526c
FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
41
diff
changeset
|
59 | const char* g_Keywords[] = { |
48
976c57f153b3
Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents:
44
diff
changeset
|
60 | "break", |
976c57f153b3
Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents:
44
diff
changeset
|
61 | "case", |
44
6bbaebc472b5
Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
43
diff
changeset
|
62 | "do", |
36
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
63 | "event", |
43
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
64 | "for", |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
65 | "goto", |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
66 | "if", |
36
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
67 | "mainloop", |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
68 | "onenter", |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
69 | "onexit", |
43
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
70 | "state", |
48
976c57f153b3
Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents:
44
diff
changeset
|
71 | "switch", |
43
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
72 | "var" |
42
5cd91fd1526c
FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
41
diff
changeset
|
73 | "while", |
36
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
74 | |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
75 | // These ones aren't implemented yet but I plan to do so, thus they are |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
76 | // reserved. Also serves as a to-do list of sorts for me. >:F |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
77 | "continue", |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
78 | "default", |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
79 | "else", |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
80 | "enum", // Would enum actually be useful? I think so. |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
81 | "func", // Would function support need external support from zandronum? |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
82 | "return", |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
83 | }; |
32
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
84 | |
48
976c57f153b3
Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents:
44
diff
changeset
|
85 | // databuffer global variable |
976c57f153b3
Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents:
44
diff
changeset
|
86 | int g_NextMark = 0; |
976c57f153b3
Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents:
44
diff
changeset
|
87 | |
0 | 88 | int main (int argc, char** argv) { |
21
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
20
diff
changeset
|
89 | // Intepret command-line parameters: |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
20
diff
changeset
|
90 | // -l: list commands |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
20
diff
changeset
|
91 | if (argc == 2 && !strcmp (argv[1], "-l")) { |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
20
diff
changeset
|
92 | ReadCommands (); |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
20
diff
changeset
|
93 | printf ("Begin list of commands:\n"); |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
20
diff
changeset
|
94 | printf ("------------------------------------------------------\n"); |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
20
diff
changeset
|
95 | |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
20
diff
changeset
|
96 | CommandDef* comm; |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
20
diff
changeset
|
97 | ITERATE_COMMANDS (comm) |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
20
diff
changeset
|
98 | printf ("%s\n", GetCommandPrototype (comm).chars()); |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
20
diff
changeset
|
99 | |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
20
diff
changeset
|
100 | printf ("------------------------------------------------------\n"); |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
20
diff
changeset
|
101 | printf ("End of command list\n"); |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
20
diff
changeset
|
102 | exit (0); |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
20
diff
changeset
|
103 | } |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
20
diff
changeset
|
104 | |
16
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
105 | // Print header |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
106 | str header; |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
107 | str headerline = "-="; |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
108 | header.appendformat ("%s version %d.%d.%d", APPNAME, VERSION_MAJOR, VERSION_MINOR, VERSION_REVISION); |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
109 | headerline.repeat ((header.len()/2)-1); |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
110 | headerline += '-'; |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
111 | printf ("%s\n%s\n", header.chars(), headerline.chars()); |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
112 | |
32
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
113 | if (argc < 2) { |
41
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
38
diff
changeset
|
114 | fprintf (stderr, "usage: %s <infile> [outfile] # compiles botscript\n", argv[0]); |
36
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
115 | fprintf (stderr, " %s -l # lists commands\n", argv[0]); |
0 | 116 | exit (1); |
117 | } | |
118 | ||
36
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
119 | // A word should always be exactly 4 bytes. The above list command |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
120 | // doesn't need it, but the rest of the program does. |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
121 | if (sizeof (word) != 4) |
42
5cd91fd1526c
FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
41
diff
changeset
|
122 | error ("%s expects a word (uint32_t) to be 4 bytes in size, is %d\n", |
36
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
123 | APPNAME, sizeof (word)); |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
124 | |
32
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
125 | str outfile; |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
126 | if (argc < 3) |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
127 | outfile = ObjectFileName (argv[1]); |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
128 | else |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
129 | outfile = argv[2]; |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
130 | |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
131 | // If we'd end up writing into an existing file, |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
132 | // ask the user if we want to overwrite it |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
133 | if (fexists (outfile)) { |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
134 | // Additional warning if the paths are the same |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
135 | str warning; |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
136 | #ifdef FILE_CASEINSENSITIVE |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
137 | if (!outfile.icompare (argv[1])) |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
138 | #else |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
139 | if (!outfile.compare (argv[1])) |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
140 | #endif |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
141 | { |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
142 | warning = "\nWARNING: Output file is the same as the input file. "; |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
143 | warning += "Answering yes here will destroy the source!\n"; |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
144 | warning += "Continue nevertheless?"; |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
145 | } |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
146 | printf ("output file `%s` already exists! overwrite?%s (y/n) ", outfile.chars(), warning.chars()); |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
147 | |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
148 | char ans; |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
149 | fgets (&ans, 2, stdin); |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
150 | if (ans != 'y') { |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
151 | printf ("abort\n"); |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
152 | exit (1); |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
153 | } |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
154 | } |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
155 | |
6
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
156 | // Read definitions |
16
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
157 | printf ("Reading definitions...\n"); |
2
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
158 | ReadEvents (); |
6
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
159 | ReadCommands (); |
2
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
160 | |
22
b48e10ca8832
Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
21
diff
changeset
|
161 | // Init stuff |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
162 | InitStringTable (); |
22
b48e10ca8832
Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
21
diff
changeset
|
163 | InitVariables (); |
20
d7b13805d1e0
Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
164 | |
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
165 | // Prepare reader and writer |
2
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
166 | ScriptReader *r = new ScriptReader (argv[1]); |
32
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
167 | ObjWriter *w = new ObjWriter (outfile); |
0 | 168 | |
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
169 | // We're set, begin parsing :) |
48
976c57f153b3
Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents:
44
diff
changeset
|
170 | printf ("Parsing script...\n"); |
0 | 171 | r->BeginParse (w); |
48
976c57f153b3
Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents:
44
diff
changeset
|
172 | printf ("Script parsed successfully.\n"); |
0 | 173 | |
32
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
174 | // Parse done, print statistics and write to file |
22
b48e10ca8832
Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
21
diff
changeset
|
175 | unsigned int globalcount = CountGlobalVars (); |
48
976c57f153b3
Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents:
44
diff
changeset
|
176 | unsigned int stringcount = CountStringTable (); |
43
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
177 | int NumMarks = w->MainBuffer->CountMarks (); |
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
178 | int NumRefs = w->MainBuffer->CountReferences (); |
48
976c57f153b3
Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents:
44
diff
changeset
|
179 | printf ("%u / %u strings written\n", stringcount, MAX_LIST_STRINGS); |
976c57f153b3
Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents:
44
diff
changeset
|
180 | printf ("%u / %u global variables\n", globalcount, MAX_SCRIPT_VARIABLES); |
976c57f153b3
Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents:
44
diff
changeset
|
181 | printf ("%d / %d bytecode marks\n", NumMarks, MAX_MARKS); |
976c57f153b3
Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents:
44
diff
changeset
|
182 | printf ("%d / %d bytecode references\n", NumRefs, MAX_MARKS); |
976c57f153b3
Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents:
44
diff
changeset
|
183 | printf ("%d / %d events\n", g_NumEvents, MAX_NUM_EVENTS); |
976c57f153b3
Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents:
44
diff
changeset
|
184 | printf ("%d state%s\n", g_NumStates, PLURAL (g_NumStates)); |
976c57f153b3
Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents:
44
diff
changeset
|
185 | |
32
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
186 | w->WriteToFile (); |
16
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
187 | |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
188 | // Clear out the junk |
0 | 189 | delete r; |
190 | delete w; | |
38 | 191 | |
192 | // Done! | |
193 | exit (0); | |
0 | 194 | } |
195 | ||
36
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
196 | // ============================================================================ |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
197 | // Utility functions |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
198 | |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
199 | // ============================================================================ |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
200 | // Does the given file exist? |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
201 | bool fexists (char* path) { |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
202 | if (FILE* test = fopen (path, "r")) { |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
203 | fclose (test); |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
204 | return true; |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
205 | } |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
206 | return false; |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
207 | } |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
208 | |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
209 | // ============================================================================ |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
210 | // Generic error |
0 | 211 | void error (const char* text, ...) { |
212 | PERFORM_FORMAT (text, c); | |
213 | fprintf (stderr, "error: %s", c); | |
214 | exit (1); | |
32
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
215 | } |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
216 | |
36
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
217 | // ============================================================================ |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
218 | // Mutates given filename to an object filename |
32
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
219 | char* ObjectFileName (str s) { |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
220 | // Locate the extension and chop it out |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
221 | unsigned int extdot = s.last ("."); |
33
fd35f6cb5f28
Added a preprocessor with proper #include support. Macro support via #define is planned too. God, was it a B-I-T-C-H to get working right, though..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
32
diff
changeset
|
222 | if (extdot >= s.len()-4) |
32
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
223 | s.trim (s.len() - extdot); |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
224 | |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
225 | s += ".o"; |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
226 | return s.chars(); |
36
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
227 | } |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
228 | |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
229 | // ============================================================================ |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
230 | // Is the given argument a reserved keyword? |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
231 | bool IsKeyword (str s) { |
42
5cd91fd1526c
FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
41
diff
changeset
|
232 | for (unsigned int u = 0; u < NumKeywords (); u++) |
36
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
233 | if (!s.icompare (g_Keywords[u])) |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
234 | return true; |
a8838b5f1213
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
235 | return false; |
42
5cd91fd1526c
FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
41
diff
changeset
|
236 | } |
5cd91fd1526c
FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
41
diff
changeset
|
237 | |
5cd91fd1526c
FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
41
diff
changeset
|
238 | unsigned int NumKeywords () { |
5cd91fd1526c
FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
41
diff
changeset
|
239 | return sizeof (g_Keywords) / sizeof (const char*); |
0 | 240 | } |