lol its in c
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

281 lines
8.5 KiB

  1. import sys
  2. import re
  3. ## Need to convert asxxxx syntax to rgbds syntax
  4. module = sys.argv[1]
  5. class Token:
  6. def __init__(self, kind, value, line_nr):
  7. self.kind = kind
  8. self.value = value
  9. self.line_nr = line_nr
  10. def isA(self, kind, value=None):
  11. return self.kind == kind and (value is None or value == self.value)
  12. def __repr__(self):
  13. return "[%s:%s:%d]" % (self.kind, self.value, self.line_nr)
  14. class Tokenizer:
  15. TOKEN_REGEX = re.compile(
  16. "|".join(
  17. "(?P<%s>%s)" % pair
  18. for pair in [
  19. ("HEX", r"0x[0-9A-Fa-f]+"),
  20. ("ASSIGN", r"="),
  21. ("ALABEL", r"\d+\$"),
  22. ("NUMBER", r"\d+(\.\d*)?"),
  23. ("COMMENT", r";[^\n]*"),
  24. ("LABEL", r":+"),
  25. ("EXPR", r"#"),
  26. ("STRING", '"[^"]*"'),
  27. ("DIRECTIVE", r"\.[A-Za-z_][A-Za-z0-9_\.]*"),
  28. ("ID", r"[A-Za-z_][A-Za-z0-9_\.]*"),
  29. ("OP", r"[+\-*/,\(\)<>]"),
  30. ("NEWLINE", r"\n"),
  31. ("SKIP", r"[ \t]+"),
  32. ("MISMATCH", r"."),
  33. ]
  34. )
  35. )
  36. def __init__(self, code):
  37. self.__tokens = []
  38. line_num = 1
  39. for mo in self.TOKEN_REGEX.finditer(code):
  40. kind = mo.lastgroup
  41. value = mo.group()
  42. if kind == "MISMATCH":
  43. print("ERR:", code.split("\n")[line_num - 1])
  44. raise RuntimeError("Syntax error on line: %d: %s\n%s", line_num, value)
  45. elif kind == "SKIP":
  46. pass
  47. else:
  48. if kind == "HEX":
  49. value = "$" + value[2:]
  50. if kind == "ALABEL":
  51. value = "._ANNO_" + value[:-1]
  52. kind = "ID"
  53. self.__tokens.append(Token(kind, value, line_num))
  54. if kind == "NEWLINE":
  55. line_num += 1
  56. self.__tokens.append(Token("NEWLINE", "\n", line_num))
  57. def peek(self):
  58. return self.__tokens[0]
  59. def pop(self):
  60. return self.__tokens.pop(0)
  61. def expect(self, kind, value=None):
  62. pop = self.pop()
  63. if not pop.isA(kind, value):
  64. if value is not None:
  65. raise SyntaxError("%s != %s:%s" % (pop, kind, value))
  66. raise SyntaxError("%s != %s" % (pop, kind))
  67. def __bool__(self):
  68. return bool(self.__tokens)
  69. tok = Tokenizer(sys.stdin.read())
  70. def processExpression():
  71. while True:
  72. t = tok.peek()
  73. if t.isA("EXPR"):
  74. tok.pop()
  75. t = tok.peek()
  76. if t.isA("OP", "<"):
  77. sys.stdout.write("LOW")
  78. tok.pop()
  79. t = tok.peek()
  80. if t.isA("OP", ">"):
  81. sys.stdout.write("HIGH")
  82. tok.pop()
  83. t = tok.peek()
  84. if t.isA("OP", "("):
  85. tok.pop()
  86. sys.stdout.write("(")
  87. processExpression()
  88. t = tok.pop()
  89. assert t.isA("OP", ")")
  90. sys.stdout.write(")")
  91. if t.isA("ID") and t.value.startswith("b_"):
  92. t.value = "BANK(%s)" % (t.value[1:])
  93. if t.isA("NEWLINE") or t.isA("OP", ")") or t.isA("OP", ","):
  94. break
  95. sys.stdout.write(t.value)
  96. tok.pop()
  97. def processParameter():
  98. t = tok.pop()
  99. if t.isA("EXPR"):
  100. processExpression()
  101. elif t.isA("NEWLINE"):
  102. return
  103. elif t.isA("ID") or t.isA("NUMBER") or t.isA("HEX"):
  104. sys.stdout.write(t.value)
  105. elif t.isA("OP", "("):
  106. sys.stdout.write("[")
  107. processExpression()
  108. t = tok.pop()
  109. while not t.isA("OP", ")"):
  110. sys.stdout.write(t.value)
  111. t = tok.pop()
  112. assert t.isA("OP", ")"), t
  113. sys.stdout.write("]")
  114. else:
  115. raise Exception(t)
  116. class AnyStr:
  117. def __eq__(self, other) -> bool:
  118. return isinstance(other, str)
  119. BYTE_ADDR_PATTERN = [
  120. "(",
  121. AnyStr(),
  122. "+",
  123. "0",
  124. ")",
  125. ",",
  126. "(",
  127. "(",
  128. AnyStr(),
  129. "+",
  130. "0",
  131. ")",
  132. ">",
  133. ">",
  134. "8",
  135. ")",
  136. "\n",
  137. ]
  138. ID_PATTERN_IDX1 = 1
  139. ID_PATTERN_IDX2 = 8
  140. def processByteAddr():
  141. tokens = [tok.pop().value for _ in range(17)]
  142. assert tokens == BYTE_ADDR_PATTERN
  143. assert tokens[ID_PATTERN_IDX1] == tokens[ID_PATTERN_IDX2]
  144. addr = tokens[ID_PATTERN_IDX1]
  145. sys.stdout.write(f"LOW({addr}), HIGH({addr})")
  146. while tok:
  147. start = tok.pop()
  148. if start.isA("NEWLINE"):
  149. pass
  150. elif start.isA("COMMENT"):
  151. print(start.value)
  152. elif start.isA("DIRECTIVE"):
  153. if start.value in {".module", ".optsdcc", ".globl"}:
  154. while not tok.pop().isA("NEWLINE"):
  155. pass
  156. elif start.value == ".area":
  157. area_name = tok.pop().value
  158. if area_name == "_DATA":
  159. print('SECTION "%s_%s", WRAM0' % (module, area_name))
  160. elif area_name == "_DABS":
  161. print('SECTION "%s_%s", SRAM' % (module, area_name))
  162. elif area_name == "_HOME":
  163. print('SECTION FRAGMENT "%s_%s", ROM0' % (module, area_name))
  164. elif area_name == "_CODE":
  165. print('SECTION FRAGMENT "%s_%s", ROM0' % (module, area_name))
  166. elif area_name.startswith("_CODE_"):
  167. print(
  168. 'SECTION FRAGMENT "%s_%s", ROMX, BANK[%d]'
  169. % (module, area_name, int(area_name[6:]))
  170. )
  171. elif area_name == "_CABS":
  172. print('SECTION FRAGMENT "%s_%s", ROM0' % (module, area_name))
  173. elif area_name == "_GSINIT":
  174. print('SECTION FRAGMENT "GSINIT", ROMX, BANK[1]')
  175. elif area_name == "_GSFINAL":
  176. print('SECTION FRAGMENT "GSFINAL", ROMX, BANK[1]')
  177. elif area_name == "_auto":
  178. print('SECTION FRAGMENT "code_%s", ROMX' % (module))
  179. else:
  180. raise Exception(area_name)
  181. while not tok.pop().isA("NEWLINE"):
  182. pass
  183. elif start.value == ".ds":
  184. sys.stdout.write("ds ")
  185. processExpression()
  186. sys.stdout.write("\n")
  187. elif start.value == ".ascii":
  188. sys.stdout.write("db ")
  189. sys.stdout.write(tok.pop().value)
  190. sys.stdout.write("\n")
  191. elif start.value == ".db":
  192. sys.stdout.write("db ")
  193. processParameter()
  194. while tok.peek().isA("OP", ","):
  195. sys.stdout.write(",")
  196. tok.pop()
  197. processParameter()
  198. sys.stdout.write("\n")
  199. elif start.value == ".byte":
  200. sys.stdout.write("db ")
  201. processByteAddr()
  202. sys.stdout.write("\n")
  203. elif start.value == ".dw":
  204. sys.stdout.write("dw ")
  205. processParameter()
  206. while tok.peek().isA("OP", ","):
  207. sys.stdout.write(",")
  208. tok.pop()
  209. processParameter()
  210. sys.stdout.write("\n")
  211. elif start.value == ".incbin":
  212. sys.stdout.write("incbin ")
  213. while not tok.peek().isA("NEWLINE"):
  214. sys.stdout.write(tok.pop().value)
  215. sys.stdout.write("\n")
  216. tok.pop()
  217. else:
  218. sys.stderr.write(f"{module}: error: could not parse\n")
  219. raise Exception(start, tok.peek())
  220. elif start.isA("ID"):
  221. if tok.peek().isA("ASSIGN"):
  222. tok.pop()
  223. sys.stdout.write("%s = " % (start.value))
  224. processExpression()
  225. sys.stdout.write("\n")
  226. elif tok.peek().isA("LABEL"):
  227. print("%s%s" % (start.value, tok.pop().value))
  228. elif start.isA("ID", "ldhl"):
  229. tok.expect("ID", "sp")
  230. tok.expect("OP", ",")
  231. sys.stdout.write("ld hl, sp + ")
  232. processParameter()
  233. sys.stdout.write("\n")
  234. elif start.isA("ID", "lda"):
  235. tok.expect("ID", "hl")
  236. tok.expect("OP", ",")
  237. t = tok.pop()
  238. assert t.isA("NUMBER") or t.isA("HEX")
  239. tok.expect("OP", "(")
  240. tok.expect("ID", "sp")
  241. tok.expect("OP", ")")
  242. sys.stdout.write("ld hl, sp + %s\n" % (t.value))
  243. else:
  244. sys.stdout.write("%s " % (start.value))
  245. if not tok.peek().isA("NEWLINE"):
  246. processParameter()
  247. if tok.peek().isA("OP", ","):
  248. tok.pop()
  249. sys.stdout.write(", ")
  250. processParameter()
  251. sys.stdout.write("\n")
  252. tok.expect("NEWLINE")
  253. else:
  254. raise Exception(start)