Skip to content

Instantly share code, notes, and snippets.

@ZoomTen
Created July 5, 2023 04:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZoomTen/928a7e80636a82907080f412be11e5d2 to your computer and use it in GitHub Desktop.
Save ZoomTen/928a7e80636a82907080f412be11e5d2 to your computer and use it in GitHub Desktop.
# requires "regex >= 0.20.2"
import strformat
import os
import strutils
import regex
when is_main_module:
var is_in_doc: bool = false
var matches: RegexMatch
let RE_COMMENTS = re"^;\s*"
let RE_IS_FUNCTION = re"(?i)^\s*([\w_0-9]+)::$|^\s+(?:xdef|export)\s+([\w_0-9]+)$"
let RE_IS_DEFINE = re"(?i)^\s*((?!_+)[\w_0-9]+(?!_+))\s+(?:equ)\s+(.+)"
let RE_IS_VARIABLE = re"(?i)^\s*([\w_0-9]+)::\s+(db|dw)$|([\w_0-9]+)::?\s+(ds)\s+(.+)"
if param_count() < 1:
stderr.write_line("Must provide file name!")
quit(1)
func `or`(x, y: string): string =
if x.strip() != "": result = x
result = y
for line in lines(param_str(1)):
if line.strip() == ";;": # doc marker
is_in_doc = not is_in_doc
if is_in_doc:
echo("/*! @brief")
else:
echo("*/")
else: # handle doxygen stuff
if is_in_doc:
echo (line.replace(RE_COMMENTS, ""))
else:
# Foo:
# XDEF Foo
if line.match(RE_IS_FUNCTION, matches):
let func_name =
matches.group_first_capture(0, line) or
matches.group_first_capture(1, line)
echo(fmt"{func_name}();")
# Foo equ 42
elif line.match(RE_IS_DEFINE, matches):
let def_name = matches.group(0, line)[0]
let def_val = matches.group(1, line)[0]
echo(fmt"#define {def_name} {def_val}")
# Foo:: db
# Foo:: dw
# Foo:: ds $10
elif line.match(RE_IS_VARIABLE, matches):
let var_name =
matches.group_first_capture(0, line) or
matches.group_first_capture(2, line)
let var_type =
matches.group_first_capture(1, line) or
matches.group_first_capture(3, line)
let var_size =
matches.group_first_capture(4, line)
var fsize = ""
var ftype = "byte"
case var_type.to_lower():
of "dw": ftype = "word"
of "ds": fsize = var_size.strip()
write(stdout, fmt"{ftype} {var_name}")
if fsize == "":
echo(";")
else:
echo(fmt"[{fsize}];")
else:
echo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment