main.cxx

Sat, 25 Aug 2012 05:05:58 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sat, 25 Aug 2012 05:05:58 +0300
changeset 60
8834e4b6aebd
parent 56
384c5605bda9
child 63
0557babc8675
permissions
-rw-r--r--

only print revision in version header if it's non-zero

0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 /*
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 * botc source code
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012 Santeri `Dusk` Piippo
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4 * All rights reserved.
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 *
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8 *
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright notice,
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 * this list of conditions and the following disclaimer.
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 * this list of conditions and the following disclaimer in the documentation
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 * and/or other materials provided with the distribution.
3
08cab2b67dce oh blerb.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 2
diff changeset
14 * 3. Neither the name of the developer nor the names of its contributors may
08cab2b67dce oh blerb.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 2
diff changeset
15 * be used to endorse or promote products derived from this software without
08cab2b67dce oh blerb.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 2
diff changeset
16 * specific prior written permission.
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 * 4. Redistributions in any form must be accompanied by information on how to
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18 * obtain complete source code for the software and any accompanying
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 * software that uses the software. The source code must either be included
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 * in the distribution or be available for no more than the cost of
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21 * distribution plus a nominal fee, and must be freely redistributable
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 * under reasonable conditions. For an executable file, complete source
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23 * code means the source code for all modules it contains. It does not
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24 * include source code for modules or files that typically accompany the
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25 * major components of the operating system on which the executable file
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26 * runs.
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27 *
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 * POSSIBILITY OF SUCH DAMAGE.
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39 */
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43 #include <stdio.h>
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44 #include <stdlib.h>
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 #include <string.h>
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 #include "common.h"
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48 #include "str.h"
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49 #include "scriptreader.h"
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56 #include "bots.h"
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 #include "botcommands.h"
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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",
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 48
diff changeset
62 "default",
44
6bbaebc472b5 Added do-while loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
63 "do",
56
384c5605bda9 Added support for else-statements
Teemu Piippo <crimsondusk64@gmail.com>
parents: 50
diff changeset
64 "else",
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
65 "event",
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
66 "for",
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
67 "goto",
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
68 "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
69 "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
70 "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
71 "onexit",
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
72 "state",
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 44
diff changeset
73 "switch",
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
74 "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
75 "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
76
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 // 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
78 // 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
79 "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
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
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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 = "-=";
60
8834e4b6aebd only print revision in version header if it's non-zero
Teemu Piippo <crimsondusk64@gmail.com>
parents: 56
diff changeset
108 header.appendformat ("%s version %d.%d", APPNAME, VERSION_MAJOR, VERSION_MINOR);
8834e4b6aebd only print revision in version header if it's non-zero
Teemu Piippo <crimsondusk64@gmail.com>
parents: 56
diff changeset
109
8834e4b6aebd only print revision in version header if it's non-zero
Teemu Piippo <crimsondusk64@gmail.com>
parents: 56
diff changeset
110 // Include revision if non-zero
8834e4b6aebd only print revision in version header if it's non-zero
Teemu Piippo <crimsondusk64@gmail.com>
parents: 56
diff changeset
111 if (VERSION_REVISION)
8834e4b6aebd only print revision in version header if it's non-zero
Teemu Piippo <crimsondusk64@gmail.com>
parents: 56
diff changeset
112 header.appendformat (".%d", VERSION_REVISION);
8834e4b6aebd only print revision in version header if it's non-zero
Teemu Piippo <crimsondusk64@gmail.com>
parents: 56
diff changeset
113
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
114 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
115 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
116 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
117
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
118 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
119 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
120 fprintf (stderr, " %s -l # lists commands\n", argv[0]);
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
121 exit (1);
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
122 }
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
123
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
124 // 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
125 // 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
126 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
127 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
128 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
129
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
130 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
131 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
132 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
133 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
134 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
135
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 // 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
137 // 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
138 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
139 // 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
140 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
141 #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
142 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
143 #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
144 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
145 #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
146 {
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 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
148 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
149 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
150 }
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 ("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
152
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 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
154 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
155 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
156 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
157 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
158 }
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
159 }
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
160
6
0005527cad62 Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 4
diff changeset
161 // 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
162 printf ("Reading definitions...\n");
2
bb2c45522eb6 Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1
diff changeset
163 ReadEvents ();
6
0005527cad62 Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 4
diff changeset
164 ReadCommands ();
2
bb2c45522eb6 Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1
diff changeset
165
22
b48e10ca8832 Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 21
diff changeset
166 // 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
167 InitStringTable ();
22
b48e10ca8832 Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 21
diff changeset
168 InitVariables ();
20
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 16
diff changeset
169
1
f0c61c204bc8 Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
170 // Prepare reader and writer
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 48
diff changeset
171 ScriptReader* r = new ScriptReader (argv[1]);
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 48
diff changeset
172 ObjWriter* w = new ObjWriter (outfile);
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
173
1
f0c61c204bc8 Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
174 // 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
175 printf ("Parsing script...\n");
50
2e333a3ca49a Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents: 48
diff changeset
176 r->ParseBotScript (w);
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 44
diff changeset
177 printf ("Script parsed successfully.\n");
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
178
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
179 // Parse done, print statistics and write to file
22
b48e10ca8832 Added rudimentary global var support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 21
diff changeset
180 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
181 unsigned int stringcount = CountStringTable ();
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
182 int NumMarks = w->MainBuffer->CountMarks ();
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
183 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
184 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
185 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
186 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
187 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
188 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
189 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
190
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
191 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
192
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
193 // Clear out the junk
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
194 delete r;
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
195 delete w;
38
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
196
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
197 // Done!
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
198 exit (0);
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
199 }
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
200
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
201 // ============================================================================
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 // 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
203
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 // ============================================================================
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 // 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
206 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
207 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
208 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
209 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
210 }
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
211 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
212 }
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
213
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
214 // ============================================================================
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
215 // Generic error
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
216 void error (const char* text, ...) {
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
217 PERFORM_FORMAT (text, c);
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
218 fprintf (stderr, "error: %s", c);
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
219 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
220 }
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
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
222 // ============================================================================
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
223 // 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
224 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
225 // 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
226 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
227 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
228 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
229
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
230 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
231 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
232 }
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
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 // ============================================================================
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 // 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
236 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
237 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
238 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
239 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
240 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
241 }
5cd91fd1526c FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 41
diff changeset
242
5cd91fd1526c FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 41
diff changeset
243 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
244 return sizeof (g_Keywords) / sizeof (const char*);
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
245 }

mercurial