Skip to content

Commit

Permalink
Disallow shadow naming of static or local vars
Browse files Browse the repository at this point in the history
  • Loading branch information
Beherith committed Jun 25, 2024
1 parent 0233ff7 commit 0fefa27
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions bos2cob_py3.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
from io import StringIO
import pcpp

version = "1.0"

version = "1.1"

parser = argparse.ArgumentParser()
parser.add_argument("--shortopcodes", action='store_true', help = "Use uint8_t opcodes (EXPERIMENTAL with engine branch CobShortOpCodes)")
Expand All @@ -25,7 +24,7 @@
parser.add_argument("--dumpast", action='store_true', help = "Dump the parsed syntax tree into a _initial.ast file")
parser.add_argument("--dumppcpp", action='store_true', help = "Dump the results of the pcpp preprocessor")
parser.add_argument("--include", type= str, help = "Additional include directory for pcpp preprocessor")
parser.add_argument("filename", type = str, help= "A bos file to compile, or a directory of bos files to work on, such as ../units/myunit.bos")
parser.add_argument("filename", type = str, help= "A bos file to compile, or a directory of bos files to work on, such as ../units/myunit.bos", default= "", nargs='?')

args = parser.parse_args()
#args.filename = "C:/Users/Peti/Documents/My Games/Spring/games/Beyond-All-Reason.sdd/scripts/Raptors/raptora2.bos"
Expand Down Expand Up @@ -812,11 +811,22 @@ def parse_arguments(self, node):
if len(node.get_children()) == 0:
return

local_var_name = node[0].get_text()
if local_var_name in self._static_vars:
raise Exception('Static-var named "%s" already exists. You cannot reuse the same name as local variable or function argument!' % (local_var_name))
if local_var_name in self._local_vars:
raise Exception('Local-var named "%s" already exists. Multiple definitions are not allowed!' % (local_var_name))

self._local_vars.append(node[0].get_text())
self._code += OPCODES['CREATE_LOCAL_VAR']

for comma_var in node.get_children()[1:]:
if comma_var.get_type() == 'commaVar':
local_var_name = comma_var[1].get_text()
if local_var_name in self._static_vars:
raise Exception('Static-var named "%s" already exists. You cannot reuse the same name as local variable!' % (local_var_name))
if local_var_name in self._local_vars:
raise Exception('Local-var named "%s" already exists. Multiple definitions are not allowed!' % (local_var_name))
self._local_vars.append(comma_var[1].get_text())
self._code += OPCODES['CREATE_LOCAL_VAR']

Expand Down Expand Up @@ -1581,5 +1591,5 @@ def main(path, output_path = None):
parser.print_help()
print ("Specify a path to a .%s file, or a path to a directory containing .%s files"%(BOS_EXT,BOS_EXT))

#main("raptorscopy/raptor_worm_m.bos")
#main("C:/Users/Peti/Documents/My Games/Spring/games/Beyond-All-Reason.sdd/scripts/units/armadvsol_clean.bos")
#main("unitscopy/")

0 comments on commit 0fefa27

Please sign in to comment.