-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add: 'const' keyword to allow users to define new constants (#302)
* Add: 'const' keyword to allow users to define new constants * Fix: delay item 'id' validation to allow use of user-defined constants * Fix: delay disable_item 'id' validations to allow use of user-defined constants * Codechange: Use "const" in 030_house regression test
- Loading branch information
Showing
8 changed files
with
131 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
__license__ = """ | ||
NML is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 2 of the License, or | ||
(at your option) any later version. | ||
NML is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License along | ||
with NML; if not, write to the Free Software Foundation, Inc., | ||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.""" | ||
|
||
from nml import expression, generic, global_constants | ||
from nml.ast import base_statement | ||
|
||
|
||
class Constant(base_statement.BaseStatement): | ||
def __init__(self, name, value): | ||
base_statement.BaseStatement.__init__(self, "constant", name.pos, False, False) | ||
self.name = name | ||
self.value = value | ||
|
||
def register_names(self): | ||
if not isinstance(self.name, expression.Identifier): | ||
raise generic.ScriptError("Constant name should be an identifier", self.name.pos) | ||
if self.name.value in global_constants.constant_numbers: | ||
raise generic.ScriptError("Redefinition of constant '{}'.".format(self.name.value), self.name.pos) | ||
global_constants.constant_numbers[self.name.value] = self.value.reduce_constant( | ||
global_constants.const_list | ||
).value | ||
|
||
def debug_print(self, indentation): | ||
generic.print_dbg(indentation, "Constant") | ||
generic.print_dbg(indentation + 2, "Name:") | ||
self.name.debug_print(indentation + 4) | ||
|
||
generic.print_dbg(indentation + 2, "Value:") | ||
self.value.debug_print(indentation + 4) | ||
|
||
def get_action_list(self): | ||
return [] | ||
|
||
def __str__(self): | ||
return "const {} = {};".format(self.name, self.value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Oops, something went wrong.