Sun, 29 Jul 2012 04:02:07 +0300
Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
6
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
1 | /* |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
2 | * botc source code |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
3 | * Copyright (C) 2012 Santeri `Dusk` Piippo |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
4 | * All rights reserved. |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
5 | * |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
8 | * |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright notice, |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
10 | * this list of conditions and the following disclaimer. |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
12 | * this list of conditions and the following disclaimer in the documentation |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
13 | * and/or other materials provided with the distribution. |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
14 | * 3. Neither the name of the developer nor the names of its contributors may |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
15 | * be used to endorse or promote products derived from this software without |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
16 | * specific prior written permission. |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
17 | * 4. Redistributions in any form must be accompanied by information on how to |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
18 | * obtain complete source code for the software and any accompanying |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
19 | * software that uses the software. The source code must either be included |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
20 | * in the distribution or be available for no more than the cost of |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
21 | * distribution plus a nominal fee, and must be freely redistributable |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
22 | * under reasonable conditions. For an executable file, complete source |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
23 | * code means the source code for all modules it contains. It does not |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
24 | * include source code for modules or files that typically accompany the |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
25 | * major components of the operating system on which the executable file |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
26 | * runs. |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
27 | * |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
28 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
29 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
30 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
31 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
32 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
33 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
34 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
35 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
36 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
0005527cad62
Command definitions are now read into memory.
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 |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
38 | * POSSIBILITY OF SUCH DAMAGE. |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
39 | */ |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
40 | |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
41 | #define __COMMANDS_CXX__ |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
42 | #include <stdlib.h> |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
43 | #include <stdio.h> |
10
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
44 | #include <string.h> |
6
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
45 | #include "common.h" |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
46 | #include "scriptreader.h" |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
47 | #include "str.h" |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
48 | #include "commands.h" |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
49 | |
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
|
50 | // ============================================================================ |
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
|
51 | // Reads command definitions from commands.def and stores them to memory. |
6
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
52 | void ReadCommands () { |
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:
12
diff
changeset
|
53 | ScriptReader* r = new ScriptReader ("commands.def"); |
6
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
54 | g_CommDef = NULL; |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
55 | CommandDef* curdef = g_CommDef; |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
56 | unsigned int numCommDefs = 0; |
6
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
57 | |
19
66993500719f
Commands w/ arguments are now written correctly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
17
diff
changeset
|
58 | while (r->PeekNext().len()) { |
6
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
59 | CommandDef* comm = new CommandDef; |
10
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
60 | comm->next = NULL; |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
61 | |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
62 | // Number |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
63 | r->MustNumber (); |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
64 | comm->number = r->token; |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
65 | |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
66 | r->MustNext (":"); |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
67 | |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
68 | // Name |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
69 | r->MustNext (); |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
70 | comm->name = r->token; |
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
|
71 | if (IsKeyword (comm->name)) |
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
|
72 | r->ParserError ("command name `%s` conflicts with keyword", comm->name.chars()); |
10
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
73 | |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
74 | r->MustNext (":"); |
6
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
75 | |
10
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
76 | // Return value |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
77 | r->MustNext (); |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
78 | comm->returnvalue = GetCommandType (r->token); |
10
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
79 | if (comm->returnvalue == -1) |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
80 | r->ParserError ("bad return value type `%s`", r->token.chars()); |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
81 | |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
82 | r->MustNext (":"); |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
83 | |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
84 | // Num args |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
85 | r->MustNumber (); |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
86 | comm->numargs = r->token; |
7
118d3d5db64f
Improved error handling; added parser warnings
Teemu Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
87 | |
10
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
88 | r->MustNext (":"); |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
89 | |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
90 | // Max args |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
91 | r->MustNumber (); |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
92 | comm->maxargs = r->token; |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
93 | |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
94 | if (comm->maxargs > MAX_MAXARGS) |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
95 | r->ParserError ("maxargs (%d) greater than %d!", comm->maxargs, MAX_MAXARGS); |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
96 | |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
97 | // Argument types |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
98 | int curarg = 0; |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
99 | while (curarg < comm->maxargs) { |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
100 | r->MustNext (":"); |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
101 | r->MustNext (); |
6
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
102 | |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
103 | int type = GetCommandType (r->token); |
10
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
104 | if (type == -1) |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
105 | r->ParserError ("bad argument %d type `%s`", curarg, r->token.chars()); |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
106 | if (type == RETURNVAL_VOID) |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
107 | r->ParserError ("void is not a valid argument type!"); |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
108 | comm->argtypes[curarg] = type; |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
109 | |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
110 | r->MustNext ("("); |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
111 | r->MustNext (); |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
112 | |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
113 | // - 1 because of terminating null character |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
114 | if (r->token.len() > MAX_ARGNAMELEN - 1) |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
115 | r->ParserWarning ("argument name is too long (%d, max is %d)", |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
116 | r->token.len(), MAX_ARGNAMELEN - 1); |
10
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
117 | |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
118 | strncpy (comm->argnames[curarg], r->token.chars(), MAX_ARGNAMELEN); |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
119 | comm->argnames[curarg][MAX_ARGNAMELEN-1] = 0; |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
120 | |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
121 | // If this is an optional parameter, we need the default value. |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
122 | if (curarg >= comm->numargs) { |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
123 | r->MustNext ("="); |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
124 | switch (type) { |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
125 | case RETURNVAL_INT: r->MustNumber(); break; |
12
1bdbfcca2fc6
MustString now behaves more like its siblings - sets token to result rather than returning it
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
126 | case RETURNVAL_STRING: r->MustString(); break; |
10
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
127 | case RETURNVAL_BOOLEAN: r->MustBool(); break; |
6
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
128 | } |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
129 | |
10
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
130 | comm->defvals[curarg] = r->token; |
6
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
131 | } |
10
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
132 | |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
133 | r->MustNext (")"); |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
134 | curarg++; |
6
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
135 | } |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
136 | |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
137 | if (!g_CommDef) |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
138 | g_CommDef = comm; |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
139 | |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
140 | if (!curdef) { |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
141 | curdef = comm; |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
142 | } else { |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
143 | curdef->next = comm; |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
144 | curdef = comm; |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
145 | } |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
146 | numCommDefs++; |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
147 | } |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
148 | |
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
|
149 | if (!numCommDefs) |
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
|
150 | r->ParserError ("no commands defined!\n"); |
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
|
151 | |
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:
28
diff
changeset
|
152 | r->CloseFile (); |
28
fb46d3d40064
Added comment support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
21
diff
changeset
|
153 | delete r; |
6
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
154 | printf ("%d command definitions read.\n", numCommDefs); |
8
c8bfa7e6ae1b
Commands are now read properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
7
diff
changeset
|
155 | } |
c8bfa7e6ae1b
Commands are now read properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
7
diff
changeset
|
156 | |
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
|
157 | // ============================================================================ |
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
|
158 | // Get command type by name |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
159 | int GetCommandType (str t) { |
10
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
160 | // "float" is for now just int. |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
161 | // TODO: find out how BotScript floats work |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
162 | // (are they real floats or fixed? how are they |
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
|
163 | // stored?) and add proper floating point number support. |
17
b4fcc69e426a
Events and commands are now treated properly case-insensitively.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
164 | t = t.tolower(); |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
165 | return !t.compare ("int") ? TYPE_INT : |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
166 | !t.compare ("float") ? TYPE_INT : |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
167 | !t.compare ("str") ? TYPE_STRING : |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
168 | !t.compare ("void") ? TYPE_VOID : |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
169 | !t.compare ("bool") ? TYPE_INT : -1; |
10
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
170 | } |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
171 | |
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
|
172 | // ============================================================================ |
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
|
173 | // Inverse operation - type name by value |
10
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
174 | str GetReturnTypeName (int r) { |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
175 | switch (r) { |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
176 | case RETURNVAL_INT: return "int"; break; |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
177 | case RETURNVAL_STRING: return "str"; break; |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
178 | case RETURNVAL_VOID: return "void"; break; |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
179 | case RETURNVAL_BOOLEAN: return "bool"; break; |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
180 | } |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
181 | |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
182 | return ""; |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
183 | } |
2c0f76090372
Restructured the command def parser, added parameter lists to definition file.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
184 | |
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
|
185 | // ============================================================================ |
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
|
186 | // Finds a command by name |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
33
diff
changeset
|
187 | CommandDef* FindCommand (str fname) { |
17
b4fcc69e426a
Events and commands are now treated properly case-insensitively.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
188 | CommandDef* comm; |
b4fcc69e426a
Events and commands are now treated properly case-insensitively.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
189 | ITERATE_COMMANDS (comm) { |
b4fcc69e426a
Events and commands are now treated properly case-insensitively.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
190 | if (!fname.icompare (comm->name)) |
b4fcc69e426a
Events and commands are now treated properly case-insensitively.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
191 | return comm; |
8
c8bfa7e6ae1b
Commands are now read properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
7
diff
changeset
|
192 | } |
c8bfa7e6ae1b
Commands are now read properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
7
diff
changeset
|
193 | |
c8bfa7e6ae1b
Commands are now read properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
7
diff
changeset
|
194 | return NULL; |
21
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
195 | } |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
196 | |
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
|
197 | // ============================================================================ |
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 | // Returns the prototype of the command |
21
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
199 | str GetCommandPrototype (CommandDef* comm) { |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
200 | str text; |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
201 | text += GetReturnTypeName (comm->returnvalue); |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
202 | text += ' '; |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
203 | text += comm->name; |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
204 | text += '('; |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
205 | |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
206 | bool hasOptionalArguments = false; |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
207 | for (int i = 0; i < comm->maxargs; i++) { |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
208 | if (i == comm->numargs) { |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
209 | hasOptionalArguments = true; |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
210 | text += '['; |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
211 | } |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
212 | |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
213 | if (i) |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
214 | text += ", "; |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
215 | |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
216 | text += GetReturnTypeName (comm->argtypes[i]); |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
217 | text += ' '; |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
218 | text += comm->argnames[i]; |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
219 | |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
220 | if (i >= comm->numargs) { |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
221 | text += '='; |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
222 | |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
223 | bool isString = comm->argtypes[i] == RETURNVAL_STRING; |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
224 | if (isString) text += '"'; |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
225 | |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
226 | char defvalstring[8]; |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
227 | sprintf (defvalstring, "%d", comm->defvals[i]); |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
228 | text += defvalstring; |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
229 | |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
230 | if (isString) text += '"'; |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
231 | } |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
232 | } |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
233 | |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
234 | if (hasOptionalArguments) |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
235 | text += ']'; |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
236 | text += ')'; |
ae602e667879
Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
237 | return text; |
6
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
238 | } |