summaryrefslogtreecommitdiff
path: root/tools/read_charmap.py
diff options
context:
space:
mode:
authorRangi <remy.oukaour+rangi@gmail.com>2018-06-01 11:10:12 -0400
committerRangi <remy.oukaour+rangi@gmail.com>2018-06-01 11:10:12 -0400
commit51a4e04768c4b64208b48c0833e1edbbfad2de92 (patch)
treea511c6ad92e69b9fa355dc4caa55bfdc76ab7f52 /tools/read_charmap.py
parent17c9b0eb5583dbfa0ff570fcaafec700c6262644 (diff)
Parse charmap.asm for dump_text.py and dump_names.py
Diffstat (limited to 'tools/read_charmap.py')
-rw-r--r--tools/read_charmap.py43
1 files changed, 43 insertions, 0 deletions
diff --git a/tools/read_charmap.py b/tools/read_charmap.py
new file mode 100644
index 0000000..9f396b4
--- /dev/null
+++ b/tools/read_charmap.py
@@ -0,0 +1,43 @@
+import os, io
+
+def parse_int(s):
+ # assumes integers are literal; no +-*/, etc
+ s = s.strip()
+ if s.startswith('$'):
+ return int(s[1:], 16)
+ if s.startswith('%'):
+ return int(s[1:], 2)
+ return int(s)
+
+def parse_string(s):
+ # assumes strings are literal; no STRCAT() etc
+ return s.strip('" ')
+
+def strip_comment(s):
+ # assumes ";" is not in the charmap
+ return s.split(';')[0].rstrip()
+
+def get_project_dir():
+ script_path = os.path.realpath(__file__)
+ script_dir = os.path.dirname(script_path)
+ project_dir = os.path.join(script_dir, '..')
+ return os.path.normpath(project_dir)
+
+def get_charmap_path():
+ project_dir = get_project_dir()
+ return os.path.join(project_dir, 'charmap.asm')
+
+def read_charmap():
+ charmap_path = get_charmap_path()
+ charmap = {}
+ with io.open(charmap_path, 'r', encoding='utf-8') as f:
+ lines = f.readlines()
+ for line in lines:
+ line = strip_comment(line).lstrip()
+ if not line.startswith('charmap '):
+ continue
+ char, value = line[len('charmap '):].rsplit(',', 1)
+ char = parse_string(char)
+ value = parse_int(value)
+ charmap[value] = char
+ return charmap