Mon, 03 Mar 2014 17:02:38 +0200
- reserved 'constexpr' as a keyword because I know I will need it someday
88 | 1 | /* |
2 | Copyright 2012-2014 Santeri Piippo | |
3 | All rights reserved. | |
4 | ||
5 | Redistribution and use in source and binary forms, with or without | |
6 | modification, are permitted provided that the following conditions | |
7 | are met: | |
8 | ||
9 | 1. Redistributions of source code must retain the above copyright | |
10 | notice, this list of conditions and the following disclaimer. | |
11 | 2. Redistributions in binary form must reproduce the above copyright | |
12 | notice, this list of conditions and the following disclaimer in the | |
13 | documentation and/or other materials provided with the distribution. | |
14 | 3. The name of the author may not be used to endorse or promote products | |
15 | derived from this software without specific prior written permission. | |
16 | ||
17 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
18 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
19 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
20 | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
21 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
22 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
23 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
24 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
26 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
27 | */ | |
28 | ||
29 | #include <stdlib.h> | |
30 | #include <stdio.h> | |
31 | #include <string.h> | |
32 | #include "Main.h" | |
33 | #include "String.h" | |
34 | #include "Commands.h" | |
35 | #include "Lexer.h" | |
36 | ||
37 | static List<CommandInfo*> gCommands; | |
38 | ||
39 | // ============================================================================ | |
40 | // | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
41 | void addCommandDefinition (CommandInfo* comm) |
88 | 42 | { |
43 | // Ensure that there is no conflicts | |
44 | for (CommandInfo* it : gCommands) | |
45 | if (it->number == comm->number) | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
46 | error ("Attempted to redefine command #%1 (%2) as %3", |
88 | 47 | gCommands[comm->number]->name, comm->name); |
48 | ||
49 | gCommands << comm; | |
50 | } | |
51 | ||
52 | // ============================================================================ | |
53 | // Finds a command by name | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
54 | CommandInfo* findCommandByName (String fname) |
88 | 55 | { |
56 | for (CommandInfo* comm : gCommands) | |
57 | { | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
58 | if (fname.toUppercase() == comm->name.toUppercase()) |
88 | 59 | return comm; |
60 | } | |
61 | ||
62 | return null; | |
63 | } | |
64 | ||
65 | // ============================================================================ | |
66 | // | |
67 | // Returns the prototype of the command | |
68 | // | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
69 | String CommandInfo::signature() |
88 | 70 | { |
71 | String text; | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
72 | text += dataTypeName (returnvalue); |
88 | 73 | text += ' '; |
74 | text += name; | |
75 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
76 | if (args.isEmpty() == false) |
88 | 77 | text += ' '; |
78 | ||
79 | text += '('; | |
80 | ||
81 | bool hasoptionals = false; | |
82 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
83 | for (int i = 0; i < args.size(); i++) |
88 | 84 | { |
99
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
85 | if (i == minargs) |
88 | 86 | { |
87 | hasoptionals = true; | |
88 | text += '['; | |
89 | } | |
90 | ||
91 | if (i) | |
92 | text += ", "; | |
93 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
94 | text += dataTypeName (args[i].type); |
88 | 95 | text += ' '; |
96 | text += args[i].name; | |
97 | ||
99
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
98 | if (i >= minargs) |
88 | 99 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
100 | bool isString = args[i].type == TYPE_String; |
88 | 101 | text += " = "; |
102 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
103 | if (isString) |
88 | 104 | text += '"'; |
105 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
106 | text += String::fromNumber (args[i].defvalue); |
88 | 107 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
108 | if (isString) |
88 | 109 | text += '"'; |
110 | } | |
111 | } | |
112 | ||
113 | if (hasoptionals) | |
114 | text += ']'; | |
115 | ||
116 | text += ')'; | |
117 | return text; | |
118 | } | |
119 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
120 | const List<CommandInfo*>& getCommands() |
88 | 121 | { |
122 | return gCommands; | |
123 | } |